KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > callback > CallbackTestClient


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.remoting.callback;
8
9 import org.jboss.dtf.MultipleTestRunner;
10 import org.jboss.logging.Logger;
11 import org.jboss.remoting.AbstractInvokerTest;
12 import org.jboss.remoting.Callback;
13 import org.jboss.remoting.Client;
14 import org.jboss.remoting.HandleCallbackException;
15 import org.jboss.remoting.InvocationRequest;
16 import org.jboss.remoting.InvokerCallbackHandler;
17 import org.jboss.remoting.InvokerLocator;
18 import org.jboss.remoting.ServerInvocationHandler;
19 import org.jboss.remoting.ServerInvoker;
20 import org.jboss.remoting.transport.Connector;
21 import org.jboss.samples.callback.CallbackServer;
22 import org.apache.log4j.Level;
23
24 import javax.management.MBeanServer JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.ArrayList JavaDoc;
28
29 /**
30  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
31  */

32 public class CallbackTestClient extends AbstractInvokerTest
33 {
34    // Default locator values
35
private static String JavaDoc transport = "socket";
36    private static String JavaDoc host = "localhost";
37    private static int port = 5500;
38
39    private String JavaDoc locatorURI = null;
40
41    private Client remotingClient;
42
43    private static final Logger log = Logger.getLogger(CallbackTestClient.class);
44
45    public static final String JavaDoc RESPONSE_VALUE = "This is the return to SampleInvocationHandler invocation";
46
47
48    public CallbackTestClient(String JavaDoc name)
49    {
50       super(name);
51    }
52
53    public CallbackTestClient(String JavaDoc name, int numberOfInstances)
54    {
55       super(name, numberOfInstances);
56    }
57
58    public CallbackTestClient(String JavaDoc name, String JavaDoc transport, int port)
59    {
60       super(name, transport, port);
61    }
62
63    public CallbackTestClient(String JavaDoc name, String JavaDoc transport, int port, int numberOfInstances)
64    {
65       super(name, transport, port, numberOfInstances);
66    }
67
68    public CallbackTestClient(String JavaDoc name, String JavaDoc locatorURI)
69    {
70       super(name);
71       this.locatorURI = locatorURI;
72    }
73
74    private void init() throws Exception JavaDoc
75    {
76 // if(remotingClient == null)
77
// {
78
createRemotingClient();
79 // }
80
}
81
82    public void createRemotingClient() throws Exception JavaDoc
83    {
84       InvokerLocator locator = new InvokerLocator(locatorURI);
85       System.out.println("Calling remoting server with locator uri of: " + locatorURI);
86
87       // This could have been new Client(locator), but want to show that subsystem param is null
88
// Could have also been new Client(locator, "sample");
89
remotingClient = new Client(locator, null);
90
91    }
92
93    public void makeInvocation() throws Throwable JavaDoc
94    {
95       Object JavaDoc response = remotingClient.invoke("Do something", null);
96       System.out.println("Invocation response: " + response);
97    }
98
99    public void testPullCallback() throws Throwable JavaDoc
100    {
101       init();
102
103       CallbackHandler callbackHandler = new CallbackHandler();
104       // by passing only the callback handler, will indicate pull callbacks
105
remotingClient.addListener(callbackHandler);
106       // now make invocation on server, which should cause a callback to happen
107
makeInvocation();
108
109       List JavaDoc callbacks = remotingClient.getCallbacks();
110       Iterator JavaDoc itr = callbacks.iterator();
111       while(itr.hasNext())
112       {
113          Object JavaDoc obj = itr.next();
114          log.info("testPullCallback - Callback object should have been " +
115                   CallbackTestServer.CALLBACK_VALUE + " and was " +
116                   (obj == null ? null : ((Callback)obj).getCallbackObject()));
117          if(obj instanceof Callback)
118          {
119             assertEquals("Callback object is NOT same.", CallbackTestServer.CALLBACK_VALUE, ((Callback) obj).getCallbackObject());
120          }
121          else
122          {
123             assertTrue("Callback object is NOT of type Callback.", false);
124          }
125       }
126
127       // remove callback handler from server
128
remotingClient.removeListener(callbackHandler);
129       remotingClient.disconnect();
130    }
131
132    public void testPushCallback() throws Throwable JavaDoc
133    {
134       init();
135
136       // Using loctor with port value one higher than the target server
137
String JavaDoc callbackLocatorURI = transport + "://" + host + ":" + (port + 5);
138       InvokerLocator callbackLocator = new InvokerLocator(callbackLocatorURI);
139
140       log.info("testPushCallback - Setting up server.");
141       // call to create remoting server to
142
// receive client callbacks.
143
setupServer(callbackLocator);
144
145       CallbackHandler callbackHandler = new CallbackHandler();
146       // Callback handle object will be passed back as part of the callback object
147
String JavaDoc callbackHandleObject = "myCallbackHandleObject";
148       log.info("testPushCallback - adding listener.");
149       // by passing only the callback handler, will indicate pull callbacks
150
remotingClient.addListener(callbackHandler, callbackLocator, callbackHandleObject);
151       log.info("testPushCallback - make invocation");
152       // now make invocation on server, which should cause a callback to happen
153
makeInvocation();
154
155       // need to wait for brief moment so server can callback
156
Thread.sleep(5000);
157
158       Callback callback = callbackHandler.getCallback();
159       log.info("testPushCallback - Callback returned was " + callback);
160       assertNotNull("Callback returned was null.", callback);
161
162       Object JavaDoc callbackObj = callback.getCallbackObject();
163       log.info("testPushCallback - Callback value should have been " + CallbackTestServer.CALLBACK_VALUE + ", and was " + callbackObj);
164       assertEquals("Callback value should have been " + CallbackTestServer.CALLBACK_VALUE + ", but was " + callbackObj,
165                    CallbackTestServer.CALLBACK_VALUE, callbackObj);
166       Object JavaDoc callbackHandleObj = callback.getCallbackHandleObject();
167       log.info("testPushCallback - Callback handle object should have been " + callbackHandleObject + ", and was " + callbackHandleObj);
168       assertEquals("Callback handle object should have been " + callbackHandleObject + ", but was " + callbackHandleObj,
169                    callbackHandleObject, callbackHandleObj );
170       InvokerLocator serverLoc = callback.getServerLocator();
171       log.info("testPushCallback - Callback server locator should have been " + remotingClient.getInvoker().getLocator() +
172                    ", and was " + serverLoc);
173       assertEquals("Callback server locator should have been " + remotingClient.getInvoker().getLocator() +
174                    ", but was " + serverLoc,
175                    remotingClient.getInvoker().getLocator(), serverLoc);
176
177       // remove callback handler from server
178
remotingClient.removeListener(callbackHandler);
179
180    }
181
182    public void setupServer(InvokerLocator locator) throws Exception JavaDoc
183    {
184       log.info("Starting remoting server with locator uri of: " + locator);
185       try
186       {
187          Connector connector = new Connector();
188          connector.setInvokerLocator(locator.getLocatorURI());
189          connector.start();
190
191          SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
192          // first parameter is sub-system name. can be any String value.
193
connector.addInvocationHandler("sample", invocationHandler);
194       }
195       catch(Exception JavaDoc e)
196       {
197          log.error("Error starting callback server", e);
198          throw e;
199       }
200    }
201
202
203    /**
204     * Can pass transport and port to be used as parameters.
205     * Valid transports are 'rmi' and 'socket'.
206     *
207     * @param args
208     */

209    public static void main(String JavaDoc[] args)
210    {
211       if(args != null && args.length == 2)
212       {
213          transport = args[0];
214          port = Integer.parseInt(args[1]);
215       }
216       String JavaDoc locatorURI = transport + "://" + host + ":" + port;
217       CallbackTestClient client = new CallbackTestClient(CallbackTestClient.class.getName(), locatorURI);
218
219       //client.setLogging();
220
try
221       {
222          MultipleTestRunner runner = new MultipleTestRunner();
223          runner.doRun(client, true);
224       }
225       catch(Throwable JavaDoc e)
226       {
227          e.printStackTrace();
228          System.exit(1);
229       }
230       System.exit(0);
231    }
232
233    /**
234     * Simple invocation handler implementation.
235     */

236    public static class SampleInvocationHandler implements ServerInvocationHandler
237    {
238
239       private List JavaDoc listeners = new ArrayList JavaDoc();
240
241
242       /**
243        * called to handle a specific invocation
244        *
245        * @param invocation
246        * @return
247        * @throws Throwable
248        */

249       public Object JavaDoc invoke(InvocationRequest invocation) throws Throwable JavaDoc
250       {
251          // Just going to return static string as this is just simple example code.
252

253          // Will also fire callback to listeners if they were to exist using
254
// simple invocation request.
255
Callback callback = new Callback("This is the payload of callback invocation.");
256          Iterator JavaDoc itr = listeners.iterator();
257          while(itr.hasNext())
258          {
259             InvokerCallbackHandler callbackHandler = (InvokerCallbackHandler) itr.next();
260             try
261             {
262                callbackHandler.handleCallback(callback);
263             }
264             catch(HandleCallbackException e)
265             {
266                e.printStackTrace();
267             }
268          }
269
270          return RESPONSE_VALUE;
271
272       }
273
274       /**
275        * Adds a callback handler that will listen for callbacks from
276        * the server invoker handler.
277        *
278        * @param callbackHandler
279        */

280       public void addListener(InvokerCallbackHandler callbackHandler)
281       {
282          listeners.add(callbackHandler);
283       }
284
285       /**
286        * Removes the callback handler that was listening for callbacks
287        * from the server invoker handler.
288        *
289        * @param callbackHandler
290        */

291       public void removeListener(InvokerCallbackHandler callbackHandler)
292       {
293          listeners.remove(callbackHandler);
294       }
295
296       /**
297        * set the mbean server that the handler can reference
298        *
299        * @param server
300        */

301       public void setMBeanServer(MBeanServer JavaDoc server)
302       {
303          // NO OP as do not need reference to MBeanServer for this handler
304
}
305
306       /**
307        * set the invoker that owns this handler
308        *
309        * @param invoker
310        */

311       public void setInvoker(ServerInvoker invoker)
312       {
313          // NO OP as do not need reference back to the server invoker
314
}
315
316    }
317
318
319    public class CallbackHandler implements InvokerCallbackHandler
320    {
321       private Callback callback;
322
323       /**
324        * Will take the callback message and send back to client.
325        * If client locator is null, will store them till client polls to get them.
326        *
327        * @param invocation
328        * @throws org.jboss.remoting.HandleCallbackException
329        *
330        */

331       public void handleCallback(InvocationRequest invocation) throws HandleCallbackException
332       {
333          if(invocation instanceof Callback)
334          {
335             callback = (Callback) invocation;
336             System.out.println("Received callback value of: " + callback.getCallbackObject());
337             System.out.println("Received callback handle object of: " + callback.getCallbackHandleObject());
338             System.out.println("Received callback server invoker of: " + callback.getServerLocator());
339          }
340          else
341          {
342             System.out.println("Received callback value of: " + invocation.getParameter());
343          }
344       }
345
346       public Callback getCallback()
347       {
348          return callback;
349       }
350
351    }
352
353 }
Popular Tags