Seam 2.1.0 Integration with GlassFish v2

Posted by    |       Seam

The Seam 2.1.0.SP1 release included a new reference guide chapter describing Seam integration with GlassFish v2. As with most container integration the bulk of the work is reconciling library differences, datasources and deployment descriptor changes. Glassfish is no different in that regard, however it is much easier to integrate and work with GlassFish than some of the other containers that I written about.

To demonstrate the ease of integration with GlassFish I'll show you how to deploy 2 different Seam examples, and migrate a seam-gen application one in one blog - and it won't even be that long - well not too long ;-)

GlasshFish Version and Setup

For this blog I used GlassFish v2ur4-b04 on a linux machine with JDK6. The instructions for installing GlassFish are at the bottom of the download page and are pretty straightforward. I'll assume that you have it installed and have created a domain as explained on the download page.

Note: all of the examples below require the embedded javaDB database that ships with GlassFish to be started. To start and stop the database execute:

$ asadmin start-database
$ asadmin stop-database

The jee5/booking Example

This example is based on the Seam Booking example and showcases how to build Java EE 5 compliant Seam application. To build and deploy it follow these steps.

  1. Navigate to the $SEAM_HOME/examples/jee5/booking directory
  2. Execute ant
  3. The example will build the ear file into the $SEAM_HOME/examples/jee5/booking/dist directory.
  4. Use the GlassFish Admin Console to deploy jboss-seam-jee5.ear as an Enterprise Application with all the default settings.
  5. Navigate to http://localhost:8080/seam-jee5/ and check out the example.

Thats it, nothing else is needed.

The jpa Example

This is the Hotel Booking example implemented with Seam POJOs and Hibernate JPA. Building and deploying this example is nearly the same as the jee5/booking example.

  1. Navigate to the $SEAM_HOME/examples/jpa directory
  2. Execute ant glassfish
  3. The example will build the war file into the $SEAM_HOME/examples/jpa/dist-glassfish directory.
  4. Use the GlassFish Admin Console to deploy jboss-seam-jpa.war as a Web Application with all the default settings.
  5. Navigate to http://localhost:8080/jboss-seam-jpa/ and check out the example.

These examples were quite easy and straightforward to build and deploy. This is partially because they were built with GlassFish integration in mind. You might be asking what actual changes are needed to deploy to GlassFish. The next section will cover that exact thing.

Migrating a seam-gen Project to GlassFish

The Seam framework uses seam-gen as a tool for developers to quickly get an application up and running, and provides a foundation to add your own functionality. seam-gen creates applications that deploy to JBoss AS. This section will detail what to change so you can deploy into GlassFish.

Initial Project Setup

First step is to get seam-gen to setup and create the base project. In the $SEAM_HOME directory execute ./seam setup. This will launch a command line questionnaire about your application. Set the JBoss home and workspace as you wish. Set the project name to seamgen_example, use RichFaces ( instead of IceFaces ), and we want to make an ear project. For all the other entries you can use the default values.

Then have seam-gen create the project by executing ./seam new-project. Then navigate to the new project for the rest of the steps.

Configuration File Changes

Below are the various changes needed to the default configuration files.

resources/META-INF/persistence-dev.xml

  • Change the jta-data-source to be jdbc/__default
  • Replace all of the persistence properties with the following:
<property name="hibernate.dialect" value="GlassFishDerbyDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider"/>
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.SunONETransactionManagerLookup"/>

resources/GlassFishDerbyDialect.class

We need to copy this file from the jpa example to work around an issue with Derby (javaDB) packaged with GlassFish. From the seamgen_example project base execute the following.

cp $SEAM_HOME/examples/jpa/resources-glassfish/WEB-INF/classes/GlassFishDerbyDialect.class ./resources

resources/WEB-INF/components.xml

  • We want GlassFish to manage our transactions so we need to add <transaction:ejb-transaction /> and its namespace xmlns:transaction="http://jboss.com/products/seam/transaction" at the top of the file.
  • Change the jndi-pattern to java:comp/env/seamgen_example/#{ejbName}

resources/WEB-INF/web.xml

We need to add the following to the bottom of web.xml so that GlassFish binds the EJBs correctly.

  <ejb-local-ref>
    <ejb-ref-name>seamgen_example/EjbSynchronizations</ejb-ref-name>  
    <ejb-ref-type>Session</ejb-ref-type>
    <local-home></local-home>
    <local>org.jboss.seam.transaction.LocalEjbSynchronizations</local>
  </ejb-local-ref>

resources/META-INF/application.xml

Our base application does not have any of its own EJBs yet, and GlassFish does not like it when an application declares an EJB module that contains no EJBs. So we need to remove the following:

 <module>
    <ejb>seamgen_example.jar</ejb>
 </module>

Updating the Build File

Now all that is left is updating the build.xml file for the seamgen_example project.

Adding GlassFishDerbyDialect.class to the Build

To do this locate the jar target and add the <include name="GlassFishDerbyDialect.class" /> where it is show below.

 <target name="jar" depends="compile,copyclasses" description="Build the distribution .jar file">
   <copy todir="${jar.dir}">
      <fileset dir="${basedir}/resources">
         <include name="seam.properties" />
         <include name="*.drl" />
         <include name="GlassFishDerbyDialect.class" />
      </fileset> 
   </copy>
  ...

Adding Dependent Libraries

Locate the ear target and add the following <include .../> elements into the <copy todir="${ear.dir}/lib"> so that the <fileset dir="${lib.dir}"> looks like this:

 <fileset dir="${lib.dir}">
   <includesfile name="deployed-jars-ear.list" />
   
   <!-- Hibernate and deps -->
   <include name="hibernate.jar"/>
   <include name="hibernate-commons-annotations.jar"/>
   <include name="hibernate-annotations.jar"/>
   <include name="hibernate-entitymanager.jar"/>
   <include name="hibernate-validator.jar"/>
   <include name="jboss-common-core.jar" />
   
   <!-- 3rd party and supporting jars -->
   <include name="javassist.jar" />
   <include name="dom4j.jar" />
   <include name="concurrent.jar" />
   <include name="cglib.jar" />
   <include name="asm.jar" />
   <include name="antlr.jar" />
   <include name="commons-logging.jar" />
   <include name="commons-collections.jar" />
 </fileset>

Build and Deploy

All that is left at this point is to build and deploy the application.

To build it all you have to do is run ant archive in the seamgen_example directory. This will create a seamgen_example/dist directory with the seamgen_example.ear file in it.

Deploy this example the same way we deployed the jee5/booking example.

Check out the fruits of your labor at http://localhost:8080/seamgen_example.

Wrap up

So this blog was a little longer than expected but we did deploy 3 applications not just 1 like the other interoperability blogs ;-) These should give you a good starting point for your Seam own applications on GlassFish.

For more details on installing GlassFish, and some of changes we made please see the Seam on GlassFish application server reference guide chapter.

Update: Some changes and fix's were recently made to the reference guide chapter. This blog contains the latest information, and updates will be in the 2.1.1.CR1 release of seam.


Back to top