normalian blog

Let's talk about Microsoft Azure, ASP.NET and Java!

JavaEE の実行環境を Microsoft Azure の Web サイト上で稼働させる

新年初ポストは Web サイトと JavaEE について言及させていただきたい。良い子の諸君は JavaEE といえば GlassFish, WebSphere, JBoss AS, WebLogic (それぞれ正式名称が違うのはご容赦頂きたい)を想像すると思うが、Java EE6 から導入されたWeb Profile の実装である TomcatEE を Web サイト上で稼働させることで JavaEE の機能を利用することが可能となることはご存知だと思う。
今回は Tomcat EE を Web サイト上で稼働させてみるが、2015年1月時点での最新版である TomcatEE 1.7.1 の実装は以下となっているので、おおよそ JavaEE6 の機能が稼働すると考えてよいだろう。

Web サイト上での Tomcat EE の稼働

Tomcat 自体は Web サイトにバンドルされているが、まずは Web サイトのギャラリーから Tomcat を選択して Web サイトを作成する(こちらで web.config が作成されるため、TomcatEE の設定を実施しやすくなる)。
f:id:waritohutsu:20150104010257p:plain
次に、Kudu に接続して以下のコマンドを実行して Web サイト上に Tomcat EE を展開する( TomcatEE の URL はバージョン次第で変えること )。

D:\home\site\wwwroot>dir
 Volume in drive D is Windows
 Volume Serial Number is 746D-E3BB

 Directory of D:\home\site\wwwroot

01/02/2015  04:10 PM    <DIR>          .
01/02/2015  04:10 PM    <DIR>          ..
01/02/2015  04:12 PM    <DIR>          bin
01/02/2015  04:13 PM             4,828 web.config
               1 File(s)          4,828 bytes
               3 Dir(s)     767,229,952 bytes free

D:\home\site\wwwroot>cd bin

D:\home\site\wwwroot\bin>curl -O http://ftp.riken.jp/net/apache/tomee/tomee-1.7.1/apache-tomee-1.7.1-plus.zip
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 41.4M  100 41.4M    0     0  11.0M      0  0:00:03  0:00:03 --:--:-- 11.1M

D:\home\site\wwwroot\bin>unzip apache-tomee-1.7.1-plus.zip 

次に、D:\home\site\wwwroot\web.config を以下のように編集する。CATALINA_HOME を編集している点に注目してほしい。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <handlers>
            <add name="httpPlatformHandler" path="*" verb="*" 
                 modules="httpPlatformHandler" resourceType="Unspecified" />
        </handlers>
        <httpPlatform processPath="dd:\home\site\wwwroot\bin\apache-tomee-plus-1.7.1\bin\startup.bat">
            <environmentVariables>
                <environmentVariable name="CATALINA_OPTS" value="-Dport.http=%HTTP_PLATFORM_PORT%" />
                <environmentVariable name="CATALINA_HOME" value="d:\home\site\wwwroot\bin\apache-tomee-plus-1.7.1" />                
                <environmentVariable name="JAVA_OPTS" 	  value="-Djava.net.preferIPv4Stack=true" />
            </environmentVariables>
        </httpPlatform>
    </system.webServer>
</configuration>

更に、D:\home\site\wwwroot\bin\apache-tomee-plus-1.7.1\conf\server.xml を以下のように編集する(抜粋しているので注意してほしい)。Connector タグの port 属性を 8080 から #{port.http} に編集している点に注意してほしい。

        <Connector port="${port.http}" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
        <!-- A "Connector" using the shared thread pool-->

ここで Azure の管理ポータルより当該 Web サイトを再起動する。再起動後、Web サイトにアクセスすることで TomcatEE となっていることが確認できる。
f:id:waritohutsu:20150104010332p:plain

Microsoft Azure の Web サイトで CDI と JSF を動かしてみる

JavaEE の実行環境を Microsoft Azure の Web サイト上で稼働させる で TomcatEE が Web サイト上で動作することを紹介できたが、今回は実際に JSFCDI を利用したアプリケーションが動くかを検証する。前回の記事でも記載しているが、TomcatEE における JavaEE の実装は以下になる。

上記の内容は アノテーションで大混乱した JavaEE6 頃の実装だが、めげずにアプリを作成したいと思う。また、今回利用したアプリは https://github.com/normalian/MavenWebApp に配置しているので、別途参照してほしい。

アプリケーションの概要

今回作成したアプリケーションは、以下の様に JSF でのデータバインディングを行い、セッションにデータを格納して次の画面で値を表示する簡単な内容だ。
f:id:waritohutsu:20150104152517p:plain

コードの詳細は GitHub を参照いただくとして、トップ画面の XHTML, Java コードを紹介する。

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <h:form>
            Hello from Facelets &amp; Please input message
            <h:inputText value="#{viewDto.message}" /> <br />
            <h:commandButton value="submit" action="#{indexAction.doAction}" />
        </h:form>
    </h:body>
</html>
package com.mycompany.mavenwebapp.common;

import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;

@SessionScoped
@Named
public class ViewDto implements Serializable {

    private String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
package com.mycompany.mavenwebapp.action;

import com.mycompany.mavenwebapp.common.ViewDto;
import java.io.Serializable;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;

@Named
@RequestScoped
public class IndexAction implements Serializable {

    @Inject
    ViewDto viewDto;

    public String doAction() {
        System.out.println("@@@@@@@@ message = " + viewDto.getMessage());
        return "show.xhtml";
    }
}

ご覧のとおり、JSFCDI を利用した簡単なアプリケーションだ。ローカルの TomcatEE でも動作を確認後、Kudu を利用して D:\home\site\wwwroot\bin\apache-tomee-plus-1.7.1\webapps 以下に war ファイルを配置する。war 配置後は冒頭で記載した様に JSFCDI が正しく動作していることが確認できる。

異常で、Jetty や Tomcat しか動かないと思われた Web サイトでも JavaEE の機能が利用できることが紹介できた。是非皆様もいろんなハックを Web サイト上でやってみよう!