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.
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.
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.
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.
23
package org.jboss.test.remoting.transport.config;
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;
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;
53
import java.io.ByteArrayInputStream;
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;
71
* @author <a href="mailto:ron.sigal@jboss.com">Ron Sigal</a>
72
* @version $Revision: 3873 $
74
* Copyright (c) Jun 13, 2006
77
public abstract class FactoryConfigTestCaseParent extends TestCase
79
protected static Logger log = Logger.getLogger(FactoryConfigTestCaseParent.class);
80
protected static boolean firstTime = true;
83
abstract protected String getTransport();
86
public static String getKeystoreFilePath()
88
File dir = (File)AccessController.doPrivileged( new PrivilegedAction()
92
return new File(FactoryConfigTestCaseParent.class.getResource(".").getFile());
95
while (!"transport".equals(dir.getName()))
97
dir = new File(dir.getParent());
99
return dir.getPath() + File.separator + "config/.keystore";
103
public static String getTruststoreFilePath()
105
File dir = new File(FactoryConfigTestCaseParent.class.getResource(".").getFile());
106
while (!"transport".equals(dir.getName()))
108
dir = new File(dir.getParent());
110
return dir.getPath() + File.separator + "config/.truststore";
119
log.info("********************************************************");
120
log.info("*********** FactoryConfigTestCase: " + getTransport() + " ***********");
121
log.info("********************************************************");
126
public void testFactoriesBySettingInvokers()
130
/////////////////////////////////////
131
///// Do server side test. ////
132
/////////////////////////////////////
133
HashMap sconfig = new HashMap();
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);
141
// Make callback Client use remote invoker.
142
sconfig.put(InvokerLocator.FORCE_REMOTE, "true");
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");
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;
156
mbeanServer = (MBeanServer) AccessController.doPrivileged( new PrivilegedExceptionAction()
158
public Object run() throws Exception
160
return MBeanServerFactory.createMBeanServer();
164
catch (PrivilegedActionException e)
166
throw (Exception) e.getCause();
168
mbeanServer.registerMBean(serverSocketService, objName);
169
sconfig.put(ServerInvoker.SERVER_SOCKET_FACTORY, serverSocketFactoryName);
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");
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");
185
Connector connector = new Connector(sconfig);
186
mbeanServer.registerMBean(connector, new ObjectName("test:type=connector"));
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());
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);
216
connector.addInvocationHandler("sample", new SampleInvocationHandler());
219
// Verify ServerSocketFactory is the one set in ServerInvoker.
220
assertTrue(ssf2 == serverInvoker.getServerSocketFactory());
223
/////////////////////////////////////
224
///// Do client side test. ////
225
/////////////////////////////////////
226
HashMap cconfig = new HashMap();
228
// Put SocketFactory in config map.
229
SocketFactory sf3 = getDefaultSocketFactory();
230
cconfig.put(Remoting.CUSTOM_SOCKET_FACTORY, sf3);
232
// Make Client use remote invoker.
233
cconfig.put(InvokerLocator.FORCE_REMOTE, "true");
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");
242
InvokerLocator locator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
243
Client client = new Client(locator, cconfig);
246
// Set SocketFactory in ClientInvoker.
247
SocketFactory sf4 = getDefaultSocketFactory();
248
ClientInvoker clientInvoker = client.getInvoker();
249
clientInvoker.setSocketFactory(sf4);
251
// Verify SocketFactory is the one set in ClientInvoker.
252
assertTrue(sf4 == clientInvoker.getSocketFactory());
255
//////////////////////////////////////////////
256
///// Do server side callback test. ////
257
//////////////////////////////////////////////
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();
270
CallbackHandler callbackHandler = new CallbackHandler();
271
String callbackHandleObject = "myCallbackHandleObject";
272
client.addListener(callbackHandler, callbackLocator, callbackHandleObject);
274
// Verify that callback succeeded.
275
assertEquals(1, callbackHandler.getCallbacks().size());
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());
292
callbackConnector.stop();
294
log.info(getName() + " PASSES");
300
log.info(getName() + " FAILS");
306
public void testFactoriesBySettingConnectorAndClient()
310
/////////////////////////////////////
311
///// Do server side test. ////
312
/////////////////////////////////////
313
HashMap sconfig = new HashMap();
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);
321
// Make callback Client use remote invoker.
322
sconfig.put(InvokerLocator.FORCE_REMOTE, "true");
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");
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;
336
mbeanServer = (MBeanServer) AccessController.doPrivileged( new PrivilegedExceptionAction()
338
public Object run() throws Exception
340
return MBeanServerFactory.createMBeanServer();
344
catch (PrivilegedActionException e)
346
throw (Exception) e.getCause();
348
mbeanServer.registerMBean(serverSocketService, objName);
349
sconfig.put(ServerInvoker.SERVER_SOCKET_FACTORY, serverSocketFactoryName);
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");
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");
365
Connector connector = new Connector(sconfig);
366
mbeanServer.registerMBean(connector, new ObjectName("test:type=connector"));
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());
388
// Set ServerSocketFactory and SocketFactory in Connector.
389
ServerSocketFactory ssf2 = getDefaultServerSocketFactory();
390
connector.setServerSocketFactory(ssf2);
391
SocketFactory sf2 = getDefaultCallbackSocketFactory();
392
connector.setSocketFactory(sf2);
394
connector.addInvocationHandler("sample", new SampleInvocationHandler());
397
// Verify ServerSocketFactory is the one set in Connector.
398
ServerInvoker serverInvoker = connector.getServerInvoker();
399
assertTrue(ssf2 == serverInvoker.getServerSocketFactory());
402
/////////////////////////////////////
403
///// Do client side test. ////
404
/////////////////////////////////////
405
HashMap cconfig = new HashMap();
407
// Put SocketFactory in config map.
408
SocketFactory sf3 = getDefaultSocketFactory();
409
cconfig.put(Remoting.CUSTOM_SOCKET_FACTORY, sf3);
411
// Make Client use remote invoker.
412
cconfig.put(InvokerLocator.FORCE_REMOTE, "true");
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");
421
InvokerLocator locator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
422
Client client = new Client(locator, cconfig);
424
// Set SocketFactory in Client.
425
SocketFactory sf4 = getDefaultSocketFactory();
426
client.setSocketFactory(sf4);
429
// Verify SocketFactory is the one set in Client.
430
ClientInvoker clientInvoker = client.getInvoker();
431
assertTrue(sf4 == clientInvoker.getSocketFactory());
434
//////////////////////////////////////////////
435
///// Do server side callback test. ////
436
//////////////////////////////////////////////
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();
449
CallbackHandler callbackHandler = new CallbackHandler();
450
String callbackHandleObject = "myCallbackHandleObject";
451
client.addListener(callbackHandler, callbackLocator, callbackHandleObject);
453
// Verify that callback succeeded.
454
assertEquals(1, callbackHandler.getCallbacks().size());
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());
471
callbackConnector.stop();
473
log.info(getName() + " PASSES");
479
log.info(getName() + " FAILS");
485
public void testFactoriesByPassingInConfig()
489
/////////////////////////////////////
490
///// Do server side test. ////
491
/////////////////////////////////////
492
HashMap sconfig = new HashMap();
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);
500
// Make callback Client use remote invoker.
501
sconfig.put(InvokerLocator.FORCE_REMOTE, "true");
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");
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;
516
mbeanServer = (MBeanServer) AccessController.doPrivileged( new PrivilegedExceptionAction()
518
public Object run() throws Exception
520
return MBeanServerFactory.createMBeanServer();
524
catch (PrivilegedActionException e)
526
throw (Exception) e.getCause();
529
mbeanServer.registerMBean(serverSocketService, objName);
530
sconfig.put(ServerInvoker.SERVER_SOCKET_FACTORY, serverSocketFactoryName);
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");
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");
546
Connector connector = new Connector(sconfig);
547
mbeanServer.registerMBean(connector, new ObjectName("test:type=connector"));
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());
570
connector.addInvocationHandler("sample", new SampleInvocationHandler());
573
// Verify ServerSocketFactory is the one passed in config map.
574
ServerInvoker serverInvoker = connector.getServerInvoker();
575
assertTrue(ssf1 == serverInvoker.getServerSocketFactory());
578
/////////////////////////////////////
579
///// Do client side test. ////
580
/////////////////////////////////////
581
HashMap cconfig = new HashMap();
583
// Put SocketFactory in config map.
584
SocketFactory sf2 = getDefaultSocketFactory();
585
cconfig.put(Remoting.CUSTOM_SOCKET_FACTORY, sf2);
587
// Make Client use remote invoker.
588
cconfig.put(InvokerLocator.FORCE_REMOTE, "true");
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");
597
InvokerLocator locator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
598
Client client = new Client(locator, cconfig);
601
// Verify SocketFactory is the one passed in config map.
602
ClientInvoker clientInvoker = client.getInvoker();
603
assertTrue(sf2 == clientInvoker.getSocketFactory());
606
//////////////////////////////////////////////
607
///// Do server side callback test. ////
608
//////////////////////////////////////////////
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();
621
CallbackHandler callbackHandler = new CallbackHandler();
622
String callbackHandleObject = "myCallbackHandleObject";
623
client.addListener(callbackHandler, callbackLocator, callbackHandleObject);
625
// Verify that callback succeeded.
626
assertEquals(1, callbackHandler.getCallbacks().size());
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());
643
callbackConnector.stop();
645
log.info(getName() + " PASSES");
651
log.info(getName() + " FAILS");
657
public void testFactoriesByClassNameInXmlDoc()
661
/////////////////////////////////////
662
///// Do server side test. ////
663
/////////////////////////////////////
664
HashMap sconfig = new HashMap();
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());
670
// Make callback Client use remote invoker.
671
sconfig.put(InvokerLocator.FORCE_REMOTE, "true");
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");
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;
685
mbeanServer = (MBeanServer) AccessController.doPrivileged( new PrivilegedExceptionAction()
687
public Object run() throws Exception
689
return MBeanServerFactory.createMBeanServer();
693
catch (PrivilegedActionException e)
695
throw (Exception) e.getCause();
699
final MBeanServer finalServer = mbeanServer;
700
AccessController.doPrivileged( new PrivilegedExceptionAction()
702
public Object run() throws Exception
704
finalServer.registerMBean(serverSocketService, objName);
709
catch (PrivilegedActionException e)
711
throw (Exception) e.getCause();
713
// mbeanServer.registerMBean(serverSocketService, objName);
714
sconfig.put(ServerInvoker.SERVER_SOCKET_FACTORY, serverSocketFactoryName);
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");
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");
730
Connector connector = new Connector(sconfig);
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() +
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());
753
connector.addInvocationHandler("sample", new SampleInvocationHandler());
756
// Verify ServerSocketFactory is the one passed in config map.
757
ServerInvoker serverInvoker = connector.getServerInvoker();
758
assertTrue(getUniqueServerSocketFactoryClass() ==
759
serverInvoker.getServerSocketFactory().getClass());
762
/////////////////////////////////////
763
///// Do client side test. ////
764
/////////////////////////////////////
765
HashMap cconfig = new HashMap();
767
// Put SocketFactory class name in config map.
768
cconfig.put(Remoting.SOCKET_FACTORY_NAME, getUniqueSocketFactoryClass().getName());
770
// Make Client use remote invoker.
771
cconfig.put(InvokerLocator.FORCE_REMOTE, "true");
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");
780
InvokerLocator locator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
781
Client client = new Client(locator, cconfig);
784
// Verify SocketFactory is the one passed in config map.
785
ClientInvoker clientInvoker = client.getInvoker();
786
assertTrue(getUniqueSocketFactoryClass() == clientInvoker.getSocketFactory().getClass());
789
//////////////////////////////////////////////
790
///// Do server side callback test. ////
791
//////////////////////////////////////////////
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();
804
CallbackHandler callbackHandler = new CallbackHandler();
805
String callbackHandleObject = "myCallbackHandleObject";
806
client.addListener(callbackHandler, callbackLocator, callbackHandleObject);
808
// Verify that callback succeeded.
809
assertEquals(1, callbackHandler.getCallbacks().size());
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());
827
callbackConnector.stop();
829
log.info(getName() + " PASSES");
835
log.info(getName() + " FAILS");
841
public void testFactoriesByClassNameInConfigMap()
845
/////////////////////////////////////
846
///// Do server side test. ////
847
/////////////////////////////////////
848
HashMap sconfig = new HashMap();
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());
854
// Make callback Client use remote invoker.
855
sconfig.put(InvokerLocator.FORCE_REMOTE, "true");
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");
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");
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");
876
int freeport = PortUtil.findFreePort(getHostName());
877
InvokerLocator locator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
878
Connector connector = new Connector(locator, sconfig);
880
connector.addInvocationHandler("sample", new SampleInvocationHandler());
883
// Verify ServerSocketFactory is the one passed in config map.
884
ServerInvoker serverInvoker = connector.getServerInvoker();
885
assertTrue(getUniqueServerSocketFactoryClass() ==
886
serverInvoker.getServerSocketFactory().getClass());
889
/////////////////////////////////////
890
///// Do client side test. ////
891
/////////////////////////////////////
892
HashMap cconfig = new HashMap();
894
// Put SocketFactory class name in config map.
895
cconfig.put(Remoting.SOCKET_FACTORY_NAME, getUniqueSocketFactoryClass().getName());
897
// Make Client use remote invoker.
898
cconfig.put(InvokerLocator.FORCE_REMOTE, "true");
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");
907
Client client = new Client(locator, cconfig);
910
// Verify SocketFactory is the one passed in config map.
911
ClientInvoker clientInvoker = client.getInvoker();
912
assertTrue(getUniqueSocketFactoryClass() == clientInvoker.getSocketFactory().getClass());
915
//////////////////////////////////////////////
916
///// Do server side callback test. ////
917
//////////////////////////////////////////////
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();
930
CallbackHandler callbackHandler = new CallbackHandler();
931
String callbackHandleObject = "myCallbackHandleObject";
932
client.addListener(callbackHandler, callbackLocator, callbackHandleObject);
934
// Verify that callback succeeded.
935
assertEquals(1, callbackHandler.getCallbacks().size());
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());
953
callbackConnector.stop();
955
log.info(getName() + " PASSES");
961
log.info(getName() + " FAILS");
967
protected String getHostName()
973
protected ServerSocketFactory getDefaultServerSocketFactory() throws Exception
975
return new SelfIdentifyingServerSocketFactory();
979
protected SocketFactory getDefaultSocketFactory() throws Exception
981
return SocketFactory.getDefault();
985
protected ServerSocketFactory getDefaultCallbackServerSocketFactory() throws Exception
987
return new SelfIdentifyingServerSocketFactory();
991
protected SocketFactory getDefaultCallbackSocketFactory() throws Exception
993
return SocketFactory.getDefault();
996
protected Class getUniqueServerSocketFactoryClass() throws Exception
998
return UniqueServerSocketFactory.class;
1001
protected Class getUniqueSocketFactoryClass() throws Exception
1003
return UniqueSocketFactory.class;
1006
protected Class getUniqueCallbackServerSocketFactoryClass() throws Exception
1008
return UniqueServerSocketFactory.class;
1011
protected Class getUniqueCallbackSocketFactoryClass() throws Exception
1013
return UniqueSocketFactory.class;
1016
protected void addExtraCallbackConfig(Map config)
1020
public static class UniqueServerSocketFactory
1021
extends ServerSocketFactory
1022
implements ServerSocketFactoryMBean
1024
public ServerSocket createServerSocket(int arg0) throws IOException
1026
return new ServerSocket(arg0);
1029
public ServerSocket createServerSocket(int arg0, int arg1) throws IOException
1031
return new ServerSocket(arg0, arg1);
1034
public ServerSocket createServerSocket(int arg0, int arg1, InetAddress arg2) throws IOException
1036
return new ServerSocket(arg0, arg1, arg2);
1040
public static class UniqueSocketFactory
1041
extends SocketFactory
1042
implements SocketFactoryMBean
1044
public Socket createSocket() throws IOException
1046
return new Socket();
1049
public Socket createSocket(String arg0, int arg1) throws IOException, UnknownHostException
1051
return new Socket(arg0, arg1);
1054
public Socket createSocket(String arg0, int arg1, InetAddress arg2, int arg3) throws IOException, UnknownHostException
1056
return new Socket(arg0, arg1, arg2, arg3);
1059
public Socket createSocket(InetAddress arg0, int arg1) throws IOException
1061
return new Socket(arg0, arg1);
1064
public Socket createSocket(InetAddress arg0, int arg1, InetAddress arg2, int arg3) throws IOException
1066
return new Socket(arg0, arg1, arg2, arg3);
1070
public interface SelfIdentifyingServerSocketFactoryMBean extends ServerSocketFactoryMBean
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;
1077
void setIdentity(long identity);
1081
public static class SelfIdentifyingServerSocketFactory
1082
extends ServerSocketFactory
1083
implements SelfIdentifyingServerSocketFactoryMBean
1085
private long identity;
1087
public SelfIdentifyingServerSocketFactory()
1091
public long getIdentity()
1096
public void setIdentity(long identity)
1098
this.identity = identity;
1101
public ServerSocket createServerSocket(int arg0) throws IOException
1103
return new SelfIdentifyingServerSocket(arg0, identity);
1106
public ServerSocket createServerSocket(int arg0, int arg1) throws IOException
1108
return new SelfIdentifyingServerSocket(arg0, arg1, identity);
1111
public ServerSocket createServerSocket(int arg0, int arg1, InetAddress arg2) throws IOException
1113
return new SelfIdentifyingServerSocket(arg0, arg1, arg2, identity);
1116
class SelfIdentifyingServerSocket extends ServerSocket
1118
private long identity;
1120
public SelfIdentifyingServerSocket(int port, int backlog, InetAddress bindAddr, long identity)
1123
super(port, backlog, bindAddr);
1124
this.identity = identity;
1127
public SelfIdentifyingServerSocket(int port, int backlog, long identity) throws IOException
1129
super(port, backlog);
1130
this.identity = identity;
1133
public SelfIdentifyingServerSocket(int port, long identity) throws IOException
1136
this.identity = identity;
1139
public SelfIdentifyingServerSocket(long identity) throws IOException
1142
this.identity = identity;
1145
public long getIdentity()
1153
public static class SampleInvocationHandler implements ServerInvocationHandler
1155
private InvokerCallbackHandler callbackHandler;
1157
public SampleInvocationHandler()
1161
public Object invoke(InvocationRequest invocation) throws Throwable
1163
return new Integer(0);
1166
public void addListener(InvokerCallbackHandler callbackHandler)
1168
log.info("entering addListener()");
1169
this.callbackHandler = callbackHandler;
1173
Callback callback = new Callback(new Integer(1));
1174
callbackHandler.handleCallback(callback);
1175
log.info("sent first callback");
1179
e.printStackTrace();
1183
public void removeListener(InvokerCallbackHandler callbackHandler)
1187
public void setMBeanServer(MBeanServer server)
1189
// NO OP as do not need reference to MBeanServer for this handler
1192
public void setInvoker(ServerInvoker invoker)
1194
// NO OP as do not need reference back to the server invoker
1197
public InvokerCallbackHandler getCallbackHandler()
1199
return callbackHandler;
1204
public static class CallbackHandler implements InvokerCallbackHandler
1206
private ArrayList callbacks = new ArrayList();
1208
public CallbackHandler()
1212
public ArrayList getCallbacks()
1218
* Will take the callback and print out its values.
1221
* @throws org.jboss.remoting.callback.HandleCallbackException
1224
public void handleCallback(Callback callback) throws HandleCallbackException
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);
b'\\ No newline at end of file'