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.remoting.samples.config.factories;
25
import java.io.ByteArrayInputStream;
26
import java.io.IOException;
27
import java.net.InetAddress;
28
import java.net.ServerSocket;
29
import java.net.Socket;
30
import java.net.UnknownHostException;
31
import java.util.HashMap;
33
import javax.management.MBeanServer;
34
import javax.net.ServerSocketFactory;
35
import javax.net.SocketFactory;
36
import javax.xml.parsers.DocumentBuilderFactory;
38
import junit.framework.TestCase;
40
import org.jboss.logging.Logger;
41
import org.jboss.remoting.Client;
42
import org.jboss.remoting.InvocationRequest;
43
import org.jboss.remoting.InvokerLocator;
44
import org.jboss.remoting.Remoting;
45
import org.jboss.remoting.ServerInvocationHandler;
46
import org.jboss.remoting.ServerInvoker;
47
import org.jboss.remoting.callback.Callback;
48
import org.jboss.remoting.callback.HandleCallbackException;
49
import org.jboss.remoting.callback.InvokerCallbackHandler;
50
import org.jboss.remoting.transport.ClientInvoker;
51
import org.jboss.remoting.transport.Connector;
52
import org.jboss.remoting.transport.PortUtil;
53
import org.w3c.dom.Document;
56
* These methods illustrate configuring socket factories and server socket factories
57
* on the server side and on the client side. The numbered
58
* options mentioned refer to the lists of configuration options discussed in the
59
* Remoting documentation in the subsections "Server side configuration" and
60
* "Client side configuration" of the section called "Socket factories and server
63
* The configuration options illustrated in this class are applicable to any kind
64
* of socket and server socket, so the <code>SampleServerSocketFactory</code>
65
* and <code>SampleSocketFactory</code> classes create ordinary sockets and server
69
* @author <a href="mailto:ron.sigal@jboss.com">Ron Sigal</a>
71
* Copyright (c) Jul 20, 2006
74
public class FactoryConfigSample extends TestCase
76
protected static Logger log = Logger.getLogger(FactoryConfigSample.class);
80
* This test illustrates the following set of configuration options:
82
* <table border cellpadding="5">
83
* <tr><td align="center"><b>side<td align="center"><b>factory<td><b>option</tr>
84
* <tr><td>server side<td align="center">server socket<td align="center">1</tr>
85
* <tr><td>server side<td align="center">socket <td align="center">1</tr>
86
* <tr><td>client side<td align="center">server socket<td align="center">1</tr>
87
* <tr><td>client side<td align="center">socket <td align="center">1</tr>
90
public void testFactoriesBySettingInvokers()
94
/////////////////////////////////////
95
///// Set up server side. ////
96
/////////////////////////////////////
97
HashMap sconfig = new HashMap();
99
// Make callback Client use remote invoker.
100
sconfig.put(InvokerLocator.FORCE_REMOTE, "true");
103
int freeport = PortUtil.findFreePort(getHostName());
104
InvokerLocator locator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
105
Connector connector = new Connector(locator, sconfig);
108
// Set ServerSocketFactory and SocketFactory in ServerInvoker.
109
ServerInvoker serverInvoker = connector.getServerInvoker();
110
ServerSocketFactory ssf1 = getDefaultServerSocketFactory();
111
serverInvoker.setServerSocketFactory(ssf1);
112
SocketFactory sf1 = getDefaultCallbackSocketFactory();
113
serverInvoker.setSocketFactory(sf1);
115
connector.addInvocationHandler("sample", new SampleInvocationHandler());
119
/////////////////////////////////////
120
///// Set up client side. ////
121
/////////////////////////////////////
122
HashMap cconfig = new HashMap();
124
// Make Client use remote invoker.
125
cconfig.put(InvokerLocator.FORCE_REMOTE, "true");
128
Client client = new Client(locator, cconfig);
131
// Set SocketFactory in ClientInvoker.
132
SocketFactory sf2 = getDefaultSocketFactory();
133
ClientInvoker clientInvoker = client.getInvoker();
134
clientInvoker.setSocketFactory(sf2);
136
System.out.println(getName() + ": " + client.invoke("test invoke()"));
139
//////////////////////////////////////////////
140
///// Set up callback handling. ////
141
//////////////////////////////////////////////
143
// Start callback Connector.
144
freeport = PortUtil.findFreePort(getHostName());
145
InvokerLocator callbackLocator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
146
Connector callbackConnector = new Connector(callbackLocator.getLocatorURI());
147
callbackConnector.create();
148
ServerInvoker callbackServerInvoker = callbackConnector.getServerInvoker();
149
ServerSocketFactory ssf2 = getDefaultCallbackServerSocketFactory();
150
callbackServerInvoker.setServerSocketFactory(ssf2);
151
callbackConnector.addInvocationHandler("sample", new SampleInvocationHandler());
152
callbackConnector.start();
154
// Add callback handler.
155
CallbackHandler callbackHandler = new CallbackHandler();
156
String callbackHandleObject = "myCallbackHandleObject";
157
client.addListener(callbackHandler, callbackLocator, callbackHandleObject);
160
callbackConnector.stop();
172
/** This test illustrates the following set of configuration options:
174
* <table border cellpadding="5">
175
* <tr><td align="center"><b>side<td align="center"><b>factory<td><b>option</tr>
176
* <tr><td>server side<td align="center">server socket<td align="center">2</tr>
177
* <tr><td>server side<td align="center">socket <td align="center">2</tr>
178
* <tr><td>client side<td align="center">server socket<td align="center">2</tr>
179
* <tr><td>client side<td align="center">socket <td align="center">2</tr>
182
public void testFactoriesBySettingConnectorAndClient()
186
/////////////////////////////////////
187
///// Set up server side. ////
188
/////////////////////////////////////
189
HashMap sconfig = new HashMap();
191
// Make callback Client use remote invoker.
192
sconfig.put(InvokerLocator.FORCE_REMOTE, "true");
195
int freeport = PortUtil.findFreePort(getHostName());
196
InvokerLocator locator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
197
Connector connector = new Connector(locator, sconfig);
199
// Set ServerSocketFactory and SocketFactory in Connector.
200
ServerSocketFactory ssf1 = getDefaultServerSocketFactory();
201
connector.setServerSocketFactory(ssf1);
202
SocketFactory sf1 = getDefaultCallbackSocketFactory();
203
connector.setSocketFactory(sf1);
205
connector.addInvocationHandler("sample", new SampleInvocationHandler());
209
/////////////////////////////////////
210
///// Set up client side. ////
211
/////////////////////////////////////
212
HashMap cconfig = new HashMap();
214
// Make Client use remote invoker.
215
cconfig.put(InvokerLocator.FORCE_REMOTE, "true");
218
Client client = new Client(locator, cconfig);
220
// Set SocketFactory in Client.
221
SocketFactory sf2 = getDefaultSocketFactory();
222
client.setSocketFactory(sf2);
224
System.out.println(getName() + ": " + client.invoke("test invoke()"));
227
//////////////////////////////////////////////
228
///// Set up callback handling. ////
229
//////////////////////////////////////////////
231
// Get callback Connector.
232
freeport = PortUtil.findFreePort(getHostName());
233
InvokerLocator callbackLocator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
234
Connector callbackConnector = new Connector(callbackLocator.getLocatorURI());
236
// Set ServerSocketFactory in callback Connector
237
ServerSocketFactory ssf2 = getDefaultCallbackServerSocketFactory();
238
callbackConnector.setServerSocketFactory(ssf2);
239
callbackConnector.create();
240
callbackConnector.addInvocationHandler("sample", new SampleInvocationHandler());
241
callbackConnector.start();
243
// Add callback handler.
244
CallbackHandler callbackHandler = new CallbackHandler();
245
String callbackHandleObject = "myCallbackHandleObject";
246
client.addListener(callbackHandler, callbackLocator, callbackHandleObject);
249
callbackConnector.stop();
261
/** This test illustrates the following set of configuration options:
263
* <table border cellpadding="5">
264
* <tr><td align="center"><b>side<td align="center"><b>factory<td><b>option</tr>
265
* <tr><td>server side<td align="center">server socket<td align="center">3</tr>
266
* <tr><td>server side<td align="center">socket <td align="center">3</tr>
267
* <tr><td>client side<td align="center">server socket<td align="center">3</tr>
268
* <tr><td>client side<td align="center">socket <td align="center">3</tr>
271
public void testFactoriesByPassingInConfig()
275
/////////////////////////////////////
276
///// Set up server side. ////
277
/////////////////////////////////////
278
HashMap sconfig = new HashMap();
280
// Put ServerSocketFactory and SocketFactory in config map.
281
ServerSocketFactory ssf1 = getDefaultServerSocketFactory();
282
sconfig.put(Remoting.CUSTOM_SERVER_SOCKET_FACTORY, ssf1);
283
SocketFactory sf1 = getDefaultCallbackSocketFactory();
284
sconfig.put(Remoting.CUSTOM_SOCKET_FACTORY, sf1);
286
// Make callback Client use remote invoker.
287
sconfig.put(InvokerLocator.FORCE_REMOTE, "true");
290
int freeport = PortUtil.findFreePort(getHostName());
291
InvokerLocator locator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
292
Connector connector = new Connector(locator, sconfig);
294
connector.addInvocationHandler("sample", new SampleInvocationHandler());
298
/////////////////////////////////////
299
///// Set up client side. ////
300
/////////////////////////////////////
301
HashMap cconfig = new HashMap();
303
// Put SocketFactory in config map.
304
SocketFactory sf2 = getDefaultSocketFactory();
305
cconfig.put(Remoting.CUSTOM_SOCKET_FACTORY, sf2);
307
// Make Client use remote invoker.
308
cconfig.put(InvokerLocator.FORCE_REMOTE, "true");
311
Client client = new Client(locator, cconfig);
314
System.out.println(getName() + ": " + client.invoke("test invoke()"));
317
//////////////////////////////////////////////
318
///// Set up callback handling. ////
319
//////////////////////////////////////////////
321
// Get callback Connector.
322
HashMap cbconfig = new HashMap();
323
ServerSocketFactory ssf2 = getDefaultCallbackServerSocketFactory();
324
cbconfig.put(Remoting.CUSTOM_SERVER_SOCKET_FACTORY, ssf2);
325
freeport = PortUtil.findFreePort(getHostName());
326
InvokerLocator callbackLocator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
327
Connector callbackConnector = new Connector(callbackLocator.getLocatorURI(), cbconfig);
328
callbackConnector.create();
329
callbackConnector.addInvocationHandler("sample", new SampleInvocationHandler());
330
callbackConnector.start();
332
// Add callback handler.
333
CallbackHandler callbackHandler = new CallbackHandler();
334
String callbackHandleObject = "myCallbackHandleObject";
335
client.addListener(callbackHandler, callbackLocator, callbackHandleObject);
338
callbackConnector.stop();
350
/** This test illustrates the following set of configuration options:
352
* <table border cellpadding="5">
353
* <tr><td align="center"><b>side<td align="center"><b>factory<td><b>option</tr>
354
* <tr><td>server side<td align="center">server socket<td align="center">5</tr>
355
* <tr><td>server side<td align="center">socket <td align="center">6</tr>
356
* <tr><td>client side<td align="center">server socket<td align="center">4</tr>
357
* <tr><td>client side<td align="center">socket <td align="center">1</tr>
360
public void testFactoriesByPassingClassnameInXml()
364
/////////////////////////////////////
365
///// Set up server side. ////
366
/////////////////////////////////////
367
HashMap sconfig = new HashMap();
369
// Make callback Client use remote invoker.
370
sconfig.put(InvokerLocator.FORCE_REMOTE, "true");
373
int freeport = PortUtil.findFreePort(getHostName());
374
InvokerLocator locator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
375
Connector connector = new Connector(sconfig);
377
// Set xml configuration element.
378
StringBuffer buf = new StringBuffer();
379
buf.append("<?xml version=\"1.0\"?>\n");
380
buf.append("<config>");
381
buf.append( "<invoker transport=\"" + getTransport() +"\">");
382
buf.append( "<attribute name=\"serverBindAddress\">" + getHostName() + "</attribute>");
383
buf.append( "<attribute name=\"serverBindPort\">" + freeport + "</attribute>");
384
buf.append( "<attribute name=\"serverSocketFactory\">" );
385
buf.append( getDefaultServerSocketFactoryClass().getName());
386
buf.append( "</attribute>");
387
buf.append( "<attribute name=\"socketFactory\">" );
388
buf.append( getDefaultSocketFactoryClass().getName());
389
buf.append( "</attribute>");
390
buf.append( "</invoker>");
391
buf.append("</config>");
393
ByteArrayInputStream bais = new ByteArrayInputStream(buf.toString().getBytes());
394
Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(bais);
395
connector.setConfiguration(xml.getDocumentElement());
398
connector.addInvocationHandler("sample", new SampleInvocationHandler());
402
/////////////////////////////////////
403
///// Set up client side. ////
404
/////////////////////////////////////
405
HashMap cconfig = new HashMap();
407
// Make Client use remote invoker.
408
cconfig.put(InvokerLocator.FORCE_REMOTE, "true");
411
Client client = new Client(locator, cconfig);
413
// Set SocketFactory in Client.
414
// Note. There is no provision for using xml configuration on client side.
415
SocketFactory sf = getDefaultSocketFactory();
416
client.setSocketFactory(sf);
418
System.out.println(getName() + ": " + client.invoke("test invoke()"));
421
//////////////////////////////////////////////
422
///// Set up callback handling. ////
423
//////////////////////////////////////////////
425
// Get callback Connector.
426
freeport = PortUtil.findFreePort(getHostName());
427
InvokerLocator callbackLocator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
428
Connector callbackConnector = new Connector();
430
// Set xml configuration element.
431
buf = new StringBuffer();
432
buf.append("<?xml version=\"1.0\"?>\n");
433
buf.append("<config>");
434
buf.append( "<invoker transport=\"" + getTransport() +"\">");
435
buf.append( "<attribute name=\"serverBindAddress\">" + getHostName() + "</attribute>");
436
buf.append( "<attribute name=\"serverBindPort\">" + freeport + "</attribute>");
437
buf.append( "<attribute name=\"serverSocketFactory\">" );
438
buf.append( getDefaultCallbackServerSocketFactoryClass().getName());
439
buf.append( "</attribute>");
440
buf.append( "</invoker>");
441
buf.append("</config>");
442
bais = new ByteArrayInputStream(buf.toString().getBytes());
443
xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(bais);
444
callbackConnector.setConfiguration(xml.getDocumentElement());
446
callbackConnector.create();
447
callbackConnector.addInvocationHandler("sample", new 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
/** This test illustrates the following set of configuration options:
470
* <table border cellpadding="5">
471
* <tr><td align="center"><b>side<td align="center"><b>factory<td><b>option</tr>
472
* <tr><td>server side<td align="center">server socket<td align="center">7</tr>
473
* <tr><td>server side<td align="center">socket <td align="center">7</tr>
474
* <tr><td>client side<td align="center">server socket<td align="center">5</tr>
475
* <tr><td>client side<td align="center">socket <td align="center">4</tr>
478
public void testFactoriesByClassNameinConfig()
482
/////////////////////////////////////
483
///// Set up server side. ////
484
/////////////////////////////////////
485
HashMap sconfig = new HashMap();
487
// Put class names of ServerSocketFactory and SocketFactory in config map.
488
sconfig.put(ServerInvoker.SERVER_SOCKET_FACTORY, getDefaultServerSocketFactoryClass().getName());
489
sconfig.put(Remoting.SOCKET_FACTORY_NAME, getDefaultCallbackSocketFactoryClass().getName());
491
// Make callback Client use remote invoker.
492
sconfig.put(InvokerLocator.FORCE_REMOTE, "true");
495
int freeport = PortUtil.findFreePort(getHostName());
496
InvokerLocator locator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
497
Connector connector = new Connector(locator, sconfig);
499
connector.addInvocationHandler("sample", new SampleInvocationHandler());
503
/////////////////////////////////////
504
///// Set up client side. ////
505
/////////////////////////////////////
506
HashMap cconfig = new HashMap();
508
// Put SocketFactory class name in config map.
509
cconfig.put(Remoting.SOCKET_FACTORY_NAME, getDefaultSocketFactoryClass().getName());
511
// Make Client use remote invoker.
512
cconfig.put(InvokerLocator.FORCE_REMOTE, "true");
515
Client client = new Client(locator, cconfig);
518
System.out.println(getName() + ": " + client.invoke("test invoke()"));
521
//////////////////////////////////////////////
522
///// Set up callback handling. ////
523
//////////////////////////////////////////////
525
// Get callback Connector.
526
HashMap cbconfig = new HashMap();
527
cbconfig.put(ServerInvoker.SERVER_SOCKET_FACTORY, getDefaultCallbackServerSocketFactoryClass().getName());
528
freeport = PortUtil.findFreePort(getHostName());
529
InvokerLocator callbackLocator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
530
Connector callbackConnector = new Connector(callbackLocator.getLocatorURI(), cbconfig);
531
callbackConnector.create();
532
callbackConnector.addInvocationHandler("sample", new SampleInvocationHandler());
533
callbackConnector.start();
535
// Add callback handler.
536
CallbackHandler callbackHandler = new CallbackHandler();
537
String callbackHandleObject = "myCallbackHandleObject";
538
client.addListener(callbackHandler, callbackLocator, callbackHandleObject);
541
callbackConnector.stop();
553
protected String getTransport()
559
protected String getHostName()
565
protected ServerSocketFactory getDefaultServerSocketFactory() throws Exception
567
return new SampleServerSocketFactory();
571
protected SocketFactory getDefaultSocketFactory() throws Exception
573
return new SampleSocketFactory();
577
protected ServerSocketFactory getDefaultCallbackServerSocketFactory() throws Exception
579
return new SampleServerSocketFactory();
583
protected SocketFactory getDefaultCallbackSocketFactory() throws Exception
585
return new SampleSocketFactory();
589
protected Class getDefaultServerSocketFactoryClass() throws Exception
591
return SampleServerSocketFactory.class;
595
protected Class getDefaultSocketFactoryClass() throws Exception
597
return SampleSocketFactory.class;
601
protected Class getDefaultCallbackServerSocketFactoryClass() throws Exception
603
return SampleServerSocketFactory.class;
607
protected Class getDefaultCallbackSocketFactoryClass() throws Exception
609
return SampleSocketFactory.class;
613
public static class SampleServerSocketFactory
614
extends ServerSocketFactory
616
public ServerSocket createServerSocket(int arg0) throws IOException
618
return new ServerSocket(arg0);
621
public ServerSocket createServerSocket(int arg0, int arg1) throws IOException
623
return new ServerSocket(arg0, arg1);
626
public ServerSocket createServerSocket(int arg0, int arg1, InetAddress arg2) throws IOException
628
return new ServerSocket(arg0, arg1, arg2);
633
public static class SampleSocketFactory
634
extends SocketFactory
636
public Socket createSocket(String arg0, int arg1) throws IOException, UnknownHostException
638
return new Socket(arg0, arg1);
641
public Socket createSocket(String arg0, int arg1, InetAddress arg2, int arg3) throws IOException, UnknownHostException
643
return new Socket(arg0, arg1, arg2, arg3);
646
public Socket createSocket(InetAddress arg0, int arg1) throws IOException
648
return new Socket(arg0, arg1);
651
public Socket createSocket(InetAddress arg0, int arg1, InetAddress arg2, int arg3) throws IOException
653
return new Socket(arg0, arg1, arg2, arg3);
658
public static class SampleInvocationHandler implements ServerInvocationHandler
660
private InvokerCallbackHandler callbackHandler;
662
public SampleInvocationHandler()
666
public Object invoke(InvocationRequest invocation) throws Throwable
668
return invocation.getParameter();
671
public void addListener(InvokerCallbackHandler callbackHandler)
673
log.info("entering addListener()");
674
this.callbackHandler = callbackHandler;
678
Callback callback = new Callback(new Integer(1));
679
callbackHandler.handleCallback(callback);
680
log.info("sent first callback");
688
public void removeListener(InvokerCallbackHandler callbackHandler)
692
public void setMBeanServer(MBeanServer server)
694
// NO OP as do not need reference to MBeanServer for this handler
697
public void setInvoker(ServerInvoker invoker)
699
// NO OP as do not need reference back to the server invoker
702
public InvokerCallbackHandler getCallbackHandler()
704
return callbackHandler;
709
public static class CallbackHandler implements InvokerCallbackHandler
712
* Will take the callback and print out its values.
715
* @throws org.jboss.remoting.callback.HandleCallbackException
718
public void handleCallback(Callback callback) throws HandleCallbackException
720
System.out.println("Received callback value of: " + callback.getCallbackObject());
b'\\ No newline at end of file'