KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > swingclient > POServiceBD


1 /* Copyright 2004 Sun Microsystems, Inc. All rights reserved. You may not modify, use, reproduce, or distribute this software except in compliance with the terms of the License at:
2  http://developer.sun.com/berkeley_license.html
3  $Id: POServiceBD.java,v 1.5 2005/05/27 23:45:28 inder Exp $ */

4
5 package com.sun.j2ee.blueprints.swingclient;
6 import java.rmi.RemoteException JavaDoc;
7 import java.net.URL JavaDoc;
8 import javax.xml.rpc.ServiceException JavaDoc;
9 import javax.xml.rpc.Call JavaDoc;
10 import javax.xml.rpc.Stub JavaDoc;
11 import javax.xml.rpc.ServiceFactory JavaDoc;
12 import javax.xml.rpc.Service JavaDoc;
13 import javax.xml.rpc.ParameterMode JavaDoc;
14 import javax.xml.namespace.QName JavaDoc;
15
16 /**
17  * This delegate class illustrates how a web service call can be made using Stubs, Dynamic Proxy, or DII.
18  * It isolates the client from Web service artifacts such as the classes generated from WSDL. To achieve that
19  * it converts the service exceptions to local exception types.
20  * @author Inderjeet Singh
21  */

22 public class POServiceBD {
23     
24     /** The namespace used by the body of the WSDL file. This constant is needed for dynamic proxy and DII. */
25     private static final String JavaDoc NS_BODY = "urn:StringPurchaseOrderService";
26     
27     public POServiceBD(String JavaDoc serviceUrl) {
28         this.serviceUrl = serviceUrl;
29     }
30     
31     /** This method shows how the submitPO web service method can be invoked using stubs */
32     public String JavaDoc submitPOUsingStubs(String JavaDoc xmlDocStr) throws InvalidPOException {
33         try {
34             com.sun.j2ee.blueprints.stringposervice.StringPurchaseOrderService_Impl svcimpl = new com.sun.j2ee.blueprints.stringposervice.StringPurchaseOrderService_Impl();
35             com.sun.j2ee.blueprints.stringposervice.StringPurchaseOrderServiceSEI poservice = svcimpl.getStringPurchaseOrderServiceSEIPort();
36             ((Stub JavaDoc)poservice)._setProperty(Stub.ENDPOINT_ADDRESS_PROPERTY, serviceUrl);
37             return poservice.submitPO(xmlDocStr);
38         } catch (com.sun.j2ee.blueprints.stringposervice.InvalidPOException e) {
39             throw new InvalidPOException(e);
40         } catch (RemoteException JavaDoc e) {
41             throw new RuntimeException JavaDoc(e);
42         }
43     }
44     
45     /** This method shows how the submitPO web service method can be invoked using a dynamic proxy */
46     public String JavaDoc submitPOUsingDynamicProxy(String JavaDoc xmlDocStr) throws InvalidPOException {
47         try {
48             ServiceFactory JavaDoc sf = ServiceFactory.newInstance();
49             URL JavaDoc wsdlURL = new URL JavaDoc(serviceUrl + "?WSDL");
50             QName JavaDoc serviceQname = new QName JavaDoc(NS_BODY, "StringPurchaseOrderService");
51             Service JavaDoc s = sf.createService(wsdlURL, serviceQname);
52             QName JavaDoc portQname = new QName JavaDoc(NS_BODY, "StringPurchaseOrderServiceSEIPort");
53             com.sun.j2ee.blueprints.stringposervice_wrapped.StringPurchaseOrderServiceSEI port = (com.sun.j2ee.blueprints.stringposervice_wrapped.StringPurchaseOrderServiceSEI)
54             s.getPort(portQname, com.sun.j2ee.blueprints.stringposervice_wrapped.StringPurchaseOrderServiceSEI.class);
55             com.sun.j2ee.blueprints.stringposervice_wrapped.SubmitPO param = new com.sun.j2ee.blueprints.stringposervice_wrapped.SubmitPO(xmlDocStr);
56             com.sun.j2ee.blueprints.stringposervice_wrapped.SubmitPOResponse response = port.submitPO(param);
57             return response.getResult();
58         } catch (com.sun.j2ee.blueprints.stringposervice_wrapped.InvalidPOException e) {
59             throw new InvalidPOException(e);
60         } catch (ServiceException JavaDoc se) {
61             throw new InvalidPOException(se);
62         } catch (java.net.MalformedURLException JavaDoc mfue) {
63             throw new RuntimeException JavaDoc(mfue);
64         } catch (RemoteException JavaDoc e) {
65             throw new RuntimeException JavaDoc(e);
66         }
67     }
68     
69     /** This method shows how the submitPO web service method can be invoked using DII */
70     public String JavaDoc submitPOUsingDII(String JavaDoc xmlDocStr) throws InvalidPOException {
71         try {
72             ServiceFactory JavaDoc sf = ServiceFactory.newInstance();
73             URL JavaDoc wsdlURL = new URL JavaDoc(serviceUrl + "?WSDL");
74             QName JavaDoc serviceQname = new QName JavaDoc(NS_BODY, "StringPurchaseOrderService");
75             Service JavaDoc s = sf.createService(wsdlURL, serviceQname);
76             QName JavaDoc portQname = new QName JavaDoc(NS_BODY, "StringPurchaseOrderServiceSEIPort");
77             
78             Call JavaDoc call = s.createCall(portQname);
79             call.setTargetEndpointAddress(serviceUrl);
80             call.setProperty(Call.SOAPACTION_USE_PROPERTY, new Boolean JavaDoc(true));
81             call.setProperty(Call.SOAPACTION_URI_PROPERTY,"");
82             
83             // For WS-I compliant document-literal, need to set the encoding style
84
// to literal by specifying "" as the encoding style,
85
// and by setting the operation style to document
86
String JavaDoc ENCODING_STYLE_PROPERTY = "javax.xml.rpc.encodingstyle.namespace.uri";
87             String JavaDoc URI_ENCODING = "";
88             call.setProperty(ENCODING_STYLE_PROPERTY, URI_ENCODING);
89             call.setProperty(Call.OPERATION_STYLE_PROPERTY, "document");
90             
91             // Note that the operation name need not be set by calling
92
// call.setOperationName(new QName(NS_BODY, "submitPO"));
93
// This is because the SOAP binding used by the Web service is document, not rpc.
94

95             // The types for the request parameter and return value are defined in the
96
// WSDL file itself, so their qnames are defined with the namespace of the body
97
QName JavaDoc requestQname = new QName JavaDoc(NS_BODY, "submitPO");
98             QName JavaDoc responseQname = new QName JavaDoc(NS_BODY, "submitPOResponse");
99             
100             // Define the type of the return value for the DII call.
101
// SubmitPOResponse must match the wrapped type sent by the Web service.
102
call.setReturnType(responseQname, com.sun.j2ee.blueprints.stringposervice_wrapped.SubmitPOResponse.class);
103             
104             // Define the type of the method parameter for the DII call.
105
// In the WSDL file, the name of the message part for submitPO is "parameters"
106
// Hence the request parameter is defined in this way.
107
call.addParameter("parameters", requestQname, com.sun.j2ee.blueprints.stringposervice_wrapped.SubmitPO.class, ParameterMode.IN);
108             com.sun.j2ee.blueprints.stringposervice_wrapped.SubmitPO param = new com.sun.j2ee.blueprints.stringposervice_wrapped.SubmitPO(xmlDocStr);
109             Object JavaDoc[] params = {param};
110             
111             // Invoke the DII call
112
com.sun.j2ee.blueprints.stringposervice_wrapped.SubmitPOResponse response = (com.sun.j2ee.blueprints.stringposervice_wrapped.SubmitPOResponse) call.invoke(params);
113             
114             return response.getResult();
115         } catch (ServiceException JavaDoc e) {
116             throw new InvalidPOException(e);
117         } catch (java.net.MalformedURLException JavaDoc mfue) {
118             throw new RuntimeException JavaDoc(mfue);
119         } catch (RemoteException JavaDoc e) {
120             throw new RuntimeException JavaDoc(e);
121         }
122     }
123     
124     private String JavaDoc serviceUrl;
125 }
126
Popular Tags