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.
22
package org.jboss.remoting.samples.config.factories;
24
import java.io.ByteArrayInputStream;
26
import java.util.HashMap;
28
import javax.management.MBeanServer;
29
import javax.management.MBeanServerFactory;
30
import javax.management.ObjectName;
31
import javax.net.ServerSocketFactory;
32
import javax.net.SocketFactory;
33
import javax.xml.parsers.DocumentBuilderFactory;
35
import junit.framework.TestCase;
37
import org.jboss.logging.Logger;
38
import org.jboss.remoting.Client;
39
import org.jboss.remoting.InvokerLocator;
40
import org.jboss.remoting.ServerInvoker;
41
import org.jboss.remoting.samples.config.factories.FactoryConfigSample.CallbackHandler;
42
import org.jboss.remoting.samples.config.factories.FactoryConfigSample.SampleInvocationHandler;
43
import org.jboss.remoting.security.SSLServerSocketFactoryService;
44
import org.jboss.remoting.security.SSLSocketBuilder;
45
import org.jboss.remoting.security.SSLSocketFactoryService;
46
import org.jboss.remoting.transport.Connector;
47
import org.jboss.remoting.transport.PortUtil;
48
import org.w3c.dom.Document;
51
* These methods illustrate configuring socket factories and server socket factories
52
* on the server side and on the client side. The numbered
53
* options mentioned refer to the lists of configuration options discussed in the
54
* Remoting documentation in the subsections "Server side configuration" and
55
* "Client side configuration" of the section called "Socket factories and server
58
* The configuration options illustrated in this class are specific to SSL
59
* sockets and server sockets. The methods
60
* <code>getDefaultServerSocketFactory()</code>,
61
* <code>getDefaultSocketFactory()</code>,
62
* <code>getDefaultCallbackServerSocketFactory()</code> and
63
* <code>getDefaultCallbackSocketFactory()</code>
64
* illustrate the use of the class <code>SSLSocketBuilder</code> to
65
* create custom SSL socket factories and SSL server socket factories.
67
* @author <a href="mailto:ron.sigal@jboss.com">Ron Sigal</a>
69
* Copyright (c) Jul 20, 2006
72
public class FactoryConfigSSLSample extends TestCase
74
private static Logger log = Logger.getLogger(FactoryConfigSSLSample.class);
78
/** This test illustrates the following set of configuration options:
80
* <table border cellpadding="5">
81
* <tr><td align="center"><b>side<td align="center"><b>factory<td><b>option</tr>
82
* <tr><td>server side<td align="center">server socket<td align="center">4</tr>
83
* <tr><td>server side<td align="center">socket <td align="center">4</tr>
84
* <tr><td>client side<td align="center">server socket<td align="center">1</tr>
85
* <tr><td>client side<td align="center">socket <td align="center">1</tr>
88
public void testFactoriesByPassingMBeanInXml()
92
/////////////////////////////////////
93
///// Set up server side. ////
94
/////////////////////////////////////
95
HashMap sconfig = new HashMap();
97
// Make callback Client use remote invoker.
98
sconfig.put(InvokerLocator.FORCE_REMOTE, "true");
100
// Create ServerSocketFactory MBean.
101
ServerSocketFactory service = getDefaultServerSocketFactory();
102
String serverSocketFactoryName = "jboss:type=serversocketfactory";
103
ObjectName objName = new ObjectName(serverSocketFactoryName);
104
MBeanServer mbeanServer = MBeanServerFactory.createMBeanServer();
105
mbeanServer.registerMBean(service, objName);
108
int freeport = PortUtil.findFreePort(getHostName());
109
InvokerLocator locator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
110
Connector connector = new Connector(sconfig);
111
mbeanServer.registerMBean(connector, new ObjectName("test:type=connector"));
113
// Set xml configuration element.
114
StringBuffer buf = new StringBuffer();
115
buf.append("<?xml version=\"1.0\"?>\n");
116
buf.append("<config>");
117
buf.append( "<invoker transport=\"" + getTransport() +"\">");
118
buf.append( "<attribute name=\"serverBindAddress\">" + getHostName() + "</attribute>");
119
buf.append( "<attribute name=\"serverBindPort\">" + freeport + "</attribute>");
120
buf.append( "<attribute name=\"serverSocketFactory\">" );
121
buf.append( serverSocketFactoryName);
122
buf.append( "</attribute>");
123
buf.append( "</invoker>");
124
buf.append("</config>");
125
ByteArrayInputStream bais = new ByteArrayInputStream(buf.toString().getBytes());
126
Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(bais);
127
connector.setConfiguration(xml.getDocumentElement());
130
connector.addInvocationHandler("sample", new SampleInvocationHandler());
134
/////////////////////////////////////
135
///// Set up client side. ////
136
/////////////////////////////////////
137
HashMap cconfig = new HashMap();
139
// Make Client use remote invoker.
140
cconfig.put(InvokerLocator.FORCE_REMOTE, "true");
143
Client client = new Client(locator, cconfig);
145
// Set SocketFactory in Client.
146
// Note. There is no provision for using xml configuration on client side.
147
SocketFactory sf = getDefaultSocketFactory();
148
client.setSocketFactory(sf);
150
System.out.println(getName() + ": " + client.invoke("test invoke()"));
153
//////////////////////////////////////////////
154
///// Set up callback handling. ////
155
//////////////////////////////////////////////
157
// Start callback Connector.
158
// Note: there is no provision for using MBeanServer on client side.
159
freeport = PortUtil.findFreePort(getHostName());
160
InvokerLocator callbackLocator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
161
Connector callbackConnector = new Connector(callbackLocator.getLocatorURI());
162
ServerSocketFactory ssf = getDefaultCallbackServerSocketFactory();
163
callbackConnector.setServerSocketFactory(ssf);
164
callbackConnector.create();
165
callbackConnector.addInvocationHandler("sample", new FactoryConfigSample.SampleInvocationHandler());
166
callbackConnector.start();
168
// Add callback handler.
169
CallbackHandler callbackHandler = new FactoryConfigSample.CallbackHandler();
170
String callbackHandleObject = "myCallbackHandleObject";
171
client.addListener(callbackHandler, callbackLocator, callbackHandleObject);
174
callbackConnector.stop();
186
/** This test illustrates the following set of configuration options:
188
* <table border cellpadding="5">
189
* <tr><td align="center"><b>side<td align="center"><b>factory<td><b>option</tr>
190
* <tr><td>server side<td align="center">server socket<td align="center">6</tr>
191
* <tr><td>server side<td align="center">socket <td align="center">5</tr>
192
* <tr><td>client side<td align="center">server socket<td align="center">1</tr>
193
* <tr><td>client side<td align="center">socket <td align="center">1</tr>
196
* <b>Note.</b> There is no provision for using an <code>MBeanServer</code> on
199
public void testFactoriesByPassingMBeanInConfig()
203
/////////////////////////////////////
204
///// Set up server side. ////
205
/////////////////////////////////////
206
HashMap sconfig = new HashMap();
208
// Put ServerSocketFactory MBean name in config map.
209
ServerSocketFactory service = getDefaultServerSocketFactory();
210
String serverSocketFactoryName = "jboss:type=serversocketfactory";
211
ObjectName objName = new ObjectName(serverSocketFactoryName);
212
MBeanServer mbeanServer = MBeanServerFactory.createMBeanServer();
213
mbeanServer.registerMBean(service, objName);
214
sconfig.put(ServerInvoker.SERVER_SOCKET_FACTORY, serverSocketFactoryName);
216
// Make callback Client use remote invoker.
217
sconfig.put(InvokerLocator.FORCE_REMOTE, "true");
220
int freeport = PortUtil.findFreePort(getHostName());
221
InvokerLocator locator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
222
Connector connector = new Connector(locator, sconfig);
223
mbeanServer.registerMBean(connector, new ObjectName("test:type=connector"));
225
connector.addInvocationHandler("sample", new FactoryConfigSample.SampleInvocationHandler());
229
/////////////////////////////////////
230
///// Set up client side. ////
231
/////////////////////////////////////
232
HashMap cconfig = new HashMap();
234
// Make Client use remote invoker.
235
cconfig.put(InvokerLocator.FORCE_REMOTE, "true");
238
Client client = new Client(locator, cconfig);
240
// Set server socket factory on Client.
241
// Note: There is no provision for using MBeanServer on client side.
242
SocketFactory sf = getDefaultSocketFactory();
243
client.setSocketFactory(sf);
245
System.out.println(getName() + ": " + client.invoke("test invoke()"));
248
//////////////////////////////////////////////
249
///// Set up callback handling. ////
250
//////////////////////////////////////////////
252
// Start callback Connector.
253
// Note: there is no provision for using MBeanServer on client side.
254
freeport = PortUtil.findFreePort(getHostName());
255
InvokerLocator callbackLocator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
256
Connector callbackConnector = new Connector(callbackLocator.getLocatorURI());
257
ServerSocketFactory ssf = getDefaultCallbackServerSocketFactory();
258
callbackConnector.setServerSocketFactory(ssf);
259
callbackConnector.create();
260
callbackConnector.addInvocationHandler("sample", new FactoryConfigSample.SampleInvocationHandler());
261
callbackConnector.start();
263
// Add callback handler.
264
CallbackHandler callbackHandler = new FactoryConfigSample.CallbackHandler();
265
String callbackHandleObject = "myCallbackHandleObject";
266
client.addListener(callbackHandler, callbackLocator, callbackHandleObject);
269
callbackConnector.stop();
281
/** This test illustrates the following set of configuration options:
283
* <table border cellpadding="5">
284
* <tr><td align="center"><b>side<td align="center"><b>factory<td><b>option</tr>
285
* <tr><td>server side<td align="center">server socket<td align="center">8</tr>
286
* <tr><td>server side<td align="center">socket <td align="center">8</tr>
287
* <tr><td>client side<td align="center">server socket<td align="center">6</tr>
288
* <tr><td>client side<td align="center">socket <td align="center">5</tr>
291
public void testFactoriesFromSSLParameters()
295
/////////////////////////////////////
296
///// Set up server side. ////
297
/////////////////////////////////////
298
HashMap sconfig = new HashMap();
300
// Make callback Client use remote invoker.
301
sconfig.put(InvokerLocator.FORCE_REMOTE, "true");
303
// Put SSL keystore parameters in config map.
304
sconfig.put(SSLSocketBuilder.REMOTING_SERVER_SOCKET_USE_CLIENT_MODE, "false");
305
sconfig.put(SSLSocketBuilder.REMOTING_KEY_STORE_TYPE, "JKS");
306
String keyStoreFilePath = getKeystoreFilePath();
307
sconfig.put(SSLSocketBuilder.REMOTING_KEY_STORE_FILE_PATH, keyStoreFilePath);
308
sconfig.put(SSLSocketBuilder.REMOTING_KEY_STORE_PASSWORD, "unit-tests-server");
311
int freeport = PortUtil.findFreePort(getHostName());
312
InvokerLocator locator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
313
Connector connector = new Connector(locator, sconfig);
315
connector.addInvocationHandler("sample", new FactoryConfigSample.SampleInvocationHandler());
319
/////////////////////////////////////
320
///// Set up client side. ////
321
/////////////////////////////////////
322
HashMap cconfig = new HashMap();
324
// Make Client use remote invoker.
325
cconfig.put(InvokerLocator.FORCE_REMOTE, "true");
327
// Put SSL parameters in config map.
328
cconfig.put(SSLSocketBuilder.REMOTING_SOCKET_USE_CLIENT_MODE, "true");
329
cconfig.put(SSLSocketBuilder.REMOTING_TRUST_STORE_TYPE, "JKS");
330
String trustStoreFilePath = getTruststoreFilePath();
331
cconfig.put(SSLSocketBuilder.REMOTING_TRUST_STORE_FILE_PATH, trustStoreFilePath);
332
cconfig.put(SSLSocketBuilder.REMOTING_TRUST_STORE_PASSWORD, "unit-tests-client");
335
Client client = new Client(locator, cconfig);
337
System.out.println(getName() + ": " + client.invoke("test invoke()"));
340
//////////////////////////////////////////////
341
///// Set up callback handling. ////
342
//////////////////////////////////////////////
344
// Start callback Connector.
345
HashMap cbconfig = new HashMap();
347
// Put SSL parameters in config map.
348
cbconfig.put(SSLSocketBuilder.REMOTING_SERVER_SOCKET_USE_CLIENT_MODE, "true");
349
cbconfig.put(SSLSocketBuilder.REMOTING_TRUST_STORE_TYPE, "JKS");
350
cbconfig.put(SSLSocketBuilder.REMOTING_TRUST_STORE_FILE_PATH, trustStoreFilePath);
351
cbconfig.put(SSLSocketBuilder.REMOTING_TRUST_STORE_PASSWORD, "unit-tests-client");
353
freeport = PortUtil.findFreePort(getHostName());
354
InvokerLocator callbackLocator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
355
Connector callbackConnector = new Connector(callbackLocator.getLocatorURI(), cbconfig);
356
callbackConnector.create();
357
callbackConnector.addInvocationHandler("sample", new FactoryConfigSample.SampleInvocationHandler());
358
callbackConnector.start();
360
// Add callback handler.
361
CallbackHandler callbackHandler = new CallbackHandler();
362
String callbackHandleObject = "myCallbackHandleObject";
363
client.addListener(callbackHandler, callbackLocator, callbackHandleObject);
366
callbackConnector.stop();
378
/** This test illustrates the following set of configuration options:
380
* <table border cellpadding="5">
381
* <tr><td align="center"><b>side<td align="center"><b>factory<td><b>option</tr>
382
* <tr><td>server side<td align="center">server socket<td align="center">9</tr>
383
* <tr><td>server side<td align="center">socket <td align="center">9</tr>
384
* <tr><td>client side<td align="center">server socket<td align="center">7</tr>
385
* <tr><td>client side<td align="center">socket <td align="center">6</tr>
388
public void testFactoriesFromSystemSSLParameters()
392
/////////////////////////////////////
393
///// Set up server side. ////
394
/////////////////////////////////////
395
HashMap sconfig = new HashMap();
397
// Make callback Client use remote invoker.
398
sconfig.put(InvokerLocator.FORCE_REMOTE, "true");
400
// Set SSL system properties.
401
System.setProperty(SSLSocketBuilder.STANDARD_KEY_STORE_TYPE, "JKS");
402
String keyStoreFilePath = getKeystoreFilePath();
403
System.setProperty(SSLSocketBuilder.STANDARD_KEY_STORE_FILE_PATH, keyStoreFilePath);
404
System.setProperty(SSLSocketBuilder.STANDARD_KEY_STORE_PASSWORD, "unit-tests-server");
405
System.setProperty(SSLSocketBuilder.STANDARD_TRUST_STORE_TYPE, "JKS");
406
String trustStoreFilePath = getTruststoreFilePath();
407
System.setProperty(SSLSocketBuilder.STANDARD_TRUST_STORE_FILE_PATH, trustStoreFilePath);
408
System.setProperty(SSLSocketBuilder.STANDARD_TRUST_STORE_PASSWORD, "unit-tests-client");
411
int freeport = PortUtil.findFreePort(getHostName());
412
InvokerLocator locator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
413
Connector connector = new Connector(locator, sconfig);
415
connector.addInvocationHandler("sample", new FactoryConfigSample.SampleInvocationHandler());
419
/////////////////////////////////////
420
///// Set up client side. ////
421
/////////////////////////////////////
422
HashMap cconfig = new HashMap();
424
// Make Client use remote invoker.
425
cconfig.put(InvokerLocator.FORCE_REMOTE, "true");
428
Client client = new Client(locator, cconfig);
430
System.out.println(getName() + ": " + client.invoke("test invoke()"));
433
//////////////////////////////////////////////
434
///// Set up callback handling. ////
435
//////////////////////////////////////////////
437
// Start callback Connector.
438
HashMap cbconfig = new HashMap();
440
// Make callback Connector server socket run in client mode.
441
cbconfig.put(SSLSocketBuilder.REMOTING_SERVER_SOCKET_USE_CLIENT_MODE, "true");
443
freeport = PortUtil.findFreePort(getHostName());
444
InvokerLocator callbackLocator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
445
Connector callbackConnector = new Connector(callbackLocator.getLocatorURI(), cbconfig);
446
callbackConnector.create();
447
callbackConnector.addInvocationHandler("sample", new FactoryConfigSample.SampleInvocationHandler());
448
callbackConnector.start();
450
// Add callback handler.
451
CallbackHandler callbackHandler = new CallbackHandler();
452
String callbackHandleObject = "myCallbackHandleObject";
453
client.addListener(callbackHandler, callbackLocator, callbackHandleObject);
456
callbackConnector.stop();
468
protected String getHostName()
474
protected String getTransport()
480
protected String getKeystoreFilePath()
482
File file = new File(FactoryConfigSSLSample.class.getResource("keystore").getFile());
483
return file.getPath();
487
protected static String getTruststoreFilePath()
489
File file = new File(FactoryConfigSSLSample.class.getResource("truststore").getFile());
490
return file.getPath();
494
protected ServerSocketFactory getDefaultServerSocketFactory() throws Exception
496
HashMap config = new HashMap();
497
config.put(SSLSocketBuilder.REMOTING_KEY_STORE_TYPE, "JKS");
498
String keyStoreFilePath = getKeystoreFilePath();
499
config.put(SSLSocketBuilder.REMOTING_KEY_STORE_FILE_PATH, keyStoreFilePath);
500
config.put(SSLSocketBuilder.REMOTING_KEY_STORE_PASSWORD, "unit-tests-server");
501
config.put(SSLSocketBuilder.REMOTING_SSL_PROTOCOL, "SSL");
502
SSLSocketBuilder builder = new SSLSocketBuilder(config);
503
builder.setUseSSLServerSocketFactory(false);
504
SSLServerSocketFactoryService service = new SSLServerSocketFactoryService();
505
service.setSSLSocketBuilder(builder);
510
protected SocketFactory getDefaultSocketFactory() throws Exception
512
HashMap config = new HashMap();
513
config.put(SSLSocketBuilder.REMOTING_TRUST_STORE_TYPE, "JKS");
514
String trustStoreFilePath = getTruststoreFilePath();
515
config.put(SSLSocketBuilder.REMOTING_TRUST_STORE_FILE_PATH, trustStoreFilePath);
516
config.put(SSLSocketBuilder.REMOTING_TRUST_STORE_PASSWORD, "unit-tests-client");
517
config.put(SSLSocketBuilder.REMOTING_SSL_PROTOCOL, "SSL");
518
SSLSocketBuilder builder = new SSLSocketBuilder(config);
519
builder.setUseSSLSocketFactory(false);
520
SSLSocketFactoryService service = new SSLSocketFactoryService();
521
service.setSSLSocketBuilder(builder);
526
protected ServerSocketFactory getDefaultCallbackServerSocketFactory() throws Exception
528
HashMap config = new HashMap();
529
config.put(SSLSocketBuilder.REMOTING_SERVER_SOCKET_USE_CLIENT_MODE, "true");
530
config.put(SSLSocketBuilder.REMOTING_TRUST_STORE_TYPE, "JKS");
531
String trustStoreFilePath = getTruststoreFilePath();
532
config.put(SSLSocketBuilder.REMOTING_TRUST_STORE_FILE_PATH, trustStoreFilePath);
533
config.put(SSLSocketBuilder.REMOTING_TRUST_STORE_PASSWORD, "unit-tests-client");
534
config.put(SSLSocketBuilder.REMOTING_SSL_PROTOCOL, "SSL");
535
SSLSocketBuilder builder = new SSLSocketBuilder(config);
536
builder.setUseSSLServerSocketFactory(false);
537
SSLServerSocketFactoryService service = new SSLServerSocketFactoryService();
538
service.setSSLSocketBuilder(builder);
543
protected SocketFactory getDefaultCallbackSocketFactory() throws Exception
545
HashMap config = new HashMap();
546
config.put(SSLSocketBuilder.REMOTING_SOCKET_USE_CLIENT_MODE, "false");
547
config.put(SSLSocketBuilder.REMOTING_KEY_STORE_TYPE, "JKS");
548
String keyStoreFilePath = getKeystoreFilePath();
549
config.put(SSLSocketBuilder.REMOTING_KEY_STORE_FILE_PATH, keyStoreFilePath);
550
config.put(SSLSocketBuilder.REMOTING_KEY_STORE_PASSWORD, "unit-tests-server");
551
config.put(SSLSocketBuilder.REMOTING_SSL_PROTOCOL, "SSL");
552
SSLSocketBuilder builder = new SSLSocketBuilder(config);
553
builder.setUseSSLSocketFactory(false);
554
SSLSocketFactoryService service = new SSLSocketFactoryService();
555
service.setSSLSocketBuilder(builder);
557
return builder.createSSLSocketFactory();