Dynamic Proxyを用いたクライアントコード
まずはC:\jwsdp-1.3\docs\jwstutorial13\examples\jaxrpc\dynamicproxy\srcにあるソースをご覧ください。
package dynamicproxy; import java.net.URL; import javax.xml.rpc.Service; import javax.xml.rpc.JAXRPCException; import javax.xml.namespace.QName; import javax.xml.rpc.ServiceFactory; import dynamicproxy.HelloIF; public class HelloClient { public static void main(String[] args) { try { String UrlString = args[0] + "?WSDL"; String nameSpaceUri = "urn:Foo"; String serviceName = "MyHelloService"; String portName = "HelloIFPort"; System.out.println("UrlString = " + UrlString); URL helloWsdlUrl = new URL(UrlString); ServiceFactory serviceFactory = ServiceFactory.newInstance(); Service helloService = serviceFactory.createService(helloWsdlUrl, new QName(nameSpaceUri, serviceName)); HelloIF myProxy = (dynamicproxy.HelloIF) helloService.getPort(new QName(nameSpaceUri, portName), dynamicproxy.HelloIF.class); System.out.println(myProxy.sayHello("Buzz")); } catch (Exception ex) { ex.printStackTrace(); } } }
まず、
Service helloService = serviceFactory.createService(helloWsdlUrl, new QName(nameSpaceUri, serviceName));
としてServiceオブジェクトを作成します。引数にはWSDLのURL、サービスのURIを渡します。Serviceオブジェクトは、Dynamic Proxyのファクトリオブジェクトです。
続いて、
HelloIF myProxy = (dynamicproxy.HelloIF) helloService.getPort(new QName(nameSpaceUri, portName), dynamicproxy.HelloIF.class);
としてServiceオブジェクトからDynamic Proxy(リモートインターフェイスのクライアント側オブジェクト)を取得します。必要な情報は、ポート名のURIとリモートインターフェイスのClassオブジェクトです。ポート名(HelloIFPort)は、WSDLファイルに記述されています。
ちなみに、WSDLはこうなっています。
<?xml version="1.0" encoding="UTF-8"?> <definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="urn:Foo" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" name="MyHelloService" targetNamespace="urn:Foo"> <types/> <message name="HelloIF_sayHello"> <part name="String_1" type="xsd:string"/> </message> <message name="HelloIF_sayHelloResponse"> <part name="result" type="xsd:string"/> </message> <portType name="HelloIF"> <operation name="sayHello" parameterOrder="String_1"> <input message="tns:HelloIF_sayHello"/> <output message="tns:HelloIF_sayHelloResponse"/> </operation>] </portType> <binding name="HelloIFBinding" type="tns:HelloIF"> <operation name="sayHello"> <input> <soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" use="encoded" namespace="urn:Foo"/> </input> <output> <soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" use="encoded" namespace="urn:Foo"/> </output> <soap:operation soapAction=""/></operation> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc"/> </binding> <service name="MyHelloService"> <port name="HelloIFPort" binding="tns:HelloIFBinding"> <soap:address xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" location="http://localhost:8080/hello-jaxrpc/hello"/> </port> </service> </definitions>
下から6行目から始まるserviceタグをご覧ください。portタグで、MyHelloServiceのポート名がHelloIFPortとなっています。