2012年12月8日土曜日

Using Jython script / Jythonを使用する

For long years, OFBiz have impremented Jython script. But it not work well now. You can use jython script by using patchJython addon.

Prepare
  1. Download patchJython addon from http://sourceforge.jp/projects/ofbiz-jp/downloads/57320/patchJython-0.1.0-v11.04.01.zip and install.
  2. Rebuild java source by using ant command (no options).

Example
  1. In this example, use helloworld sample. see http://ofbizj.blogspot.jp/2012/11/hello-world.html
  2. Create directory hot-deploy/helloworld/script/org/ofbiz/test. In this directory, create file JythonScriptTest.py and edit like bellow.

    from java.util import HashMap
    response = HashMap()
    response.put("result", "OK")
    bsf.registerBean("response", response)
    print "Hello !"

  3. Create directory hot-deploy/helloworld/servicedef. In this directory, create file service.xml and edit like bellow.

    <services xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nonamespaceschemalocation="http://ofbiz.apache.org/dtds/services.xsd">
        <description>OFBIZ Test Services</description>
        <vendor>OFBiz</vendor>
        <version>1.0</version>

        <service auth="false" engine="jython" invoke="" location="component://helloworld/script/org/ofbiz/test/JythonScriptTest.py" name="testJythonScript">
            <description>Test JythonScript Service</description>
            <attribute mode="OUT" name="result" type="String">
        </attribute></service>

    </services>

  4. Edit hot-deploy/helloworld/webapp/helloworld/WEB-INF/controller.xml like bellow. Service invoked before view screen.

        <request-map uri="main">
           <security https="false" auth="false"/>
           <event type="service" invoke="testJythonScript"/> <= add this line
           <response name="success" type="view" value="main"/>
        </request-map>

  5. see http://localhost:8080/helloworld/control/main page using browser and see logs. Browser displays "helloworld" and in logs shows "Hello !".


昔からOFBizではJythonスクリプト言語が実装されていましたが、現在はそのままではうまく動作しない状態です。きっと今では誰も使っていないのでしょう。次の方法で使用することができます。

準備
  1. patchJython アドオンを次の場所からダウンロードします http://sourceforge.jp/projects/ofbiz-jp/downloads/57320/patchJython-0.1.0-v11.04.01.zip 。その後、adm install patchJython-0.1.0-v11.04.01コマンドでインストールします。
  2. Javaソースがパッチで変更されるのでantコマンドで再ビルドします。antコマンドのオプションは不要です。OFBIZ_HOMEディレクトリに移動してから実行してください。

実行
  1. helloworldのサンプルを使用しますのであらかじめ準備してください http://ofbizj.blogspot.jp/2012/11/hello-world.html
  2. hot-deploy/helloworld/script/org/ofbiz/test ディレクトリを作成し、その中に JythonScriptTest.py ファイルを次の内容で作成します。

    from java.util import HashMap
    response = HashMap()
    response.put("result", "OK")
    bsf.registerBean("response", response)
    print "Hello !"

  3. hot-deploy/helloworld/servicedef ディレクトリを作成し、その中に service.xml ファイルを次の内容で作成します。

    <services xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nonamespaceschemalocation="http://ofbiz.apache.org/dtds/services.xsd">
        <description>OFBIZ Test Services</description>
        <vendor>OFBiz</vendor>
        <version>1.0</version>

        <service auth="false" engine="jython" invoke="" location="component://helloworld/script/org/ofbiz/test/JythonScriptTest.py" name="testJythonScript">
            <description>Test JythonScript Service</description>
            <attribute mode="OUT" name="result" type="String">
        </attribute></service>

    </services>

  4. helloworldサンプルで作成済の hot-deploy/helloworld/webapp/helloworld/WEB-INF/controller.xml ファイルに次の1行を追加します。画面を表示する前にサービスが呼び出されます。

        <request-map uri="main">
           <security https="false" auth="false"/>
           <event type="service" invoke="testJythonScript"/> <= add this line
           <response name="success" type="view" value="main"/>
        </request-map>

  5. 次のURLをブラウザで表示します http://localhost:8080/helloworld/control/main 。画面には "helloworld" が表示されて、ログには "Hello !" が表示されて、サービスが呼び出されたことが確認できます。




0 件のコメント:

コメントを投稿