Creating a Glassfish domain and run it as a Windows service

So, my on-going saga of ways to run glassfish as a service progresses to glassfish 3.1.1.

Here are the ant snippets I am using to create the glassfish domain and set it up to run as a windows service:

<exec executable="${glassfish.home}/bin/asadmin.bat" 
      failonerror="true" 
      output="${glassfish.home}/glassfish-create-domain.log"
      inputstring="AS_ADMIN_PASSWORD=${glassfish.admin.password}">   
   <arg value="--user"/>
   <arg value="${glassfish.admin.username}"/>
   <arg value="--passwordfile"/>
   <arg value="-"/>
   <arg value="create-domain"/>
   <arg value="--savelogin"/>
   <arg value="--checkports=false"/>
   <arg value="--adminport"/>
   <arg value="${glassfish.admin.port}"/>
   <arg value="--instanceport"/>
   <arg value="${glassfish.http.port}"/>
   <arg value="--domainproperties=jms.port=7676:domain.jmxPort=8686:orb.listener.port=3700:http.ssl.port=8181:orb.ssl.port=3820:orb.mutualauth.port=3920"/>
   <arg value="myDomain"/>
</exec>
<exec executable="${glassfish.home}/bin/asadmin.bat" 
      failonerror="true"
      output="${glassfish.home}/glassfish-create-service.log">
   <arg value="create-service"/>
   <arg value="--name"/>
   <arg value="MyServiceName"/>
   <arg value="myDomain"/>
</exec>

Update Glassfish 3.0 to use Jersey 1.3

In order to update Glassfish 3.0 to use Jersey 1.3 we had to write over the Jersey files in the “modules” subfolder of the glassfish installation.

Here are some maven dependencies for the required files:

   <properties>
      <jersey.version>1.3</jersey.version>
      <jackson.version>1.5.6</jackson.version>
   </properties>
   <dependencies>
      <dependency>
         <groupId>com.sun.jersey</groupId>
         <artifactId>jersey-server</artifactId>
         <version>${jersey.version}</version>
      </dependency>
      <dependency>
         <groupId>com.sun.jersey</groupId>
         <artifactId>jersey-client</artifactId>
         <version>${jersey.version}</version>
      </dependency>
      <dependency>
         <groupId>com.sun.jersey</groupId>
         <artifactId>jersey-json</artifactId>
         <version>${jersey.version}</version>
      </dependency>
      <dependency>
         <groupId>com.sun.jersey.glassfish.v3.osgi</groupId>
         <artifactId>jersey-gf-server</artifactId>
         <version>${jersey.version}</version>
      </dependency>
     <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-xc</artifactId>
        <version>${jackson.version}</version>
     </dependency>
     <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-jaxrs</artifactId>
        <version>${jackson.version}</version>
     </dependency>
     <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-asl</artifactId>
        <version>${jackson.version}</version>
     </dependency>
     <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>${jackson.version}</version>
     </dependency> 
   </dependencies>

And here is a assembly plugin snippet which will rename the dependencies the way glassfish expects them to be named

   <dependencySets>
      <dependencySet>
         <outputFileNameMapping>${artifact.artifactId}.${artifact.extension}</outputFileNameMapping>
         <outputDirectory>modules</outputDirectory>
      </dependencySet>
   </dependencySets>

Serving static content and Jersey

If you want to serve static content from the same WAR as Jersey resources, it is possible to configure Jersey to run as a filter rather than a Servlet.

You can then define the property com.sun.jersey.config.property.WebPageContentRegex to exclude URLs matching the regular expression from being handled by Jersey. Instead request matching the pattern will fall through to the next filter and eventually the default Servlet, which can respond with your static content in the normal way.

   <filter>
      <filter-name>jerseyFilter</filter-name>
      <filter-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</filter-class>
      <init-param>
         <param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name>
         <param-value>/static/.*</param-value>
      </init-param>
   </filter>
   <filter-mapping>
      <filter-name>jerseyFilter</filter-name>
      <url-pattern>/*</url-pattern>
   </filter-mapping>

Glassfish Answer File

I’ve managed to get the glassfish v3 silent install working.

Maybe I should have just used the zip file distribution… anyway….

Here is the answer file I used. all the ${variables} need to be filled in with the values relevant to your situation. You have to pass the -s option as well as the -a option to the glassfish installer exe.

RegistrationOptions.regoptions.CREATE_NEWACCT=CREATE_NEWACCT
glassfish.Administration.HTTP_PORT=${glassfish.http.port}
updatetool.Configuration.PROXY_PORT=
glassfish.Administration.ADMIN_PASSWORD=${glassfish.admin.password}
JDKSelection.directory.JDK_FROM_LIST_CHOICE=false
RegistrationOptions.regoptions.SKIP_REGISTRATION=SKIP_REGISTRATION
updatetool.Configuration.PROXY_HOST=
SOAccountCreation.accountinfo.COUNTRY=
InstallHome.directory.INSTALL_HOME=${INSTALL_PATH}
RegistrationOptions.regoptions.USE_EXISTINGACCT=USE_EXISTINGACCT
SOAccountCreation.accountinfo.FIRSTNAME=
updatetool.Configuration.ALLOW_UPDATE_CHECK=false
glassfish.Administration.ADMIN_USER=${glassfish.admin.user}
JDKSelection.directory.JDK_LIST=
SOAccountCreation.accountinfo.PASSWORD=
SOAccountCreation.accountinfo.COMPANYNAME=
JDKSelection.directory.HIDDEN_JDK=
SOAccountCreation.accountinfo.COUNTRY_DROP_DOWN=
SOAccountCreation.accountinfo.REENTERPASSWORD=
License.license.ACCEPT_LICENSE=0
RegistrationOptions.regoptions.USERNAME=
updatetool.Configuration.BOOTSTRAP_UPDATETOOL=false
SOAccountCreation.accountinfo.LASTNAME=
glassfish.Administration.ADMIN_PORT=${glassfish.admin.port}
RegistrationOptions.regoptions.USERPASSWORD=
JDKSelection.directory.JDK_TYPED_IN_CHOICE=true
SOAccountCreation.accountinfo.EMAIL=
JDKSelection.directory.JDK_TYPE_IN=${JDKPath}
RegistrationOptions.regoptions.DUMMY_PROP=

Glassfish Automated Installation

I’m struggling with the Glassfish v3 automated installation. I’m trying to follow the instructions in the glassfish install documentation. But the variables they mention as configurable don’t correspond with the variables that are in the generated answer file.

Things I know so far are:

  • you can generate an answer file using the -n option
  • you can read the answer file in using the -a option
  • you have to specify -s if you want the installer to run silently
  • you can create a windows service using asadmin create-service which seems to be new to version 3

Using the maven-embedded-glassfish-plugin

Here is my first working XML for running unit tests against an application deployed on an embedded glassfish v3 instance:

<plugin>
   <groupId>org.glassfish</groupId>
   <artifactId>maven-embedded-glassfish-plugin</artifactId>
   <version>3.0</version>
   <configuration>
      <goalPrefix>glassfish</goalPrefix>
      <app>App.war</app>
      <port>8080</port>
      <contextRoot>App</contextRoot>
   </configuration>
   <executions>
      <execution>
         <id>start-glassfish</id>
         <phase>generate-test-resources</phase>
         <goals>
            <goal>start</goal>
         </goals>
      </execution>
      <execution>
         <id>deploy-glassfish</id>
         <phase>pre-integration-test</phase>
         <goals>
            <goal>deploy</goal>
         </goals>
      </execution>
      <execution>
         <id>stop-glassfish</id>
         <phase>post-integration-test</phase>
         <goals>
            <goal>stop</goal>
         </goals>
      </execution>
  </executions>
</plugin>

Glassfish v3 Maven plugin

Apparently there is a plugin for Maven that allows you to deploy your application against an embedded maven server http://blogs.sun.com/sirajg/entry/using_maven_plugin_for_v3

If it works, I’ll be able to get rid of all the complicated Maven XML I have for downloading and installing Glassfish V2.

There is documentation available for the plugin in the Sun GlassFish Enterprise Server v3 Embedded Server Guide

Domain properties in Glassfish

You can make properties files available to you WAR and EAR applications by putting the file in the folder glassfish/domains/${glassfish.domain.name}/lib/classes

You can use these in your spring configuration by prefixing the resource name with classpath:. For example:

<beans:bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <beans:property name="location" value="classpath:some.properties"/>
</beans:bean>