SeamFramework.orgCommunity Documentation
GlassFish は、Java EE 5 を完全に実装したオープンソースのアプリケーションサーバーです。最新の安定リリースは v2 UR2 です。
まず最初に GlassFish 環境について述べて、どのように jee5 サンプルをデプロイするのかを詳しく説明します。次に、jpa サンプルアプリケーションをデプロイします。最後に、seam-gen により生成されたアプリケーションをどのようにして GlassFish 上で動作させるのかを示します。
この章のサンプルと情報は、すべて執筆時の GlassFish の最新バージョンに基づいています。
GlassFish をダウンロードして、インストールしてください。
$ java -Xmx256m -jar glassfish-installer-v2ur2-b04-linux.jar
インストールした後に、GlassFish をセットアップしてください。
$ cd glassfish; ant -f setup.xml
作成されたドメインの名前はdomain1
です。
次に、組み込み JavaDB サーバを起動します。
$ bin/asadmin start-database
JavaDB は、HSQLDB が JBoss AS に含まれているのと同様に、GlassFish の組み込みデータベースです。
最後に、GlassFish サーバを起動してください。
$ 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.
サーバとデータベースを停止するには、以下のようにしてください。
$ bin/asadmin stop-domain domain1; bin/asadmin stop-database
jee5/booking
サンプルは、(JBoss AS 上で動作する) ホテル予約サンプルに基づいています。そのままで GlassFish 上で動作するように設計されています。このサンプルは $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.
GlassFish 管理コンソールを利用して GlassFish にアプリケーションをデプロイします。
http://localhost:4848
から管理コンソールにログインします。
左側のサイドメニュー Applications (アプリケーション)
の下にあるメニューオプションで Enterprise Applications (エンタープライズアプリケーション)
にアクセスしてください。
Enterprise Application (エンタープライズアプリケーション)
テーブルのトップで Deploy (配備)
を選択してください。以下のヒントを使って、ウィザードを最後まで進めてください。
アプリケーションのインストール準備
ブラウザで examples/jee5/booking/dist/jboss-seam-jee5.ear
を指定してください。
OK
ボタンを選択してください。
http://localhost:8081/seam-jee5/
からアプリケーションにアクセスできます。
これは、Hibernate JPA と JPA トランザクションを利用して Seam POJO で実装されたホテル予約サンプルです。アプリケーションサーバで動作させるのに、EJB3 サポートを必要としません。
サンプルには、GlassFish も含めた多くのコンテナ用の構成とビルドスクリプトが既に用意されています。
サンプルをビルドするためには、glassfish
ターゲットを使用します。
$ ant glassfish
これによりコンテナに対応した dist-glassfish
ディレクトリと exploded-archives-glassfish
ディレクトリが作成されます。
This is very similar to the jee5
example at 項41.2.2. 「GlassFish へのアプリケーションのデプロイ」 except that this is a war
and not an ear
.
管理コンソールへログインしてください。
http://localhost:4848
左側のサイドメニュー Applications (アプリケーション)
の下にあるメニューオプションで Web Applications (Web アプリケーション)
にアクセスしてください。
アプリケーションのインストール準備
ブラウザで examples/jpa/dist-glassfish/jboss-seam-jpa.war
を指定してください。
OK
ボタンを選択してください。
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. 構成ファイルの変更
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
— ダイアレクトと Derby DB のいずれでも ID
カラムは生成されないので、このファイルから削除されています。
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.
第一ステップは、雛形となるプロジェクトを生成できるように seam-gen
をセットアップすることです。以下に実行したように、設定すべき項目がいくつかあります。特に、データソースと Hibernate の設定値は、プロジェクトを生成する環境に合わせて設定します。
$ ./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
プロジェクトを作成するためには、$ ./seam new-project
と入力してください。そして cd /projects/seamgen_example
と入力して新しく作成されたディレクトリへ移動してください。
生成されたプロジェクトに変更を行う必要があります。
resources/META-INF/persistence-dev.xml
jta-data-source
を jdbc/__default
に修正してください。組み込みの GlassFish Derby DB を使用します。
Replace all of the properties with the following. The key differences are briefly described in 項41.3.3. 「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"/>
prod プロファイルを使用して GlassFish にデプロイしたければ、persistence-prod.xml
も同様に修正する必要があります。
resources/GlassfishDerbyDialect.class
他のサンプルと同様に、データベースサポートのためのこのクラスを含める必要があります。jpa
サンプルから seamgen_example/resources
ディレクトリへコピーしてください。
$ cp \ $SEAM_DIST/examples/jpa/resources-glassfish/WEB-INF/classes/GlassfishDerbyDialect.class \ ./resources
resources/META-INF/jboss-app.xml
JBoss AS にはデプロイしないのでこのファイルを削除できます (JBoss AS では jboss-app.xml
を使用して、クラスローディングの分離を有効にします)
resources/*-ds.xml
JBoss AS にはデプロイしないのでこのファイルを削除できます (これらのファイルは、JBoss AS ではデータソースを定義していますが、GlassFish ではデフォルトのデータソースを使用しています)
resources/WEB-INF/components.xml
コンテナ管理トランザクション統合を有効にします - <transaction:ejb-transaction/>
コンポーネントと、その名前空間宣言 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>
既存の Authenticator
Seam POJO コンポーネントを利用して、EJB3 を作成します。
クラス名を AuthenticatorAction
に変更します。
@Stateless
アノテーションを新しい AuthenticatorAction
クラスに付与します。
AuthenticatorAction
が実装する Authenticator
という名前のインタフェースを作成してください (EJB3 ではセッション Bean にローカルインタフェースが必要)。インタフェースに @Local
アノテーションを付与し、AuthenticatorAction
の authenticate
と同じシグニチャのメソッドを一つ追加してください。
@Name("authenticator")
@Stateless
public class AuthenticatorAction implements Authenticator {
@Local
public interface Authenticator {
public boolean authenticate();
}
すでに web.xml
ファイルには EJB 参照を追加したので、次に進めます。
このアプリケーションは、jee5/booking
サンプルと同様の変更が必要となります。
デフォルトのターゲットを archive
に変更します (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.
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"/>
サードパーティ依存関係を追加してください。
<!-- 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 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>
プロジェクトのベースディレクトリ(例えば /projects/seamgen-example
)で ant
を実行してアプリケーションをビルドしてください。ビルドされるターゲットファイルは dist/seamgen-example.ear
です。
アプリケーションをデプロイするには、 項41.2.2. 「GlassFish へのアプリケーションのデプロイ」 の手順に従ってください 。但し、jboss-seam-jee5
の代わりにこのプロジェクト seamgen-example
へ置き換えて使用してください。
http://localhost:8081/seamgen_example/
をブラウズしてアプリケーションを確認してください。