2012年12月9日日曜日

Using JRuby / JRubyを使用する

You can use JRuby on Apache OFBiz Serrvice like Jython, Javascript etc.

Prepare
  1. Install patchJython addon. see http://ofbizj.blogspot.jp/2012/12/jython-script.html
  2. Download and install JRuby. see http://www.jruby.org/download
  3. Copy jruby.jar file from <jruby_home>/lib directory to <ofbiz_home>/framework/base/lib/scripting directory.
  4. Edit <ofbiz_home>/framework/service/config/serviceengine.xml file and add next line.

    <engine name="ruby" class="org.ofbiz.service.engine.BSFEngine"/>

JythonやJavascript同様にApache OFBizのサービスでJRubyを使用することができます。

準備
  1. あらかじめpatchJython アドオンをインストールしておきます。 http://ofbizj.blogspot.jp/2012/12/jython-script.html
  2. JRubyをダウンロードしてインストールします。.  http://www.jruby.org/download
  3. jruby.jar ファイルを <jruby_home>/lib ディレクトリから <ofbiz_home>/framework/base/lib/scripting ディレクトリにコピーします。
  4. <ofbiz_home>/framework/service/config/serviceengine.xml ファイルに次の行を追加します。

    <engine name="ruby" class="org.ofbiz.service.engine.BSFEngine"/>



Using Javascript / Javascriptを使用する

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

Example
  1. In this example, modify Jython sample. see http://ofbizj.blogspot.jp/2012/12/jython-script.html
  2. In directory hot-deploy/helloworld/script/org/ofbiz/test, create file JavaScriptTest.js and edit like bellow.

    importPackage(java.lang);
    importPackage(java.util);

    response = new HashMap();
    response.put("result", "OK");
    bsf.registerBean("response", response)
    System.out.println("Hello !");

  3. Edit file hot-deploy/helloworld/servicedef/service.xml 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>
        <service auth="false" engine="javascript" invoke="" location="component://helloworld/script/org/ofbiz/test/JavaScriptTest.js" name="testJavaScript">
            <description>Test JavaScript 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.

        <request-map uri="main">
           <security https="false" auth="false"/>
           <event type="service" invoke="testJavaScript"/> <= edit 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 !".


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

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

    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>
        <service auth="false" engine="javascript" invoke="" location="component://helloworld/script/org/ofbiz/test/JavaScriptTest.js" name="testJavaScript">
            <description>Test JavaScript Service</description>
            <attribute mode="OUT" name="result" type="String">
        </attribute></service>
    </services>

  4. hot-deploy/helloworld/webapp/helloworld/WEB-INF/controller.xml ファイルを変更します。

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

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




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 !" が表示されて、サービスが呼び出されたことが確認できます。