KickJava   Java API By Example, From Geeks To Geeks.

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


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

7 package org.jboss.webservice.client;
8
9 // $Id: PortProxy.java,v 1.4.2.8 2005/06/15 08:08:18 tdiesler Exp $
10

11 import org.jboss.axis.client.AxisClientProxy;
12 import org.jboss.logging.Logger;
13
14 import javax.xml.rpc.JAXRPCException JavaDoc;
15 import java.lang.reflect.InvocationHandler JavaDoc;
16 import java.lang.reflect.InvocationTargetException JavaDoc;
17 import java.lang.reflect.Method JavaDoc;
18 import java.lang.reflect.Proxy JavaDoc;
19 import java.rmi.Remote JavaDoc;
20 import java.rmi.RemoteException JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Arrays JavaDoc;
23 import java.util.List JavaDoc;
24
25 /**
26  * This is the proxy that implements the service endpoint interface.
27  * <p/>
28  * Additionally it handles some ws4ee functionality that is not part of jaxrpc behaviour.
29  *
30  * @author Thomas.Diesler@jboss.org
31  * @since 15-Jun-2004
32  */

33 public class PortProxy implements InvocationHandler JavaDoc
34 {
35    // provide logging
36
private static final Logger log = Logger.getLogger(PortProxy.class);
37
38    // The underlying jaxrpc call
39
private Remote JavaDoc port;
40    private CallImpl call;
41
42    // The methods from the SEI
43
private List JavaDoc seiMethods = new ArrayList JavaDoc();
44    // The methods from java.lang.Object
45
private List JavaDoc objectMethods = new ArrayList JavaDoc();
46    // The methods from javax.xml.rpc.Stub
47
private List JavaDoc stubMethods = new ArrayList JavaDoc();
48
49    // The stub property methods
50
private Method JavaDoc getPropertyMethod;
51    private Method JavaDoc setPropertyMethod;
52
53    /**
54     * Construct a client side call proxy.
55     * <p/>
56     * This proxy implements the (generated) service interface and the service endpoint interface
57     * for each port component ref.
58     *
59     * @param port The underlying proxy to the service endpoint
60     * @param seiClass The service endpoint interface
61     */

62    public PortProxy(Remote JavaDoc port, Class JavaDoc seiClass)
63    {
64       this.port = port;
65
66       // Get the underlying call object
67
AxisClientProxy axisClientProxy = (AxisClientProxy)Proxy.getInvocationHandler(port);
68       this.call = (CallImpl)axisClientProxy.getCall();
69
70       // initialize java.lang.Object methods
71
objectMethods.addAll(Arrays.asList(Object JavaDoc.class.getMethods()));
72
73       // initialize SEI methods
74
seiMethods.addAll(Arrays.asList(seiClass.getMethods()));
75
76       // initialize javax.xml.rpc.Stub methods
77
stubMethods.addAll(Arrays.asList(org.jboss.webservice.client.Stub.class.getMethods()));
78
79       // initialize javax.xml.rpc.Stub property methods
80
try
81       {
82          getPropertyMethod = Stub.class.getMethod("_getProperty", new Class JavaDoc[]{String JavaDoc.class});
83          setPropertyMethod = Stub.class.getMethod("_setProperty", new Class JavaDoc[]{String JavaDoc.class, Object JavaDoc.class});
84       }
85       catch (NoSuchMethodException JavaDoc ignore)
86       {
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 (seiMethods.contains(method))
102          {
103             log.debug("Invoke on service endpoint interface: " + methodName);
104
105             retObj = method.invoke(port, args);
106             return retObj;
107          }
108          if (stubMethods.contains(method))
109          {
110             log.debug("Invoke on stub interface: " + methodName);
111
112             // Handle standard properties
113
if ("getUsername".equals(methodName))
114                retObj = getPropertyMethod.invoke(port, new Object JavaDoc[]{Stub.USERNAME_PROPERTY});
115             else if ("setUsername".equals(methodName))
116                retObj = setPropertyMethod.invoke(port, new Object JavaDoc[]{Stub.USERNAME_PROPERTY, args[0]});
117             else if ("getPassword".equals(methodName))
118                retObj = getPropertyMethod.invoke(port, new Object JavaDoc[]{Stub.PASSWORD_PROPERTY});
119             else if ("setPassword".equals(methodName))
120                retObj = setPropertyMethod.invoke(port, new Object JavaDoc[]{Stub.PASSWORD_PROPERTY, args[0]});
121             else if ("getEndpointAddress".equals(methodName))
122                retObj = getPropertyMethod.invoke(port, new Object JavaDoc[]{Stub.ENDPOINT_ADDRESS_PROPERTY});
123             else if ("setEndpointAddress".equals(methodName))
124                retObj = setPropertyMethod.invoke(port, new Object JavaDoc[]{Stub.ENDPOINT_ADDRESS_PROPERTY, args[0]});
125             else if ("getSessionMaintain".equals(methodName))
126                retObj = getPropertyMethod.invoke(port, new Object JavaDoc[]{Stub.SESSION_MAINTAIN_PROPERTY});
127             else if ("setSessionMaintain".equals(methodName))
128                retObj = setPropertyMethod.invoke(port, new Object JavaDoc[]{Stub.SESSION_MAINTAIN_PROPERTY, args[0]});
129
130             // Handle non standard timeout
131
else if ("getTimeout".equals(methodName))
132                retObj = call.getTimeout();
133             else if ("setTimeout".equals(methodName))
134                call.setTimeout((Integer JavaDoc)args[0]);
135             else if (getPropertyMethod.equals(method) && Stub.CLIENT_TIMEOUT_PROPERTY.equals(args[0]))
136                retObj = call.getTimeout();
137             else if (setPropertyMethod.equals(method) && Stub.CLIENT_TIMEOUT_PROPERTY.equals(args[0]))
138                call.setTimeout((Integer JavaDoc)args[1]);
139
140             // Handle non standard attachments
141
else if ("addAttachment".equals(methodName))
142                call.addAttachment((String JavaDoc)args[0], args[1]);
143             else if ("removeAttachment".equals(methodName))
144                call.removeAttachment((String JavaDoc)args[0]);
145             else if ("getAttachments".equals(methodName))
146                retObj = call.getAttachmentIdentifiers();
147             else if ("getAttachment".equals(methodName))
148                retObj = call.getAttachment((String JavaDoc)args[0]);
149             else if ("getTransportOption".equals(methodName))
150                retObj = call.getTransportOption((String JavaDoc)args[0]);
151             else if ("setTransportOption".equals(methodName))
152                call.setTransportOption((String JavaDoc)args[0], args[1]);
153
154             // It's a standard Stub method
155
else
156                retObj = method.invoke(port, args);
157
158             return retObj;
159          }
160          if (objectMethods.contains(method))
161          {
162             log.debug("Invoke on object: " + methodName);
163
164             retObj = method.invoke(port, args);
165             return retObj;
166          }
167
168          throw new JAXRPCException JavaDoc("Don't know how to invoke: " + method);
169       }
170       catch (Exception JavaDoc e)
171       {
172          Throwable JavaDoc th = processException(e);
173
174          // On the client side, the JAXRPCException or runtime exception is propagated to the
175
// client code as a RemoteException or its subtype.
176
if (seiMethods.contains(method))
177          {
178             if (th instanceof JAXRPCException JavaDoc || th instanceof RuntimeException JavaDoc)
179             {
180                RemoteException JavaDoc re = new RemoteException JavaDoc(th.getMessage(), th);
181                th = re;
182             }
183          }
184
185          throw th;
186       }
187    }
188
189    /**
190     * Log the client side exception
191     */

192    private Throwable JavaDoc processException(Exception JavaDoc ex)
193    {
194       Throwable JavaDoc th = ex;
195       if (ex instanceof InvocationTargetException JavaDoc)
196          th = ((InvocationTargetException JavaDoc)ex).getTargetException();
197
198       log.error("Port error", th);
199       return th;
200    }
201 }
Popular Tags