1
package org.jboss.test.remoting.transport.http.connection.socketfactory.by_mbean;
3
import org.jboss.jrunit.extensions.ServerTestCase;
4
import org.jboss.remoting.InvocationRequest;
5
import org.jboss.remoting.ServerInvocationHandler;
6
import org.jboss.remoting.ServerInvoker;
7
import org.jboss.remoting.callback.InvokerCallbackHandler;
8
import org.jboss.remoting.security.ServerSocketFactoryMBean;
9
import org.jboss.remoting.security.SocketFactoryMBean;
10
import org.jboss.remoting.transport.Connector;
11
import org.w3c.dom.Document;
13
import javax.management.MBeanServer;
14
import javax.management.MBeanServerFactory;
15
import javax.management.ObjectName;
16
import javax.net.ServerSocketFactory;
17
import javax.net.SocketFactory;
18
import javax.xml.parsers.DocumentBuilderFactory;
19
import java.io.ByteArrayInputStream;
20
import java.io.IOException;
21
import java.io.Serializable;
22
import java.net.InetAddress;
23
import java.net.ServerSocket;
24
import java.net.Socket;
25
import java.net.UnknownHostException;
28
* @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
30
public class SocketFactoryTestServer extends ServerTestCase
32
// Default locator values
33
private static String transport = "http";
34
private static String host = "localhost";
35
private static int port = 5400;
37
private static int callCounter = 0;
39
private Connector connector = null;
41
public void setupServerWithMBeanServer() throws Exception
43
String serverSocketFactoryValue = "jboss:type=serversocketfactory";
45
//setup mbean server socket factory
46
MBeanServer mbeanServer = MBeanServerFactory.createMBeanServer();
48
ServerSocketFactoryMock service = new SocketFactoryTestServer.ServerSocketFactoryMock();
49
ObjectName objName = new ObjectName(serverSocketFactoryValue);
50
mbeanServer.registerMBean(service, objName);
52
connector = new Connector();
54
mbeanServer.registerMBean(connector, new ObjectName("jboss:type=connector"));
56
StringBuffer buf = new StringBuffer();
57
buf.append("<?xml version=\"1.0\"?>\n");
58
buf.append("<config>");
59
buf.append("<invoker transport=\"" + transport + "\">");
60
buf.append("<attribute name=\"serverSocketFactory\">" + serverSocketFactoryValue + "</attribute>");
61
buf.append("<attribute name=\"serverBindAddress\">" + host + "</attribute>");
62
buf.append("<attribute name=\"serverBindPort\">" + port + "</attribute>");
63
buf.append("</invoker>");
64
buf.append("</config>");
65
Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(buf.toString().getBytes()));
66
//connector.setInvokerLocator(locator.getLocatorURI());
67
connector.setConfiguration(xml.getDocumentElement());
70
// create the handler to receive the invocation request from the client for processing
71
SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
72
// first parameter is sub-system name. can be any String value.
73
connector.addInvocationHandler("sample", invocationHandler);
75
// start with a new non daemon thread so
76
// server will wait for request and not exit
81
public void tearDown()
90
public void setUp() throws Exception
92
setupServerWithMBeanServer();
96
* Can pass transport and port to be used as parameters.
97
* Valid transports are 'rmi' and 'socket'.
101
public static void main(String[] args)
103
if(args != null && args.length == 3)
107
port = Integer.parseInt(args[2]);
109
SocketFactoryTestServer server = new SocketFactoryTestServer();
114
// wait forever, let the user kill us at any point (at which point, the client will detect we went down)
128
* Simple invocation handler implementation.
129
* This is the code that will be called with the invocation payload from the client.
131
public static class SampleInvocationHandler implements ServerInvocationHandler
134
* called to handle a specific invocation
140
public Object invoke(InvocationRequest invocation) throws Throwable
142
// Print out the invocation request
143
System.out.println("Invocation request is: " + invocation.getParameter());
144
Integer resp = new Integer(++callCounter);
145
System.out.println("Returning response of: " + resp);
146
// Just going to return static string as this is just simple example code.
151
* Adds a callback handler that will listen for callbacks from
152
* the server invoker handler.
154
* @param callbackHandler
156
public void addListener(InvokerCallbackHandler callbackHandler)
158
// NO OP as do not handling callback listeners in this example
162
* Removes the callback handler that was listening for callbacks
163
* from the server invoker handler.
165
* @param callbackHandler
167
public void removeListener(InvokerCallbackHandler callbackHandler)
169
// NO OP as do not handling callback listeners in this example
173
* set the mbean server that the handler can reference
177
public void setMBeanServer(MBeanServer server)
179
// NO OP as do not need reference to MBeanServer for this handler
183
* set the invoker that owns this handler
187
public void setInvoker(ServerInvoker invoker)
189
// NO OP as do not need reference back to the server invoker
194
public static class ServerSocketFactoryMock extends ServerSocketFactory implements ServerSocketFactoryMockMBean
196
private ServerSocketFactory factory = null;
198
public ServerSocketFactoryMock()
200
factory = ServerSocketFactory.getDefault();
203
public ServerSocket createServerSocket() throws IOException
205
return factory.createServerSocket();
208
public ServerSocket createServerSocket(int i) throws IOException
210
return factory.createServerSocket(i);
213
public ServerSocket createServerSocket(int i, int i1) throws IOException
215
return factory.createServerSocket(i, i1);
218
public ServerSocket createServerSocket(int i, int i1, InetAddress inetAddress) throws IOException
220
return factory.createServerSocket(i, i1, inetAddress);
224
public interface ServerSocketFactoryMockMBean extends ServerSocketFactoryMBean
229
public static class SocketFactoryMock extends SocketFactory implements SocketFactoryMockMBean, Serializable
231
private SocketFactory factory = null;
233
public SocketFactoryMock()
239
System.out.println("SocketFactoryMock - init() called");
240
this.factory = SocketFactory.getDefault();
243
public Socket createSocket(String host, int port) throws IOException, UnknownHostException
249
return factory.createSocket(host, port);
252
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException
258
return factory.createSocket(host, port, localHost, localPort);
261
public Socket createSocket(InetAddress host, int port) throws IOException
267
return factory.createSocket(host, port);
270
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException
276
return factory.createSocket(address, port, localAddress, localPort);
280
public interface SocketFactoryMockMBean extends SocketFactoryMBean