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.socket.connection.socketfactory.by_classname;
24
import java.io.ByteArrayInputStream;
25
import java.io.IOException;
26
import java.net.InetAddress;
27
import java.net.ServerSocket;
28
import javax.management.MBeanServer;
29
import javax.net.ServerSocketFactory;
30
import javax.xml.parsers.DocumentBuilderFactory;
31
import org.jboss.jrunit.extensions.ServerTestCase;
32
import org.jboss.remoting.InvocationRequest;
33
import org.jboss.remoting.ServerInvocationHandler;
34
import org.jboss.remoting.ServerInvoker;
35
import org.jboss.remoting.callback.InvokerCallbackHandler;
36
import org.jboss.remoting.security.ServerSocketFactoryMBean;
37
import org.jboss.remoting.transport.Connector;
38
import org.w3c.dom.Document;
41
* @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
43
public class SocketFactoryTestServer extends ServerTestCase
45
// Default locator values
46
private static String transport = "socket";
47
private static String host = "localhost";
48
private static int port = 5400;
50
private static int callCounter = 0;
52
private Connector connector = null;
54
public void tearDown()
63
public void setupServerWithClassname() throws Exception
65
String serverSocketFactoryValue = ServerSocketFactoryMock.class.getName();
67
connector = new Connector();
69
StringBuffer buf = new StringBuffer();
70
buf.append("<?xml version=\"1.0\"?>\n");
71
buf.append("<config>");
72
buf.append("<invoker transport=\"" + transport + "\">");
73
buf.append("<attribute name=\"serverSocketFactory\">" + serverSocketFactoryValue + "</attribute>");
74
buf.append("<attribute name=\"serverBindAddress\">" + host + "</attribute>");
75
buf.append("<attribute name=\"serverBindPort\">" + port + "</attribute>");
76
buf.append("</invoker>");
77
buf.append("</config>");
78
Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(buf.toString().getBytes()));
79
connector.setConfiguration(xml.getDocumentElement());
82
// create the handler to receive the invocation request from the client for processing
83
SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
84
// first parameter is sub-system name. can be any String value.
85
connector.addInvocationHandler("sample", invocationHandler);
87
// start with a new non daemon thread so
88
// server will wait for request and not exit
92
public void setUp() throws Exception
94
setupServerWithClassname();
98
* Can pass transport and port to be used as parameters.
99
* Valid transports are 'rmi' and 'socket'.
103
public static void main(String[] args)
105
if(args != null && args.length == 3)
109
port = Integer.parseInt(args[2]);
111
SocketFactoryTestServer server = new SocketFactoryTestServer();
116
// wait forever, let the user kill us at any point (at which point, the client will detect we went down)
130
* Simple invocation handler implementation.
131
* This is the code that will be called with the invocation payload from the client.
133
public static class SampleInvocationHandler implements ServerInvocationHandler
136
* called to handle a specific invocation
142
public Object invoke(InvocationRequest invocation) throws Throwable
144
// Print out the invocation request
145
System.out.println("Invocation request is: " + invocation.getParameter());
146
Integer resp = new Integer(++callCounter);
147
System.out.println("Returning response of: " + resp);
148
// Just going to return static string as this is just simple example code.
153
* Adds a callback handler that will listen for callbacks from
154
* the server invoker handler.
156
* @param callbackHandler
158
public void addListener(InvokerCallbackHandler callbackHandler)
160
// NO OP as do not handling callback listeners in this example
164
* Removes the callback handler that was listening for callbacks
165
* from the server invoker handler.
167
* @param callbackHandler
169
public void removeListener(InvokerCallbackHandler callbackHandler)
171
// NO OP as do not handling callback listeners in this example
175
* set the mbean server that the handler can reference
179
public void setMBeanServer(MBeanServer server)
181
// NO OP as do not need reference to MBeanServer for this handler
185
* set the invoker that owns this handler
189
public void setInvoker(ServerInvoker invoker)
191
// NO OP as do not need reference back to the server invoker
196
public static class ServerSocketFactoryMock extends ServerSocketFactory implements ServerSocketFactoryMockMBean
198
private javax.net.ServerSocketFactory factory = null;
200
public ServerSocketFactoryMock()
202
factory = javax.net.ServerSocketFactory.getDefault();
205
public ServerSocket createServerSocket() throws IOException
207
return factory.createServerSocket();
210
public ServerSocket createServerSocket(int i) throws IOException
212
return factory.createServerSocket(i);
215
public ServerSocket createServerSocket(int i, int i1) throws IOException
217
return factory.createServerSocket(i, i1);
220
public ServerSocket createServerSocket(int i, int i1, InetAddress inetAddress) throws IOException
222
return factory.createServerSocket(i, i1, inetAddress);
226
public interface ServerSocketFactoryMockMBean extends ServerSocketFactoryMBean
b'\\ No newline at end of file'