SeamFramework.orgCommunity Documentation
GlassFish is an open source application server which fully implements Java EE 5. The latest stable release is v2 UR2.
First, we'll discuss the GlassFish environment. Then we will go over the how you deploy the jee5 example. Next, we will deploy the JPA example application. Finally we show how to get a seam-gen's generated application running on GlassFish.
All of the examples and information in this chapter are based on the the latest version of GlassFish at the time of this writing.
After downloading GlassFish, install it:
$ java -Xmx256m -jar glassfish-installer-v2ur2-b04-linux.jar
After installing, setup GlassFish:
$ cd glassfish; ant -f setup.xml
The created domain's name is domain1
.
Next, we start the embedded JavaDB server:
$ bin/asadmin start-database
JavaDB is an embedded database that is included with GlassFish, just as HSQLDB is included in JBoss AS.
Now, start the GlassFish server:
$ bin/asadmin start-domain domain1
The web administration console is available at http://localhost:4848/
. You can access the web admin console with the default username (admin
) and password (adminadmin
). We will be using the the admin console to deploy our examples. You can also copy EAR/WAR files to the glassfish/domains/domain1/autodeploy
directory to deploy them, although we are not going to cover that.
You can stop the server and database using:
$ bin/asadmin stop-domain domain1; bin/asadmin stop-database
The jee5/booking
example is based on the Hotel Booking example (which runs on JBoss AS). Out of the box it is also designed to run on GlassFish. It is located in $SEAM_DIST/examples/jee5/booking
.
To build the example, simply execute the default ant
target:
$ ant
in the examples/jee5/booking
directory. This will create the dist
and exploded-archives
directories.
We will deploy the application on GlassFish using the GlassFish admin console.
Log in to the admin console at http://localhost:4848
Access the Enterprise Applications
in the menu option under the Applications
left side menu.
At the top of the Enterprise Application
table select Deploy
. Follow through the wizard, using these hints:
Preparing for the application installation
Browse to examples/jee5/booking/dist/jboss-seam-jee5.ear
.
Selezionare il pulsante OK
.
You can now access the application at http://localhost:8081/seam-jee5/
.
This is the Hotel Booking example implemented in Seam POJOs and using Hibernate JPA with JPA transactions. It does not require EJB3 support to run on application server.
The example already has a break-out of configurations and build scripts for many of the common containers including GlassFish.
To build the example, use the glassfish
target:
$ ant glassfish
This will create the container specific dist-glassfish
and exploded-archives-glasfish
directories.
This is very similar to the jee5
example at Sezione 40.2.2, «Deploying the application to GlassFish» except that this is a war
and not an ear
.
Log in to the administration console:
http://localhost:4848
Access the Web Applications
in the menu option under the Applications
left side menu.
Preparing for the application installation
Browse to examples/jpa/dist-glassfish/jboss-seam-jpa.war
.
Selezionare il pulsante OK
.
You can now access the application at http://localhost:8081/jboss-seam-jpa/
.
examples/jpa/resources-glassfish/WEB-INF/classes/GlassfishDerbyDialect.class
is a hack to get around a Derby bug in GlassFish server. You must use it as your Hibernate dialect if you use Derby with GlassFish. Cambiamenti al file di configurazione
META-INF/persistence.xml
— the main changes needed are the datasource JNDI, switching to the GlassFish transaction manager lookup class, and changing the hibernate dialect to be GlassfishDerbyDialect
.
WEB-INF/classes/GlassfishDerbyDialect.class
— this class is needed for the Hibernate dialect change to GlassfishDerbyDialect
import.sql
— either for the dialect or Derby DB the ID
column can not be populated by this file and was removed.
seam-gen
is a very useful tool for developers to quickly get an application up and running, and provides a foundation to add your own functionality. Out of box seam-gen
will produce applications configured to run on JBoss AS. These instructions will show the steps needed to get it to run on GlassFish.
The first step is setting up seam-gen
to construct the base project. There are several choices made below, specifically the datasource and hibernate values that we will adjust once the project is created.
$ ./seam setup Buildfile: build.xml init: setup: [echo] Welcome to seam-gen :-) [input] Enter your Java project workspace (the directory that contains your Seam projects) [C:/Projects] [C:/Projects] /projects [input] Enter your JBoss home directory [C:/Program Files/jboss-4.2.3.GA] [C:/Program Files/jboss-4.2.3.GA] [input] Enter the project name [myproject] [myproject] seamgen_example [echo] Accepted project name as: seamgen_example [input] Do you want to use ICEfaces instead of RichFaces [n] (y, [n]) [input] skipping input as property icefaces.home.new has already been set. [input] Select a RichFaces skin [blueSky] ([blueSky], classic, ruby, wine, deepMarine, emeraldTown, japanCherry, DEFAULT) [input] Is this project deployed as an EAR (with EJB components) or a WAR (with no EJB support) [ear] ([ear], war) [input] Enter the Java package name for your session beans [com.mydomain.seamgen_example] [com.mydomain.seamgen_example] org.jboss.seam.tutorial.glassfish.action [input] Enter the Java package name for your entity beans [org.jboss.seam.tutorial.glassfish.action] [org.jboss.seam.tutorial.glassfish.action] org.jboss.seam.tutorial.glassfish.model [input] Enter the Java package name for your test cases [org.jboss.seam.tutorial.glassfish.action.test] [org.jboss.seam.tutorial.glassfish.action.test] org.jboss.seam.tutorial.glassfish.test [input] What kind of database are you using? [hsql] ([hsql], mysql, oracle, postgres, mssql, db2, sybase, enterprisedb, h2) [input] Enter the Hibernate dialect for your database [org.hibernate.dialect.HSQLDialect] [org.hibernate.dialect.HSQLDialect] [input] Enter the filesystem path to the JDBC driver jar [/tmp/seam/lib/hsqldb.jar] [/tmp/seam/lib/hsqldb.jar] [input] Enter JDBC driver class for your database [org.hsqldb.jdbcDriver] [org.hsqldb.jdbcDriver] [input] Enter the JDBC URL for your database [jdbc:hsqldb:.] [jdbc:hsqldb:.] [input] Enter database username [sa] [sa] [input] Enter database password [] [] [input] Enter the database schema name (it is OK to leave this blank) [] [] [input] Enter the database catalog name (it is OK to leave this blank) [] [] [input] Are you working with tables that already exist in the database? [n] (y, [n]) [input] Do you want to drop and recreate the database tables and data in import.sql each time you deploy? [n] (y, [n]) [propertyfile] Creating new property file: /home/mnovotny/workspaces/jboss/jboss-seam/seam-gen/build.properties [echo] Installing JDBC driver jar to JBoss server [copy] Copying 1 file to /home/mnovotny/workspaces/jboss/jboss-seam/seam-gen/C:/Program Files/jboss-4.2.3.GA/server/default/lib [echo] Type 'seam create-project' to create the new project BUILD SUCCESSFUL Total time: 4 minutes 5 seconds
Type $ ./seam new-project
to create your project and then cd /projects/seamgen_example
to the newly created structure.
We now need to make some changes to the generated project.
resources/META-INF/persistence-dev.xml
Alter the jta-data-source
to be jdbc/__default
. We are going to be using the integrated GlassFish Derby DB.
Replace all of the properties with the following. The key differences are briefly described in Sezione 40.3.3, «What's different for GlassFish v2 UR2»:
<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"/>
You'll need to alter persistence-prod.xml
as well if you want to deploy to GlassFish using the prod profile.
resources/GlassfishDerbyDialect.class
As with other examples we need to include this class for DB support. It can be copied from the jpa
example into the seamgen_example/resources
directory.
$ cp \ $SEAM_DIST/examples/jpa/resources-glassfish/WEB-INF/classes/GlassfishDerbyDialect.class \ ./resources
resources/META-INF/jboss-app.xml
You can delete this file as we aren't deploying to JBoss AS (jboss-app.xml
is used to enable classloading isolation in JBoss AS)
resources/*-ds.xml
You can delete these file as we aren't deploying to JBoss AS (these files define data sources in JBoss AS, we are using GlassFish's default data source)
resources/WEB-INF/components.xml
Enable container managed transaction integration - add the <transaction:ejb-transaction/>
component, and it's namespace declaration xmlns:transaction="http://jboss.com/products/seam/transaction"
Alter the jndi-pattern
to java:comp/env/seamgen_example/#{ejbName}
resources/WEB-INF/web.xml
As with the jee5/booking
example, we need to add EJB references to web.xml. Technically, the reference type is not required, but we add it here for good measure. Note that these references require the presence of an empty local-home
element to retain compatibility with a JBoss AS 4.x deployment.
<ejb-local-ref
>
<ejb-ref-name
>seamgen_example/AuthenticatorAction</ejb-ref-name
>
<ejb-ref-type
>Session</ejb-ref-type
>
<local-home/>
<local
>org.jboss.seam.tutorial.glassfish.action.Authenticator</local
>
</ejb-local-ref>
<ejb-local-ref>
<ejb-ref-name
>seamgen_example/EjbSynchronizations</ejb-ref-name
>
<ejb-ref-type
>Session</ejb-ref-type>
<local-home/>
<local
>org.jboss.seam.transaction.LocalEjbSynchronizations</local>
</ejb-local-ref
>
Keep in mind that if you are deploying to JBoss AS 4.x, and have defined the EJB references shown above in your web.xml, you will need to also define local JNDI names for each of them in jboss-web.xml, as shown below. This step is not required when deploying to GlassFish, but it's mentioned here in case you are also deploying the application to JBoss AS 4.x (not required for JBoss AS 5).
<ejb-local-ref
>
<ejb-ref-name
>seamgen_example/AuthenticatorAction</ejb-ref-name
>
<local-jndi-name
>AuthenticatorAction</local-jndi-name
>
</ejb-local-ref>
<ejb-local-ref>
<ejb-ref-name
>seamgen_example/EjbSynchronizations</ejb-ref-name
>
<local-jndi-name
>EjbSynchronizations</local-jndi-name>
</ejb-local-ref
>
We want to take the existing Authenticator
Seam POJO component and create an EJB3 out of it.
Rename the class to AuthenticatorAction
Add the @Stateless
annotation to the new AuthenticatorAction
class.
Create an interface called Authenticator
which AuthenticatorAction
implements (EJB3 requires session beans to have a local interface). Annotate the interface with @Local
, and add a single method with same signature as the authenticate
in AuthenticatorAction
.
@Name("authenticator")
@Stateless
public class AuthenticatorAction implements Authenticator {
@Local
public interface Authenticator {
public boolean authenticate();
}
We've already added its reference to the web.xml
file so we are good to go.
This application has similar requirements as the jee5/booking
example.
Change the default target to archive
(we aren't going to cover automatic deployment to GlassFish).
<project name="seamgen_example" default="archive" basedir="."
>
We need to get the GlassfishDerbyDialect.class
into our application jar. To do that find the jar
task and add the GlassfishDerbyDialect.class
line as shown 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>
...
Now we need to get extra jars into the ear
file. Look for the <copy todir="${ear.dir}/lib"
>
section of the ear
target. Add the following to the child <fileset dir="${lib.dir}"
>
element.
Aggiungere dipendenze Hibernate
<!-- 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"/>
Aggiungere dipendenze di terze parti
<!-- 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" />
You should end up with something like:
<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 your application by calling ant
in the base directory of your project (for example /projects/seamgen-example
). The target of the build will be dist/seamgen-example.ear
.
To deploy the application follow the instructions here Sezione 40.2.2, «Deploying the application to GlassFish» but use references to this project seamgen-example
instead of jboss-seam-jee5
.
Controllare l'applicazione all'indirizzo http://localhost:8081/seamgen_example/