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

« back to all changes in this revision

Viewing changes to tests/org/jboss/test/remoting/transport/config/FactoryConfigTestCaseParent.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.test.remoting.transport.config;
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.Remoting;
31
 
import org.jboss.remoting.ServerInvocationHandler;
32
 
import org.jboss.remoting.ServerInvoker;
33
 
import org.jboss.remoting.callback.Callback;
34
 
import org.jboss.remoting.callback.HandleCallbackException;
35
 
import org.jboss.remoting.callback.InvokerCallbackHandler;
36
 
import org.jboss.remoting.callback.ServerInvokerCallbackHandler;
37
 
import org.jboss.remoting.security.SSLSocketBuilder;
38
 
import org.jboss.remoting.security.ServerSocketFactoryMBean;
39
 
import org.jboss.remoting.security.SocketFactoryMBean;
40
 
import org.jboss.remoting.transport.ClientInvoker;
41
 
import org.jboss.remoting.transport.Connector;
42
 
import org.jboss.remoting.transport.PortUtil;
43
 
import org.jboss.remoting.transport.http.ssl.HTTPSClientInvoker;
44
 
import org.w3c.dom.Document;
45
 
 
46
 
import javax.management.MBeanServer;
47
 
import javax.management.MBeanServerFactory;
48
 
import javax.management.ObjectName;
49
 
import javax.net.ServerSocketFactory;
50
 
import javax.net.SocketFactory;
51
 
import javax.xml.parsers.DocumentBuilderFactory;
52
 
 
53
 
import java.io.ByteArrayInputStream;
54
 
import java.io.File;
55
 
import java.io.IOException;
56
 
import java.lang.reflect.Field;
57
 
import java.net.InetAddress;
58
 
import java.net.ServerSocket;
59
 
import java.net.Socket;
60
 
import java.net.UnknownHostException;
61
 
import java.security.AccessController;
62
 
import java.security.PrivilegedAction;
63
 
import java.security.PrivilegedActionException;
64
 
import java.security.PrivilegedExceptionAction;
65
 
import java.util.ArrayList;
66
 
import java.util.HashMap;
67
 
import java.util.Map;
68
 
 
69
 
/**
70
 
 * 
71
 
 * @author <a href="mailto:ron.sigal@jboss.com">Ron Sigal</a>
72
 
 * @version $Revision: 3873 $
73
 
 * <p>
74
 
 * Copyright (c) Jun 13, 2006
75
 
 * </p>
76
 
 */
77
 
public abstract class FactoryConfigTestCaseParent extends TestCase
78
 
{
79
 
   protected static Logger log = Logger.getLogger(FactoryConfigTestCaseParent.class);
80
 
   protected static boolean firstTime = true;
81
 
   
82
 
   
83
 
   abstract protected String getTransport();
84
 
   
85
 
   
86
 
   public static String getKeystoreFilePath()
87
 
   {
88
 
      File dir = (File)AccessController.doPrivileged( new PrivilegedAction()
89
 
      {
90
 
         public Object run()
91
 
         {
92
 
            return  new File(FactoryConfigTestCaseParent.class.getResource(".").getFile());
93
 
         }
94
 
      });
95
 
      while (!"transport".equals(dir.getName()))
96
 
      {
97
 
         dir = new File(dir.getParent());
98
 
      }
99
 
      return dir.getPath() + File.separator + "config/.keystore";
100
 
   }
101
 
   
102
 
   
103
 
   public static String getTruststoreFilePath()
104
 
   {
105
 
      File dir = new File(FactoryConfigTestCaseParent.class.getResource(".").getFile());
106
 
      while (!"transport".equals(dir.getName()))
107
 
      {
108
 
         dir = new File(dir.getParent());
109
 
      }
110
 
      return dir.getPath() + File.separator + "config/.truststore";
111
 
   }
112
 
   
113
 
   
114
 
   public void setUp()
115
 
   {
116
 
      if (firstTime)
117
 
      {
118
 
         firstTime = false;
119
 
         log.info("********************************************************");
120
 
         log.info("*********** FactoryConfigTestCase: " + getTransport() + " ***********");
121
 
         log.info("********************************************************");
122
 
      }
123
 
   }
124
 
   
125
 
  
126
 
   public void testFactoriesBySettingInvokers()
127
 
   {
128
 
      try
129
 
      {
130
 
         /////////////////////////////////////
131
 
         /////    Do server side test.    //// 
132
 
         /////////////////////////////////////
133
 
         HashMap sconfig = new HashMap();
134
 
         
135
 
         // Put ServerSocketFactory and SocketFactory in config map.
136
 
         ServerSocketFactory ssf1 = getDefaultServerSocketFactory();
137
 
         sconfig.put(Remoting.CUSTOM_SERVER_SOCKET_FACTORY, ssf1);
138
 
         SocketFactory sf1 = getDefaultCallbackSocketFactory();
139
 
         sconfig.put(Remoting.CUSTOM_SOCKET_FACTORY, sf1);
140
 
         
141
 
         // Make callback Client use remote invoker.
142
 
         sconfig.put(InvokerLocator.FORCE_REMOTE, "true");
143
 
         
144
 
         // Special case: tell HTTPSClientInvoker to ignore hostname in certificates.
145
 
         // This is because InvokerLocator turns "localhost" into "127.0.0.1". Should
146
 
         // be fixed by JBREM-497.
147
 
         sconfig.put(HTTPSClientInvoker.IGNORE_HTTPS_HOST, "true");
148
 
         
149
 
         // Put ServerSocketFactory MBean name in config map.
150
 
         ServerSocketFactory serverSocketService = getDefaultServerSocketFactory();
151
 
         String serverSocketFactoryName = "jboss:type=serversocketfactory";
152
 
         ObjectName objName = new ObjectName(serverSocketFactoryName);
153
 
         MBeanServer mbeanServer = null;
154
 
         try
155
 
         {
156
 
            mbeanServer = (MBeanServer) AccessController.doPrivileged( new PrivilegedExceptionAction()
157
 
            {
158
 
               public Object run() throws Exception
159
 
               {
160
 
                  return MBeanServerFactory.createMBeanServer();
161
 
               }
162
 
            });
163
 
         }
164
 
         catch (PrivilegedActionException e)
165
 
         {
166
 
            throw (Exception) e.getCause();
167
 
         }
168
 
         mbeanServer.registerMBean(serverSocketService, objName);
169
 
         sconfig.put(ServerInvoker.SERVER_SOCKET_FACTORY, serverSocketFactoryName);
170
 
         
171
 
         // Put SSL keystore parameters in config map.
172
 
         sconfig.put(SSLSocketBuilder.REMOTING_SERVER_SOCKET_USE_CLIENT_MODE, "true");
173
 
         sconfig.put(SSLSocketBuilder.REMOTING_KEY_STORE_TYPE, "JKS");
174
 
         String keyStoreFilePath = getKeystoreFilePath();
175
 
         sconfig.put(SSLSocketBuilder.REMOTING_KEY_STORE_FILE_PATH, keyStoreFilePath);
176
 
         sconfig.put(SSLSocketBuilder.REMOTING_KEY_STORE_PASSWORD, "unit-tests-server");
177
 
         
178
 
         // Put SSL truststore parameters in config map.
179
 
         sconfig.put(SSLSocketBuilder.REMOTING_SOCKET_USE_CLIENT_MODE, "true");
180
 
         sconfig.put(SSLSocketBuilder.REMOTING_TRUST_STORE_TYPE, "JKS");
181
 
         String trustStoreFilePath = getTruststoreFilePath();
182
 
         sconfig.put(SSLSocketBuilder.REMOTING_TRUST_STORE_FILE_PATH, trustStoreFilePath);
183
 
         sconfig.put(SSLSocketBuilder.REMOTING_TRUST_STORE_PASSWORD, "unit-tests-client");
184
 
         
185
 
         Connector connector = new Connector(sconfig);
186
 
         mbeanServer.registerMBean(connector, new ObjectName("test:type=connector"));
187
 
         
188
 
         // Create and set xml configuration document.
189
 
         int freeport = PortUtil.findFreePort(getHostName());
190
 
         StringBuffer buf = new StringBuffer();
191
 
         buf.append("<?xml version=\"1.0\"?>\n");
192
 
         buf.append("<config>");
193
 
         buf.append("   <invoker transport=\"" + getTransport() + "\">");
194
 
         buf.append("      <attribute name=\"serverBindAddress\">" + getHostName() + "</attribute>");
195
 
         buf.append("      <attribute name=\"serverBindPort\">" + freeport + "</attribute>");
196
 
         buf.append("      <attribute name=\"serverSocketFactory\">");
197
 
         buf.append(         getUniqueServerSocketFactoryClass());
198
 
         buf.append("      </attribute>");
199
 
         buf.append("      <attribute name=\"socketFactory\">");
200
 
         buf.append(         getUniqueSocketFactoryClass());
201
 
         buf.append("      </attribute>");
202
 
         buf.append("   </invoker>");
203
 
         buf.append("</config>");
204
 
         ByteArrayInputStream bais = new ByteArrayInputStream(buf.toString().getBytes());
205
 
         Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(bais);
206
 
         connector.setConfiguration(xml.getDocumentElement());
207
 
         connector.create();
208
 
         
209
 
         // Set ServerSocketFactory and SocketFactory in ServerInvoker.
210
 
         ServerInvoker serverInvoker = connector.getServerInvoker();
211
 
         ServerSocketFactory ssf2 = getDefaultServerSocketFactory();
212
 
         serverInvoker.setServerSocketFactory(ssf2);
213
 
         SocketFactory sf2 = getDefaultCallbackSocketFactory();
214
 
         serverInvoker.setSocketFactory(sf2);
215
 
 
216
 
         connector.addInvocationHandler("sample", new SampleInvocationHandler());
217
 
         connector.start();
218
 
 
219
 
         // Verify ServerSocketFactory is the one set in ServerInvoker.
220
 
         assertTrue(ssf2 == serverInvoker.getServerSocketFactory());
221
 
         
222
 
         
223
 
         /////////////////////////////////////
224
 
         /////    Do client side test.    //// 
225
 
         /////////////////////////////////////
226
 
         HashMap cconfig = new HashMap();
227
 
         
228
 
         // Put SocketFactory in config map.
229
 
         SocketFactory sf3 = getDefaultSocketFactory();
230
 
         cconfig.put(Remoting.CUSTOM_SOCKET_FACTORY, sf3);
231
 
         
232
 
         // Make Client use remote invoker.
233
 
         cconfig.put(InvokerLocator.FORCE_REMOTE, "true");
234
 
         
235
 
         // Put SSL parameters in config map.
236
 
         cconfig.put(SSLSocketBuilder.REMOTING_SOCKET_USE_CLIENT_MODE, "true");
237
 
         cconfig.put(SSLSocketBuilder.REMOTING_TRUST_STORE_TYPE, "JKS");
238
 
         trustStoreFilePath = getTruststoreFilePath();
239
 
         cconfig.put(SSLSocketBuilder.REMOTING_TRUST_STORE_FILE_PATH, trustStoreFilePath);
240
 
         cconfig.put(SSLSocketBuilder.REMOTING_TRUST_STORE_PASSWORD, "unit-tests-client");
241
 
         
242
 
         InvokerLocator locator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
243
 
         Client client = new Client(locator, cconfig);
244
 
         client.connect();
245
 
         
246
 
         // Set SocketFactory in ClientInvoker.
247
 
         SocketFactory sf4 = getDefaultSocketFactory();
248
 
         ClientInvoker clientInvoker = client.getInvoker();
249
 
         clientInvoker.setSocketFactory(sf4);
250
 
         
251
 
         // Verify SocketFactory is the one set in ClientInvoker.
252
 
         assertTrue(sf4 == clientInvoker.getSocketFactory());
253
 
         
254
 
         
255
 
         //////////////////////////////////////////////
256
 
         /////     Do server side callback test.   //// 
257
 
         //////////////////////////////////////////////
258
 
         Thread.sleep(500);
259
 
         freeport = PortUtil.findFreePort(getHostName());
260
 
         InvokerLocator callbackLocator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
261
 
         HashMap config = new HashMap();
262
 
         addExtraCallbackConfig(config);
263
 
         Connector callbackConnector = new Connector(callbackLocator.getLocatorURI(), config);
264
 
         ServerSocketFactory ssf3 = getDefaultCallbackServerSocketFactory();
265
 
         callbackConnector.setServerSocketFactory(ssf3);
266
 
         callbackConnector.create();
267
 
         callbackConnector.addInvocationHandler("sample", new SampleInvocationHandler());
268
 
         callbackConnector.start();
269
 
         
270
 
         CallbackHandler callbackHandler = new CallbackHandler();
271
 
         String callbackHandleObject = "myCallbackHandleObject";
272
 
         client.addListener(callbackHandler, callbackLocator, callbackHandleObject);
273
 
         
274
 
         // Verify that callback succeeded.
275
 
         assertEquals(1, callbackHandler.getCallbacks().size());
276
 
         
277
 
         // Verify callback SocketFactory is the one set in SocketInvoker.
278
 
         Field field = ServerInvoker.class.getDeclaredField("handlers");
279
 
         field.setAccessible(true);
280
 
         Map handlers = (Map) field.get(serverInvoker);
281
 
         Object obj = handlers.values().iterator().next();
282
 
         SampleInvocationHandler sampleInvocationHandler = (SampleInvocationHandler) obj;
283
 
         obj = sampleInvocationHandler.getCallbackHandler();
284
 
         ServerInvokerCallbackHandler serverInvokerCallbackHandler = (ServerInvokerCallbackHandler) obj;
285
 
         field = ServerInvokerCallbackHandler.class.getDeclaredField("callBackClient");
286
 
         field.setAccessible(true);
287
 
         Client callbackClient = (Client) field.get(serverInvokerCallbackHandler);
288
 
         ClientInvoker callbackClientInvoker = callbackClient.getInvoker();
289
 
         assertTrue(sf2 == callbackClientInvoker.getSocketFactory());
290
 
         
291
 
         client.disconnect();
292
 
         callbackConnector.stop();
293
 
         connector.stop();
294
 
         log.info(getName() + " PASSES");
295
 
      }
296
 
      catch (Throwable t)
297
 
      {
298
 
         log.error(t);
299
 
         t.printStackTrace();
300
 
         log.info(getName() + " FAILS");
301
 
         fail();
302
 
      }
303
 
   }
304
 
   
305
 
   
306
 
   public void testFactoriesBySettingConnectorAndClient()
307
 
   {
308
 
      try
309
 
      {
310
 
         /////////////////////////////////////
311
 
         /////    Do server side test.    //// 
312
 
         /////////////////////////////////////
313
 
         HashMap sconfig = new HashMap();
314
 
         
315
 
         // Put ServerSocketFactory and SocketFactory in config map.
316
 
         ServerSocketFactory ssf1 = getDefaultServerSocketFactory();
317
 
         sconfig.put(Remoting.CUSTOM_SERVER_SOCKET_FACTORY, ssf1);
318
 
         SocketFactory sf1 = getDefaultCallbackSocketFactory();
319
 
         sconfig.put(Remoting.CUSTOM_SOCKET_FACTORY, sf1);
320
 
         
321
 
         // Make callback Client use remote invoker.
322
 
         sconfig.put(InvokerLocator.FORCE_REMOTE, "true");
323
 
         
324
 
         // Special case: tell HTTPSClientInvoker to ignore hostname in certificates.
325
 
         // This is because InvokerLocator turns "localhost" into "127.0.0.1". Should
326
 
         // be fixed by JBREM-497.
327
 
         sconfig.put(HTTPSClientInvoker.IGNORE_HTTPS_HOST, "true");
328
 
         
329
 
         // Put ServerSocketFactory MBean name in config map.
330
 
         ServerSocketFactory serverSocketService = getDefaultServerSocketFactory();
331
 
         String serverSocketFactoryName = "jboss:type=serversocketfactory";
332
 
         ObjectName objName = new ObjectName(serverSocketFactoryName);
333
 
         MBeanServer mbeanServer = null;
334
 
         try
335
 
         {
336
 
            mbeanServer = (MBeanServer) AccessController.doPrivileged( new PrivilegedExceptionAction()
337
 
            {
338
 
               public Object run() throws Exception
339
 
               {
340
 
                  return MBeanServerFactory.createMBeanServer();
341
 
               }
342
 
            });
343
 
         }
344
 
         catch (PrivilegedActionException e)
345
 
         {
346
 
            throw (Exception) e.getCause();
347
 
         }
348
 
         mbeanServer.registerMBean(serverSocketService, objName);
349
 
         sconfig.put(ServerInvoker.SERVER_SOCKET_FACTORY, serverSocketFactoryName);
350
 
         
351
 
         // Put SSL keystore parameters in config map.
352
 
         sconfig.put(SSLSocketBuilder.REMOTING_SERVER_SOCKET_USE_CLIENT_MODE, "true");
353
 
         sconfig.put(SSLSocketBuilder.REMOTING_KEY_STORE_TYPE, "JKS");
354
 
         String keyStoreFilePath = getKeystoreFilePath();
355
 
         sconfig.put(SSLSocketBuilder.REMOTING_KEY_STORE_FILE_PATH, keyStoreFilePath);
356
 
         sconfig.put(SSLSocketBuilder.REMOTING_KEY_STORE_PASSWORD, "unit-tests-server");
357
 
         
358
 
         // Put SSL truststore parameters in config map.
359
 
         sconfig.put(SSLSocketBuilder.REMOTING_SOCKET_USE_CLIENT_MODE, "true");
360
 
         sconfig.put(SSLSocketBuilder.REMOTING_TRUST_STORE_TYPE, "JKS");
361
 
         String trustStoreFilePath = getTruststoreFilePath();
362
 
         sconfig.put(SSLSocketBuilder.REMOTING_TRUST_STORE_FILE_PATH, trustStoreFilePath);
363
 
         sconfig.put(SSLSocketBuilder.REMOTING_TRUST_STORE_PASSWORD, "unit-tests-client");
364
 
         
365
 
         Connector connector = new Connector(sconfig);
366
 
         mbeanServer.registerMBean(connector, new ObjectName("test:type=connector"));
367
 
         
368
 
         // Create and set xml configuration document.
369
 
         int freeport = PortUtil.findFreePort(getHostName());
370
 
         StringBuffer buf = new StringBuffer();
371
 
         buf.append("<?xml version=\"1.0\"?>\n");
372
 
         buf.append("<config>");
373
 
         buf.append("   <invoker transport=\"" + getTransport() + "\">");
374
 
         buf.append("      <attribute name=\"serverBindAddress\">" + getHostName() + "</attribute>");
375
 
         buf.append("      <attribute name=\"serverBindPort\">" + freeport + "</attribute>");
376
 
         buf.append("      <attribute name=\"serverSocketFactory\">");
377
 
         buf.append(         getUniqueServerSocketFactoryClass());
378
 
         buf.append("      </attribute>");
379
 
         buf.append("      <attribute name=\"socketFactory\">");
380
 
         buf.append(         getUniqueSocketFactoryClass());
381
 
         buf.append("      </attribute>");
382
 
         buf.append("   </invoker>");
383
 
         buf.append("</config>");
384
 
         ByteArrayInputStream bais = new ByteArrayInputStream(buf.toString().getBytes());
385
 
         Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(bais);
386
 
         connector.setConfiguration(xml.getDocumentElement());
387
 
                  
388
 
         // Set ServerSocketFactory and SocketFactory in Connector.
389
 
         ServerSocketFactory ssf2 = getDefaultServerSocketFactory();
390
 
         connector.setServerSocketFactory(ssf2);
391
 
         SocketFactory sf2 = getDefaultCallbackSocketFactory();
392
 
         connector.setSocketFactory(sf2);
393
 
         connector.create();
394
 
         connector.addInvocationHandler("sample", new SampleInvocationHandler());
395
 
         connector.start();
396
 
         
397
 
         // Verify ServerSocketFactory is the one set in Connector.
398
 
         ServerInvoker serverInvoker = connector.getServerInvoker();
399
 
         assertTrue(ssf2 == serverInvoker.getServerSocketFactory());
400
 
         
401
 
         
402
 
         /////////////////////////////////////
403
 
         /////    Do client side test.    //// 
404
 
         /////////////////////////////////////
405
 
         HashMap cconfig = new HashMap();
406
 
         
407
 
         // Put SocketFactory in config map.
408
 
         SocketFactory sf3 = getDefaultSocketFactory();
409
 
         cconfig.put(Remoting.CUSTOM_SOCKET_FACTORY, sf3);
410
 
         
411
 
         // Make Client use remote invoker.
412
 
         cconfig.put(InvokerLocator.FORCE_REMOTE, "true");
413
 
         
414
 
         // Put SSL parameters in config map.
415
 
         cconfig.put(SSLSocketBuilder.REMOTING_SOCKET_USE_CLIENT_MODE, "true");
416
 
         cconfig.put(SSLSocketBuilder.REMOTING_TRUST_STORE_TYPE, "JKS");
417
 
         trustStoreFilePath = getTruststoreFilePath();
418
 
         cconfig.put(SSLSocketBuilder.REMOTING_TRUST_STORE_FILE_PATH, trustStoreFilePath);
419
 
         cconfig.put(SSLSocketBuilder.REMOTING_TRUST_STORE_PASSWORD, "unit-tests-client");
420
 
         
421
 
         InvokerLocator locator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
422
 
         Client client = new Client(locator, cconfig);
423
 
         
424
 
         // Set SocketFactory in Client.
425
 
         SocketFactory sf4 = getDefaultSocketFactory();
426
 
         client.setSocketFactory(sf4);
427
 
         client.connect();
428
 
         
429
 
         // Verify SocketFactory is the one set in Client.
430
 
         ClientInvoker clientInvoker = client.getInvoker();
431
 
         assertTrue(sf4 == clientInvoker.getSocketFactory());
432
 
         
433
 
         
434
 
         //////////////////////////////////////////////
435
 
         /////     Do server side callback test.   //// 
436
 
         //////////////////////////////////////////////
437
 
         Thread.sleep(500);
438
 
         freeport = PortUtil.findFreePort(getHostName());
439
 
         InvokerLocator callbackLocator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
440
 
         HashMap config = new HashMap();
441
 
         addExtraCallbackConfig(config);
442
 
         Connector callbackConnector = new Connector(callbackLocator.getLocatorURI(), config);
443
 
         ServerSocketFactory ssf3 = getDefaultCallbackServerSocketFactory();
444
 
         callbackConnector.setServerSocketFactory(ssf3);
445
 
         callbackConnector.create();
446
 
         callbackConnector.addInvocationHandler("sample", new SampleInvocationHandler());
447
 
         callbackConnector.start();
448
 
         
449
 
         CallbackHandler callbackHandler = new CallbackHandler();
450
 
         String callbackHandleObject = "myCallbackHandleObject";
451
 
         client.addListener(callbackHandler, callbackLocator, callbackHandleObject);
452
 
         
453
 
         // Verify that callback succeeded.
454
 
         assertEquals(1, callbackHandler.getCallbacks().size());
455
 
         
456
 
         // Verify callback SocketFactory is the one set in Connector.
457
 
         Field field = ServerInvoker.class.getDeclaredField("handlers");
458
 
         field.setAccessible(true);
459
 
         Map handlers = (Map) field.get(serverInvoker);
460
 
         Object obj = handlers.values().iterator().next();
461
 
         SampleInvocationHandler sampleInvocationHandler = (SampleInvocationHandler) obj;
462
 
         obj = sampleInvocationHandler.getCallbackHandler();
463
 
         ServerInvokerCallbackHandler serverInvokerCallbackHandler = (ServerInvokerCallbackHandler) obj;
464
 
         field = ServerInvokerCallbackHandler.class.getDeclaredField("callBackClient");
465
 
         field.setAccessible(true);
466
 
         Client callbackClient = (Client) field.get(serverInvokerCallbackHandler);
467
 
         ClientInvoker callbackClientInvoker = callbackClient.getInvoker();
468
 
         assertTrue(sf2 == callbackClientInvoker.getSocketFactory());
469
 
         
470
 
         client.disconnect();
471
 
         callbackConnector.stop();
472
 
         connector.stop();
473
 
         log.info(getName() + " PASSES");
474
 
      }
475
 
      catch (Throwable t)
476
 
      {
477
 
         log.error(t);
478
 
         t.printStackTrace();
479
 
         log.info(getName() + " FAILS");
480
 
         fail();
481
 
      }
482
 
   }
483
 
 
484
 
   
485
 
   public void testFactoriesByPassingInConfig()
486
 
   {
487
 
      try
488
 
      {
489
 
         /////////////////////////////////////
490
 
         /////    Do server side test.    //// 
491
 
         /////////////////////////////////////
492
 
         HashMap sconfig = new HashMap();
493
 
         
494
 
         // Put ServerSocketFactory and SocketFactory in config map.
495
 
         ServerSocketFactory ssf1 = getDefaultServerSocketFactory();
496
 
         sconfig.put(Remoting.CUSTOM_SERVER_SOCKET_FACTORY, ssf1);
497
 
         SocketFactory sf1 = getDefaultCallbackSocketFactory();
498
 
         sconfig.put(Remoting.CUSTOM_SOCKET_FACTORY, sf1);
499
 
         
500
 
         // Make callback Client use remote invoker.
501
 
         sconfig.put(InvokerLocator.FORCE_REMOTE, "true");
502
 
         
503
 
         // Special case: tell HTTPSClientInvoker to ignore hostname in certificates.
504
 
         // This is because InvokerLocator turns "localhost" into "127.0.0.1". Should
505
 
         // be fixed by JBREM-497.
506
 
         sconfig.put(HTTPSClientInvoker.IGNORE_HTTPS_HOST, "true");
507
 
         
508
 
         // Put ServerSocketFactory MBean name in config map.
509
 
         ServerSocketFactory serverSocketService = getDefaultServerSocketFactory();
510
 
         String serverSocketFactoryName = "jboss:type=serversocketfactory";
511
 
         ObjectName objName = new ObjectName(serverSocketFactoryName);
512
 
         MBeanServer mbeanServer = null;
513
 
 
514
 
         try
515
 
         {
516
 
            mbeanServer = (MBeanServer) AccessController.doPrivileged( new PrivilegedExceptionAction()
517
 
            {
518
 
               public Object run() throws Exception
519
 
               {
520
 
                  return MBeanServerFactory.createMBeanServer();
521
 
               }
522
 
            });
523
 
         }
524
 
         catch (PrivilegedActionException e)
525
 
         {
526
 
            throw (Exception) e.getCause();
527
 
         }
528
 
             
529
 
         mbeanServer.registerMBean(serverSocketService, objName);
530
 
         sconfig.put(ServerInvoker.SERVER_SOCKET_FACTORY, serverSocketFactoryName);
531
 
         
532
 
         // Put SSL keystore parameters in config map.
533
 
         sconfig.put(SSLSocketBuilder.REMOTING_SERVER_SOCKET_USE_CLIENT_MODE, "true");
534
 
         sconfig.put(SSLSocketBuilder.REMOTING_KEY_STORE_TYPE, "JKS");
535
 
         String keyStoreFilePath = getKeystoreFilePath();
536
 
         sconfig.put(SSLSocketBuilder.REMOTING_KEY_STORE_FILE_PATH, keyStoreFilePath);
537
 
         sconfig.put(SSLSocketBuilder.REMOTING_KEY_STORE_PASSWORD, "unit-tests-server");
538
 
         
539
 
         // Put SSL truststore parameters in config map.
540
 
         sconfig.put(SSLSocketBuilder.REMOTING_SOCKET_USE_CLIENT_MODE, "true");
541
 
         sconfig.put(SSLSocketBuilder.REMOTING_TRUST_STORE_TYPE, "JKS");
542
 
         String trustStoreFilePath = getTruststoreFilePath();
543
 
         sconfig.put(SSLSocketBuilder.REMOTING_TRUST_STORE_FILE_PATH, trustStoreFilePath);
544
 
         sconfig.put(SSLSocketBuilder.REMOTING_TRUST_STORE_PASSWORD, "unit-tests-client");
545
 
         
546
 
         Connector connector = new Connector(sconfig);
547
 
         mbeanServer.registerMBean(connector, new ObjectName("test:type=connector"));
548
 
         
549
 
         // Create and set xml configuration document.
550
 
         int freeport = PortUtil.findFreePort(getHostName());
551
 
         StringBuffer buf = new StringBuffer();
552
 
         buf.append("<?xml version=\"1.0\"?>\n");
553
 
         buf.append("<config>");
554
 
         buf.append("   <invoker transport=\"" + getTransport() + "\">");
555
 
         buf.append("      <attribute name=\"serverBindAddress\">" + getHostName() + "</attribute>");
556
 
         buf.append("      <attribute name=\"serverBindPort\">" + freeport + "</attribute>");
557
 
         buf.append("      <attribute name=\"serverSocketFactory\">");
558
 
         buf.append(         getUniqueServerSocketFactoryClass());
559
 
         buf.append("      </attribute>");
560
 
         buf.append("      <attribute name=\"socketFactory\">");
561
 
         buf.append(         getUniqueSocketFactoryClass());
562
 
         buf.append("      </attribute>");
563
 
         buf.append("   </invoker>");
564
 
         buf.append("</config>");
565
 
         ByteArrayInputStream bais = new ByteArrayInputStream(buf.toString().getBytes());
566
 
         Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(bais);
567
 
         connector.setConfiguration(xml.getDocumentElement());
568
 
         
569
 
         connector.create();
570
 
         connector.addInvocationHandler("sample", new SampleInvocationHandler());
571
 
         connector.start();
572
 
 
573
 
         // Verify ServerSocketFactory is the one passed in config map.
574
 
         ServerInvoker serverInvoker = connector.getServerInvoker();
575
 
         assertTrue(ssf1 == serverInvoker.getServerSocketFactory());
576
 
         
577
 
         
578
 
         /////////////////////////////////////
579
 
         /////    Do client side test.    //// 
580
 
         /////////////////////////////////////
581
 
         HashMap cconfig = new HashMap();
582
 
         
583
 
         // Put SocketFactory in config map.
584
 
         SocketFactory sf2 = getDefaultSocketFactory();
585
 
         cconfig.put(Remoting.CUSTOM_SOCKET_FACTORY, sf2);
586
 
         
587
 
         // Make Client use remote invoker.
588
 
         cconfig.put(InvokerLocator.FORCE_REMOTE, "true");
589
 
         
590
 
         // Put SSL parameters in config map.
591
 
         cconfig.put(SSLSocketBuilder.REMOTING_SOCKET_USE_CLIENT_MODE, "true");
592
 
         cconfig.put(SSLSocketBuilder.REMOTING_TRUST_STORE_TYPE, "JKS");
593
 
         trustStoreFilePath = getTruststoreFilePath();
594
 
         cconfig.put(SSLSocketBuilder.REMOTING_TRUST_STORE_FILE_PATH, trustStoreFilePath);
595
 
         cconfig.put(SSLSocketBuilder.REMOTING_TRUST_STORE_PASSWORD, "unit-tests-client");
596
 
 
597
 
         InvokerLocator locator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
598
 
         Client client = new Client(locator, cconfig);
599
 
         client.connect();
600
 
 
601
 
         // Verify SocketFactory is the one passed in config map.
602
 
         ClientInvoker clientInvoker = client.getInvoker();
603
 
         assertTrue(sf2 == clientInvoker.getSocketFactory());
604
 
         
605
 
         
606
 
         //////////////////////////////////////////////
607
 
         /////     Do server side callback test.   //// 
608
 
         //////////////////////////////////////////////
609
 
         Thread.sleep(500);
610
 
         freeport = PortUtil.findFreePort(getHostName());
611
 
         InvokerLocator callbackLocator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
612
 
         HashMap config = new HashMap();
613
 
         addExtraCallbackConfig(config);
614
 
         Connector callbackConnector = new Connector(callbackLocator.getLocatorURI(), config);
615
 
         ServerSocketFactory ssf3 = getDefaultCallbackServerSocketFactory();
616
 
         callbackConnector.setServerSocketFactory(ssf3);
617
 
         callbackConnector.create();
618
 
         callbackConnector.addInvocationHandler("sample", new SampleInvocationHandler());
619
 
         callbackConnector.start();
620
 
         
621
 
         CallbackHandler callbackHandler = new CallbackHandler();
622
 
         String callbackHandleObject = "myCallbackHandleObject";
623
 
         client.addListener(callbackHandler, callbackLocator, callbackHandleObject);
624
 
         
625
 
         // Verify that callback succeeded.
626
 
         assertEquals(1, callbackHandler.getCallbacks().size());
627
 
         
628
 
         // Verify callback SocketFactory is the one passed in config map.
629
 
         Field field = ServerInvoker.class.getDeclaredField("handlers");
630
 
         field.setAccessible(true);
631
 
         Map handlers = (Map) field.get(serverInvoker);
632
 
         Object obj = handlers.values().iterator().next();
633
 
         SampleInvocationHandler sampleInvocationHandler = (SampleInvocationHandler) obj;
634
 
         obj = sampleInvocationHandler.getCallbackHandler();
635
 
         ServerInvokerCallbackHandler serverInvokerCallbackHandler = (ServerInvokerCallbackHandler) obj;
636
 
         field = ServerInvokerCallbackHandler.class.getDeclaredField("callBackClient");
637
 
         field.setAccessible(true);
638
 
         Client callbackClient = (Client) field.get(serverInvokerCallbackHandler);
639
 
         ClientInvoker callbackClientInvoker = callbackClient.getInvoker();
640
 
         assertTrue(sf1 == callbackClientInvoker.getSocketFactory());
641
 
         
642
 
         client.disconnect();
643
 
         callbackConnector.stop();
644
 
         connector.stop();
645
 
         log.info(getName() + " PASSES");
646
 
      }
647
 
      catch (Throwable t)
648
 
      {
649
 
         log.error(t);
650
 
         t.printStackTrace();
651
 
         log.info(getName() + " FAILS");
652
 
         fail();
653
 
      }
654
 
   }
655
 
   
656
 
   
657
 
   public void testFactoriesByClassNameInXmlDoc()
658
 
   {
659
 
      try
660
 
      {
661
 
         /////////////////////////////////////
662
 
         /////    Do server side test.    //// 
663
 
         /////////////////////////////////////
664
 
         HashMap sconfig = new HashMap();
665
 
         
666
 
         // Put class names of ServerSocketFactory and SocketFactory in config map.
667
 
         sconfig.put(ServerInvoker.SERVER_SOCKET_FACTORY, ServerSocketFactory.getDefault().getClass().getName());
668
 
         sconfig.put(Remoting.SOCKET_FACTORY_NAME, SocketFactory.getDefault().getClass().getName());
669
 
         
670
 
         // Make callback Client use remote invoker.
671
 
         sconfig.put(InvokerLocator.FORCE_REMOTE, "true");
672
 
         
673
 
         // Special case: tell HTTPSClientInvoker to ignore hostname in certificates.
674
 
         // This is because InvokerLocator turns "localhost" into "127.0.0.1". Should
675
 
         // be fixed by JBREM-497.
676
 
         sconfig.put(HTTPSClientInvoker.IGNORE_HTTPS_HOST, "true");
677
 
         
678
 
         // Put ServerSocketFactory MBean name in config map.
679
 
         final ServerSocketFactory serverSocketService = getDefaultServerSocketFactory();
680
 
         String serverSocketFactoryName = "jboss:type=serversocketfactory";
681
 
         final ObjectName objName = new ObjectName(serverSocketFactoryName);
682
 
         MBeanServer mbeanServer = null;
683
 
         try
684
 
         {
685
 
            mbeanServer = (MBeanServer) AccessController.doPrivileged( new PrivilegedExceptionAction()
686
 
            {
687
 
               public Object run() throws Exception
688
 
               {
689
 
                  return MBeanServerFactory.createMBeanServer();
690
 
               }
691
 
            });
692
 
         }
693
 
         catch (PrivilegedActionException e)
694
 
         {
695
 
            throw (Exception) e.getCause();
696
 
         }
697
 
         try
698
 
         {
699
 
            final MBeanServer finalServer = mbeanServer;
700
 
            AccessController.doPrivileged( new PrivilegedExceptionAction()
701
 
            {
702
 
               public Object run() throws Exception
703
 
               {
704
 
                  finalServer.registerMBean(serverSocketService, objName);
705
 
                  return null;
706
 
               }
707
 
            });
708
 
         }
709
 
         catch (PrivilegedActionException e)
710
 
         {
711
 
            throw (Exception) e.getCause();
712
 
         }
713
 
//         mbeanServer.registerMBean(serverSocketService, objName);
714
 
         sconfig.put(ServerInvoker.SERVER_SOCKET_FACTORY, serverSocketFactoryName);
715
 
         
716
 
         // Put SSL keystore parameters in config map.
717
 
         sconfig.put(SSLSocketBuilder.REMOTING_SERVER_SOCKET_USE_CLIENT_MODE, "true");
718
 
         sconfig.put(SSLSocketBuilder.REMOTING_KEY_STORE_TYPE, "JKS");
719
 
         String keyStoreFilePath = getKeystoreFilePath();
720
 
         sconfig.put(SSLSocketBuilder.REMOTING_KEY_STORE_FILE_PATH, keyStoreFilePath);
721
 
         sconfig.put(SSLSocketBuilder.REMOTING_KEY_STORE_PASSWORD, "unit-tests-server");
722
 
         
723
 
         // Put SSL truststore parameters in config map.
724
 
         sconfig.put(SSLSocketBuilder.REMOTING_SOCKET_USE_CLIENT_MODE, "true");
725
 
         sconfig.put(SSLSocketBuilder.REMOTING_TRUST_STORE_TYPE, "JKS");
726
 
         String trustStoreFilePath = getTruststoreFilePath();
727
 
         sconfig.put(SSLSocketBuilder.REMOTING_TRUST_STORE_FILE_PATH, trustStoreFilePath);
728
 
         sconfig.put(SSLSocketBuilder.REMOTING_TRUST_STORE_PASSWORD, "unit-tests-client");
729
 
         
730
 
         Connector connector = new Connector(sconfig);
731
 
         
732
 
         // Create and set xml configuration document.
733
 
         int freeport = PortUtil.findFreePort(getHostName());
734
 
         StringBuffer buf = new StringBuffer();
735
 
         buf.append("<?xml version=\"1.0\"?>\n");
736
 
         buf.append("<config>");
737
 
         buf.append("   <invoker transport=\"" + getTransport() + "\">");
738
 
         buf.append("      <attribute name=\"serverBindAddress\">" + getHostName() + "</attribute>");
739
 
         buf.append("      <attribute name=\"serverBindPort\">" + freeport + "</attribute>");
740
 
         buf.append("      <attribute name=\"serverSocketFactory\">" +
741
 
                              getUniqueServerSocketFactoryClass().getName() +
742
 
                          "</attribute>");
743
 
         buf.append("      <attribute name=\"socketFactory\">");
744
 
         buf.append(         getUniqueCallbackSocketFactoryClass().getName());
745
 
         buf.append(      "</attribute>");
746
 
         buf.append("   </invoker>");
747
 
         buf.append("</config>");
748
 
         ByteArrayInputStream bais = new ByteArrayInputStream(buf.toString().getBytes());
749
 
         Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(bais);
750
 
         connector.setConfiguration(xml.getDocumentElement());
751
 
         
752
 
         connector.create();
753
 
         connector.addInvocationHandler("sample", new SampleInvocationHandler());
754
 
         connector.start();
755
 
 
756
 
         // Verify ServerSocketFactory is the one passed in config map.
757
 
         ServerInvoker serverInvoker = connector.getServerInvoker();
758
 
         assertTrue(getUniqueServerSocketFactoryClass() ==
759
 
                    serverInvoker.getServerSocketFactory().getClass());
760
 
         
761
 
         
762
 
         /////////////////////////////////////
763
 
         /////    Do client side test.    //// 
764
 
         /////////////////////////////////////
765
 
         HashMap cconfig = new HashMap();
766
 
         
767
 
         // Put SocketFactory class name in config map.
768
 
         cconfig.put(Remoting.SOCKET_FACTORY_NAME, getUniqueSocketFactoryClass().getName());
769
 
         
770
 
         // Make Client use remote invoker.
771
 
         cconfig.put(InvokerLocator.FORCE_REMOTE, "true");
772
 
         
773
 
         // Put SSL parameters in config map.
774
 
         cconfig.put(SSLSocketBuilder.REMOTING_SOCKET_USE_CLIENT_MODE, "true");
775
 
         cconfig.put(SSLSocketBuilder.REMOTING_TRUST_STORE_TYPE, "JKS");
776
 
         trustStoreFilePath = getTruststoreFilePath();
777
 
         cconfig.put(SSLSocketBuilder.REMOTING_TRUST_STORE_FILE_PATH, trustStoreFilePath);
778
 
         cconfig.put(SSLSocketBuilder.REMOTING_TRUST_STORE_PASSWORD, "unit-tests-client");
779
 
         
780
 
         InvokerLocator locator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
781
 
         Client client = new Client(locator, cconfig);
782
 
         client.connect();
783
 
 
784
 
         // Verify SocketFactory is the one passed in config map.
785
 
         ClientInvoker clientInvoker = client.getInvoker();
786
 
         assertTrue(getUniqueSocketFactoryClass() == clientInvoker.getSocketFactory().getClass());
787
 
         
788
 
         
789
 
         //////////////////////////////////////////////
790
 
         /////     Do server side callback test.   //// 
791
 
         //////////////////////////////////////////////
792
 
         Thread.sleep(500);
793
 
         freeport = PortUtil.findFreePort(getHostName());
794
 
         InvokerLocator callbackLocator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
795
 
         HashMap config = new HashMap();
796
 
         addExtraCallbackConfig(config);
797
 
         Connector callbackConnector = new Connector(callbackLocator.getLocatorURI(), config);
798
 
         ServerSocketFactory ssf3 = getDefaultCallbackServerSocketFactory();
799
 
         callbackConnector.setServerSocketFactory(ssf3);
800
 
         callbackConnector.create();
801
 
         callbackConnector.addInvocationHandler("sample", new SampleInvocationHandler());
802
 
         callbackConnector.start();
803
 
         
804
 
         CallbackHandler callbackHandler = new CallbackHandler();
805
 
         String callbackHandleObject = "myCallbackHandleObject";
806
 
         client.addListener(callbackHandler, callbackLocator, callbackHandleObject);
807
 
         
808
 
         // Verify that callback succeeded.
809
 
         assertEquals(1, callbackHandler.getCallbacks().size());
810
 
         
811
 
         // Verify callback SocketFactory is the one passed in config map.
812
 
         Field field = ServerInvoker.class.getDeclaredField("handlers");
813
 
         field.setAccessible(true);
814
 
         Map handlers = (Map) field.get(serverInvoker);
815
 
         Object obj = handlers.values().iterator().next();
816
 
         SampleInvocationHandler sampleInvocationHandler = (SampleInvocationHandler) obj;
817
 
         obj = sampleInvocationHandler.getCallbackHandler();
818
 
         ServerInvokerCallbackHandler serverInvokerCallbackHandler = (ServerInvokerCallbackHandler) obj;
819
 
         field = ServerInvokerCallbackHandler.class.getDeclaredField("callBackClient");
820
 
         field.setAccessible(true);
821
 
         Client callbackClient = (Client) field.get(serverInvokerCallbackHandler);
822
 
         ClientInvoker callbackClientInvoker = callbackClient.getInvoker();
823
 
         assertTrue(getUniqueCallbackSocketFactoryClass() ==
824
 
                    callbackClientInvoker.getSocketFactory().getClass());
825
 
         
826
 
         client.disconnect();
827
 
         callbackConnector.stop();
828
 
         connector.stop();
829
 
         log.info(getName() + " PASSES");
830
 
      }
831
 
      catch (Throwable t)
832
 
      {
833
 
         log.error(t);
834
 
         t.printStackTrace();
835
 
         log.info(getName() + " FAILS");
836
 
         fail();
837
 
      }
838
 
   }
839
 
   
840
 
   
841
 
   public void testFactoriesByClassNameInConfigMap()
842
 
   {
843
 
      try
844
 
      {
845
 
         /////////////////////////////////////
846
 
         /////    Do server side test.    //// 
847
 
         /////////////////////////////////////
848
 
         HashMap sconfig = new HashMap();
849
 
         
850
 
         // Put class names of ServerSocketFactory and SocketFactory in config map.
851
 
         sconfig.put(ServerInvoker.SERVER_SOCKET_FACTORY, getUniqueServerSocketFactoryClass().getName());
852
 
         sconfig.put(Remoting.SOCKET_FACTORY_NAME, getUniqueCallbackSocketFactoryClass().getName());
853
 
         
854
 
         // Make callback Client use remote invoker.
855
 
         sconfig.put(InvokerLocator.FORCE_REMOTE, "true");
856
 
         
857
 
         // Special case: tell HTTPSClientInvoker to ignore hostname in certificates.
858
 
         // This is because InvokerLocator turns "localhost" into "127.0.0.1". Should
859
 
         // be fixed by JBREM-497.
860
 
         sconfig.put(HTTPSClientInvoker.IGNORE_HTTPS_HOST, "true");
861
 
         
862
 
         // Put SSL keystore parameters in config map.
863
 
         sconfig.put(SSLSocketBuilder.REMOTING_SERVER_SOCKET_USE_CLIENT_MODE, "true");
864
 
         sconfig.put(SSLSocketBuilder.REMOTING_KEY_STORE_TYPE, "JKS");
865
 
         String keyStoreFilePath = getKeystoreFilePath();
866
 
         sconfig.put(SSLSocketBuilder.REMOTING_KEY_STORE_FILE_PATH, keyStoreFilePath);
867
 
         sconfig.put(SSLSocketBuilder.REMOTING_KEY_STORE_PASSWORD, "unit-tests-server");
868
 
         
869
 
         // Put SSL truststore parameters in config map.
870
 
         sconfig.put(SSLSocketBuilder.REMOTING_SOCKET_USE_CLIENT_MODE, "true");
871
 
         sconfig.put(SSLSocketBuilder.REMOTING_TRUST_STORE_TYPE, "JKS");
872
 
         String trustStoreFilePath = getTruststoreFilePath();
873
 
         sconfig.put(SSLSocketBuilder.REMOTING_TRUST_STORE_FILE_PATH, trustStoreFilePath);
874
 
         sconfig.put(SSLSocketBuilder.REMOTING_TRUST_STORE_PASSWORD, "unit-tests-client");
875
 
         
876
 
         int freeport = PortUtil.findFreePort(getHostName());
877
 
         InvokerLocator locator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
878
 
         Connector connector = new Connector(locator, sconfig);
879
 
         connector.create();
880
 
         connector.addInvocationHandler("sample", new SampleInvocationHandler());
881
 
         connector.start();
882
 
 
883
 
         // Verify ServerSocketFactory is the one passed in config map.
884
 
         ServerInvoker serverInvoker = connector.getServerInvoker();
885
 
         assertTrue(getUniqueServerSocketFactoryClass() ==
886
 
                    serverInvoker.getServerSocketFactory().getClass());
887
 
         
888
 
         
889
 
         /////////////////////////////////////
890
 
         /////    Do client side test.    //// 
891
 
         /////////////////////////////////////
892
 
         HashMap cconfig = new HashMap();
893
 
         
894
 
         // Put SocketFactory class name in config map.
895
 
         cconfig.put(Remoting.SOCKET_FACTORY_NAME, getUniqueSocketFactoryClass().getName());
896
 
         
897
 
         // Make Client use remote invoker.
898
 
         cconfig.put(InvokerLocator.FORCE_REMOTE, "true");
899
 
         
900
 
         // Put SSL parameters in config map.
901
 
         cconfig.put(SSLSocketBuilder.REMOTING_SOCKET_USE_CLIENT_MODE, "true");
902
 
         cconfig.put(SSLSocketBuilder.REMOTING_TRUST_STORE_TYPE, "JKS");
903
 
         trustStoreFilePath = getTruststoreFilePath();
904
 
         cconfig.put(SSLSocketBuilder.REMOTING_TRUST_STORE_FILE_PATH, trustStoreFilePath);
905
 
         cconfig.put(SSLSocketBuilder.REMOTING_TRUST_STORE_PASSWORD, "unit-tests-client");
906
 
         
907
 
         Client client = new Client(locator, cconfig);
908
 
         client.connect();
909
 
 
910
 
         // Verify SocketFactory is the one passed in config map.
911
 
         ClientInvoker clientInvoker = client.getInvoker();
912
 
         assertTrue(getUniqueSocketFactoryClass() == clientInvoker.getSocketFactory().getClass());
913
 
         
914
 
         
915
 
         //////////////////////////////////////////////
916
 
         /////     Do server side callback test.   //// 
917
 
         //////////////////////////////////////////////
918
 
         Thread.sleep(500);
919
 
         freeport = PortUtil.findFreePort(getHostName());
920
 
         InvokerLocator callbackLocator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
921
 
         HashMap config = new HashMap();
922
 
         addExtraCallbackConfig(config);
923
 
         Connector callbackConnector = new Connector(callbackLocator.getLocatorURI(), config);
924
 
         ServerSocketFactory ssf3 = getDefaultCallbackServerSocketFactory();
925
 
         callbackConnector.setServerSocketFactory(ssf3);
926
 
         callbackConnector.create();
927
 
         callbackConnector.addInvocationHandler("sample", new SampleInvocationHandler());
928
 
         callbackConnector.start();
929
 
         
930
 
         CallbackHandler callbackHandler = new CallbackHandler();
931
 
         String callbackHandleObject = "myCallbackHandleObject";
932
 
         client.addListener(callbackHandler, callbackLocator, callbackHandleObject);
933
 
         
934
 
         // Verify that callback succeeded.
935
 
         assertEquals(1, callbackHandler.getCallbacks().size());
936
 
         
937
 
         // Verify callback SocketFactory is the one passed in config map.
938
 
         Field field = ServerInvoker.class.getDeclaredField("handlers");
939
 
         field.setAccessible(true);
940
 
         Map handlers = (Map) field.get(serverInvoker);
941
 
         Object obj = handlers.values().iterator().next();
942
 
         SampleInvocationHandler sampleInvocationHandler = (SampleInvocationHandler) obj;
943
 
         obj = sampleInvocationHandler.getCallbackHandler();
944
 
         ServerInvokerCallbackHandler serverInvokerCallbackHandler = (ServerInvokerCallbackHandler) obj;
945
 
         field = ServerInvokerCallbackHandler.class.getDeclaredField("callBackClient");
946
 
         field.setAccessible(true);
947
 
         Client callbackClient = (Client) field.get(serverInvokerCallbackHandler);
948
 
         ClientInvoker callbackClientInvoker = callbackClient.getInvoker();
949
 
         assertTrue(getUniqueCallbackSocketFactoryClass() ==
950
 
                    callbackClientInvoker.getSocketFactory().getClass());
951
 
         
952
 
         client.disconnect();
953
 
         callbackConnector.stop();
954
 
         connector.stop();
955
 
         log.info(getName() + " PASSES");
956
 
      }
957
 
      catch (Throwable t)
958
 
      {
959
 
         log.error(t);
960
 
         t.printStackTrace();
961
 
         log.info(getName() + " FAILS");
962
 
         fail();
963
 
      }
964
 
   }
965
 
   
966
 
   
967
 
   protected String getHostName()
968
 
   {
969
 
      return "localhost";
970
 
   }
971
 
   
972
 
 
973
 
   protected ServerSocketFactory getDefaultServerSocketFactory() throws Exception
974
 
   {
975
 
      return new SelfIdentifyingServerSocketFactory();
976
 
   }
977
 
   
978
 
   
979
 
   protected SocketFactory getDefaultSocketFactory() throws Exception
980
 
   {
981
 
      return SocketFactory.getDefault();
982
 
   }
983
 
   
984
 
   
985
 
   protected ServerSocketFactory getDefaultCallbackServerSocketFactory() throws Exception
986
 
   {
987
 
      return new SelfIdentifyingServerSocketFactory();
988
 
   }
989
 
   
990
 
   
991
 
   protected SocketFactory getDefaultCallbackSocketFactory() throws Exception
992
 
   {
993
 
      return SocketFactory.getDefault();
994
 
   }
995
 
   
996
 
   protected Class getUniqueServerSocketFactoryClass() throws Exception
997
 
   {
998
 
      return UniqueServerSocketFactory.class;
999
 
   }
1000
 
   
1001
 
   protected Class getUniqueSocketFactoryClass() throws Exception
1002
 
   {
1003
 
      return UniqueSocketFactory.class;
1004
 
   }
1005
 
   
1006
 
   protected Class getUniqueCallbackServerSocketFactoryClass() throws Exception
1007
 
   {
1008
 
      return UniqueServerSocketFactory.class;
1009
 
   }
1010
 
   
1011
 
   protected Class getUniqueCallbackSocketFactoryClass() throws Exception
1012
 
   {
1013
 
      return UniqueSocketFactory.class;
1014
 
   }
1015
 
   
1016
 
   protected void addExtraCallbackConfig(Map config)
1017
 
   {
1018
 
   }
1019
 
   
1020
 
   public static class UniqueServerSocketFactory
1021
 
   extends ServerSocketFactory
1022
 
   implements ServerSocketFactoryMBean
1023
 
   {
1024
 
      public ServerSocket createServerSocket(int arg0) throws IOException
1025
 
      {
1026
 
         return new ServerSocket(arg0);
1027
 
      }
1028
 
 
1029
 
      public ServerSocket createServerSocket(int arg0, int arg1) throws IOException
1030
 
      {
1031
 
         return new ServerSocket(arg0, arg1);
1032
 
      }
1033
 
 
1034
 
      public ServerSocket createServerSocket(int arg0, int arg1, InetAddress arg2) throws IOException
1035
 
      {
1036
 
         return new ServerSocket(arg0, arg1, arg2);
1037
 
      }
1038
 
   }
1039
 
   
1040
 
   public static class UniqueSocketFactory
1041
 
   extends SocketFactory
1042
 
   implements SocketFactoryMBean
1043
 
   {
1044
 
      public Socket createSocket() throws IOException
1045
 
      {
1046
 
         return new Socket();
1047
 
      }
1048
 
      
1049
 
      public Socket createSocket(String arg0, int arg1) throws IOException, UnknownHostException
1050
 
      {
1051
 
         return new Socket(arg0, arg1);
1052
 
      }
1053
 
 
1054
 
      public Socket createSocket(String arg0, int arg1, InetAddress arg2, int arg3) throws IOException, UnknownHostException
1055
 
      {
1056
 
         return new Socket(arg0, arg1, arg2, arg3);
1057
 
      }
1058
 
 
1059
 
      public Socket createSocket(InetAddress arg0, int arg1) throws IOException
1060
 
      {
1061
 
         return new Socket(arg0, arg1);
1062
 
      }
1063
 
 
1064
 
      public Socket createSocket(InetAddress arg0, int arg1, InetAddress arg2, int arg3) throws IOException
1065
 
      {
1066
 
         return new Socket(arg0, arg1, arg2, arg3);
1067
 
      }
1068
 
   }
1069
 
   
1070
 
   public interface SelfIdentifyingServerSocketFactoryMBean extends ServerSocketFactoryMBean
1071
 
   {
1072
 
      ServerSocket createServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException;
1073
 
      ServerSocket createServerSocket(int port, int backlog) throws IOException;
1074
 
      ServerSocket createServerSocket(int port) throws IOException;
1075
 
      ServerSocket createServerSocket() throws IOException;
1076
 
      long getIdentity();
1077
 
      void setIdentity(long identity);
1078
 
   }
1079
 
   
1080
 
   
1081
 
   public static class SelfIdentifyingServerSocketFactory
1082
 
   extends ServerSocketFactory
1083
 
   implements SelfIdentifyingServerSocketFactoryMBean
1084
 
   {  
1085
 
      private long identity;
1086
 
      
1087
 
      public SelfIdentifyingServerSocketFactory()
1088
 
      {
1089
 
      }
1090
 
      
1091
 
      public long getIdentity()
1092
 
      {
1093
 
         return identity;
1094
 
      }
1095
 
      
1096
 
      public void setIdentity(long identity)
1097
 
      {
1098
 
         this.identity = identity;
1099
 
      }
1100
 
      
1101
 
      public ServerSocket createServerSocket(int arg0) throws IOException
1102
 
      {
1103
 
         return new SelfIdentifyingServerSocket(arg0, identity);
1104
 
      }
1105
 
 
1106
 
      public ServerSocket createServerSocket(int arg0, int arg1) throws IOException
1107
 
      {
1108
 
         return new SelfIdentifyingServerSocket(arg0, arg1, identity);
1109
 
      }
1110
 
 
1111
 
      public ServerSocket createServerSocket(int arg0, int arg1, InetAddress arg2) throws IOException
1112
 
      {
1113
 
         return new SelfIdentifyingServerSocket(arg0, arg1, arg2, identity);
1114
 
      }
1115
 
      
1116
 
      class SelfIdentifyingServerSocket extends ServerSocket
1117
 
      {
1118
 
         private long identity;
1119
 
         
1120
 
         public SelfIdentifyingServerSocket(int port, int backlog, InetAddress bindAddr, long identity)
1121
 
         throws IOException
1122
 
         {
1123
 
            super(port, backlog, bindAddr);
1124
 
            this.identity = identity;
1125
 
         }
1126
 
 
1127
 
         public SelfIdentifyingServerSocket(int port, int backlog, long identity) throws IOException
1128
 
         {
1129
 
            super(port, backlog);
1130
 
            this.identity = identity;
1131
 
         }
1132
 
 
1133
 
         public SelfIdentifyingServerSocket(int port, long identity) throws IOException
1134
 
         {
1135
 
            super(port);
1136
 
            this.identity = identity;
1137
 
         }
1138
 
 
1139
 
         public SelfIdentifyingServerSocket(long identity) throws IOException
1140
 
         {
1141
 
            super();
1142
 
            this.identity = identity;
1143
 
         }
1144
 
         
1145
 
         public long getIdentity()
1146
 
         {
1147
 
            return identity;
1148
 
         }
1149
 
      }
1150
 
   }
1151
 
   
1152
 
   
1153
 
   public static class SampleInvocationHandler implements ServerInvocationHandler
1154
 
   {
1155
 
      private InvokerCallbackHandler callbackHandler;
1156
 
      
1157
 
      public SampleInvocationHandler()
1158
 
      {   
1159
 
      }
1160
 
      
1161
 
      public Object invoke(InvocationRequest invocation) throws Throwable
1162
 
      {
1163
 
         return new Integer(0);
1164
 
      }
1165
 
      
1166
 
      public void addListener(InvokerCallbackHandler callbackHandler)
1167
 
      {
1168
 
         log.info("entering addListener()");
1169
 
         this.callbackHandler = callbackHandler;
1170
 
         
1171
 
         try
1172
 
         {
1173
 
            Callback callback = new Callback(new Integer(1));
1174
 
            callbackHandler.handleCallback(callback);
1175
 
            log.info("sent first callback");
1176
 
         }
1177
 
         catch(Exception e)
1178
 
         {
1179
 
            e.printStackTrace();
1180
 
         }
1181
 
      }
1182
 
      
1183
 
      public void removeListener(InvokerCallbackHandler callbackHandler)
1184
 
      {
1185
 
      }
1186
 
      
1187
 
      public void setMBeanServer(MBeanServer server)
1188
 
      {
1189
 
         // NO OP as do not need reference to MBeanServer for this handler
1190
 
      }
1191
 
      
1192
 
      public void setInvoker(ServerInvoker invoker)
1193
 
      {
1194
 
         // NO OP as do not need reference back to the server invoker
1195
 
      }
1196
 
      
1197
 
      public InvokerCallbackHandler getCallbackHandler()
1198
 
      {
1199
 
         return callbackHandler;
1200
 
      }
1201
 
   }
1202
 
   
1203
 
   
1204
 
   public static class CallbackHandler implements InvokerCallbackHandler
1205
 
   {
1206
 
      private ArrayList callbacks = new ArrayList();
1207
 
      
1208
 
      public CallbackHandler()
1209
 
      {
1210
 
      }
1211
 
      
1212
 
      public ArrayList getCallbacks()
1213
 
      {
1214
 
         return callbacks;
1215
 
      }
1216
 
      
1217
 
      /**
1218
 
       * Will take the callback and print out its values.
1219
 
       *
1220
 
       * @param callback
1221
 
       * @throws org.jboss.remoting.callback.HandleCallbackException
1222
 
       *
1223
 
       */
1224
 
      public void handleCallback(Callback callback) throws HandleCallbackException
1225
 
      {
1226
 
         log.info("Received push callback.");
1227
 
         log.info("Received callback value of: " + callback.getCallbackObject());
1228
 
         log.info("Received callback server invoker of: " + callback.getServerLocator());
1229
 
         callbacks.add(callback);
1230
 
      }
1231
 
   }
1232
 
}
 
 
b'\\ No newline at end of file'