エンタープライズ:特集 2003/11/24 19:40:00 更新

[JAVA Developer特別企画]2003年12月号
AxisによるAmazon Webサービス活用記 (3/4)

JAVA Developer 2003年12月号より転載

3.Axisでスタブ生成
 AxisをインストールしてWebサービスクライアントの作成をはじめます。Webサービスクライアントは、第1段階では通常のJavaアプリケーションを作り、Webサービスにアクセスできることを確認し、第2段階でWebアプリケーションにWebサービスクライアントの機能を実装します。

●Axisのインストール
 Axisをダウンロードして、解凍します。公開されているアーカイブは、ソース版とバイナリ版で、どちらもzip形式とtar.gz形式があります。ここではzip形式のソース版(axis-1_1-src.zip)をダウンロードします(このソース版には、バイナリ版の内容も含まれています)。ダウンロードしたaxis-1_1-src.zipをAXIS_HOME(ここではC:\axis-1_1ディレクトリ)に解凍します。

図3-1
図3-1 http://ws.apache.org/axis/から「1.1 final」をクリックしたAxis 1.1のダウンロード画面

 CLASSPATHに、Axisのjarファイル(表3-1)を追加します。これでAxisの準備は整いました。

表3-1 CLASSPATHに追加するAxisのjarファイル

AXIS_HOME\lib\axis.jar
AXIS_HOME\lib\commons-discovery.jar
AXIS_HOME\lib\commons-logging.jar
AXIS_HOME\lib\jaxrpc.jar
AXIS_HOME\lib\log4j-1.2.8.jar
AXIS_HOME\lib\saaj.jar
AXIS_HOME\lib\wsdl4j.jar

●WSDL2Javaツールを使う
 Axisの便利な機能の1つが、WSDL2Javaツールです。これは、WSDLからJavaのスタブ/スケルトンコードを生成するJavaアプリケーションです。主なオプションを表3-2に示します。

表3-2 WSDL2Javaツールの主なオプション

-h ヘルプ表示
-v 情報メッセージの表示指定
-s サーバーサイド指定。省略時はクライアントスタブを生成
-p <argument> パッケージ名指定。指定時はすべてのパッケージ名がオーバーライドされる
-o <argument> 出力先ディレクトリ指定
-d <argument> スコープとして"Application"、"Request"、"Session"のいずれかを指定

 実際に、WSDL2Javaツールを使ってWSDLからスタブコードを生成します。まず作業用ディレクトリとしてWSDL_WORK(ここではC:\amazon.ws)ディレクトリを作成し、AmazonWebServices.wsdlをコピーします。コマンドプロンプトを開き、カレントディレクトリをWSDL_WORKディレクトリに移動して、次のコマンドを実行します。

java org.apache.axis.wsdl.WSDL2Java
-v -p com.amazon.soap.axis AmazonWeb
Services.wsdl
 実行が終わると、com\amazon\soap\axisディレクトリが作成され、スタブコード(Javaソース)が生成されます(図3-2)。

図3-2
図3-2 WSDL2Javaツールで生成したAmazon Webサービスのスタブコード

●スタブコードを確認
 Javaアプリケーションを作る前に生成されたスタブコードを見てみましょう。
 まずリスト3-1とリスト3-2の抽象データ型定義です。リスト3-1のProductLine型が、リスト3-2のクラスファイル名になっています。ProductLine型の要素として、xsd:string型のModeがprivate java.lang.String型のmodeに、ProductInfo型がcom.amazon.soap.axis.ProductInfo型のproductInfoにマッピングされていることがわかります。変数名は先頭文字が英小文字に変更されています。

リスト3-1 AmazonWebServices.wsdl 抽象データ型定義部(一部抜粋)

<wsdl:types>
 <xsd:schema xmlns=""
             xmlns:xsd="http://www.w3.org/2001/XMLSchema"
             targetNamespace="http://soap.amazon.com">
   <xsd:complexType name="ProductLineArray">
     <xsd:complexContent>
       <xsd:restriction base="soapenc:Array">
         <xsd:attribute ref="soapenc:arrayType"
                        wsdl:arrayType="typens:ProductLine[]"/>
       </xsd:restriction>
     </xsd:complexContent>
   </xsd:complexType>
   <xsd:complexType name="ProductLine">
     <xsd:all>
       <xsd:element name="Mode"
                    type="xsd:string" minOccurs="0"/>
       <xsd:element name="ProductInfo"
                    type="typens:ProductInfo" minOccurs="0"/>
     </xsd:all>
   </xsd:complexType>

   以下,省略

リスト3-2 ProductLine.java(一部抜粋)

/**
* ProductLine.java
*
* This file was auto-generated from WSDL
* by the Apache Axis WSDL2Java emitter.
*/

package com.amazon.soap.axis;

public class ProductLine  implements java.io.Serializable {
   private java.lang.String mode;
   private com.amazon.soap.axis.ProductInfo productInfo;

   public ProductLine() {
   }

   public java.lang.String getMode() {
       return mode;
   }

   public void setMode(java.lang.String mode) {
       this.mode = mode;
   }

   public com.amazon.soap.axis.ProductInfo getProductInfo() {
       return productInfo;
   }

   public void setProductInfo(
       com.amazon.soap.axis.ProductInfo productInfo) {
       this.productInfo = productInfo;
   }

   以下,省略

 リスト3-3とリスト3-4は、Webサービスオペレーション定義に関しては、portType要素のname属性の指定であるAmazonSearchPortがクラスファイル名になっています。operation要素のname属性の属性値がメソッド名となり、入力メッセージがメソッドのパラメータ出力メッセージが戻り値となります。すべてpublicメソッドであり、JavaのプログラムからWebサービスを使用する場合は、これらのメソッドをコールします。

リスト3-3 AmazonWebServices.wsdl Webサービスオペレーション定義部(一部抜粋)

<portType name="AmazonSearchPort">
 <!-- Port for Amazon Web APIs -->
 <operation name="KeywordSearchRequest">
   <input message="typens:KeywordSearchRequest"/>
   <output message="typens:KeywordSearchResponse"/>
 </operation>
 <operation name="TextStreamSearchRequest">
   <input message="typens:TextStreamSearchRequest"/>
   <output message="typens:TextStreamSearchResponse"/>
 </operation>
 <operation name="PowerSearchRequest">
   <input message="typens:PowerSearchRequest"/>
   <output message="typens:PowerSearchResponse"/>
 </operation>
 <operation name="BrowseNodeSearchRequest">
   <input message="typens:BrowseNodeSearchRequest"/>
   <output message="typens:BrowseNodeSearchResponse"/>
 </operation>
 <operation name="AsinSearchRequest">
   <input message="typens:AsinSearchRequest"/>
   <output message="typens:AsinSearchResponse"/>
 </operation>

   以下、省略

リスト3-4 AmazonSearchPort.java

/**
* AmazonSearchPort.java
*
* This file was auto-generated from WSDL
* by the Apache Axis WSDL2Java emitter.
*/

package com.amazon.soap.axis;

public interface AmazonSearchPort extends java.rmi.Remote {
   public com.amazon.soap.axis.ProductInfo
keywordSearchRequest(
       com.amazon.soap.axis.KeywordRequest
keywordSearchRequest)
       throws java.rmi.RemoteException;
   public com.amazon.soap.axis.ProductInfo
textStreamSearchRequest(
       com.amazon.soap.axis.TextStreamRequest
textStreamSearchRequest)
       throws java.rmi.RemoteException;
   public com.amazon.soap.axis.ProductInfo
powerSearchRequest(
       com.amazon.soap.axis.PowerRequest powerSearchRequest)
       throws java.rmi.RemoteException;
   public com.amazon.soap.axis.ProductInfo
browseNodeSearchRequest(
       com.amazon.soap.axis.BrowseNodeRequest
browseNodeSearchRequest)
       throws java.rmi.RemoteException;
   public com.amazon.soap.axis.ProductInfo
asinSearchRequest(
       com.amazon.soap.axis.AsinRequest asinSearchRequest)
       throws java.rmi.RemoteException;

   以下、省略

 リスト3-5とリスト3-6がWebサービス定義です。エンドポイントの実装は覚えておきましょう。

リスト3-5 AmazonWebServices.wsdl Webサービス定義部

<service name="AmazonSearchService">
 <!-- Endpoint for Amazon Web APIs -->
 <port name="AmazonSearchPort"
       binding="typens:AmazonSearchBinding">
   <soap:address location="http://soap.amazon.com/onca/soap3"/>
 </port>
</service>

リスト3-6 AmazonSearchServiceLocator.java

/**
* AmazonSearchServiceLocator.java
*
* This file was auto-generated from WSDL
* by the Apache Axis WSDL2Java emitter.
*/

package com.amazon.soap.axis;

public class AmazonSearchServiceLocator
      extends org.apache.axis.client.Service
      implements com.amazon.soap.axis.AmazonSearchService {

   // Use to get a proxy class for AmazonSearchPort
   private final java.lang.String AmazonSearchPort_address =
       "http://soap.amazon.com/onca/soap3";

   public java.lang.String getAmazonSearchPortAddress() {
       return AmazonSearchPort_address;
   }

   // The WSDD service name defaults to the port name.
   private java.lang.String AmazonSearchPortWSDDServiceName =
       "AmazonSearchPort";

   public java.lang.String getAmazonSearchPortWSDDServiceName() {
       return AmazonSearchPortWSDDServiceName;
   }

   public void setAmazonSearchPortWSDDServiceName(
       java.lang.String name) {
       AmazonSearchPortWSDDServiceName = name;
   }

   以下、省略

関連リンク
▼JAVA Developer
▼定期購読のご案内
▼バックナンバー販売協力店


JAVA Developer12月号表紙
JAVA Developer 12月号

大特集
UML2.0のすべて

特集2 AS徹底解説
 Cosminexus

[特別企画]
・UMLダイヤグラムベースで次世代プログラミング
・OptimalJによるMDA開発の実際
・WSDLの秘密を探る!
・AxisによるWebサービス活用記

前のページ | 1 2 3 4 | 次のページ

[松浦 武範,JAVA Developer]

Copyright(C) 2010 SOFTBANK Creative Inc. All Right Reserved.