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.deadlock;
25
import org.apache.log4j.Level;
26
import org.apache.log4j.Logger;
27
import org.jboss.remoting.InvocationRequest;
28
import org.jboss.remoting.InvokerLocator;
29
import org.jboss.remoting.ServerInvocationHandler;
30
import org.jboss.remoting.ServerInvoker;
31
import org.jboss.remoting.callback.InvokerCallbackHandler;
32
import org.jboss.remoting.detection.jndi.JNDIDetector;
33
import org.jboss.remoting.samples.detection.jndi.SimpleJNDIServer;
34
import org.jboss.remoting.security.SSLSocketBuilder;
35
import org.jboss.remoting.transport.Connector;
36
import org.jboss.remoting.transport.sslmultiplex.SSLMultiplexServerInvoker;
37
import org.jboss.remoting.util.SecurityUtility;
38
import org.jnp.server.Main;
40
import javax.management.MBeanServer;
41
import javax.management.MBeanServerFactory;
42
import javax.management.ObjectName;
43
import javax.net.ServerSocketFactory;
45
import java.lang.reflect.Method;
46
import java.net.InetAddress;
47
import java.net.UnknownHostException;
48
import java.security.AccessController;
49
import java.security.PrivilegedActionException;
50
import java.security.PrivilegedExceptionAction;
51
import java.util.HashMap;
55
* @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
56
* @author Michael Voss
58
public class Server implements Runnable
60
private static Connector connector = null;
61
private static Logger logger;
62
private MBeanServer server;
63
private JNDIDetector detector;
65
private String jndiAddress = null;
66
private int jndiPort = 2410;
67
private String port = "1001";
72
String localHost = "";
75
jndiAddress = localHost = InetAddress.getLocalHost().getHostAddress();
77
catch (UnknownHostException uhe)
79
uhe.printStackTrace();
85
// String serverKeyStorePath = this.getClass().getResource("certificate/serverKeyStore").getFile();
86
// String serverTrustStorePath = this.getClass().getResource("certificate/serverTrustStore").getFile();
87
String serverKeyStorePath = this.getClass().getResource("../../../transport/socket/ssl/.keystore").getFile();
88
String serverTrustStorePath = this.getClass().getResource("../../../transport/socket/ssl/.trustStore").getFile();
90
Map configuration = new HashMap();
91
configuration.put(SSLSocketBuilder.REMOTING_KEY_STORE_TYPE, "JKS");
92
configuration.put(SSLSocketBuilder.REMOTING_KEY_STORE_FILE_PATH, serverKeyStorePath);
93
// configuration.put(SSLSocketBuilder.REMOTING_KEY_STORE_FILE_PATH, "certificate/serverKeyStore");
94
configuration.put(SSLSocketBuilder.REMOTING_KEY_STORE_PASSWORD, "testpw");
95
configuration.put(SSLSocketBuilder.REMOTING_KEY_STORE_PASSWORD, "unit-tests-server");
96
configuration.put(SSLSocketBuilder.REMOTING_KEY_STORE_ALGORITHM, "SunX509");
98
configuration.put(SSLSocketBuilder.REMOTING_TRUST_STORE_TYPE, "JKS");
99
configuration.put(SSLSocketBuilder.REMOTING_TRUST_STORE_FILE_PATH, serverTrustStorePath);
100
// configuration.put(SSLSocketBuilder.REMOTING_TRUST_STORE_FILE_PATH, "certificate/serverTrustStore");
101
configuration.put(SSLSocketBuilder.REMOTING_TRUST_STORE_PASSWORD, "testpw");
102
configuration.put(SSLSocketBuilder.REMOTING_TRUST_STORE_PASSWORD, "unit-tests-client");
103
configuration.put(SSLSocketBuilder.REMOTING_TRUST_STORE_ALGORITHM, "SunX509");
105
configuration.put(SSLSocketBuilder.REMOTING_SOCKET_USE_CLIENT_MODE, "false");
108
configuration.put(RemotingSSLSocketFactory.REMOTING_KEY_STORE_TYPE, "JKS");
109
configuration.put(RemotingSSLSocketFactory.REMOTING_KEY_STORE_FILE_PATH, serverKeyStorePath);
110
// configuration.put(RemotingSSLSocketFactory.REMOTING_KEY_STORE_FILE_PATH, "certificate/serverKeyStore");
111
configuration.put(RemotingSSLSocketFactory.REMOTING_KEY_STORE_PASSWORD, "testpw");
112
configuration.put(RemotingSSLSocketFactory.REMOTING_ALGORITHM, "SunX509");
114
configuration.put(RemotingSSLSocketFactory.REMOTING_TRUST_STORE_TYPE, "JKS");
115
configuration.put(RemotingSSLSocketFactory.REMOTING_TRUST_STORE_FILE_PATH, serverTrustStorePath);
116
// configuration.put(RemotingSSLSocketFactory.REMOTING_TRUST_STORE_FILE_PATH, "certificate/serverTrustStore");
117
configuration.put(RemotingSSLSocketFactory.REMOTING_TRUST_STORE_PASSWORD, "testpw");
118
configuration.put(RemotingSSLSocketFactory.REMOTING_TRUST_ALGORITHM, "SunX509");
119
configuration.put(RemotingSSLSocketFactory.REMOTING_USE_CLIENT_MODE, "false");
121
connector = new Connector(configuration);
123
InvokerLocator locator = new InvokerLocator("sslmultiplex://" + localHost + ":" + port);
124
connector.setInvokerLocator(locator.getLocatorURI());
130
// ServerSocketFactory svrSocketFactory = SSL.createServerSocketFactory("testpw", "testpw", "certificate/serverKeyStore", "certificate/serverTrustStore");
131
ServerSocketFactory svrSocketFactory = SSL.createServerSocketFactory("unit-tests-server", "unit-tests-client", serverKeyStorePath, serverTrustStorePath);
133
SSLMultiplexServerInvoker socketSvrInvoker = (SSLMultiplexServerInvoker) connector.getServerInvoker();
134
socketSvrInvoker.setServerSocketFactory(svrSocketFactory);
141
/*connector = new Connector();
142
InvokerLocator locator = new InvokerLocator("multiplex://"+localHost+":"+port);
143
connector.setInvokerLocator(locator.getLocatorURI());
144
connector.create(); */
148
connector.addInvocationHandler("sample", new SampleInvocationHandler());
157
server.registerMBean(connector, new ObjectName("jboss.remoting:type=Connector"));
170
private void registerJNDI()
173
detector = new JNDIDetector();
177
server.registerMBean(detector, new ObjectName("remoting:type=JNDIDetector"));
179
catch (Exception ignored)
183
detector.setPort(jndiPort);
184
detector.setHost(jndiAddress);
197
private void setUp() throws Exception
200
org.apache.log4j.BasicConfigurator.configure();
201
org.apache.log4j.Category.getRoot().setLevel(Level.DEBUG);
202
org.apache.log4j.Category.getInstance("org.jboss.remoting").setLevel(Level.DEBUG);
203
org.apache.log4j.Category.getInstance("org.jgroups").setLevel(Level.FATAL);
207
Thread t = new Thread(this);
208
Runtime.getRuntime().addShutdownHook(t);
210
catch (Exception ignored)
214
logger = Logger.getLogger(getClass());
215
logger.setLevel((Level) Level.DEBUG);
217
server = MBeanServerFactory.createMBeanServer();
225
private void startJNDIServer() throws Exception
227
Object namingBean = null;
228
Class namingBeanImplClass = null;
231
namingBeanImplClass = Class.forName("org.jnp.server.NamingBeanImpl");
232
namingBean = namingBeanImplClass.newInstance();
233
Method startMethod = namingBeanImplClass.getMethod("start", new Class[] {});
234
setSystemProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
235
startMethod.invoke(namingBean, new Object[] {});
239
SimpleJNDIServer.println("Cannot find NamingBeanImpl: must be running jdk 1.4");
242
String host = InetAddress.getLocalHost().getHostAddress();
244
Main jserver = new Main();
245
if (namingBean != null)
247
Class namingBeanClass = Class.forName("org.jnp.server.NamingBean");
248
Method setNamingInfoMethod = jserver.getClass().getMethod("setNamingInfo", new Class[] {namingBeanClass});
249
setNamingInfoMethod.invoke(jserver, new Object[] {namingBean});
251
jserver.setPort(2410);
252
jserver.setBindAddress(host);
253
jserver.setRmiPort(31000);
264
server.unregisterMBean(new ObjectName("remoting:type=JNDIDetector"));
265
if (detector != null)
271
catch (Exception ignored)
275
if (connector != null)
281
catch (Exception ignored)
286
server.unregisterMBean(new ObjectName("jboss.remoting:type=Connector"));
298
public static void main(String[] args)
301
Server server = new Server();
318
public static class SampleInvocationHandler implements ServerInvocationHandler
321
public Object invoke(InvocationRequest invocation) throws Throwable
323
System.out.println("invoke(): " + invocation.getParameter());
324
logger.info("invoke(): " + invocation.getParameter());
325
return invocation.getParameter();
330
public void addListener(InvokerCallbackHandler callbackHandler)
336
public void removeListener(InvokerCallbackHandler callbackHandler)
342
public void setMBeanServer(MBeanServer server)
347
public void setInvoker(ServerInvoker invoker)
354
static private void setSystemProperty(final String name, final String value)
356
if (SecurityUtility.skipAccessControl())
358
System.setProperty(name, value);
364
AccessController.doPrivileged( new PrivilegedExceptionAction()
366
public Object run() throws Exception
368
return System.setProperty(name, value);
372
catch (PrivilegedActionException e)
374
throw (RuntimeException) e.getCause();