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.http.connection.socketfactory.by_instance;
24
import org.jboss.jrunit.extensions.ServerTestCase;
25
import org.jboss.remoting.InvocationRequest;
26
import org.jboss.remoting.InvokerLocator;
27
import org.jboss.remoting.ServerInvocationHandler;
28
import org.jboss.remoting.ServerInvoker;
29
import org.jboss.remoting.callback.InvokerCallbackHandler;
30
import org.jboss.remoting.security.ServerSocketFactoryMBean;
31
import org.jboss.remoting.security.SocketFactoryMBean;
32
import org.jboss.remoting.transport.Connector;
34
import javax.management.MBeanServer;
35
import javax.net.ServerSocketFactory;
36
import javax.net.SocketFactory;
37
import java.io.IOException;
38
import java.io.Serializable;
39
import java.net.InetAddress;
40
import java.net.ServerSocket;
41
import java.net.Socket;
42
import java.net.UnknownHostException;
45
* @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
47
public class SocketFactoryTestServer extends ServerTestCase
49
// Default locator values
50
private static String transport = "http";
51
private static String host = "localhost";
52
private static int port = 5400;
54
private static int callCounter = 0;
56
private Connector connector = null;
58
public void tearDown()
67
public void setUp() throws Exception
69
setupServerWithInstance();
72
private void setupServerWithInstance() throws Exception
74
String locatorURI = transport + "://" + host + ":" + port;
76
// create the InvokerLocator based on url string format
77
// to indicate the transport, host, and port to use for the
79
InvokerLocator locator = new InvokerLocator(locatorURI);
80
System.out.println("Starting remoting server with locator uri of: " + locatorURI);
81
connector = new Connector(locator);
83
connector.setServerSocketFactory(ServerSocketFactory.getDefault());
85
// creates all the connector's needed resources, such as the server invoker
88
// create the handler to receive the invocation request from the client for processing
89
SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
90
// first parameter is sub-system name. can be any String value.
91
connector.addInvocationHandler("sample", invocationHandler);
93
// start with a new non daemon thread so
94
// server will wait for request and not exit
100
* Can pass transport and port to be used as parameters.
101
* Valid transports are 'rmi' and 'socket'.
105
public static void main(String[] args)
107
if(args != null && args.length == 3)
111
port = Integer.parseInt(args[2]);
113
SocketFactoryTestServer server = new SocketFactoryTestServer();
118
// wait forever, let the user kill us at any point (at which point, the client will detect we went down)
132
* Simple invocation handler implementation.
133
* This is the code that will be called with the invocation payload from the client.
135
public static class SampleInvocationHandler implements ServerInvocationHandler
138
* called to handle a specific invocation
144
public Object invoke(InvocationRequest invocation) throws Throwable
146
// Print out the invocation request
147
System.out.println("Invocation request is: " + invocation.getParameter());
148
Integer resp = new Integer(++callCounter);
149
System.out.println("Returning response of: " + resp);
150
// Just going to return static string as this is just simple example code.
155
* Adds a callback handler that will listen for callbacks from
156
* the server invoker handler.
158
* @param callbackHandler
160
public void addListener(InvokerCallbackHandler callbackHandler)
162
// NO OP as do not handling callback listeners in this example
166
* Removes the callback handler that was listening for callbacks
167
* from the server invoker handler.
169
* @param callbackHandler
171
public void removeListener(InvokerCallbackHandler callbackHandler)
173
// NO OP as do not handling callback listeners in this example
177
* set the mbean server that the handler can reference
181
public void setMBeanServer(MBeanServer server)
183
// NO OP as do not need reference to MBeanServer for this handler
187
* set the invoker that owns this handler
191
public void setInvoker(ServerInvoker invoker)
193
// NO OP as do not need reference back to the server invoker
198
public static class ServerSocketFactoryMock extends ServerSocketFactory implements ServerSocketFactoryMockMBean
200
private ServerSocketFactory factory = null;
202
public ServerSocketFactoryMock()
204
factory = ServerSocketFactory.getDefault();
207
public ServerSocket createServerSocket() throws IOException
209
return factory.createServerSocket();
212
public ServerSocket createServerSocket(int i) throws IOException
214
return factory.createServerSocket(i);
217
public ServerSocket createServerSocket(int i, int i1) throws IOException
219
return factory.createServerSocket(i, i1);
222
public ServerSocket createServerSocket(int i, int i1, InetAddress inetAddress) throws IOException
224
return factory.createServerSocket(i, i1, inetAddress);
228
public interface ServerSocketFactoryMockMBean extends ServerSocketFactoryMBean
233
public static class SocketFactoryMock extends SocketFactory implements SocketFactoryMockMBean, Serializable
235
private SocketFactory factory = null;
237
public SocketFactoryMock()
243
System.out.println("SocketFactoryMock - init() called");
244
this.factory = SocketFactory.getDefault();
247
public Socket createSocket(String host, int port) throws IOException, UnknownHostException
253
return factory.createSocket(host, port);
256
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException
262
return factory.createSocket(host, port, localHost, localPort);
265
public Socket createSocket(InetAddress host, int port) throws IOException
271
return factory.createSocket(host, port);
274
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException
280
return factory.createSocket(address, port, localAddress, localPort);
284
public interface SocketFactoryMockMBean extends SocketFactoryMBean