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.detection.jndi;
24
import org.jboss.jrunit.extensions.ServerTestCase;
25
import org.jboss.logging.Logger;
26
import org.jboss.remoting.InvocationRequest;
27
import org.jboss.remoting.InvokerLocator;
28
import org.jboss.remoting.ServerInvocationHandler;
29
import org.jboss.remoting.ServerInvoker;
30
import org.jboss.remoting.callback.InvokerCallbackHandler;
31
import org.jboss.remoting.detection.jndi.JNDIDetector;
32
import org.jboss.remoting.samples.detection.jndi.SimpleJNDIServer;
33
import org.jboss.remoting.transport.Connector;
34
import org.jboss.remoting.util.SecurityUtility;
35
import org.jnp.server.Main;
37
import javax.management.MBeanServer;
38
import javax.management.MBeanServerFactory;
39
import javax.management.ObjectName;
41
import java.io.InputStream;
42
import java.io.OutputStream;
43
import java.lang.reflect.Method;
44
import java.net.InetAddress;
45
import java.net.ServerSocket;
46
import java.net.Socket;
47
import java.security.AccessController;
48
import java.security.PrivilegedActionException;
49
import java.security.PrivilegedExceptionAction;
50
import java.util.HashMap;
54
* @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
55
* @author <a href="mailto:mazz@jboss.com">John Mazzitelli</a>
56
* @author <a href="mailto:ron.sigal@jboss.com">Ron Sigal</a>
58
public class RestartTestServer extends ServerTestCase
60
public static int syncPort = 6402;
62
protected static Logger log = Logger.getLogger(RestartTestServer.class);
63
protected static String transport;
64
protected static String host;
65
protected static int port = 5402;
66
protected static Thread serverThread;
68
protected int detectorPort = 1099;
69
protected String contextFactory = "org.jnp.interfaces.NamingContextFactory";
70
protected String urlPackage = "org.jboss.naming:org.jnp.interfaces";
71
protected Main jserver;
72
protected JNDIDetector detector;
73
protected Connector connector;
75
public void setUp() throws Exception
77
host = InetAddress.getLocalHost().getHostName();
78
final String locatorURI = getTransport() + "://" + host + ":" + port;
79
log.info("This server's endpoint will be: " + locatorURI);
81
serverThread = new Thread()
89
setupServer(locatorURI);
90
ServerSocket ss = new ServerSocket(syncPort, 0, InetAddress.getLocalHost());
91
log.info("bound to: " + InetAddress.getLocalHost());
92
Socket s = ss.accept();
93
InputStream is = s.getInputStream();
94
OutputStream os = s.getOutputStream();
96
// Indicate server is started.
98
log.info("indicated server started");
101
log.info("got request to shut down server");
102
shutdownServer(locatorURI);
103
log.info("shut down server");
106
log.info("got request to restart server");
107
setupServer(locatorURI);
109
log.info("restarted server");
112
log.info("got request to shut down server");
113
shutdownServer(locatorURI);
116
log.info("shut down server");
126
serverThread.start();
129
protected void setupJNDI() throws Exception
131
Object namingBean = null;
132
Class namingBeanImplClass = null;
135
namingBeanImplClass = Class.forName("org.jnp.server.NamingBeanImpl");
136
namingBean = namingBeanImplClass.newInstance();
137
Method startMethod = namingBeanImplClass.getMethod("start", new Class[] {});
138
setSystemProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
139
startMethod.invoke(namingBean, new Object[] {});
143
SimpleJNDIServer.println("Cannot find NamingBeanImpl: must be running jdk 1.4");
146
String host = InetAddress.getLocalHost().getHostAddress();
148
jserver = new Main();
149
if (namingBean != null)
151
Class namingBeanClass = Class.forName("org.jnp.server.NamingBean");
152
Method setNamingInfoMethod = jserver.getClass().getMethod("setNamingInfo", new Class[] {namingBeanClass});
153
setNamingInfoMethod.invoke(jserver, new Object[] {namingBean});
155
jserver.setPort(detectorPort);
156
jserver.setBindAddress(host);
157
jserver.setRmiPort(31000);
159
System.out.println("Started JNDI server on " + host + ":" + port);
162
protected void shutdownJNDI()
167
protected void setupDetector() throws Exception
169
// we need an MBeanServer to store our network registry and multicast detector services
170
MBeanServer server = MBeanServerFactory.createMBeanServer();
172
String detectorHost = InetAddress.getLocalHost().getHostName();
174
detector = new JNDIDetector(getConfiguration());
175
// set config info for detector and start it.
176
detector.setPort(detectorPort);
177
detector.setHost(detectorHost);
178
detector.setContextFactory(contextFactory);
179
detector.setURLPackage(urlPackage);
181
server.registerMBean(detector, new ObjectName("remoting:type=JNDIDetector"));
183
log.info("JNDIDetector has been created and is listening for new NetworkRegistries to come online");
186
protected void shutdownDetector() throws Exception
192
* Sets up our JBoss/Remoting server by creating our Connector on the given locator URI
193
* and installing our invocation handler that will handle incoming messages.
195
* @param locatorURI defines our server endpoing
198
protected void setupServer(String locatorURI) throws Exception
200
InvokerLocator locator = new InvokerLocator(locatorURI);
201
log.info("Starting remoting server with locator uri of: " + locatorURI);
202
connector = new Connector(locator, getConfiguration());
204
SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
205
connector.addInvocationHandler("sample", invocationHandler);
210
protected void shutdownServer(String locatorURI)
213
log.info("Stopped remoting server with locator uri of: " + locatorURI);
217
protected String getTransport()
224
* @return configuration map for Connector and JNDIDetector
226
protected Map getConfiguration()
228
return new HashMap();
232
public static void main(String[] args)
236
RestartTestServer server = new RestartTestServer();
249
* Simple invocation handler implementation. This is the handler that processes incoming messages from clients.
251
public static class SampleInvocationHandler implements ServerInvocationHandler
254
* This is the method that is called when a new message comes in from a client.
256
* @param invocation the incoming client invocation, encapsulates the message object
257
* @return the response object we send back to the client.
260
public Object invoke(InvocationRequest invocation) throws Throwable
262
// Print out the invocation request
263
String msg = invocation.getParameter().toString();
265
log.info("RECEIVED A CLIENT MESSAGE: " + msg);
267
String response = "Server received your message that said [" + msg + "]";
269
if(msg.indexOf("Welcome") > -1)
271
response = "Received your welcome message. Thank you!";
274
log.info("Returning the following message back to the client: " + response);
280
* Adds a callback handler that will listen for callbacks from the server invoker handler.
282
* @param callbackHandler
284
public void addListener(InvokerCallbackHandler callbackHandler)
286
// NO OP as we do not handle callback listeners in this example
290
* Removes the callback handler that was listening for callbacks from the server invoker handler.
292
* @param callbackHandler
294
public void removeListener(InvokerCallbackHandler callbackHandler)
296
// NO OP as we do not handle callback listeners in this example
300
* set the mbean server that the handler can reference
304
public void setMBeanServer(MBeanServer server)
306
// NO OP as we do not need a reference to the MBeanServer for this handler
310
* set the invoker that owns this handler
314
public void setInvoker(ServerInvoker invoker)
316
// NO OP as we do not need a reference back to the server invoker
320
static private void setSystemProperty(final String name, final String value)
322
if (SecurityUtility.skipAccessControl())
324
System.setProperty(name, value);
330
AccessController.doPrivileged( new PrivilegedExceptionAction()
332
public Object run() throws Exception
334
return System.setProperty(name, value);
338
catch (PrivilegedActionException e)
340
throw (RuntimeException) e.getCause();