4.5.3 INOUTパラメタを使用する場合
INOUTパラメタを使用する場合のWSDLの例およびDIIのコーディング例を示します。なお,WSDLの例では次の情報が定義されています。
-
WSDLのサービス名
名前空間URI…http://example.com
ローカル名…StockQuoteService
-
WSDLのポート名
名前空間URI…http://example.com
ローカル名…StockQuote
-
WSDLのオペレーション名
名前空間URI…http://example.com
ローカル名…getLastTradePrice
-
オペレーションのパラメタ名…in0
(1) WSDLの例(StockQuote.wsdl)
<wsdl:definitions
targetNamespace="http://example.com"
xmlns:apachesoap="http://xml.apache.org/xml-soap"
xmlns:impl="http://example.com-impl"
xmlns:intf="http://example.com"
xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<wsdl:message name="getLastTradePriceResponse">
<wsdl:part name="getLastTradePriceReturn" type="xsd:int"/>
<wsdl:part name="in0" type="xsd:string"/>
</wsdl:message>
<wsdl:message name="getLastTradePriceRequest">
<wsdl:part name="in0" type="xsd:string"/>
</wsdl:message>
<wsdl:portType name="StockQuote">
<wsdl:operation name="getLastTradePrice" parameterOrder="in0">
<wsdl:input message="intf:getLastTradePriceRequest" name="getLastTradePriceRequest"/>
<wsdl:output message="intf:getLastTradePriceResponse" name="getLastTradePriceResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="StockQuoteSoapBinding" type="intf:StockQuote">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="getLastTradePrice">
<soap:operation soapAction=""/>
<wsdl:input name="getLastTradePriceRequest">
<soap:body namespace="http://example.com" use="literal"/>
</wsdl:input>
<wsdl:output name="getLastTradePriceResponse">
<soap:body namespace="http://example.com" use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="StockQuoteService">
<wsdl:port binding="intf:StockQuoteSoapBinding" name="StockQuote">
<soap:address location="http://localhost:8081/StockQuote/services/StockQuote"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
(2) DIIのコーディング例
ClientID cltID = null;
try {
// SOAPクライアントの開始
cltID = Management.initializeClient();
// クライアント識別子と実行スレッドを関連づける
Management.connectClientIDtoCurrentThread(cltID);
// Serviceオブジェクトの生成
QName serviceName = new QName(
"http://example.com",
"StockQuoteService");
URL wsdlDocumentLocation = null;
try {
wsdlDocumentLocation = new URL("file:///F:/soap/V71/test/dii/module/fsSample/out/StockQuote.wsdl");
} catch (MalformedURLException e) {
System.out.println(e.getMessage());
}
ServiceFactory factory = null;
Service service = null;
try {
factory = ServiceFactory.newInstance();
service = factory.createService(wsdlDocumentLocation,
serviceName);
} catch (ServiceException e) {
System.out.println(e.getMessage());
}
// Callオブジェクトの生成
QName portName = new QName(
"http://example.com",
"StockQuote");
QName operationName = new QName(
"http://example.com",
"getLastTradePrice");
javax.xml.rpc.Call call = null;
try {
call = service.createCall(portName, operationName);
} catch (ServiceException e) {
System.out.println(e.getMessage());
}
// サービスメソッドの呼出し
// 入力パラメタを生成
Class inputClass = ((com.cosminexus.cws.xml.rpc.Call) call ).getParameterClassByName("in0");
Object in0 = null;
if(inputClass == String.class) {
in0 = "foo";
}
Object[] inParams = new Object[] {in0};
Object ret = null;
try {
Object ret = call.invoke(inParams);
} catch(RemoteException e) {
// 異常時の処理
Throwable cause = e.getCause();
if(cause != null) {
// 原因例外が存在する場合の処理
System.out.println(cause.getMessage());
}
}
// 出力パラメタの処理
Map out = call.getOutputParams();
Iterator itr = out.entrySet().iterator();
while(itr.hasNext()) {
Map.Entry entry = (Map.Entry) itr.next();
String key = (String) entry.getKey();
if(key.equals("in0")) {
System.out.println(entry.getValue());
}
}
// 戻り値の処理
Class retClass = ret.getClass();
int price = 0;
if(retClass == Integer.class) {
price = ((Integer)ret).intValue();
}
System.out.println("price: " + price);
} finally {
// クライアント識別子と実行スレッドの関連づけを解除する
Management.disconnectClientIDtoCurrentThread(cltID);
// SOAPクライアントの終了
Management.finalizeClient(cltID);
}例では,javax.xml.rpc.Callクラスとcom.cosminexus.cws.xml.rpc.Callクラスという二つのCallクラスが使われています。Javaでは,別パッケージに存在する,同じ名前のクラスをimport宣言に書けないため,同じクラス内でjavax.xml.rpc.Callクラスとcom.cosminexus.cws.xml.rpc.Callクラスを使用する場合には,例のように完全修飾名で指定する必要があります。
DIIでは,引数にHolderクラスを指定しません。Holderクラスに対応するJavaの基本型クラスを指定してください。例では,javax.xml.rpc.holders.StringHolderに対応するjava.lang.Stringクラスを指定しています。