JSF2.0+JPA2.0+eclipse 構築編:glassfish-embeddedを動かす
プロジェクトを作ったところで、次は何をするか・・・
選択肢としては
と2つくらいでしょうか。
まだ私がJSFを触ったことがないので、そちらにつなげるために、まずはJavaEEコンテナを動かすのを目標にします。
起動クラスを作る
起動クラスはアプリ本体とは関係ないので、メインのソースフォルダとは分けるべき。というわけで早くもセットアップが足りなかったことに気付いてしまいました。
src/test/javaフォルダを新しいソースフォルダとして作成します。
クラス自体はid:shinさんのをほぼ丸々写経です。過不足なくていじるところがない・・・
http://d.hatena.ne.jp/shin/20100207/p4
余談ですがid:shinさんのブログはおもしろいしためになります。
クラス名はsandbox.server.Bootにしました。
以下全文。
package sandbox.server; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import org.glassfish.api.deployment.DeployCommandParameters; import org.glassfish.api.embedded.ContainerBuilder; import org.glassfish.api.embedded.EmbeddedContainer; import org.glassfish.api.embedded.EmbeddedDeployer; import org.glassfish.api.embedded.EmbeddedFileSystem; import org.glassfish.api.embedded.LifecycleException; import org.glassfish.api.embedded.Server; /** * */ public class Boot { private static final String APPLICATION_NAME = "sandbox-application"; private static final String CONTEXT = "/"; private static final int PORT = 18080; private final String fileName; private EmbeddedDeployer deployer; private Server server; /** * @param pFileName */ public Boot(final String pFileName) { this.fileName = pFileName; } /** * */ public void boot() { try { start(); deploy(); } catch (final IOException e) { throw new IllegalStateException(e); } catch (final LifecycleException e) { throw new IllegalStateException(e); } } /** * @throws LifecycleException */ public void undeploy() throws LifecycleException { System.out.println("アンデプロイ開始…"); this.deployer.undeploy(APPLICATION_NAME, null); System.out.println("サーバー停止…"); this.server.stop(); System.out.println("サーバー停止完了"); } private void deploy() { System.out.println("デプロイ開始…"); this.deployer = this.server.getDeployer(); final DeployCommandParameters deployParams = new DeployCommandParameters(); deployParams.name = APPLICATION_NAME; deployParams.contextroot = CONTEXT; final File archive = new File(this.fileName); this.deployer.deploy(archive, deployParams); System.out.println("デプロイ完了"); } private void start() throws IOException, LifecycleException { System.out.println("サーバー起動開始…"); final Server.Builder serverBuilder = new Server.Builder("testBuilder"); final EmbeddedFileSystem.Builder fsBuilder = new EmbeddedFileSystem.Builder(); final EmbeddedFileSystem fs = fsBuilder.build(); serverBuilder.embeddedFileSystem(fs); this.server = serverBuilder.build(); final ContainerBuilder<EmbeddedContainer> containerBuilder = this.server .createConfig(ContainerBuilder.Type.web); this.server.addContainer(containerBuilder); containerBuilder.create(this.server); this.server.createPort(PORT); this.server.start(); System.out.println("サーバー起動完了"); } /** * @param args * @throws Exception */ public static void main(final String[] args) throws Exception { final Boot server = new Boot("WebContent"); server.boot(); new BufferedReader(new InputStreamReader(System.in)).readLine(); server.undeploy(); } }
おお、ソースコードのハイライト機能は便利だ。
ここでのポイントはmainメソッドでWebContentフォルダを指定しているところです。これでGlassfishがWebContentフォルダをWebアプリのルートフォルダとみなしてくれます。
さてサーバが起動しているかどうかが分かるようにindex.htmlも作成しておきます。どうせ後で消すと思うので、ごくかんたんに。
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>JSF2.0+JPA2.0+eclipse</title> </head> <body> JSF2.0+JPA2.0+eclipse </body> </html>
HTML5で記述(笑)
起動クラスの起動
さーBootクラスを起動してみます。
コンソールに
デプロイ完了
と表示されたら起動完了。ちなみに私の環境では約11秒かかります。正直、ちょっと重いです。ヘビーにJavaEEの機能を使わない場合はもっと軽いJettyという選択肢もあるのですが、今回はJSFやCDIなどの機能を手軽に使いたいので、ガマンします。
以下のURLにアクセスしてみます。
http://localhost:18080/index.html