2
* JBoss, Home of Professional Open Source
3
* Copyright 2005, 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.test.remoting.transport.rmi.connection.socketfactory.by_mbean;
24
import org.jboss.jrunit.extensions.ServerTestCase;
25
import org.jboss.remoting.InvocationRequest;
26
import org.jboss.remoting.ServerInvocationHandler;
27
import org.jboss.remoting.ServerInvoker;
28
import org.jboss.remoting.callback.InvokerCallbackHandler;
29
import org.jboss.remoting.security.ServerSocketFactoryMBean;
30
import org.jboss.remoting.security.SocketFactoryMBean;
31
import org.jboss.remoting.transport.Connector;
32
import org.w3c.dom.Document;
34
import javax.management.MBeanServer;
35
import javax.management.MBeanServerFactory;
36
import javax.management.ObjectName;
37
import javax.net.ServerSocketFactory;
38
import javax.net.SocketFactory;
39
import javax.xml.parsers.DocumentBuilderFactory;
40
import java.io.ByteArrayInputStream;
41
import java.io.IOException;
42
import java.io.Serializable;
43
import java.net.InetAddress;
44
import java.net.ServerSocket;
45
import java.net.Socket;
46
import java.net.UnknownHostException;
49
* @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
51
public class SocketFactoryTestServer extends ServerTestCase
53
// Default locator values
54
private static String transport = "rmi";
55
private static String host = "localhost";
56
private static int port = 5400;
58
private static int callCounter = 0;
60
private Connector connector = null;
62
public void setupServerWithMBeanServer() throws Exception
64
String serverSocketFactoryValue = "jboss:type=serversocketfactory";
66
//setup mbean server socket factory
67
MBeanServer mbeanServer = MBeanServerFactory.createMBeanServer();
69
ServerSocketFactoryMock service = new ServerSocketFactoryMock();
70
ObjectName objName = new ObjectName(serverSocketFactoryValue);
71
mbeanServer.registerMBean(service, objName);
74
* Can not have the socket factory be an mbean as will later
75
* try to export the socket factory to the client and requires
76
* it to be serializable, which is not possible with an mbean proxy
77
* since no way to call back to the implementation on the server side.
79
// String socketFactoryValue = "jboss:type=socketfactory";
80
// SocketFactoryMock service2= new SocketFactoryMock();
81
// ObjectName objName2 = new ObjectName(socketFactoryValue);
82
// mbeanServer.registerMBean(service2, objName2);
85
connector = new Connector();
87
mbeanServer.registerMBean(connector, new ObjectName("jboss:type=connector"));
89
StringBuffer buf = new StringBuffer();
90
buf.append("<?xml version=\"1.0\"?>\n");
91
buf.append("<config>");
92
buf.append("<invoker transport=\"" + transport + "\">");
93
buf.append("<attribute name=\"serverSocketFactory\">" + serverSocketFactoryValue + "</attribute>");
94
// buf.append("<attribute name=\"socketFactory\">" + socketFactoryValue + "</attribute>");
95
buf.append("<attribute name=\"serverBindAddress\">" + host + "</attribute>");
96
buf.append("<attribute name=\"serverBindPort\">" + port + "</attribute>");
97
buf.append("</invoker>");
98
buf.append("</config>");
99
Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(buf.toString().getBytes()));
100
connector.setConfiguration(xml.getDocumentElement());
103
// create the handler to receive the invocation request from the client for processing
104
SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
105
// first parameter is sub-system name. can be any String value.
106
connector.addInvocationHandler("sample", invocationHandler);
108
// start with a new non daemon thread so
109
// server will wait for request and not exit
114
public void tearDown()
116
if(connector != null)
123
public void setUp() throws Exception
125
setupServerWithMBeanServer();
129
* Can pass transport and port to be used as parameters.
130
* Valid transports are 'rmi' and 'socket'.
134
public static void main(String[] args)
136
if(args != null && args.length == 3)
140
port = Integer.parseInt(args[2]);
142
SocketFactoryTestServer server = new SocketFactoryTestServer();
147
// wait forever, let the user kill us at any point (at which point, the client will detect we went down)
161
* Simple invocation handler implementation.
162
* This is the code that will be called with the invocation payload from the client.
164
public static class SampleInvocationHandler implements ServerInvocationHandler
167
* called to handle a specific invocation
173
public Object invoke(InvocationRequest invocation) throws Throwable
175
// Print out the invocation request
176
System.out.println("Invocation request is: " + invocation.getParameter());
177
Integer resp = new Integer(++callCounter);
178
System.out.println("Returning response of: " + resp);
179
// Just going to return static string as this is just simple example code.
184
* Adds a callback handler that will listen for callbacks from
185
* the server invoker handler.
187
* @param callbackHandler
189
public void addListener(InvokerCallbackHandler callbackHandler)
191
// NO OP as do not handling callback listeners in this example
195
* Removes the callback handler that was listening for callbacks
196
* from the server invoker handler.
198
* @param callbackHandler
200
public void removeListener(InvokerCallbackHandler callbackHandler)
202
// NO OP as do not handling callback listeners in this example
206
* set the mbean server that the handler can reference
210
public void setMBeanServer(MBeanServer server)
212
// NO OP as do not need reference to MBeanServer for this handler
216
* set the invoker that owns this handler
220
public void setInvoker(ServerInvoker invoker)
222
// NO OP as do not need reference back to the server invoker
227
public static class ServerSocketFactoryMock extends ServerSocketFactory implements ServerSocketFactoryMockMBean
229
private ServerSocketFactory factory = null;
231
public ServerSocketFactoryMock()
233
factory = ServerSocketFactory.getDefault();
236
public ServerSocket createServerSocket() throws IOException
238
return factory.createServerSocket();
241
public ServerSocket createServerSocket(int i) throws IOException
243
return factory.createServerSocket(i);
246
public ServerSocket createServerSocket(int i, int i1) throws IOException
248
return factory.createServerSocket(i, i1);
251
public ServerSocket createServerSocket(int i, int i1, InetAddress inetAddress) throws IOException
253
return factory.createServerSocket(i, i1, inetAddress);
257
public interface ServerSocketFactoryMockMBean extends ServerSocketFactoryMBean
262
public static class SocketFactoryMock extends SocketFactory implements SocketFactoryMockMBean, Serializable
264
private SocketFactory factory = null;
266
public SocketFactoryMock()
272
System.out.println("SocketFactoryMock - init() called");
273
this.factory = SocketFactory.getDefault();
276
public Socket createSocket(String host, int port) throws IOException, UnknownHostException
282
return factory.createSocket(host, port);
285
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException
291
return factory.createSocket(host, port, localHost, localPort);
294
public Socket createSocket(InetAddress host, int port) throws IOException
300
return factory.createSocket(host, port);
303
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException
309
return factory.createSocket(address, port, localAddress, localPort);
313
public interface SocketFactoryMockMBean extends SocketFactoryMBean