日本語のページみてもまともに最後まで動くドキュメントがないから、本家のStartGuide?を和訳する。たぶん途中で、和訳に飽きるから、だれか気が向いたら手伝ってくれたらうれしい。
http://ws.apache.org/axis2/1_5_1/quickstartguide.html#deploy
結構はしょって訳しているかもしれないが、動作確認をおこなっている感じが好印象です。
http://daipresents.com/weblog/fujihalab/archives/2008/04/axis2-111-1.php
サービスをスタートさせよう!。以下、簡単な例を示します。それによって、サービスの構築方法や配置方法が理解できます。 StockQuoteService? という名前のサービスを用意しますが、これは、サービス構築に必須の例です。 では、以下の コード1をみてみましょう。
(訳者注:このコードは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クラスをサービス化する方法をみていきましょう。
Axis2を使って作業する前に、ちょっと準備必要です。 まず第一に、Axis2が動作する環境が整っている必要があるからです。
大抵は, さらにサービス用のWSDL fileが必要になります。 Axis2に含まれているJava2WSDLは WSDL作成ツールです。JAVAクラスからWSDLファイルを生成します。やり方の手順を示します。
(訳者注:コンパイルしたクラスがないと下記のコマンドは動きません。eclipseでコンパイルしたとすると、コンパイルしたファイルはデフォルトではclassesフォルダ以下にできるはずなので、コマンドラインでCDコマンドを使ってディレクトリを移動してから、下記のコマンドを実行すると動作します。)
%AXIS2_HOME%\bin\java2wsdl -cp . -cn samples.quickstart.service.pojo.StockQuoteService -of StockQuoteService.wsdl
$AXIS2_HOME/bin/java2wsdl -cp . -cn samples.quickstart.service.pojo.StockQuoteService -of StockQuoteService.wsdl
axis2を解凍するとサンプルがふくまれており、quickstartのサンプルにant用のbuildファイルがあります。
これを使って.aarファイルや.wsdlファイルを生成することができます。
org.apache.ws.java2wsdl.Java2WSDLTaskはaxis2-ant-plugin-1.5.1にふくまれています。
<?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>
いったんWSDLを生成していれば、お好みに合わせて修正することができます。たとえば、 オリジナルな名前への変更したい時や不都合があった時にです。
たとえば、 AXIS2_HOME/samples/quickstartadb/resources/META-INF フォルダ
(訳者注:生成コマンドを実行したフォルダはクラス生成されたフォルダであり、そこにWSDLファイルが生成されることから考えて、このフォルダはコンパイルで生成されたクラスファイルが存在したフォルダと思われる。)
にできたStockQuoteService?.wsdl は, このガイドでずっと引き合いに出すんですが, このWSDLファイルの生成コマンドで勝手につくられたパラメータを修正することになるかと思います。
構築前に助けになるのは、どのようなものが、できるのか理解しておくことです。
Axis2はどんなサーバにでも配置できます。そして、下記のような構造をもちます。コード2を示したのでご覧ください。
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
一番上からいくと、axis2-webはAXIS2の操作を構成するJSPが格納されています。これを使って、サービスを増やしたり減らしたりができます。
WEB-INF ディレクトリは contains the actual java classes and other support files to run any services deployed to the services directory.
重要なファイルはaxis2.xmlです。どのようにどのようにメッセージを受けたりするのかや、どのモジュールを適用するか決定するかなどを定義しています。
ごらんのようにサービスは *.aar ファイルとして提供されます。しかしそれらの内容は特定の方法で配備しなくてはなりません。
例として,このサービスの構成は次のようになります。
- StockQuoteService - META-INF - services.xml - lib - samples - quickstart - service - pojo - StockQuoteService.class
ここで、サービスの名前はStockQuoteService?で, この名前はservices.xmlで定義されており、 このサービスの一番上のフォルダと見なせます。コンパイル済みのJAVAクラスはそれらのパッケージ名に基づいたふさわしいディレクトリ配下に配置されています。libディレクトリはサービス固有のJARファイルなどを保持します。(この例ではないんですが、この他これらとAxis2のWARファイルとサーバ部品を格納してあるcommon JARディレクトリが格納されています。
最後に, META-INFディレクトリにはその他のAxis2がしっかり動くための追加ファイルが格納されています。services.xmlファイルは自身のサービスの定義やJavaクラスへのリンクが記載されています。ではコード3をみてみましょう。
<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>
ここでサービスは定義されています。 messageReceiverタグに関連付けたクラスを通して異なるタイプのメッセージへと変換されていきます。
あなたがこのアプリケーションを取り込むつもりならば、META-INFディレクトリにカスタマイズしたWSDLファイルをおきます。
あなたのサーブレットエンジンのディレクトリがthe webapps/axis2/WEB-INF/services だとしてそこにコピーすることでサービスを配備することができます。 (注意: Axis2のWARファイルはサーバエンジンに真っ先にインストールされるべきです。) 膨れ上がったフォーマットとして知られています。あなたは、これらをひとつの*.aarファイルに圧縮しておけます。, 同様に*.jarファイルや, *.aarファイルをサーブレットエンジンのディレクトリである webapps/axis2/WEB-INF/services ディレクトリにおきます。 .
いま、あなたはわれわれが構築の準備がもう少しで完了しようとしていることを理解できるはずです。
まず,Axis2の Standard Binaryをダウンロードし解凍してください。適切なバージョンのStandard Binary Distributionがよいでしょう。
環境変数のAXIS2_HOMEを先ほどダウンロードして解凍した展開先にあっているか確認します。
さあ、いくつかクライアントやサービスの作成方法をみていきましょう。
この章では, 5通りのWebサービスの作り方示していきます。(訳:この和訳ではPOJO(deploying Plain Old Java Objects のこと)だけだけどね)。
POJOsをつかってWebサービスを配備する手順を示します。 注意: ディレクトリ構成は AXIS2_HOME/samples/quickstart で(XMLファイルのservices.xml はこのガイドの1番最初にでてきたものです。
- quickstart - README.txt - build.xml - resources - META-INF - services.xml - src - samples - quickstart - service - pojo - StockQuoteService.java
注意: 次をタイプすることでディレクトリからWSDLを生成することができます。
ant generate.wsdl
しかしながら, StockQuoteService?.wsdl の生成はANTのオプションとなっています。 Javaクラスから直接生成されたバージョンにできます。 そしてservices.xmlはこの資料で出てきたのと同じです。
いま、コマンドのタイピングでAntからプロジェクトをビルドしました。quickstartディレクトリ内のサービスは下記の構成になっています。
- quickstart/build/classes - META-INF - services.xml - samples - quickstart - service - pojo - StockQuoteService.class
もしあなたがサービスを配置したいのなら/ 膨大なディレクトリに/ StockQuoteService?の classes ディレクトリの名前を変更して、次のあなたのサーブレットエンジンのディレクトリにコピーします。webapps/axis2/WEB-INF/services directory . さもないと, copy the build/StockQuoteService?.aar ファイルをthe webapps/axis2/WEB-INF/services ディレクトリにコピーすることになるからです。そして正しくサービスが配置されたかどうかサービス一覧で確認しましょう。
http://localhost:8080/axis2/services/listServices
または、下記のURLでWSDLを観察できます。
http://localhost:8080/axis2/services/StockQuoteService?wsdl
そしてスキーマのURL:
http://localhost:8080/axis2/services/StockQuoteService?xsd
いったんこれらのURLが動作していれば, 速やかにサービスをテストすることが可能です。下記のURLを示しますので、ご自身のブラウザで確かめてみましょう。
http://localhost:8080/axis2/services/StockQuoteService/getPrice?symbol=IBM
あなたは下記のレスポンスを得ることができたと思います。
<ns:getPriceResponse xmlns:ns="http://pojo.service.quickstart.samples/xsd"><ns:return>42</ns:return></ns:getPriceResponse>
あなたはupdate methodを実行することができます。
http://localhost:8080/axis2/services/StockQuoteService/update?symbol=IBM&price=100
そして最初にでてきたgetPriceのURLですが、あなたはupdatedメソッドで変更されたことを確認できます。