1
package org.jboss.remoting.samples.detection.jndi;
3
import org.jboss.remoting.util.SecurityUtility;
4
import org.jnp.server.Main;
6
import java.lang.reflect.Method;
7
import java.net.InetAddress;
8
import java.security.AccessController;
9
import java.security.PrivilegedActionException;
10
import java.security.PrivilegedExceptionAction;
13
* A JNDI server that should be run before running the simple detector client/server.
14
* Leave running while shutting down different server instances.
16
* @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
18
public class SimpleJNDIServer
20
// Default locator values - command line args can override transport and port
21
private static String transport = "socket";
22
private static String host = "localhost";
23
private static int port = 5400;
24
private int detectorPort = 1099;
28
* Can pass transport and port to be used as parameters. Valid transports are 'rmi' and 'socket'.
30
* @param args transport and port number
32
public static void main(String[] args)
34
// get system property -Dargs that is in format "transport:port"
35
String prop = System.getProperty("args");
40
SimpleJNDIServer.transport = prop.substring(0, prop.indexOf("-"));
41
SimpleJNDIServer.port = Integer.parseInt(prop.substring(prop.indexOf("-") + 1));
43
catch(NumberFormatException nfe)
45
SimpleJNDIServer.println("INVALID ARGUMENTS: Bad port from property args: " + prop);
50
SimpleJNDIServer.println("INVALID ARGUMENTS: -Dargs property must be in the form '{socket|rmi}-{port#}': " + prop);
55
// command line args override defaults and system property
56
if((args != null) && (args.length != 0))
60
SimpleJNDIServer.transport = args[0];
61
SimpleJNDIServer.port = Integer.parseInt(args[1]);
65
SimpleJNDIServer.println("INVALID ARGUMENTS: Usage: " + SimpleJNDIServer.class.getName()
66
+ " [rmi|socket <port>]");
71
SimpleJNDIServer.println("Starting JNDI server... to stop this server, kill it manually via Control-C");
73
SimpleJNDIServer server = new SimpleJNDIServer();
76
server.setupJNDIServer();
78
// wait forever, let the user kill us at any point (at which point, the client will detect we went down)
89
SimpleJNDIServer.println("Stopping JBoss/Remoting server");
92
private void setupJNDIServer() throws Exception
94
Object namingBean = null;
95
Class namingBeanImplClass = null;
98
namingBeanImplClass = Class.forName("org.jnp.server.NamingBeanImpl");
99
namingBean = namingBeanImplClass.newInstance();
100
Method startMethod = namingBeanImplClass.getMethod("start", new Class[] {});
101
setSystemProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
102
startMethod.invoke(namingBean, new Object[] {});
106
SimpleJNDIServer.println("Cannot find NamingBeanImpl: must be running jdk 1.4");
110
String detectorHost = InetAddress.getLocalHost().getHostName();
112
Main JNDIServer = new Main();
113
if (namingBean != null)
115
Class namingBeanClass = Class.forName("org.jnp.server.NamingBean");
116
Method setNamingInfoMethod = JNDIServer.getClass().getMethod("setNamingInfo", new Class[] {namingBeanClass});
117
setNamingInfoMethod.invoke(JNDIServer, new Object[] {namingBean});
119
JNDIServer.setPort(detectorPort);
120
JNDIServer.setBindAddress(detectorHost);
122
System.out.println("Started JNDI server on " + detectorHost + ":" + detectorPort);
127
* Outputs a message to stdout.
129
* @param msg the message to output
131
public static void println(String msg)
133
System.out.println(new java.util.Date() + ": [SERVER]: " + msg);
136
static private void setSystemProperty(final String name, final String value)
138
if (SecurityUtility.skipAccessControl())
140
System.setProperty(name, value);
146
AccessController.doPrivileged( new PrivilegedExceptionAction()
148
public Object run() throws Exception
150
return System.setProperty(name, value);
154
catch (PrivilegedActionException e)
156
throw (RuntimeException) e.getCause();