KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > webservice > client > ServiceProxy


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 // $Id: ServiceProxy.java,v 1.6.4.2 2005/04/22 11:28:32 tdiesler Exp $
9
package org.jboss.webservice.client;
10
11 // $Id: ServiceProxy.java,v 1.6.4.2 2005/04/22 11:28:32 tdiesler Exp $
12

13 import org.jboss.logging.Logger;
14 import org.jboss.webservice.metadata.jaxrpcmapping.JavaWsdlMapping;
15 import org.jboss.webservice.util.WSDLUtilities;
16
17 import javax.wsdl.Definition;
18 import javax.xml.rpc.JAXRPCException JavaDoc;
19 import javax.xml.rpc.Service JavaDoc;
20 import javax.xml.rpc.ServiceException JavaDoc;
21 import java.lang.reflect.InvocationHandler JavaDoc;
22 import java.lang.reflect.InvocationTargetException JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24 import java.rmi.Remote JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.List JavaDoc;
28
29 /**
30  * This is the proxy that implements the service interface .
31  * <p/>
32  * Additionally it handles some ws4ee functionality that is not part of jaxrpc behaviour.
33  *
34  * @author Thomas.Diesler@jboss.org
35  * @since 15-May-2004
36  */

37 public class ServiceProxy implements InvocationHandler JavaDoc
38 {
39    // provide logging
40
private static final Logger log = Logger.getLogger(ServiceProxy.class);
41
42    // The underlying jaxrpc service
43
private ServiceImpl jaxrpcService;
44
45    // The methods from java.lang.Object
46
private List JavaDoc objectMethods = new ArrayList JavaDoc();
47    // The methods from javax.xml.rpc.Service
48
private List JavaDoc jaxrpcServiceMethods = new ArrayList JavaDoc();
49    // The methods from the service interface, in case of javax.xml.rpc.Service it is empty
50
private List JavaDoc serviceInterfaceMethods = new ArrayList JavaDoc();
51    // The SEI classes that have corresponding port types
52
private List JavaDoc endorsedServiceEndpointClasses = new ArrayList JavaDoc();
53
54    // The cached getPort method
55
private Method JavaDoc getPortMethod;
56
57    /**
58     * Construct a client side service proxy.
59     * <p/>
60     * This proxy implements the (generated) service interface.
61     *
62     * @param service The underlying {@link javax.xml.rpc.Service}
63     * @param siClass The service interface, a subclass of {@link javax.xml.rpc.Service}
64     */

65    public ServiceProxy(ServiceImpl service, Class JavaDoc siClass)
66    {
67       this.jaxrpcService = service;
68
69       // initialize java.lang.Object methods
70
objectMethods.addAll(Arrays.asList(java.lang.Object JavaDoc.class.getMethods()));
71
72       // initialize javax.xml.rpc.Service methods
73
jaxrpcServiceMethods.addAll(Arrays.asList(javax.xml.rpc.Service JavaDoc.class.getMethods()));
74
75       // initialize SI methods
76
if (siClass.getName().equals("javax.xml.rpc.Service") == false)
77          serviceInterfaceMethods.addAll(Arrays.asList(siClass.getDeclaredMethods()));
78
79       // initialize special ws4ee methods
80
try
81       {
82          getPortMethod = Service JavaDoc.class.getMethod("getPort", new Class JavaDoc[]{Class JavaDoc.class});
83       }
84       catch (NoSuchMethodException JavaDoc e)
85       {
86          throw new JAXRPCException JavaDoc(e.toString());
87       }
88    }
89
90    /**
91     * Processes a method invocation on a proxy instance and returns
92     * the result.
93     */

94    public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc
95    {
96       String JavaDoc methodName = method.getName();
97
98       try
99       {
100          Object JavaDoc retObj = null;
101          if (jaxrpcServiceMethods.contains(method))
102          {
103             log.debug("Invoke on jaxrpc service: " + methodName);
104
105             if (method.getName().equals("getPort"))
106             {
107                Class JavaDoc seiClass = (Class JavaDoc)(args.length == 1 ? args[0] : args[1]);
108                endorseServiceEndpointClass(seiClass);
109
110                Remote JavaDoc port = (Remote JavaDoc)method.invoke(jaxrpcService, args);
111                return port;
112             }
113             else
114             {
115                retObj = method.invoke(jaxrpcService, args);
116                return retObj;
117             }
118          }
119          if (serviceInterfaceMethods.contains(method))
120          {
121             log.debug("Invoke on service interface: " + methodName);
122
123             Class JavaDoc seiClass = method.getReturnType();
124             retObj = getPortMethod.invoke(jaxrpcService, new Object JavaDoc[]{seiClass});
125             return retObj;
126          }
127          if (objectMethods.contains(method))
128          {
129             log.debug("Invoke on object: " + methodName);
130
131             retObj = method.invoke(jaxrpcService, args);
132             return retObj;
133          }
134
135          throw new JAXRPCException JavaDoc("Don't know how to invoke: " + method);
136       }
137       catch (Exception JavaDoc e)
138       {
139          handleException(e);
140          return null;
141       }
142    }
143
144    /**
145     * Log the client side exception
146     */

147    private void handleException(Exception JavaDoc ex) throws Throwable JavaDoc
148    {
149       Throwable JavaDoc th = ex;
150       if (ex instanceof InvocationTargetException JavaDoc)
151          th = ((InvocationTargetException JavaDoc)ex).getTargetException();
152
153       log.error("Service error", th);
154
155       throw th;
156    }
157
158    /**
159     * Check if we can find a wsdl PortType that has all SEI methods
160     */

161    private void endorseServiceEndpointClass(Class JavaDoc seiClass) throws ServiceException JavaDoc
162    {
163       // return if we already endorsed this SEI
164
if (endorsedServiceEndpointClasses.contains(seiClass))
165          return;
166
167       Definition wsdlDefinition = jaxrpcService.getWsdlDefinition();
168       JavaWsdlMapping jaxrpcMapping = jaxrpcService.getJavaWsdlMapping();
169       WSDLUtilities.endorseServiceEndpointInterface(wsdlDefinition, seiClass, jaxrpcMapping);
170
171       endorsedServiceEndpointClasses.add(seiClass);
172    }
173 }
Popular Tags