~ubuntu-branches/ubuntu/raring/libjboss-remoting-java/raring

« back to all changes in this revision

Viewing changes to tests/org/jboss/test/remoting/callback/CallbackTestClient.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
* JBoss, Home of Professional Open Source
3
 
* Copyright 2005, JBoss Inc., and individual contributors as indicated
4
 
* by the @authors tag. See the copyright.txt in the distribution for a
5
 
* full listing of individual contributors.
6
 
*
7
 
* This is free software; you can redistribute it and/or modify it
8
 
* under the terms of the GNU Lesser General Public License as
9
 
* published by the Free Software Foundation; either version 2.1 of
10
 
* the License, or (at your option) any later version.
11
 
*
12
 
* This software is distributed in the hope that it will be useful,
13
 
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
 
* Lesser General Public License for more details.
16
 
*
17
 
* You should have received a copy of the GNU Lesser General Public
18
 
* License along with this software; if not, write to the Free
19
 
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
 
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21
 
*/
22
 
 
23
 
package org.jboss.test.remoting.callback;
24
 
 
25
 
import junit.framework.TestCase;
26
 
import org.jboss.logging.Logger;
27
 
import org.jboss.remoting.Client;
28
 
import org.jboss.remoting.InvocationRequest;
29
 
import org.jboss.remoting.InvokerLocator;
30
 
import org.jboss.remoting.ServerInvocationHandler;
31
 
import org.jboss.remoting.ServerInvoker;
32
 
import org.jboss.remoting.callback.Callback;
33
 
import org.jboss.remoting.callback.HandleCallbackException;
34
 
import org.jboss.remoting.callback.InvokerCallbackHandler;
35
 
import org.jboss.remoting.transport.Connector;
36
 
 
37
 
import javax.management.MBeanServer;
38
 
import java.util.ArrayList;
39
 
import java.util.Iterator;
40
 
import java.util.List;
41
 
 
42
 
/**
43
 
 * @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
44
 
 */
45
 
public class CallbackTestClient extends TestCase
46
 
{
47
 
   // Default locator values
48
 
   private static String transport = "socket";
49
 
   private static String host = "localhost";
50
 
   private static int port = 5500;
51
 
 
52
 
   private String locatorURI = transport + "://" + host + ":" + port;
53
 
 
54
 
   private Client remotingClient;
55
 
   private Connector connector;
56
 
 
57
 
   private static final Logger log = Logger.getLogger(CallbackTestClient.class);
58
 
 
59
 
   public static final String RESPONSE_VALUE = "This is the return to SampleInvocationHandler invocation";
60
 
 
61
 
 
62
 
   private void init() throws Exception
63
 
   {
64
 
      createRemotingClient();
65
 
   }
66
 
 
67
 
   public void createRemotingClient() throws Exception
68
 
   {
69
 
      InvokerLocator locator = new InvokerLocator(locatorURI);
70
 
      System.out.println("Calling remoting server with locator uri of: " + locatorURI);
71
 
 
72
 
      // This could have been new Client(locator), but want to show that subsystem param is null
73
 
      // Could have also been new Client(locator, "sample");
74
 
      remotingClient = new Client(locator);
75
 
      remotingClient.connect();
76
 
 
77
 
   }
78
 
 
79
 
   public void makeInvocation() throws Throwable
80
 
   {
81
 
      Object response = remotingClient.invoke("Do something", null);
82
 
      System.out.println("Invocation response: " + response);
83
 
   }
84
 
 
85
 
   public void setUp() throws Exception
86
 
   {
87
 
      init();
88
 
   }
89
 
 
90
 
   public void tearDown() throws Exception
91
 
   {
92
 
      if(remotingClient != null)
93
 
      {
94
 
         remotingClient.disconnect();
95
 
      }
96
 
      if(connector != null)
97
 
      {
98
 
         connector.stop();
99
 
         connector.destroy();
100
 
      }
101
 
   }
102
 
 
103
 
   public void testPullCallback() throws Throwable
104
 
   {
105
 
      CallbackHandler callbackHandler = new CallbackHandler();
106
 
      try
107
 
      {
108
 
         // by passing only the callback handler, will indicate pull callbacks
109
 
         remotingClient.addListener(callbackHandler);
110
 
         // now make invocation on server, which should cause a callback to happen
111
 
         makeInvocation();
112
 
 
113
 
         List callbacks = remotingClient.getCallbacks(callbackHandler);
114
 
         Iterator itr = callbacks.iterator();
115
 
         while(itr.hasNext())
116
 
         {
117
 
            Object obj = itr.next();
118
 
            log.info("testPullCallback - Callback object should have been " +
119
 
                     CallbackTestServer.CALLBACK_VALUE + " and was " +
120
 
                     (obj == null ? null : ((Callback) obj).getCallbackObject()));
121
 
            if(obj instanceof Callback)
122
 
            {
123
 
               assertEquals("Callback object is NOT same.", CallbackTestServer.CALLBACK_VALUE, ((Callback) obj).getCallbackObject());
124
 
            }
125
 
            else
126
 
            {
127
 
               assertTrue("Callback object is NOT of type Callback.", false);
128
 
            }
129
 
         }
130
 
      }
131
 
      finally
132
 
      {
133
 
         if(remotingClient != null)
134
 
         {
135
 
            // remove callback handler from server
136
 
            remotingClient.removeListener(callbackHandler);
137
 
         }
138
 
      }
139
 
   }
140
 
 
141
 
   public void testPushCallback() throws Throwable
142
 
   {
143
 
 
144
 
      CallbackHandler callbackHandler = new CallbackHandler();
145
 
 
146
 
      try
147
 
      {
148
 
         // Using loctor with port value one higher than the target server
149
 
         String callbackLocatorURI = transport + "://" + host + ":" + (port + 5);
150
 
         InvokerLocator callbackLocator = new InvokerLocator(callbackLocatorURI);
151
 
 
152
 
         log.info("testPushCallback - Setting up server.");
153
 
         // call to create remoting server to
154
 
         // receive client callbacks.
155
 
         setupServer(callbackLocator);
156
 
 
157
 
         // Callback handle object will be passed back as part of the callback object
158
 
         String callbackHandleObject = "myCallbackHandleObject";
159
 
         log.info("testPushCallback - adding listener.");
160
 
         // by passing only the callback handler, will indicate pull callbacks
161
 
         remotingClient.addListener(callbackHandler, callbackLocator, callbackHandleObject);
162
 
         log.info("testPushCallback - make invocation");
163
 
         // now make invocation on server, which should cause a callback to happen
164
 
         makeInvocation();
165
 
 
166
 
         // need to wait for brief moment so server can callback
167
 
         Thread.sleep(5000);
168
 
 
169
 
         Callback callback = callbackHandler.getCallback();
170
 
         log.info("testPushCallback - Callback returned was " + callback);
171
 
         assertNotNull("Callback returned was null.", callback);
172
 
 
173
 
         Object callbackObj = callback.getCallbackObject();
174
 
         log.info("testPushCallback - Callback value should have been " + CallbackTestServer.CALLBACK_VALUE + ", and was " + callbackObj);
175
 
         assertEquals("Callback value should have been " + CallbackTestServer.CALLBACK_VALUE + ", but was " + callbackObj,
176
 
                      CallbackTestServer.CALLBACK_VALUE, callbackObj);
177
 
         Object callbackHandleObj = callback.getCallbackHandleObject();
178
 
         log.info("testPushCallback - Callback handle object should have been " + callbackHandleObject + ", and was " + callbackHandleObj);
179
 
         assertEquals("Callback handle object should have been " + callbackHandleObject + ", but was " + callbackHandleObj,
180
 
                      callbackHandleObject, callbackHandleObj);
181
 
         InvokerLocator serverLoc = callback.getServerLocator();
182
 
         log.info("testPushCallback - Callback server locator should have been " + remotingClient.getInvoker().getLocator() +
183
 
                  ", and was " + serverLoc);
184
 
         assertEquals("Callback server locator should have been " + remotingClient.getInvoker().getLocator() +
185
 
                      ", but was " + serverLoc,
186
 
                      remotingClient.getInvoker().getLocator(), serverLoc);
187
 
 
188
 
      }
189
 
      finally
190
 
      {
191
 
         if(remotingClient != null)
192
 
         {
193
 
            // remove callback handler from server
194
 
            remotingClient.removeListener(callbackHandler);
195
 
         }
196
 
      }
197
 
 
198
 
 
199
 
   }
200
 
 
201
 
   public void setupServer(InvokerLocator locator) throws Exception
202
 
   {
203
 
      log.info("Starting remoting server with locator uri of: " + locator);
204
 
      try
205
 
      {
206
 
         connector = new Connector();
207
 
         connector.setInvokerLocator(locator.getLocatorURI());
208
 
         connector.start();
209
 
 
210
 
         SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
211
 
         // first parameter is sub-system name.  can be any String value.
212
 
         connector.addInvocationHandler("sample", invocationHandler);
213
 
      }
214
 
      catch(Exception e)
215
 
      {
216
 
         log.error("Error starting callback server", e);
217
 
         throw e;
218
 
      }
219
 
   }
220
 
 
221
 
   /**
222
 
    * Simple invocation handler implementation.
223
 
    */
224
 
   public static class SampleInvocationHandler implements ServerInvocationHandler
225
 
   {
226
 
 
227
 
      private List listeners = new ArrayList();
228
 
 
229
 
 
230
 
      /**
231
 
       * called to handle a specific invocation
232
 
       *
233
 
       * @param invocation
234
 
       * @return
235
 
       * @throws Throwable
236
 
       */
237
 
      public Object invoke(InvocationRequest invocation) throws Throwable
238
 
      {
239
 
         // Just going to return static string as this is just simple example code.
240
 
 
241
 
         // Will also fire callback to listeners if they were to exist using
242
 
         // simple invocation request.
243
 
         Callback callback = new Callback("This is the payload of callback invocation.");
244
 
         Iterator itr = listeners.iterator();
245
 
         while(itr.hasNext())
246
 
         {
247
 
            InvokerCallbackHandler callbackHandler = (InvokerCallbackHandler) itr.next();
248
 
            try
249
 
            {
250
 
               callbackHandler.handleCallback(callback);
251
 
            }
252
 
            catch(HandleCallbackException e)
253
 
            {
254
 
               e.printStackTrace();
255
 
            }
256
 
         }
257
 
 
258
 
         return RESPONSE_VALUE;
259
 
 
260
 
      }
261
 
 
262
 
      /**
263
 
       * Adds a callback handler that will listen for callbacks from
264
 
       * the server invoker handler.
265
 
       *
266
 
       * @param callbackHandler
267
 
       */
268
 
      public void addListener(InvokerCallbackHandler callbackHandler)
269
 
      {
270
 
         listeners.add(callbackHandler);
271
 
      }
272
 
 
273
 
      /**
274
 
       * Removes the callback handler that was listening for callbacks
275
 
       * from the server invoker handler.
276
 
       *
277
 
       * @param callbackHandler
278
 
       */
279
 
      public void removeListener(InvokerCallbackHandler callbackHandler)
280
 
      {
281
 
         listeners.remove(callbackHandler);
282
 
      }
283
 
 
284
 
      /**
285
 
       * set the mbean server that the handler can reference
286
 
       *
287
 
       * @param server
288
 
       */
289
 
      public void setMBeanServer(MBeanServer server)
290
 
      {
291
 
         // NO OP as do not need reference to MBeanServer for this handler
292
 
      }
293
 
 
294
 
      /**
295
 
       * set the invoker that owns this handler
296
 
       *
297
 
       * @param invoker
298
 
       */
299
 
      public void setInvoker(ServerInvoker invoker)
300
 
      {
301
 
         // NO OP as do not need reference back to the server invoker
302
 
      }
303
 
 
304
 
   }
305
 
 
306
 
 
307
 
   public class CallbackHandler implements InvokerCallbackHandler
308
 
   {
309
 
      private Callback callback;
310
 
 
311
 
      /**
312
 
       * Will take the callback message and send back to client.
313
 
       * If client locator is null, will store them till client polls to get them.
314
 
       *
315
 
       * @param callback
316
 
       * @throws org.jboss.remoting.callback.HandleCallbackException
317
 
       *
318
 
       */
319
 
      public void handleCallback(Callback callback) throws HandleCallbackException
320
 
      {
321
 
         this.callback = callback;
322
 
         System.out.println("Received callback value of: " + callback.getCallbackObject());
323
 
         System.out.println("Received callback handle object of: " + callback.getCallbackHandleObject());
324
 
         System.out.println("Received callback server invoker of: " + callback.getServerLocator());
325
 
      }
326
 
 
327
 
      public Callback getCallback()
328
 
      {
329
 
         return callback;
330
 
      }
331
 
 
332
 
   }
333
 
 
334
 
}
 
 
b'\\ No newline at end of file'