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

« back to all changes in this revision

Viewing changes to src/main/org/jboss/remoting/samples/config/factories/FactoryConfigSample.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 2006, 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.remoting.samples.config.factories;
 
24
 
 
25
import java.io.ByteArrayInputStream;
 
26
import java.io.IOException;
 
27
import java.net.InetAddress;
 
28
import java.net.ServerSocket;
 
29
import java.net.Socket;
 
30
import java.net.UnknownHostException;
 
31
import java.util.HashMap;
 
32
 
 
33
import javax.management.MBeanServer;
 
34
import javax.net.ServerSocketFactory;
 
35
import javax.net.SocketFactory;
 
36
import javax.xml.parsers.DocumentBuilderFactory;
 
37
 
 
38
import junit.framework.TestCase;
 
39
 
 
40
import org.jboss.logging.Logger;
 
41
import org.jboss.remoting.Client;
 
42
import org.jboss.remoting.InvocationRequest;
 
43
import org.jboss.remoting.InvokerLocator;
 
44
import org.jboss.remoting.Remoting;
 
45
import org.jboss.remoting.ServerInvocationHandler;
 
46
import org.jboss.remoting.ServerInvoker;
 
47
import org.jboss.remoting.callback.Callback;
 
48
import org.jboss.remoting.callback.HandleCallbackException;
 
49
import org.jboss.remoting.callback.InvokerCallbackHandler;
 
50
import org.jboss.remoting.transport.ClientInvoker;
 
51
import org.jboss.remoting.transport.Connector;
 
52
import org.jboss.remoting.transport.PortUtil;
 
53
import org.w3c.dom.Document;
 
54
 
 
55
/**
 
56
 * These methods illustrate configuring socket factories and server socket factories
 
57
 * on the server side and on the client side.  The numbered
 
58
 * options mentioned refer to the lists of configuration options discussed in the
 
59
 * Remoting documentation in the subsections "Server side configuration" and
 
60
 * "Client side configuration" of the section called "Socket factories and server
 
61
 * socket factories".
 
62
 * <p>
 
63
 * The configuration options illustrated in this class are applicable to any kind
 
64
 * of socket and server socket, so the <code>SampleServerSocketFactory</code>
 
65
 * and <code>SampleSocketFactory</code> classes create ordinary sockets and server
 
66
 * sockets.
 
67
 * <p>
 
68
 * 
 
69
 * @author <a href="mailto:ron.sigal@jboss.com">Ron Sigal</a>
 
70
 * <p>
 
71
 * Copyright (c) Jul 20, 2006
 
72
 * </p>
 
73
 */
 
74
public class FactoryConfigSample extends TestCase
 
75
{
 
76
   protected static Logger log = Logger.getLogger(FactoryConfigSample.class);
 
77
  
 
78
   
 
79
   /**
 
80
    * This test illustrates the following set of configuration options:
 
81
    * <p>
 
82
    * <table border cellpadding="5">
 
83
    *  <tr><td align="center"><b>side<td align="center"><b>factory<td><b>option</tr>
 
84
    *  <tr><td>server side<td align="center">server socket<td align="center">1</tr>
 
85
    *  <tr><td>server side<td align="center">socket       <td align="center">1</tr>
 
86
    *  <tr><td>client side<td align="center">server socket<td align="center">1</tr>
 
87
    *  <tr><td>client side<td align="center">socket       <td align="center">1</tr>
 
88
    * </table>
 
89
    */
 
90
   public void testFactoriesBySettingInvokers()
 
91
   {
 
92
      try
 
93
      {
 
94
         /////////////////////////////////////
 
95
         /////    Set up server side.     //// 
 
96
         /////////////////////////////////////
 
97
         HashMap sconfig = new HashMap();
 
98
         
 
99
         // Make callback Client use remote invoker.
 
100
         sconfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
101
         
 
102
         // Get Connector.
 
103
         int freeport = PortUtil.findFreePort(getHostName());
 
104
         InvokerLocator locator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
 
105
         Connector connector = new Connector(locator, sconfig);
 
106
         connector.create();
 
107
         
 
108
         // Set ServerSocketFactory and SocketFactory in ServerInvoker.
 
109
         ServerInvoker serverInvoker = connector.getServerInvoker();
 
110
         ServerSocketFactory ssf1 = getDefaultServerSocketFactory();
 
111
         serverInvoker.setServerSocketFactory(ssf1);
 
112
         SocketFactory sf1 = getDefaultCallbackSocketFactory();
 
113
         serverInvoker.setSocketFactory(sf1);
 
114
 
 
115
         connector.addInvocationHandler("sample", new SampleInvocationHandler());
 
116
         connector.start();
 
117
         
 
118
         
 
119
         /////////////////////////////////////
 
120
         /////    Set up client side.     //// 
 
121
         /////////////////////////////////////
 
122
         HashMap cconfig = new HashMap();
 
123
 
 
124
         // Make Client use remote invoker.
 
125
         cconfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
126
  
 
127
         // Create Client.
 
128
         Client client = new Client(locator, cconfig);
 
129
         client.connect();
 
130
         
 
131
         // Set SocketFactory in ClientInvoker.
 
132
         SocketFactory sf2 = getDefaultSocketFactory();
 
133
         ClientInvoker clientInvoker = client.getInvoker();
 
134
         clientInvoker.setSocketFactory(sf2);
 
135
 
 
136
         System.out.println(getName() + ": " + client.invoke("test invoke()"));
 
137
         
 
138
         
 
139
         //////////////////////////////////////////////
 
140
         /////       Set up callback handling.     //// 
 
141
         //////////////////////////////////////////////
 
142
 
 
143
         // Start callback Connector.
 
144
         freeport = PortUtil.findFreePort(getHostName());
 
145
         InvokerLocator callbackLocator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
 
146
         Connector callbackConnector = new Connector(callbackLocator.getLocatorURI());
 
147
         callbackConnector.create();
 
148
         ServerInvoker callbackServerInvoker = callbackConnector.getServerInvoker();
 
149
         ServerSocketFactory ssf2 = getDefaultCallbackServerSocketFactory();
 
150
         callbackServerInvoker.setServerSocketFactory(ssf2);
 
151
         callbackConnector.addInvocationHandler("sample", new SampleInvocationHandler());
 
152
         callbackConnector.start();
 
153
         
 
154
         // Add callback handler.
 
155
         CallbackHandler callbackHandler = new CallbackHandler();
 
156
         String callbackHandleObject = "myCallbackHandleObject";
 
157
         client.addListener(callbackHandler, callbackLocator, callbackHandleObject);
 
158
         
 
159
         client.disconnect();
 
160
         callbackConnector.stop();
 
161
         connector.stop();
 
162
      }
 
163
      catch (Throwable t)
 
164
      {
 
165
         log.error(t);
 
166
         t.printStackTrace();
 
167
         fail();
 
168
      }
 
169
   }
 
170
   
 
171
   
 
172
   /** This test illustrates the following set of configuration options:
 
173
    * <p>
 
174
    * <table border cellpadding="5">
 
175
    *  <tr><td align="center"><b>side<td align="center"><b>factory<td><b>option</tr>
 
176
    *  <tr><td>server side<td align="center">server socket<td align="center">2</tr>
 
177
    *  <tr><td>server side<td align="center">socket       <td align="center">2</tr>
 
178
    *  <tr><td>client side<td align="center">server socket<td align="center">2</tr>
 
179
    *  <tr><td>client side<td align="center">socket       <td align="center">2</tr>
 
180
    * </table>
 
181
    */
 
182
   public void testFactoriesBySettingConnectorAndClient()
 
183
   {
 
184
      try
 
185
      {
 
186
         /////////////////////////////////////
 
187
         /////     Set up server side.    //// 
 
188
         /////////////////////////////////////
 
189
         HashMap sconfig = new HashMap();
 
190
         
 
191
         // Make callback Client use remote invoker.
 
192
         sconfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
193
         
 
194
         // Get Connector.
 
195
         int freeport = PortUtil.findFreePort(getHostName());
 
196
         InvokerLocator locator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
 
197
         Connector connector = new Connector(locator, sconfig);
 
198
         
 
199
         // Set ServerSocketFactory and SocketFactory in Connector.
 
200
         ServerSocketFactory ssf1 = getDefaultServerSocketFactory();
 
201
         connector.setServerSocketFactory(ssf1);
 
202
         SocketFactory sf1 = getDefaultCallbackSocketFactory();
 
203
         connector.setSocketFactory(sf1);
 
204
         connector.create();
 
205
         connector.addInvocationHandler("sample", new SampleInvocationHandler());
 
206
         connector.start();
 
207
         
 
208
         
 
209
         /////////////////////////////////////
 
210
         /////    Set up client side.     //// 
 
211
         /////////////////////////////////////
 
212
         HashMap cconfig = new HashMap();
 
213
 
 
214
         // Make Client use remote invoker.
 
215
         cconfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
216
         
 
217
         // Create Client.
 
218
         Client client = new Client(locator, cconfig);
 
219
         
 
220
         // Set SocketFactory in Client.
 
221
         SocketFactory sf2 = getDefaultSocketFactory();
 
222
         client.setSocketFactory(sf2);
 
223
         client.connect();
 
224
         System.out.println(getName() + ": " + client.invoke("test invoke()"));
 
225
         
 
226
         
 
227
         //////////////////////////////////////////////
 
228
         /////      Set up callback handling.      //// 
 
229
         //////////////////////////////////////////////
 
230
 
 
231
         // Get callback Connector.
 
232
         freeport = PortUtil.findFreePort(getHostName());
 
233
         InvokerLocator callbackLocator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
 
234
         Connector callbackConnector = new Connector(callbackLocator.getLocatorURI());
 
235
        
 
236
         // Set ServerSocketFactory in callback Connector
 
237
         ServerSocketFactory ssf2 = getDefaultCallbackServerSocketFactory();
 
238
         callbackConnector.setServerSocketFactory(ssf2);
 
239
         callbackConnector.create();
 
240
         callbackConnector.addInvocationHandler("sample", new SampleInvocationHandler());
 
241
         callbackConnector.start();
 
242
         
 
243
         // Add callback handler.
 
244
         CallbackHandler callbackHandler = new CallbackHandler();
 
245
         String callbackHandleObject = "myCallbackHandleObject";
 
246
         client.addListener(callbackHandler, callbackLocator, callbackHandleObject);
 
247
 
 
248
         client.disconnect();
 
249
         callbackConnector.stop();
 
250
         connector.stop();
 
251
      }
 
252
      catch (Throwable t)
 
253
      {
 
254
         log.error(t);
 
255
         t.printStackTrace();
 
256
         fail();
 
257
      }
 
258
   }
 
259
   
 
260
   
 
261
  /** This test illustrates the following set of configuration options:
 
262
    * <p>
 
263
    * <table border cellpadding="5">
 
264
    *  <tr><td align="center"><b>side<td align="center"><b>factory<td><b>option</tr>
 
265
    *  <tr><td>server side<td align="center">server socket<td align="center">3</tr>
 
266
    *  <tr><td>server side<td align="center">socket       <td align="center">3</tr>
 
267
    *  <tr><td>client side<td align="center">server socket<td align="center">3</tr>
 
268
    *  <tr><td>client side<td align="center">socket       <td align="center">3</tr>
 
269
    * </table>
 
270
    */
 
271
   public void testFactoriesByPassingInConfig()
 
272
   {
 
273
      try
 
274
      {
 
275
         /////////////////////////////////////
 
276
         /////    Set up server side.     //// 
 
277
         /////////////////////////////////////
 
278
         HashMap sconfig = new HashMap();
 
279
         
 
280
         // Put ServerSocketFactory and SocketFactory in config map.
 
281
         ServerSocketFactory ssf1 = getDefaultServerSocketFactory();
 
282
         sconfig.put(Remoting.CUSTOM_SERVER_SOCKET_FACTORY, ssf1);
 
283
         SocketFactory sf1 = getDefaultCallbackSocketFactory();
 
284
         sconfig.put(Remoting.CUSTOM_SOCKET_FACTORY, sf1);
 
285
         
 
286
         // Make callback Client use remote invoker.
 
287
         sconfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
288
         
 
289
         // Get Connector.
 
290
         int freeport = PortUtil.findFreePort(getHostName());
 
291
         InvokerLocator locator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
 
292
         Connector connector = new Connector(locator, sconfig);
 
293
         connector.create();
 
294
         connector.addInvocationHandler("sample", new SampleInvocationHandler());
 
295
         connector.start();
 
296
         
 
297
         
 
298
         /////////////////////////////////////
 
299
         /////    Set up client side.     //// 
 
300
         /////////////////////////////////////
 
301
         HashMap cconfig = new HashMap();
 
302
         
 
303
         // Put SocketFactory in config map.
 
304
         SocketFactory sf2 = getDefaultSocketFactory();
 
305
         cconfig.put(Remoting.CUSTOM_SOCKET_FACTORY, sf2);
 
306
         
 
307
         // Make Client use remote invoker.
 
308
         cconfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
309
         
 
310
         // Create Client.
 
311
         Client client = new Client(locator, cconfig);
 
312
         client.connect();
 
313
 
 
314
         System.out.println(getName() + ": " + client.invoke("test invoke()"));
 
315
         
 
316
         
 
317
         //////////////////////////////////////////////
 
318
         /////       Set up callback handling.     //// 
 
319
         //////////////////////////////////////////////
 
320
         
 
321
         // Get callback Connector.
 
322
         HashMap cbconfig = new HashMap();
 
323
         ServerSocketFactory ssf2 = getDefaultCallbackServerSocketFactory();
 
324
         cbconfig.put(Remoting.CUSTOM_SERVER_SOCKET_FACTORY, ssf2);
 
325
         freeport = PortUtil.findFreePort(getHostName());
 
326
         InvokerLocator callbackLocator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
 
327
         Connector callbackConnector = new Connector(callbackLocator.getLocatorURI(), cbconfig);
 
328
         callbackConnector.create();
 
329
         callbackConnector.addInvocationHandler("sample", new SampleInvocationHandler());
 
330
         callbackConnector.start();
 
331
         
 
332
         // Add callback handler.
 
333
         CallbackHandler callbackHandler = new CallbackHandler();
 
334
         String callbackHandleObject = "myCallbackHandleObject";
 
335
         client.addListener(callbackHandler, callbackLocator, callbackHandleObject);
 
336
         
 
337
         client.disconnect();
 
338
         callbackConnector.stop();
 
339
         connector.stop();
 
340
      }
 
341
      catch (Throwable t)
 
342
      {
 
343
         log.error(t);
 
344
         t.printStackTrace();
 
345
         fail();
 
346
      }
 
347
   }
 
348
   
 
349
   
 
350
   /** This test illustrates the following set of configuration options:
 
351
    * <p>
 
352
    * <table border cellpadding="5">
 
353
    *  <tr><td align="center"><b>side<td align="center"><b>factory<td><b>option</tr>
 
354
    *  <tr><td>server side<td align="center">server socket<td align="center">5</tr>
 
355
    *  <tr><td>server side<td align="center">socket       <td align="center">6</tr>
 
356
    *  <tr><td>client side<td align="center">server socket<td align="center">4</tr>
 
357
    *  <tr><td>client side<td align="center">socket       <td align="center">1</tr>
 
358
    * </table>
 
359
    */
 
360
   public void testFactoriesByPassingClassnameInXml()
 
361
   {
 
362
      try
 
363
      {
 
364
         /////////////////////////////////////
 
365
         /////    Set up server side.     //// 
 
366
         /////////////////////////////////////
 
367
         HashMap sconfig = new HashMap();
 
368
         
 
369
         // Make callback Client use remote invoker.
 
370
         sconfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
371
           
 
372
         // Get Connector.
 
373
         int freeport = PortUtil.findFreePort(getHostName());
 
374
         InvokerLocator locator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
 
375
         Connector connector = new Connector(sconfig);
 
376
         
 
377
         // Set xml configuration element.
 
378
         StringBuffer buf = new StringBuffer();
 
379
         buf.append("<?xml version=\"1.0\"?>\n");
 
380
         buf.append("<config>");
 
381
         buf.append(   "<invoker transport=\"" + getTransport() +"\">");
 
382
         buf.append(      "<attribute name=\"serverBindAddress\">" + getHostName() + "</attribute>");
 
383
         buf.append(      "<attribute name=\"serverBindPort\">" + freeport + "</attribute>");
 
384
         buf.append(      "<attribute name=\"serverSocketFactory\">" );
 
385
         buf.append(          getDefaultServerSocketFactoryClass().getName());
 
386
         buf.append(      "</attribute>");
 
387
         buf.append(      "<attribute name=\"socketFactory\">" );
 
388
         buf.append(         getDefaultSocketFactoryClass().getName());
 
389
         buf.append(      "</attribute>");
 
390
         buf.append(   "</invoker>");
 
391
         buf.append("</config>");
 
392
         
 
393
         ByteArrayInputStream bais = new ByteArrayInputStream(buf.toString().getBytes());
 
394
         Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(bais);
 
395
         connector.setConfiguration(xml.getDocumentElement());
 
396
         
 
397
         connector.create();
 
398
         connector.addInvocationHandler("sample", new SampleInvocationHandler());
 
399
         connector.start();
 
400
         
 
401
         
 
402
         /////////////////////////////////////
 
403
         /////    Set up client side.     //// 
 
404
         /////////////////////////////////////
 
405
         HashMap cconfig = new HashMap();
 
406
 
 
407
         // Make Client use remote invoker.
 
408
         cconfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
409
         
 
410
         // Create Client.
 
411
         Client client = new Client(locator, cconfig);
 
412
         
 
413
         // Set SocketFactory in Client.
 
414
         // Note. There is no provision for using xml configuration on client side.
 
415
         SocketFactory sf = getDefaultSocketFactory();
 
416
         client.setSocketFactory(sf);
 
417
         client.connect();
 
418
         System.out.println(getName() + ": " + client.invoke("test invoke()"));
 
419
         
 
420
         
 
421
         //////////////////////////////////////////////
 
422
         /////       Set up callback handling.     //// 
 
423
         //////////////////////////////////////////////
 
424
         
 
425
         // Get callback Connector.
 
426
         freeport = PortUtil.findFreePort(getHostName());
 
427
         InvokerLocator callbackLocator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
 
428
         Connector callbackConnector = new Connector();
 
429
         
 
430
         // Set xml configuration element.
 
431
         buf = new StringBuffer();
 
432
         buf.append("<?xml version=\"1.0\"?>\n");
 
433
         buf.append("<config>");
 
434
         buf.append(   "<invoker transport=\"" + getTransport() +"\">");
 
435
         buf.append(      "<attribute name=\"serverBindAddress\">" + getHostName() + "</attribute>");
 
436
         buf.append(      "<attribute name=\"serverBindPort\">" + freeport + "</attribute>");
 
437
         buf.append(      "<attribute name=\"serverSocketFactory\">" );
 
438
         buf.append(         getDefaultCallbackServerSocketFactoryClass().getName());
 
439
         buf.append(      "</attribute>");
 
440
         buf.append(   "</invoker>");
 
441
         buf.append("</config>");
 
442
         bais = new ByteArrayInputStream(buf.toString().getBytes());
 
443
         xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(bais);
 
444
         callbackConnector.setConfiguration(xml.getDocumentElement());
 
445
         
 
446
         callbackConnector.create();
 
447
         callbackConnector.addInvocationHandler("sample", new SampleInvocationHandler());
 
448
         callbackConnector.start();
 
449
         
 
450
         // Add callback handler.
 
451
         CallbackHandler callbackHandler = new CallbackHandler();
 
452
         String callbackHandleObject = "myCallbackHandleObject";
 
453
         client.addListener(callbackHandler, callbackLocator, callbackHandleObject);
 
454
         
 
455
         client.disconnect();
 
456
         callbackConnector.stop();
 
457
         connector.stop();
 
458
      }
 
459
      catch (Throwable t)
 
460
      {
 
461
         log.error(t);
 
462
         t.printStackTrace();
 
463
         fail();
 
464
      }
 
465
   }
 
466
   
 
467
   
 
468
  /** This test illustrates the following set of configuration options:
 
469
    * <p>
 
470
    * <table border cellpadding="5">
 
471
    *  <tr><td align="center"><b>side<td align="center"><b>factory<td><b>option</tr>
 
472
    *  <tr><td>server side<td align="center">server socket<td align="center">7</tr>
 
473
    *  <tr><td>server side<td align="center">socket       <td align="center">7</tr>
 
474
    *  <tr><td>client side<td align="center">server socket<td align="center">5</tr>
 
475
    *  <tr><td>client side<td align="center">socket       <td align="center">4</tr>
 
476
    * </table>
 
477
    */
 
478
   public void testFactoriesByClassNameinConfig()
 
479
   {
 
480
      try
 
481
      {
 
482
         /////////////////////////////////////
 
483
         /////    Set up server side.     //// 
 
484
         /////////////////////////////////////
 
485
         HashMap sconfig = new HashMap();
 
486
         
 
487
         // Put class names of ServerSocketFactory and SocketFactory in config map.
 
488
         sconfig.put(ServerInvoker.SERVER_SOCKET_FACTORY, getDefaultServerSocketFactoryClass().getName());
 
489
         sconfig.put(Remoting.SOCKET_FACTORY_NAME, getDefaultCallbackSocketFactoryClass().getName());
 
490
         
 
491
         // Make callback Client use remote invoker.
 
492
         sconfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
493
         
 
494
         // Get Connector.
 
495
         int freeport = PortUtil.findFreePort(getHostName());
 
496
         InvokerLocator locator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
 
497
         Connector connector = new Connector(locator, sconfig);
 
498
         connector.create();
 
499
         connector.addInvocationHandler("sample", new SampleInvocationHandler());
 
500
         connector.start();
 
501
         
 
502
         
 
503
         /////////////////////////////////////
 
504
         /////    Set up client side.     //// 
 
505
         /////////////////////////////////////
 
506
         HashMap cconfig = new HashMap();
 
507
         
 
508
         // Put SocketFactory class name in config map.
 
509
         cconfig.put(Remoting.SOCKET_FACTORY_NAME, getDefaultSocketFactoryClass().getName());
 
510
         
 
511
         // Make Client use remote invoker.
 
512
         cconfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
513
         
 
514
         // Create Client.
 
515
         Client client = new Client(locator, cconfig);
 
516
         client.connect();
 
517
 
 
518
         System.out.println(getName() + ": " + client.invoke("test invoke()"));
 
519
         
 
520
         
 
521
         //////////////////////////////////////////////
 
522
         /////       Set up callback handling.     //// 
 
523
         //////////////////////////////////////////////
 
524
         
 
525
         // Get callback Connector.
 
526
         HashMap cbconfig = new HashMap();
 
527
         cbconfig.put(ServerInvoker.SERVER_SOCKET_FACTORY, getDefaultCallbackServerSocketFactoryClass().getName());
 
528
         freeport = PortUtil.findFreePort(getHostName());
 
529
         InvokerLocator callbackLocator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
 
530
         Connector callbackConnector = new Connector(callbackLocator.getLocatorURI(), cbconfig);
 
531
         callbackConnector.create();
 
532
         callbackConnector.addInvocationHandler("sample", new SampleInvocationHandler());
 
533
         callbackConnector.start();
 
534
         
 
535
         // Add callback handler.
 
536
         CallbackHandler callbackHandler = new CallbackHandler();
 
537
         String callbackHandleObject = "myCallbackHandleObject";
 
538
         client.addListener(callbackHandler, callbackLocator, callbackHandleObject);
 
539
         
 
540
         client.disconnect();
 
541
         callbackConnector.stop();
 
542
         connector.stop();
 
543
      }
 
544
      catch (Throwable t)
 
545
      {
 
546
         log.error(t);
 
547
         t.printStackTrace();
 
548
         fail();
 
549
      }
 
550
   }
 
551
         
 
552
   
 
553
   protected String getTransport()
 
554
   {
 
555
      return "socket";
 
556
   }
 
557
   
 
558
   
 
559
   protected String getHostName()
 
560
   {
 
561
      return "localhost";
 
562
   }
 
563
   
 
564
 
 
565
   protected ServerSocketFactory getDefaultServerSocketFactory() throws Exception
 
566
   {
 
567
      return new SampleServerSocketFactory();
 
568
   }
 
569
   
 
570
   
 
571
   protected SocketFactory getDefaultSocketFactory() throws Exception
 
572
   {
 
573
      return new SampleSocketFactory();
 
574
   }
 
575
   
 
576
   
 
577
   protected ServerSocketFactory getDefaultCallbackServerSocketFactory() throws Exception
 
578
   {
 
579
      return new SampleServerSocketFactory();
 
580
   }
 
581
   
 
582
   
 
583
   protected SocketFactory getDefaultCallbackSocketFactory() throws Exception
 
584
   {
 
585
      return new SampleSocketFactory();
 
586
   }
 
587
   
 
588
   
 
589
   protected Class getDefaultServerSocketFactoryClass() throws Exception
 
590
   {
 
591
      return SampleServerSocketFactory.class;
 
592
   }
 
593
   
 
594
   
 
595
   protected Class getDefaultSocketFactoryClass() throws Exception
 
596
   {
 
597
      return SampleSocketFactory.class;
 
598
   }
 
599
   
 
600
   
 
601
   protected Class getDefaultCallbackServerSocketFactoryClass() throws Exception
 
602
   {
 
603
      return SampleServerSocketFactory.class;
 
604
   }
 
605
   
 
606
   
 
607
   protected Class getDefaultCallbackSocketFactoryClass() throws Exception
 
608
   {
 
609
      return SampleSocketFactory.class;
 
610
   }
 
611
   
 
612
   
 
613
   public static class SampleServerSocketFactory
 
614
   extends ServerSocketFactory
 
615
   {
 
616
      public ServerSocket createServerSocket(int arg0) throws IOException
 
617
      {
 
618
         return new ServerSocket(arg0);
 
619
      }
 
620
 
 
621
      public ServerSocket createServerSocket(int arg0, int arg1) throws IOException
 
622
      {
 
623
         return new ServerSocket(arg0, arg1);
 
624
      }
 
625
 
 
626
      public ServerSocket createServerSocket(int arg0, int arg1, InetAddress arg2) throws IOException
 
627
      {
 
628
         return new ServerSocket(arg0, arg1, arg2);
 
629
      }
 
630
   }
 
631
   
 
632
   
 
633
   public static class SampleSocketFactory
 
634
   extends SocketFactory
 
635
   {
 
636
      public Socket createSocket(String arg0, int arg1) throws IOException, UnknownHostException
 
637
      {
 
638
         return new Socket(arg0, arg1);
 
639
      }
 
640
 
 
641
      public Socket createSocket(String arg0, int arg1, InetAddress arg2, int arg3) throws IOException, UnknownHostException
 
642
      {
 
643
         return new Socket(arg0, arg1, arg2, arg3);
 
644
      }
 
645
 
 
646
      public Socket createSocket(InetAddress arg0, int arg1) throws IOException
 
647
      {
 
648
         return new Socket(arg0, arg1);
 
649
      }
 
650
 
 
651
      public Socket createSocket(InetAddress arg0, int arg1, InetAddress arg2, int arg3) throws IOException
 
652
      {
 
653
         return new Socket(arg0, arg1, arg2, arg3);
 
654
      }
 
655
   }
 
656
   
 
657
   
 
658
   public static class SampleInvocationHandler implements ServerInvocationHandler
 
659
   {
 
660
      private InvokerCallbackHandler callbackHandler;
 
661
      
 
662
      public SampleInvocationHandler()
 
663
      {   
 
664
      }
 
665
      
 
666
      public Object invoke(InvocationRequest invocation) throws Throwable
 
667
      {
 
668
         return invocation.getParameter();
 
669
      }
 
670
      
 
671
      public void addListener(InvokerCallbackHandler callbackHandler)
 
672
      {
 
673
         log.info("entering addListener()");
 
674
         this.callbackHandler = callbackHandler;
 
675
         
 
676
         try
 
677
         {
 
678
            Callback callback = new Callback(new Integer(1));
 
679
            callbackHandler.handleCallback(callback);
 
680
            log.info("sent first callback");
 
681
         }
 
682
         catch(Exception e)
 
683
         {
 
684
            e.printStackTrace();
 
685
         }
 
686
      }
 
687
      
 
688
      public void removeListener(InvokerCallbackHandler callbackHandler)
 
689
      {
 
690
      }
 
691
      
 
692
      public void setMBeanServer(MBeanServer server)
 
693
      {
 
694
         // NO OP as do not need reference to MBeanServer for this handler
 
695
      }
 
696
      
 
697
      public void setInvoker(ServerInvoker invoker)
 
698
      {
 
699
         // NO OP as do not need reference back to the server invoker
 
700
      }
 
701
      
 
702
      public InvokerCallbackHandler getCallbackHandler()
 
703
      {
 
704
         return callbackHandler;
 
705
      }
 
706
   }
 
707
   
 
708
   
 
709
   public static class CallbackHandler implements InvokerCallbackHandler
 
710
   {
 
711
      /**
 
712
       * Will take the callback and print out its values.
 
713
       *
 
714
       * @param callback
 
715
       * @throws org.jboss.remoting.callback.HandleCallbackException
 
716
       *
 
717
       */
 
718
      public void handleCallback(Callback callback) throws HandleCallbackException
 
719
      {
 
720
         System.out.println("Received callback value of: " + callback.getCallbackObject());
 
721
      }
 
722
   }
 
723
}
 
 
b'\\ No newline at end of file'