wsdl と jax-rpc ソフトウェア特論 第 10 回 / 2004-07-20. お知らせ...
Post on 18-Dec-2015
217 Views
Preview:
TRANSCRIPT
WSDL と JAX-RPC
ソフトウェア特論第 10 回 / 2004-07-20
お知らせ レポート課題を出しています。
提出は 7/30 ( 金 ) まで。
きょうの目標 WSDL と JAX-RPC の関係について理
解する。
Google Web APIs とスタブの生成
Google Web APIs を使うには?
Google の Web サービスにアクセスするためのクライアントプログラムと、スタブが必要になる。
スタブの生成 (1)
Google Web APIs に付属している WSDL から生成する。自分でプログラミングする必要はない。
生成には、 JWSDP に含まれている wscompile というツールを使う。
wscompile を実行すると、 32 個のクラスファイルが生成される。
スタブの生成 (2)生成されたスタブ GoogleSearchService GoogleSearchService_Impl GoogleSearchPort GoogleSearchPort_Stub GoogleSearchResult ResultElement などなど
クライアントプログラムの作成 クライアントプログラムでは、スタブ
を利用して Google の Web サービスにアクセスする。
クライアントプログラムとWSDL の対応
WSDL の全体的な構造<definitions>
<types> …… </types><message> …… </message><portType> …… </portType><binding> …… </binding><service> …… </service>
</definitions>
WSDL の各要素の概要 service 要素では、主にエンドポイントを指
定する。 binding 要素では、主に Web サービスの通
信の方法について定める。 portType 要素は、 Java のインタフェースに
相当する。 Message 要素では、 portType 要素の inpu
t ・ output 要素で使われる型を指定する。 types 要素では、 XML Schema を使って、
新しい型を定義する。
スタブの生成 (1)
// (1) スタブの生成GoogleSearchPort_Stub stub =
(GoogleSearchPort_Stub)(new GoogleSearchService_Impl()
.getGoogleSearchPort());
スタブの生成 (2)省略せずに書いたソースGoogleSearchService service = new GoogleSearchService_Impl();GoogleSearchPort port = service.getGoogleSearchPort();GoogleSearchPort_Stub stub = (GoogleSearchPort_Stub)port;
スタブの生成 (3)service 要素の名前との対応GoogleSearchService service = new GoogleSearchService_Impl();
<service name="GoogleSearchService">……
</service>
スタブの生成 (4)
GoogleSearchService インタフェース
GoogleSearchService_Impl GoogleSearchService の実装クラス
スタブの生成 (5)portType 要素の名前との対応
GoogleSearchPort port = service.getGoogleSearchPort();
<portType name="GoogleSearchPort"> ......</portType>
スタブの生成 (6)
GoogleSearchPort_Stub stub = (GoogleSearchPort_Stub)port;
GoogleSearchPort インタフェース
GoogleSearchPort_Stub GoogleSearchPort の実装クラス
この後は、この GoogleSearchPort_Stub を操作して、 Google の Web サービスにアクセスする。
スタブにエンドポイントを設定する (1)
// (2) スタブにエンドポイントを設定する
stub._setProperty(Stub.ENDPOINT_ADDRESS_PROPERTY,“http://api.google.com/search/beta2”
);
スタブにエンドポイントを設定する (2) エンドポイント = Web サービスを提供
している URL この Web サービスは、
http://api.google.com/search/beta2 という URL にアクセスする。
<soap:address location="http://api.google.com/search/beta2"/>
Google の検索処理 (1)
// (3)Google の検索処理を呼び出す// “xxxxx” が ライセンスキー// args[0] が検索したい言葉GoogleSearchResult result =
stub.doGoogleSearch(“xxxxx", args[0], 1, 10, false, "", false, "", "", "");
Google の検索処理 (2)
stub.doGoogleSearch(……)
GoogleSearchPort_Stub クラスは、 GoogleSearchPort インタフェースを実装している。
GoogleSearchPort WSDL の portType 要素に対応
Google の検索処理 (3)WSDL の portType 要素<portType name="GoogleSearchPort"> ...... <operation name="doGoogleSearch"> <input message= "typens:doGoogleSearch"/> <output message= "typens:doGoogleSearchResponse"/> </operation></portType>
Google の検索処理 (4)GoogleSearchPort インタフェース
public interface GoogleSearchPortextends java.rmi.Remote {
public byte[] doGetCachedPage(......)throws RemoteException;
public String doSpellingSuggestion(......) throws RemoteException;
public GoogleSearchResult doGoogleSearch(......)thorws RemoteException;
}
Google の検索処理 (5)メソッド呼び出しと WSDL
stub.doGoogleSearch(……);
<operation name="doGoogleSearch"><input
message="typens:doGoogleSearch"/><output message=
"typens:doGoogleSearchResponse"/></operation>
Google の検索処理 (6)メソッドの引数stub.doGoogleSearch(“xxxxx", args[0], 1,
10, false, "", false, "", "", "");
doGoogleSearch メソッドには、 10 個の引数がある。
Google の検索処理 (7)portType 要素での引数の指定
<portType name="GoogleSearchPort"> <operation name="doGoogleSearch">
<input message= "typens:doGoogleSearch"/> <output message= "typens:doGoogleSearchResponse"/> </operation></portType>
Google の検索処理 (8)message 要素での引数の指定<message name="doGoogleSearch"> <part name="key“ type="xsd:string"/> <part name="q“ type="xsd:string"/>
......</message>
メソッドの 10 個の引数の定義は、ここにある。
Google の検索処理 (9)引数のそれぞれの型<message name="doGoogleSearch"> <part name="key“ type="xsd:string"/> <part name="start“ type="xsd:int"/>
......</message> xsd:string は Java の String 型に、 xs
d:int は Java の int 型にそれぞれ変換される。
Google の検索処理 (10)doGoogleSeach の引数の型
public GoogleSearchResult doGoogleSearch(String key, String q,int start, int maxResults, boolean filter, String restrict, boolean safeSearch,String lr, String ie, String oe)
throws RemoteException;
Google の検索処理 (11)メソッドの返値GoogleSearchResult result =
stub.doGoogleSearch(“xxxxx", args[0], 1, 10, false, "", false, "", "", "");
GoogleSearchResult 型のオブジェクトが返値となる。
Google の検索処理 (12)portType 要素での返値の指定
<portType name="GoogleSearchPort"> <operation name="doGoogleSearch">
<input message= "typens:doGoogleSearch"/> <output message= "typens:doGoogleSearchResponse"/> </operation></portType>
Google の検索処理 (13)message 要素での返値の指定<message name="doGoogleSearchResponse"> <part name=“return” type="typens:GoogleSearchResult"/>
</message>
メソッドの返値として、 GoogleSearchResult という型が定義されている。
具体的な定義は、 types 要素の中で
Google の検索処理 (14)types 要素での新しい型の指定
<xsd:complexType name="GoogleSearchResult"> <xsd:all> <xsd:element name="documentFiltering" type="xsd:boolean"/> <xsd:element name="resultElements" type="typens:ResultElementArray"/> ...... </xsd:all></xsd:complexType>
Google の検索処理 (15)types 要素と Java の型 types 要素で指定された型 (GoogleSea
rchResult) が、 Java のオブジェクトとなる。
検索結果の出力 (1)
// (4) 検索結果の出力ResultElement[] elements =
result.getResultElements();for (int i=0; i<10; i++) {
System.out.println(i);System.out.println(elements[i].getTitle());System.out.println(elements[i].getURL());System.out.println();
}
検索結果の出力 (2)result のメソッド呼び出し
result.getResultElements();
<xsd:complexType name="GoogleSearchResult"> <xsd:all>
<xsd:element name="resultElements"
type="typens:ResultElementArray"/> ...... </xsd:all></xsd:complexType>
検索結果の出力 (3)GoogleSearchResult のプロパティ
types 要素で定義された GoogleSearchResult には、いくつかの子要素がある。
そうした子要素は、 Java オブジェクトの GoogleSearchResult ではプロパティとなっている。 getter (get + 子要素名のメソッド ) でアク
セスできる。
検索結果の出力 (4)返値が新しい型になる
ResultElement[] elements = result.getResultElements();
<xsd:complexType name="GoogleSearchResult"> <xsd:all>
<xsd:element name="resultElements"
type="typens:ResultElementArray"/> ...... </xsd:all></xsd:complexType>
検索結果の出力 (5)types 要素中の ResultElement 型
<xsd:complexType name="ResultElement"> <xsd:all> <xsd:element name="URL" type="xsd:string"/> <xsd:element name="title" type="xsd:string"/> ...... </xsd:all></xsd:complexType>
検索結果の出力 (6)ResultElement オブジェクトの利用
System.out.println(elements[i].getTitle());System.out.println(elements[i].getURL());
top related