Web サービスを使用して管理する必要があるネットワーク・プリンターを所有する IT 組織について検討します。 この組織では、各プリンターを、 エンドポイント参照によって指定するリソースとしています。 この例は、WebSphere Application Server および JAX-WS が提供する、Web サービス・アドレッシング (WS-Addressing) アプリケーション・プログラミング・インターフェイス (API) を使用したサービスのコード化方法を示しています。
この IT 組織では、CreatePrinter portType エレメントを提供する PrinterFactory サービスをインプリメントします。 この portType エレメントは、CreatePrinterRequest メッセージを受け入れ、論理プリンターを示すリソースを作成し、 そのリソースへの参照となるエンドポイント参照をつけて応答します。
<wsdl:definitions targetNamespace="http://example.org/printer" ... xmlns:pr=" http://example.org/printer"> <wsdl:types> ... <xsd:schema...> <xsd:element name="CreatePrinterRequest"/> <xsd:element name="CreatePrinterResponse" type="wsa:EndpointReferenceType"/> </xsd:schema> </wsdl:types> <wsdl:message name="CreatePrinterRequest"> <wsdl:part name="CreatePrinterRequest" element="pr:CreatePrinterRequest" /> </wsdl:message> <wsdl:message name="CreatePrinterResponse"> <wsdl:part name="CreatePrinterResponse" element="pr:CreatePrinterResponse" /> </wsdl:message> <wsdl:portType name="CreatePrinter"> <wsdl:operation name="createPrinter"> <wsdl:input name="CreatePrinterRequest" message="pr:CreatePrinterRequest" /> <wsdl:output name="CreatePrinterResponse" message="pr:CreatePrinterResponse" /> </wsdl:operation> </wsdl:portType> </wsdl:definitions>
上記の例の CreatePrinter オペレーションは、 新規に作成されたプリンター・リソースを 示す wsa:EndpointReference オブジェクトを戻します。 クライアントは、このエンドポイント参照を使用して、プリンターを示すサービス・インスタンスにメッセージを送信します。
以下の例にある createPrinter メソッドは、 プリンター・サービスへのエンドポイント参照を作成します。 次に、そのオペレーションは、個々のプリンター・リソース・インスタンスの ID を取得し、 その ID をエンドポイント参照に関連付けます。 最後に、createPrinter メソッドは EndpointReference オブジェクトを W3CEndpointReference オブジェクトに変換し、変換したエンドポイント参照を戻します。このオブジェクトが、新規プリンターとなります。
import com.ibm.websphere.wsaddressing.EndpointReferenceManager; import com.ibm.websphere.wsaddressing.EndpointReference; import.com.ibm.websphere.wsaddressing.jaxws.EndpointReferenceConverter; ... public Constants { // Create the printer ... // Define the printer resource ID as a static constant as it is required in later steps public static final QName PRINTER_ID_PARAM_QNAME = new QName("example.printersample","IBM_WSRF_PRINTERID", "ws-rf-pr" ); public static final QName PRINTER_SERVICE_QNAME = new QName("example.printer.com", "printer", "..."); public static final String PRINTER_ENDPOINT_NAME = new String("PrinterService"); } public EndpointReference createPrinter(java.lang.Object createPrinterRequest) { // Create an EndpointReference that targets the appropriate WebService URI and port name. EndpointReference epr = EndpointReferenceManager.createEndpointReference(PRINTER_SERVICE_QNAME, PRINTER_ENDPOINT_NAME); // Create or lookup the stateful resource and derive a resource // identifier string. String resource_identifier = ...; // Associate this resource identifier with the EndpointReference as // a reference parameter. // The choice of name is arbitrary, but should be unique // to the service. epr.setReferenceParameter(PRINTER_ID_PARAM_QNAME,resource_identifier); // The endpoint reference now targets the resource rather than the service. ... return EndpointReferenceConverter.createW3CEndpointReference(epr); }
これまでに説明した Web サービス実装によって、 プリンター・リソース・インスタンスには、 そのエンドポイント参照で固有の ID が組み込まれます。 この ID は、Web サービスをターゲットとする、 後続のメッセージの SOAP ヘッダーで、 参照パラメーターになります。 また、Web サービスが着信メッセージを 適切なプリンターとマッチングさせるために、 この ID を使用できます。
Web サービスが、WS-Addressing メッセージの アドレッシング・プロパティーを含むメッセージを 受信すると、WebSphere Application Server は、 メッセージがアプリケーションのエンドポイントに ディスパッチされる前に、プロパティーを処理し、 スレッド上のメッセージ・コンテキストの中に、 このプロパティーを設定します。 以下の例で説明されているとおり、 Printer Web サービス・アプリケーションは、WebServiceContext オブジェクトの ターゲット・エンドポイントに関連付けられている、 参照パラメーターにアクセスします。
import com.ibm.websphere.wsaddressing.EndpointReferenceManager; ... // Initialize the reference parameter name QName name = new QName(..); // Extract the String value. String resource_identifier = EndpointReferenceManager.getReferenceParameterFromMessageContext(PRINTER_ID_PARAM_QNAME);
Web サービス・インプリメンテーションは、getReferenceParameterFromMessageContext メソッドから獲得したプリンター ID に基づいて、メッセージを適切なプリンター・インスタンスに転送できます。
import javax.xml.ws.jaxwsServiceObject; import javax.xml.ws.BindingProvider; ... javax.xml.ws.Service jaxwsServiceObject= …; Printer myPrinterProxy = jaxwsServiceObject.getPort(portName, Printer.class); javax.xml.ws.BindingProvider bp = (javax.xml.ws.BindingProvider)myPrinterProxy; // Retrieve the request context for the BindingProvider object Map myMap = myBindingProvider.getRequestContext(); // Associate the endpoint reference that represents the new printer to the request context // so that the BindingProvider object now represents a specific printer instance. myMap.put(WSADDRESSING_DESTINATION_EPR, destinationEpr); ...これで、BindingProvider オブジェクトは、新規プリンター・リソース・インスタンスとなります。 クライアントはこのオブジェクトを使用して、Printer Web サービス経由でメッセージをプリンターに送信することができます。 クライアントが BindingProvider オブジェクトを起動する際に、WebSphere Application Server は該当するメッセージ・アドレッシング・プロパティーをメッセージ・ヘッダーに追加します。この場合、これは、ターゲット・プリンター・リソースを識別するエンドポイント参照内に含まれる参照パラメーターです。
代わりに、クライアントは JAX-RPC Stub オブジェクトまたは Call オブジェクトを使用することができます。クライアントはこれらを、新規プリンターを表すように構成します。 Call オブジェクトの使用方法は、以下の例のようになります。
import javax.xml.rpc.Call; ... : // Associate the endpoint reference that represents the new printer with the call. call.setProperty( "com.ibm.websphere.wsaddressing.WSAConstants. WSADDRESSING_DESTINATION_EPR ", epr);
クライアント側から見ると、エンドポイント参照は不透明です。 クライアントは、いずれのエンドポイント参照パラメーターの内容も解釈できず、 完全に使用できません。 クライアントは、エンドポイント参照のインスタンスを直接作成できません。 これは、参照パラメーターが、サービス・プロバイダー専用になっているためです。 クライアントは、サービス・プロバイダーからエンドポイント参照を取得し (例えば、プロバイダー・ファクトリー・サービスを通じて)、 そのエンドポイント参照を使用して、 エンドポイント参照によって表されている エンドポイントに、Web サービス・オペレーションを送信する必要があります。