Hitachi

Cosminexus V11 アプリケーションサーバ SOAPアプリケーション開発の手引


4.5.5 配列を使用する場合

配列を使用する場合のWSDLの例およびDIIのコーディング例を示します。なお,WSDLの例では次の情報が定義されています。

〈この項の構成〉

(1) WSDLの例(StockQuote.wsdl)

<wsdl:definitions 
targetNamespace="http://example.com"
xmlns:intf="http://example.com"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 
<wsdl:types>
  <schema elementFormDefault="qualified"
   targetNamespace="http://example.com"
   xmlns="http://www.w3.org/2001/XMLSchema">
 
    <element name="getLastTradePrice">
      <complexType>
        <sequence>
          <element name="in0" maxOccurs="unbounded" minOccurs="0" type="xsd:string"/>
        </sequence>
      </complexType>
    </element>
 
    <element name="getLastTradePriceResponse">
      <complexType>
        <sequence>
          <element name="getLastTradePriceReturn" maxOccurs="unbounded" minOccurs="0" type="xsd:in/>
        </sequence>
      </complexType>
    </element>
 
  </schema>
</wsdl:types>
 
<wsdl:message name="getLastTradePriceRequest">
  <wsdl:part element="intf:getLastTradePrice" name="parameters"/>
</wsdl:message>
 
<wsdl:message name="getLastTradePriceResponse">
  <wsdl:part element="intf:getLastTradePriceResponse" name="parameters"/>
</wsdl:message>
 
<wsdl:portType name="StockQuote">
  <wsdl:operation name="getLastTradePrice">
    <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="document" transport="http://schemas.xmlsoap.org/soap/http"/>
 
  <wsdl:operation name="getLastTradePrice">
    <soap:operation soapAction=""/>
 
    <wsdl:input name="getLastTradePriceRequest">
      <soap:body use="literal"/>
    </wsdl:input>
 
    <wsdl:output name="getLastTradePriceResponse">
      <soap:body use="literal"/>
    </wsdl:output>
  </wsdl:operation>
</wsdl:binding>
 
<wsdl:service name="StockQuoteService">
  <wsdl:port binding="intf:StockQuoteSoapBinding" name="StockQuote">
    <soap:address location="http://localhost:8080/StockQuote/services/StockQuote"/>
  </wsdl:port>
</wsdl:service>
 
</wsdl:definitions>

(2) DIIのコーディング例

// SOAPクライアントの開始
ClientID cltID = Management.initializeClient();
// クライアント識別子と実行スレッドを関連づける
Management.connectClientIDtoCurrentThread(cltID);
 
try {
// Serviceオブジェクトの生成
QName serviceName = new QName(
    "http://example.com",
    "StockQuoteService");
    URL wsdlDocumentLocation = null;
try {
    wsdlDocumentLocation = new URL("file:///C:/foo/StockQuote.wsdl");
} catch (MalformedURLException e) {
// 異常時の処理
}
 
ServiceFactory factory = null;
Service service = null;
try {
    factory = ServiceFactory.newInstance();
    service = factory.createService(wsdlDocumentLocation, serviceName);
} catch (ServiceException e) {
// 異常時の処理
}
 
// 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) {
// 異常時の処理
}
 
// サービス呼び出し
// 入力パラメータを生成
Class inputClass = ((com.cosminexus.cws.xml.rpc.Call)call).getParameterClassByName("in0");
Object in0 = null;
if (inputClass == String[].class) {
    in0 = new String[]{"20070101", "20070201"};
}
 
Object[] inParams = new Object[]{in0};
Object ret = null;
try {
    ret = call.invoke(inParams);
} catch (RemoteException e) {
// 異常時の処理
    Throwable cause = e.getCause();
    if (cause != null) {
// 原因例外が存在する場合の処理
        System.out.println(cause.getMessage());
    }
}
 
// 戻り値の処理
Class retClass = ret.getClass();
int price = 0;
if (retClass == Integer[].class) {
    Integer[] prices = (Integer[])ret;
    if (prices.length > 0) {
        price = prices[0].intValue();
    }
}
System.out.println("price : " + price);
} finally {
// クライアント識別子と実行スレッドの関連づけを解除する
Management.disconnectClientIDtoCurrentThread(cltID);
// SOAPクライアントの終了
Management.finalizeClient(cltID);
}