KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > ClientInvokerAdapter


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

9
10 package org.jboss.remoting;
11
12 import org.jboss.logging.Logger;
13 import org.jboss.remoting.invocation.InternalInvocation;
14 import org.jboss.remoting.transport.ClientInvoker;
15 import org.jboss.util.id.GUID;
16
17 import java.io.Serializable JavaDoc;
18 import java.util.List JavaDoc;
19
20
21
22 /**
23  * ClientInvokerAdapter.java is a ClientInterceptor for the end of the
24  * interceptor chain: it finds the right RemoteClientInvoker and calls
25  * it. This is an mbean only for ease of configuration of interceptor
26  * chains.
27  *
28  *
29  * Created: Wed Apr 30 19:30:36 2003
30  *
31  * @author <a HREF="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
32  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
33  * @version 1.0
34  *
35  * @jmx.mbean name="jboss.remoting:service=ClientInvokerAdapter"
36  * display-name="ClientInvokerAdapter"
37  * description="RemoteClientInvoker adapter that finds the RemoteClientInvoker based on the locator and calls it."
38  * @jboss.service servicefile="remoting"
39  * @jboss.xmbean
40  */

41 public class ClientInvokerAdapter
42    implements ClientInterceptor, Serializable JavaDoc
43 {
44    static final long serialVersionUID = -1795129092816780782L;
45    private static Logger log = Logger.getLogger(ClientInvokerAdapter.class);
46    transient ClassLoader JavaDoc cl;
47    String JavaDoc internalSessionId = new GUID().toString();
48
49    public ClientInvokerAdapter()
50    {
51       this(Thread.currentThread().getContextClassLoader());
52    } // ClientInvokerAdapter constructor
53

54    public ClientInvokerAdapter(ClassLoader JavaDoc cl)
55    {
56       this.cl = cl;
57    } // ClientInvokerAdapter constructor
58

59
60    // Implementation of org.jboss.remoting.ClientInterceptor
61

62    /**
63     * The <code>addListener</code> method
64     *
65     * @param invokerLocator an <code>InvokerLocator</code> value
66     * @param string a <code>String</code> value
67     * @param invokerCallbackHandler an <code>InvokerCallbackHandler</code> value
68     * @exception Throwable if an error occurs
69     */

70    public void addListener(InvokerLocator locator, String JavaDoc subsystem, InvokerCallbackHandler callbackHandler) throws Throwable JavaDoc
71    {
72       ClientInvoker invoker = getClientInvoker(locator);
73       // connect to the given client locator and add handler as listener
74
InvokerLocator clientLocator = invoker.getClientLocator();
75       if(clientLocator != null) // async callback
76
{
77             connect(clientLocator);
78             invoke(new InvocationRequest(internalSessionId,
79                                          subsystem,
80                                          new InternalInvocation(InternalInvocation.ADDCLIENTLISTENER,
81                                                                 new Object JavaDoc[]{callbackHandler}),
82                                          null,
83                                          null,
84                                          clientLocator));
85
86             disconnect(clientLocator);//seems dangerous
87
}
88         // now call server to add listener
89
invoke(new InvocationRequest(internalSessionId, subsystem, new InternalInvocation(InternalInvocation.ADDLISTENER, null), null, null, locator));
90
91
92    }
93
94    /**
95     * The <code>removeListener</code> method
96     *
97     * @param invokerLocator an <code>InvokerLocator</code> value
98     * @param string a <code>String</code> value
99     * @param invokerCallbackHandler an <code>InvokerCallbackHandler</code> value
100     * @exception Throwable if an error occurs
101     */

102    public void removeListener(InvokerLocator locator, String JavaDoc subsystem, InvokerCallbackHandler callbackHandler) throws Throwable JavaDoc
103    {
104       ClientInvoker invoker = getClientInvoker(locator);
105         // connect to the given client locator and remove handler as listener
106
InvokerLocator clientLocator = invoker.getClientLocator();
107         if(clientLocator != null) // async callback
108
{
109             connect(clientLocator);
110             invoke(new InvocationRequest(internalSessionId,
111                                          subsystem,
112                                          new InternalInvocation(InternalInvocation.REMOVECLIENTLISTENER,
113                                                                 new Object JavaDoc[]{callbackHandler}),
114                                          null,
115                                          null,
116                                          clientLocator));
117
118             disconnect(clientLocator);
119         }
120         // now call server to remove listener
121
invoke(new InvocationRequest(internalSessionId, subsystem, new InternalInvocation(InternalInvocation.REMOVELISTENER, null), null, null, locator));
122    }
123
124    /**
125     * The <code>getCallbacks</code> method
126     *
127     * @param invokerLocator an <code>InvokerLocator</code> value
128     * @param string a <code>String</code> value
129     * @return a <code>List</code> value
130     * @exception Throwable if an error occurs
131     */

132    public List JavaDoc getCallbacks(InvokerLocator locator, String JavaDoc subsystem) throws Throwable JavaDoc
133    {
134       return (List JavaDoc)invoke(new InvocationRequest(internalSessionId, subsystem, new InternalInvocation(InternalInvocation.GETCALLBACKS, null), null, null, locator));
135
136    }
137
138    /**
139     * The <code>invoke</code> method
140     *
141     * @param invokerLocator an <code>InvokerLocator</code> value
142     * @param string a <code>String</code> value
143     * @param string1 a <code>String</code> value
144     * @param object an <code>Object</code> value
145     * @param map a <code>Map</code> value
146     * @return an <code>Object</code> value
147     * @exception Throwable if an error occurs
148     */

149    public Object JavaDoc invoke(InvocationRequest invocation)
150       throws Throwable JavaDoc
151    {
152       ClientInvoker invoker = getClientInvoker(invocation.getLocator());
153       if (invoker.isConnected()==false)
154       {
155          if (log.isDebugEnabled())
156          {
157             log.debug("invoke called, but our invoker is disconnected, discarding and fetching another fresh invoker for: "+invoker.getLocator());
158          }
159          invoker.connect();
160       }
161       return invoker.invoke(invocation);
162    }
163
164    /**
165     * The <code>connect</code> method
166     *
167     * @param invokerLocator an <code>InvokerLocator</code> value
168     * @exception Exception if an error occurs
169     */

170    public void connect(InvokerLocator locator) throws Exception JavaDoc
171    {
172       ClientInvoker invoker = getClientInvoker(locator);
173       invoker.connect();
174    }
175
176    /**
177     * The <code>isConnected</code> method
178     *
179     * @param invokerLocator an <code>InvokerLocator</code> value
180     * @return a <code>boolean</code> value
181     */

182    public boolean isConnected(InvokerLocator locator) throws Exception JavaDoc
183    {
184       ClientInvoker invoker = getClientInvoker(locator);
185       return invoker.isConnected();
186    }
187
188    /**
189     * The <code>disconnect</code> method
190     *
191     * @param invokerLocator an <code>InvokerLocator</code> value
192     */

193    public void disconnect(InvokerLocator locator) throws Exception JavaDoc
194    {
195       ClientInvoker invoker = getClientInvoker(locator);
196       invoker.disconnect();
197    }
198
199    protected ClientInvoker getClientInvoker(InvokerLocator locator) throws Exception JavaDoc
200    {
201       return InvokerRegistry.createClientInvoker(locator);
202    }
203
204 } // ClientInvokerAdapter
205
Popular Tags