[[JAVAの記事一覧]]

*Axis2の本家のスタートガイドによるWebサービスの作り方(てきとうな和訳版) [#s4ec26d3]
*目次 [#w027dfbe]
#contents

*趣旨 [#p1d39041]
日本語のページみてもまともに最後まで動くドキュメントがないから、本家のStartGuideを和訳する。たぶん途中で、和訳に飽きるから、だれか気が向いたら手伝ってくれたらうれしい。

URLはこちら

**本家 [#f62af722]

http://ws.apache.org/axis2/1_5_1/quickstartguide.html#deploy

**同じドキュメントをだいたい翻訳している方 [#cf2e1022]

結構はしょって訳しているかもしれないが、動作確認をおこなっている感じが好印象です。

http://daipresents.com/weblog/fujihalab/archives/2008/04/axis2-111-1.php


**はじめに [#db009e6b]
サービスをスタートさせよう!。以下、簡単な例を示します。それによって、サービスの構築方法や配置方法が理解できます。
StockQuoteService という名前のサービスを用意しますが、これは、サービス構築に必須の例です。
では、以下の コード1をみてみましょう。

**コード1 StockQuoteServiceクラス [#j4c8e53f]
(訳者注:このコードはAXIS2を解凍したフォルダのquickstart以下にあります。)

 package samples.quickstart.service.pojo;
 
 import java.util.HashMap;
 
 public class StockQuoteService {
    private HashMap map = new HashMap();
 
    public double getPrice(String symbol) {
        Double price = (Double) map.get(symbol);
        if(price != null){
            return price.doubleValue();
        }
        return 42.00;
    }
 
    public void update(String symbol, double price) {
        map.put(symbol, new Double(price));
    }
 }

これは、呼び出し可能なシンプルな2つのサービスです。1つ目のgetPriceはメッセージの入出力が可能な例で。もう片方のupdateは、メッセージの戻り値がない例です。後ほど、4つの異なった方法を例に示すつもりです。

手始めに、単純なJavaクラスをサービス化する方法をみていきましょう。

**準備[#g4dae3a9]
Axis2を使って作業する前に、ちょっと準備必要です。
まず第一に、Axis2が動作する環境が整っている必要があるからです。

+.Javaのダウンロードとインストールがすんでいること(最低限 JDK1.4).環境変数 JAVA_HOME がJDKのインストールディレクトリに設定してあること.(訳者注:さらに、ディレクトリ名に半角スペースがあるデフォルトのインストール場所はスクリプトが正しく動作しないので、避けなくてはなりません。)
+.Axis2をダウンロードし、任意のディレクトリに解凍してあること。
+.axis2.warを動作させるサーブレットエンジンのwebappsディレクトリにコピーしてあること。
+.環境変数AXIS2_HOMEを上記のディレクトリに設定してあること。注意点としては、Axis2のスクリプトやバッチファイルは、Axis2が生成する際にこれらの環境変数を参照しているということです。
だからこの手順を省略してはいけません! リナックスユーザはsetenv.sh file で使うAXIS2_HOME/bin のディレクトリを環境変数AXIS2_HOMEの設定を Axis2の解凍ディレクトリのパス名に設定します。(訳者注:このあたりの訳はLinuxなので、ちょっと自信ないというか、確認してない)

大抵は, さらにサービス用のWSDL fileが必要になります。 Axis2に含まれているJava2WSDLは  WSDL作成ツールです。JAVAクラスからWSDLファイルを生成します。やり方の手順を示します。

**1.Javaクラスの作成とコンパイル [#i927b47f]
(訳者注:コンパイルしたクラスがないと下記のコマンドは動きません。eclipseでコンパイルしたとすると、コンパイルしたファイルはデフォルトではclassesフォルダ以下にできるはずなので、コマンドラインでCDコマンドを使ってディレクトリを移動してから、下記のコマンドを実行すると動作します。)

***(Windows) [#z76e9076]
 %AXIS2_HOME%\bin\java2wsdl -cp . -cn samples.quickstart.service.pojo.StockQuoteService -of StockQuoteService.wsdl

***(Linux) [#o265d424]
 $AXIS2_HOME/bin/java2wsdl -cp . -cn samples.quickstart.service.pojo.StockQuoteService -of StockQuoteService.wsdl

***(訳者注)eclipseを使った方法 [#md754762]

axis2を解凍するとサンプルがふくまれており、quickstartのサンプルにant用のbuildファイルがあります。

これを使って.aarファイルや.wsdlファイルを生成することができます。

-メモ

org.apache.ws.java2wsdl.Java2WSDLTaskはaxis2-ant-plugin-1.5.1にふくまれています。


***生成されたStockQuoteService.wsdlファイル [#tbe65b7f]
 <?xml version="1.0" encoding="UTF-8"?>
 <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:ns="http://pojo.service.quickstart.samples" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace="http://pojo.service.quickstart.samples">
    <wsdl:types>
        <xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://pojo.service.quickstart.samples">
            <xs:element name="update">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs="0" name="symbol" nillable="true" type="xs:string"/>
                        <xs:element minOccurs="0" name="price" type="xs:double"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:element name="getPrice">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs="0" name="symbol" nillable="true" type="xs:string"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:element name="getPriceResponse">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs="0" name="return" type="xs:double"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:schema>
    </wsdl:types>
    <wsdl:message name="getPriceRequest">
        <wsdl:part name="parameters" element="ns:getPrice"/>
    </wsdl:message>
    <wsdl:message name="getPriceResponse">
        <wsdl:part name="parameters" element="ns:getPriceResponse"/>
    </wsdl:message>
    <wsdl:message name="updateRequest">
        <wsdl:part name="parameters" element="ns:update"/>
    </wsdl:message>
    <wsdl:portType name="StockQuoteServicePortType">
        <wsdl:operation name="getPrice">
            <wsdl:input message="ns:getPriceRequest" wsaw:Action="urn:getPrice"/>
            <wsdl:output message="ns:getPriceResponse" wsaw:Action="urn:getPriceResponse"/>
        </wsdl:operation>
        <wsdl:operation name="update">
            <wsdl:input message="ns:updateRequest" wsaw:Action="urn:update"/>
        </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="StockQuoteServiceSoap11Binding" type="ns:StockQuoteServicePortType">
        <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
        <wsdl:operation name="getPrice">
            <soap:operation soapAction="urn:getPrice" style="document"/>
            <wsdl:input>
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
        <wsdl:operation name="update">
            <soap:operation soapAction="urn:update" style="document"/>
            <wsdl:input>
                <soap:body use="literal"/>
            </wsdl:input>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:binding name="StockQuoteServiceSoap12Binding" type="ns:StockQuoteServicePortType">
        <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
        <wsdl:operation name="getPrice">
            <soap12:operation soapAction="urn:getPrice" style="document"/>
            <wsdl:input>
                <soap12:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap12:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
        <wsdl:operation name="update">
            <soap12:operation soapAction="urn:update" style="document"/>
            <wsdl:input>
                <soap12:body use="literal"/>
            </wsdl:input>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:binding name="StockQuoteServiceHttpBinding" type="ns:StockQuoteServicePortType">
        <http:binding verb="POST"/>
        <wsdl:operation name="getPrice">
            <http:operation location="StockQuoteService/getPrice"/>
            <wsdl:input>
                <mime:content type="text/xml" part="getPrice"/>
            </wsdl:input>
            <wsdl:output>
                <mime:content type="text/xml" part="getPrice"/>
            </wsdl:output>
        </wsdl:operation>
        <wsdl:operation name="update">
            <http:operation location="StockQuoteService/update"/>
            <wsdl:input>
                <mime:content type="text/xml" part="update"/>
            </wsdl:input>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="StockQuoteService">
        <wsdl:port name="StockQuoteServiceHttpSoap11Endpoint" binding="ns:StockQuoteServiceSoap11Binding">
            <soap:address location="http://localhost:8080/axis2/services/StockQuoteService"/>
        </wsdl:port>
        <wsdl:port name="StockQuoteServiceHttpSoap12Endpoint" binding="ns:StockQuoteServiceSoap12Binding">
            <soap12:address location="http://localhost:8080/axis2/services/StockQuoteService"/>
        </wsdl:port>
        <wsdl:port name="StockQuoteServiceHttpEndpoint" binding="ns:StockQuoteServiceHttpBinding">
            <http:address location="http://localhost:8080/axis2/services/StockQuoteService"/>
        </wsdl:port>
    </wsdl:service>
 </wsdl:definitions>


**2.コマンドを使用したWSDLの生成: [#j6c42103]
いったんWSDLを生成していれば、お好みに合わせて修正することができます。たとえば、
オリジナルな名前への変更したい時や不都合があった時にです。

たとえば、  AXIS2_HOME/samples/quickstartadb/resources/META-INF フォルダにできたStockQuoteService.wsdl では, which we'll be using throughout the rest of this guide, replaces the generic parameters created by the generation process.
たとえば、  AXIS2_HOME/samples/quickstartadb/resources/META-INF フォルダ

*Axis2 Services [#n612434d]
Before we build anything, it's helpful to understand what the finished product looks like.
(訳者注:生成コマンドを実行したフォルダはクラス生成されたフォルダであり、そこにWSDLファイルが生成されることから考えて、このフォルダはコンパイルで生成されたクラスファイルが存在したフォルダと思われる。)

The server side of Axis2 can be deployed on any Servlet engine, and has the following structure. Shown in Code Listing 2.
にできたStockQuoteService.wsdl は, このガイドでずっと引き合いに出すんですが, このWSDLファイルの生成コマンドで勝手につくられたパラメータを修正することになるかと思います。

Code Listing 2: The Directory Structure of axis2.war 
*Axis2のサービス[#n612434d]
構築前に助けになるのは、どのようなものが、できるのか理解しておくことです。

Axis2はどんなサーバにでも配置できます。そして、下記のような構造をもちます。コード2を示したのでご覧ください。

**Code Listing 2: The Directory Structure of axis2.war [#udb0e9f9]

 axis2-web 
 META-INF
 WEB-INF
    classes 
    conf
        axis2.xml 
    lib
        activation.jar
        ...
        xmlSchema.jar
    modules
        modules.list 
        addressing.mar
        ...
        soapmonitor.mar
    services
        services.list
        aservice.aar
        ...
        version.aar
    web.xml

Starting at the top, axis2-web is a collection of JSPs that make up the Axis2 administration application, through which you can perform any action such as adding services and engaging and dis-engaging modules. The WEB-INF directory contains the actual java classes and other support files to run any services deployed to the services directory.
一番上からいくと、axis2-webはAXIS2の操作を構成するJSPが格納されています。これを使って、サービスを増やしたり減らしたりができます。 

The main file in all this is axis2.xml, which controls how the application deals with the received messages, determining whether Axis2 needs to apply any of the modules defined in the modules directory.
WEB-INF ディレクトリは contains the actual java classes and other support files to run any services deployed to the services directory.

Services can be deployed as *.aar files, as you can see here, but their contents must be arranged in a specific way. For example, the structure of this service will be as follows:
重要なファイルはaxis2.xmlです。どのようにどのようにメッセージを受けたりするのかや、どのモジュールを適用するか決定するかなどを定義しています。

ごらんのようにサービスは *.aar ファイルとして提供されます。しかしそれらの内容は特定の方法で配備しなくてはなりません。

例として,このサービスの構成は次のようになります。
 - StockQuoteService
   - META-INF
     - services.xml
   - lib
   - samples
     - quickstart
       - service
         - pojo
           - StockQuoteService.class

Here, the name of the service is StockQuoteService, which is specified in the services.xml file and corresponds to the top-level folder of this service. Compiled Java classes are placed underneath this in their proper place based on the package name. The lib directory holds any service-specific JAR files needed for the service to run (none in this case) besides those already stored with the Axis2 WAR file and the servlet container's common JAR directories. Finally, the META-INF directory contains any additional information about the service that Axis2 needs to execute it properly. The services.xml file defines the service itself and links the Java class to it (See Code Listing 3).
ここで、サービスの名前はStockQuoteServiceで, この名前はservices.xmlで定義されており、 このサービスの一番上のフォルダと見なせます。コンパイル済みのJAVAクラスはそれらのパッケージ名に基づいたふさわしいディレクトリ配下に配置されています。libディレクトリはサービス固有のJARファイルなどを保持します。(この例ではないんですが、この他これらとAxis2のWARファイルとサーバ部品を格納してあるcommon JARディレクトリが格納されています。

**Code Listing 3: The Service Definition File [#y5265167]
最後に, META-INFディレクトリにはその他のAxis2がしっかり動くための追加ファイルが格納されています。services.xmlファイルは自身のサービスの定義やJavaクラスへのリンクが記載されています。ではコード3をみてみましょう。
**コード3:サービス定義ファイル [#y5265167]

 <service name="StockQuoteService" scope="application">
    <description>
        Stock Quote Sample Service
    </description>
    <messageReceivers>
        <messageReceiver 
            mep="http://www.w3.org/2004/08/wsdl/in-only"
    class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>
        <messageReceiver
            mep="http://www.w3.org/2004/08/wsdl/in-out"
    class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
    </messageReceivers>
    <parameter name="ServiceClass">
        samples.quickstart.service.pojo.StockQuoteService
    </parameter>
 </service>

Here the service is defined, along with the relevant messageReceiver types for the different message exchange patterns.
ここでサービスは定義されています。 messageReceiverタグに関連付けたクラスを通して異なるタイプのメッセージへと変換されていきます。

The META-INF directory is also the location for any custom WSDL files you intend to include for this application.
あなたがこのアプリケーションを取り込むつもりならば、META-INFディレクトリにカスタマイズしたWSDLファイルをおきます。

You can deploy a service by simply taking this hierarchy of files and copying it to the webapps/axis2/WEB-INF/services directory of your servlet engine. (Note the Axis2 WAR file must be installed first in the servlet engine.) This is known as the "exploded" format. You can also compress your documents into an *.aar file, similar to a *.jar file, and place the *.aar file directly in the servlet engine's webapps/axis2/WEB-INF/services directory.
あなたのサーブレットエンジンのディレクトリがthe webapps/axis2/WEB-INF/services だとしてそこにコピーすることでサービスを配備することができます。 (注意: Axis2のWARファイルはサーバエンジンに真っ先にインストールされるべきです。) 膨れ上がったフォーマットとして知られています。あなたは、これらをひとつの*.aarファイルに圧縮しておけます。, 同様に*.jarファイルや, *.aarファイルをサーブレットエンジンのディレクトリである webapps/axis2/WEB-INF/services ディレクトリにおきます。 .

Now that you understand what we're trying to accomplish, we're almost ready to start building.
いま、あなたはわれわれが構築の準備がもう少しで完了しようとしていることを理解できるはずです。 

First, download and unzip the appropriate version of Axis2 Standard Binary Distribution. Make sure that you set the value of the AXIS2_HOME variable to match the location into which you extracted the contents of this release.
まず,Axis2の Standard Binaryをダウンロードし解凍してください。適切なバージョンのStandard Binary Distributionがよいでしょう。

Let's look at some different ways to create clients and services.
環境変数のAXIS2_HOMEを先ほどダウンロードして解凍した展開先にあっているか確認します。

さあ、いくつかクライアントやサービスの作成方法をみていきましょう。

*サービスの作成 [#v30c395c]
この章では, 5通りのWebサービスの作り方示していきます。(訳:この和訳ではPOJO(deploying Plain Old Java Objects のこと)だけだけどね)。
+StockQuoteService と命名したPOJOなクラス。
+AXIOM形式、
+Axis2のデータをくっつけるフレームワークであるADB(Axis2 Databinding Frameworkの略)
+XML
+JiBX

**JavaのPOJOを用いた作成方法 [#lbe0b35e]
POJOsをつかってWebサービスを配備する手順を示します。 
注意: ディレクトリ構成は AXIS2_HOME/samples/quickstart で(XMLファイルのservices.xml はこのガイドの1番最初にでてきたものです。

***ディレクトリ構成 [#d256eb52]
 - quickstart
   - README.txt
   - build.xml
   - resources
     - META-INF
       - services.xml
   - src
     - samples
       - quickstart
         - service
           - pojo
             - StockQuoteService.java

注意: that you can generate a WSDL from the quickstart directory by typing: 
注意: 次をタイプすることでディレクトリからWSDLを生成することができます。

 ant generate.wsdl

However, creating StockQuoteService.wsdl is optional. It can be the version generated directly from the Java class, or a customized version of that file, and that services.xml is the same file referenced earlier in this document.
しかしながら, StockQuoteService.wsdl の生成はANTのオプションとなっています。 Javaクラスから直接生成されたバージョンにできます。 そしてservices.xmlはこの資料で出てきたのと同じです。

Now build the project by typing ant generate.service in the quickstart directory, which creates the following directory structure:
いま、コマンドのタイピングでAntからプロジェクトをビルドしました。quickstartディレクトリ内のサービスは下記の構成になっています。

 - quickstart/build/classes
   - META-INF
     - services.xml
   - samples
     - quickstart
       - service
         - pojo
           - StockQuoteService.class

If you want to deploy the service in an exploded directory format, rename the classes directory to StockQuoteService, and copy it to the webapps/axis2/WEB-INF/services directory in your servlet engine. Otherwise, copy the build/StockQuoteService.aar file to the webapps/axis2/WEB-INF/services directory in your servlet engine. Then check to make sure that the service has been properly deployed by viewing the list of services at:
もしあなたがサービスを配置したいのなら/ 膨大なディレクトリに/ StockQuoteServiceの classes ディレクトリの名前を変更して、次のあなたのサーブレットエンジンのディレクトリにコピーします。webapps/axis2/WEB-INF/services directory . さもないと, copy the build/StockQuoteService.aar ファイルをthe webapps/axis2/WEB-INF/services ディレクトリにコピーすることになるからです。そして正しくサービスが配置されたかどうかサービス一覧で確認しましょう。

**サービス一覧URL [#c97a78e9]

 http://localhost:8080/axis2/services/listServices

You can also checkout the WSDL at:
または、下記のURLでWSDLを観察できます。

 http://localhost:8080/axis2/services/StockQuoteService?wsdl

And the schema at:
そしてスキーマのURL:

 http://localhost:8080/axis2/services/StockQuoteService?xsd

Once the URLs are working, quickly test the service. Try pointing your browser to the following URL:
いったんこれらのURLが動作していれば, 速やかにサービスをテストすることが可能です。下記のURLを示しますので、ご自身のブラウザで確かめてみましょう。

 http://localhost:8080/axis2/services/StockQuoteService/getPrice?symbol=IBM

You will get the following response:
あなたは下記のレスポンスを得ることができたと思います。

 <ns:getPriceResponse xmlns:ns="http://pojo.service.quickstart.samples/xsd"><ns:return>42</ns:return></ns:getPriceResponse>

If you invoke the update method as,
あなたはupdate methodを実行することができます。

 http://localhost:8080/axis2/services/StockQuoteService/update?symbol=IBM&price=100

and then execute the first getPrice URL, you will see that the price has got updated.
そして最初にでてきたgetPriceのURLですが、あなたはupdatedメソッドで変更されたことを確認できます。


トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS