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_classname;
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.net.ServerSocketFactory;
36
import javax.net.SocketFactory;
37
import javax.xml.parsers.DocumentBuilderFactory;
38
import java.io.ByteArrayInputStream;
39
import java.io.IOException;
40
import java.io.Serializable;
41
import java.net.InetAddress;
42
import java.net.ServerSocket;
43
import java.net.Socket;
44
import java.net.UnknownHostException;
47
* @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
49
public class SocketFactoryTestServer extends ServerTestCase
51
// Default locator values
52
private static String transport = "rmi";
53
private static String host = "localhost";
54
private static int port = 5400;
56
private static int callCounter = 0;
58
private Connector connector = null;
60
public void setupServerWithClassname() throws Exception
62
String serverSocketFactoryValue = ServerSocketFactoryMock.class.getName();
63
String socketFactoryValue = SocketFactoryMock.class.getName();
65
connector = new Connector();
67
StringBuffer buf = new StringBuffer();
68
buf.append("<?xml version=\"1.0\"?>\n");
69
buf.append("<config>");
70
buf.append("<invoker transport=\"" + transport + "\">");
71
buf.append("<attribute name=\"serverSocketFactory\">" + serverSocketFactoryValue + "</attribute>");
72
buf.append("<attribute name=\"socketFactory\">" + socketFactoryValue + "</attribute>");
73
buf.append("<attribute name=\"serverBindAddress\">" + host + "</attribute>");
74
buf.append("<attribute name=\"serverBindPort\">" + port + "</attribute>");
75
buf.append("</invoker>");
76
buf.append("</config>");
77
Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(buf.toString().getBytes()));
78
connector.setConfiguration(xml.getDocumentElement());
81
// create the handler to receive the invocation request from the client for processing
82
SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
83
// first parameter is sub-system name. can be any String value.
84
connector.addInvocationHandler("sample", invocationHandler);
86
// start with a new non daemon thread so
87
// server will wait for request and not exit
91
public void setUp() throws Exception
93
setupServerWithClassname();
97
* Can pass transport and port to be used as parameters.
98
* Valid transports are 'rmi' and 'socket'.
102
public static void main(String[] args)
104
if(args != null && args.length == 3)
108
port = Integer.parseInt(args[2]);
110
SocketFactoryTestServer server = new SocketFactoryTestServer();
115
// wait forever, let the user kill us at any point (at which point, the client will detect we went down)
129
* Simple invocation handler implementation.
130
* This is the code that will be called with the invocation payload from the client.
132
public static class SampleInvocationHandler implements ServerInvocationHandler
135
* called to handle a specific invocation
141
public Object invoke(InvocationRequest invocation) throws Throwable
143
// Print out the invocation request
144
System.out.println("Invocation request is: " + invocation.getParameter());
145
Integer resp = new Integer(++callCounter);
146
System.out.println("Returning response of: " + resp);
147
// Just going to return static string as this is just simple example code.
152
* Adds a callback handler that will listen for callbacks from
153
* the server invoker handler.
155
* @param callbackHandler
157
public void addListener(InvokerCallbackHandler callbackHandler)
159
// NO OP as do not handling callback listeners in this example
163
* Removes the callback handler that was listening for callbacks
164
* from the server invoker handler.
166
* @param callbackHandler
168
public void removeListener(InvokerCallbackHandler callbackHandler)
170
// NO OP as do not handling callback listeners in this example
174
* set the mbean server that the handler can reference
178
public void setMBeanServer(MBeanServer server)
180
// NO OP as do not need reference to MBeanServer for this handler
184
* set the invoker that owns this handler
188
public void setInvoker(ServerInvoker invoker)
190
// NO OP as do not need reference back to the server invoker
195
public static class ServerSocketFactoryMock extends ServerSocketFactory implements ServerSocketFactoryMockMBean
197
private ServerSocketFactory factory = null;
199
public ServerSocketFactoryMock()
201
factory = ServerSocketFactory.getDefault();
204
public ServerSocket createServerSocket() throws IOException
206
return factory.createServerSocket();
209
public ServerSocket createServerSocket(int i) throws IOException
211
return factory.createServerSocket(i);
214
public ServerSocket createServerSocket(int i, int i1) throws IOException
216
return factory.createServerSocket(i, i1);
219
public ServerSocket createServerSocket(int i, int i1, InetAddress inetAddress) throws IOException
221
return factory.createServerSocket(i, i1, inetAddress);
225
public interface ServerSocketFactoryMockMBean extends ServerSocketFactoryMBean
230
public static class SocketFactoryMock extends SocketFactory implements SocketFactoryMockMBean, Serializable
232
private SocketFactory factory = null;
234
public SocketFactoryMock()
240
System.out.println("SocketFactoryMock - init() called");
241
this.factory = SocketFactory.getDefault();
244
public Socket createSocket(String host, int port) throws IOException, UnknownHostException
250
return factory.createSocket(host, port);
253
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException
259
return factory.createSocket(host, port, localHost, localPort);
262
public Socket createSocket(InetAddress host, int port) throws IOException
268
return factory.createSocket(host, port);
271
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException
277
return factory.createSocket(address, port, localAddress, localPort);
281
public interface SocketFactoryMockMBean extends SocketFactoryMBean