Wicket and Embedded GlassFish

One of the newer features available in GlassFish v3 is the ability to run glassfish in an embedded mode similar to how jetty is often used. This is approach is very common when working with Wicket and is, in fact, part of the quickstart maven archetype.  So what I'd like to see is GlassFish in place of Jetty.  It's not terribly difficult once you get past the first few steps.  This is a pretty new (and evolving) option so the documentation isn't all that easy to find, necessarily, but it's building.  I'll list some resources at the end. The first step, of course, is to configure maven appropriately.  In my pom.xml, I have the following:

    
        
            glassfish-repository
            GlassFish Nexus Repository
            http://maven.glassfish.org/content/groups/glassfish
        
    

This will add the repository to find the deployed GlassFish artifacts. With that done, we can define a few dependencies:

        
            org.glassfish.common
            glassfish-api
            ${glassfish.version}
            provided
        
        
            org.glassfish.extras
            glassfish-embedded-web
            ${glassfish.version}
            provided
        
        
            org.glassfish.web
            web-embed-impl
            ${glassfish.version}
            provided
        

${glassfish.version} here is defined elsewhere in the pom to 3.0. The class at the center of it all looks like this:

public class Embedded {
    public void start() throws IOException {
        final Server server = new Server.Builder("testBuilder").build();
        ContainerBuilder containerBuilder = server.createConfig(ContainerBuilder.Type.web);
        server.addContainer(containerBuilder);
        final EmbeddedWebContainer container = (EmbeddedWebContainer) containerBuilder.create(server);
        container.setLogLevel(Level.INFO);
        server.createPort(8888);
        final EmbeddedDeployer deployer = server.getDeployer();
        DeployCommandParameters deployParams = new DeployCommandParameters();
        deployParams.contextroot = "/";     // overrides whatever the WAR contains
        File archive = new File("target/wicket-glassfish").getAbsoluteFile();
        System.out.println("Deployed: " + deployer.deploy(archive, deployParams));
    }

    public static void main(String[] args) throws Exception {
        new Embedded().start();
        while (true) {
            Thread.sleep(1000);
        }
    }
}

Now, that's all rather dense, I know. This code is a condensed version of a much larger, more complete version written by Alexis Moussine-Pouchkine. His blog is a great resource not only for embedded GlassFish but all sorts of GlassFish related topics. This is all that's needed to run your app on GlassFish.

There are a number of other avenues left to explore. If you want to run glassfish from maven, you'll want to probably look at this entry. You can also enable some security as noted here. For the definitive word on the matter, however, please see the formal documentation for embedded glassfish here.