1
package org.jboss.test.remoting.detection.jndi.deadlock3;
3
import org.apache.log4j.Level;
4
import org.apache.log4j.Logger;
5
import org.jboss.remoting.InvokerLocator;
6
import org.jboss.remoting.callback.Callback;
7
import org.jboss.remoting.callback.HandleCallbackException;
8
import org.jboss.remoting.callback.InvokerCallbackHandler;
9
import org.jboss.remoting.detection.jndi.JNDIDetector;
10
import org.jboss.remoting.network.NetworkNotification;
11
import org.jboss.remoting.network.NetworkRegistry;
12
import org.jboss.remoting.security.SSLSocketBuilder;
13
import org.jboss.remoting.transport.Connector;
14
import org.jboss.remoting.transport.multiplex.MultiplexInvokerConstants;
15
import org.jboss.remoting.transport.sslmultiplex.SSLMultiplexServerInvoker;
17
import javax.management.MBeanServer;
18
import javax.management.MBeanServerFactory;
19
import javax.management.Notification;
20
import javax.management.NotificationListener;
21
import javax.management.ObjectName;
22
import javax.net.ServerSocketFactory;
23
import java.io.IOException;
24
import java.net.InetAddress;
25
import java.net.ServerSocket;
26
import java.net.Socket;
27
import java.net.UnknownHostException;
28
import java.util.HashMap;
32
* @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
34
public class Client implements NotificationListener, Runnable
37
private static Logger logger;
38
private String localHost = "";
39
private String localPort;
40
private static NetworkRegistry registry;
43
private MBeanServer server;
44
private ObjectName objConnector = null;
46
private JNDIDetector jdet = null;
47
private Connector initialConnector = null;
48
private Connector connector;
49
private org.jboss.remoting.Client client = null;
50
private String jndiAddress = null;
51
private int jndiPort = 2410;
53
private void setLocalHost(String host)
55
jndiAddress = localHost = host;
64
Thread s = new Thread(this);
65
Runtime.getRuntime().addShutdownHook(s);
73
registry = NetworkRegistry.getInstance();
77
server = MBeanServerFactory.createMBeanServer();
78
server.registerMBean(registry, new ObjectName("remoting:type=NetworkRegistry"));
87
localPort = FindFreePort();
88
InvokerLocator locator = new InvokerLocator("multiplex://" + localHost + ":" + localPort);
90
//used until a server is found
91
initialConnector = new Connector(locator.getLocatorURI());
95
objConnector = new ObjectName("jboss.remoting:type=Connector,transport=" + locator.getProtocol());
96
server.registerMBean(initialConnector, objConnector);
103
initialConnector.create();
104
initialConnector.start();
114
jdet = new JNDIDetector();
115
jdet.setPort(jndiPort);
116
jdet.setHost(jndiAddress);
117
jdet.setContextFactory("org.jnp.interfaces.NamingContextFactory");
118
jdet.setURLPackage("org.jboss.naming:org.jnp.interfaces");
119
jdet.setCleanDetectionNumber(2147483647);//avoids that the server is detected as gone
120
//but it's no use if there are two or more servers
125
server.registerMBean(jdet, new ObjectName("remoting:type=Detector,transport=jndi"));
135
registry.addNotificationListener(this, null, null);
139
ex.printStackTrace();
143
//a third person tries to connect to the server
146
Socket s = new Socket(localHost, 1001);
149
catch (UnknownHostException e)
153
catch (IOException e)
160
public void handleNotification(Notification notification, Object handback)
163
if (notification instanceof NetworkNotification)
165
NetworkNotification networkNotification = (NetworkNotification) notification;
167
if (NetworkNotification.SERVER_ADDED.equals(networkNotification.getType()))
168
{ // notification is for new servers being added
169
InvokerLocator[] locators = networkNotification.getLocator();
170
for (int x = 0; x < locators.length; x++)
174
//for this test make sure it's not a client
175
if (networkNotification.getLocator()[x].getPort() == 1001)
177
System.out.println("-+-Discovered server '" + locators[x].getLocatorURI() + "'-+-");
181
catch (Exception ignored)
187
else if (NetworkNotification.SERVER_REMOVED.equals(networkNotification.getType()))
190
InvokerLocator[] locators = networkNotification.getLocator();
191
for (int x = 0; x < locators.length; x++)
196
//for this test make sure it's not a client
197
if (networkNotification.getLocator()[x].getPort() == 1001)
199
System.out.println("-!-Server '" + locators[x].getLocatorURI() + "' has gone-!-");
202
catch (Throwable throwable)
204
throwable.printStackTrace();
216
private static String FindFreePort()
219
ServerSocket socket = null;
222
socket = new ServerSocket(0);
223
return new Integer(socket.getLocalPort()).toString();
225
catch (IOException e)
237
catch (IOException e)
247
private synchronized void init(InvokerLocator locator)
249
localPort = FindFreePort();
251
initServer(locator);//Test#1: commented out; Test#2: not commented out
256
Map configuration = new HashMap();
257
configuration.put(MultiplexInvokerConstants.MULTIPLEX_BIND_HOST_KEY, localHost);
258
configuration.put(MultiplexInvokerConstants.MULTIPLEX_BIND_PORT_KEY, localPort);
261
configuration.put(SSLSocketBuilder.REMOTING_KEY_STORE_TYPE, "JKS");
262
configuration.put(SSLSocketBuilder.REMOTING_KEY_STORE_FILE_PATH, "certificate/clientKeyStore");
263
configuration.put(SSLSocketBuilder.REMOTING_KEY_STORE_PASSWORD, "testpw");
264
configuration.put(SSLSocketBuilder.REMOTING_KEY_STORE_ALGORITHM, "SunX509");
266
configuration.put(SSLSocketBuilder.REMOTING_TRUST_STORE_TYPE, "JKS");
267
configuration.put(SSLSocketBuilder.REMOTING_TRUST_STORE_FILE_PATH, "certificate/clientTrustStore");
268
configuration.put(SSLSocketBuilder.REMOTING_TRUST_STORE_PASSWORD, "testpw");
269
configuration.put(SSLSocketBuilder.REMOTING_TRUST_STORE_ALGORITHM, "SunX509");
271
client = new org.jboss.remoting.Client(locator, "sample", configuration);
274
//initServer(locator);//Test#1: not commented out; Test#2: commented out
276
InvokerLocator clientLocator = new InvokerLocator("sslmultiplex://" + localHost + ":" + localPort);
277
ClientCallbackHandler handler = new ClientCallbackHandler();
278
client.addListener(handler, clientLocator);
279
System.out.println("successful");
291
private void initServer(InvokerLocator remoteLocator)
293
//building connector for found server
294
Map configuration = new HashMap();
295
String addr = remoteLocator.getHost();
296
int port = remoteLocator.getPort();
298
configuration.put(SSLSocketBuilder.REMOTING_KEY_STORE_TYPE, "JKS");
299
configuration.put(SSLSocketBuilder.REMOTING_KEY_STORE_FILE_PATH, "certificate/clientKeyStore");
300
configuration.put(SSLSocketBuilder.REMOTING_KEY_STORE_PASSWORD, "testpw");
301
configuration.put(SSLSocketBuilder.REMOTING_KEY_STORE_ALGORITHM, "SunX509");
303
configuration.put(SSLSocketBuilder.REMOTING_TRUST_STORE_TYPE, "JKS");
304
configuration.put(SSLSocketBuilder.REMOTING_TRUST_STORE_FILE_PATH, "certificate/clientTrustStore");
305
configuration.put(SSLSocketBuilder.REMOTING_TRUST_STORE_PASSWORD, "testpw");
306
configuration.put(SSLSocketBuilder.REMOTING_TRUST_STORE_ALGORITHM, "SunX509");
310
connector = new Connector("sslmultiplex://" + localHost + ":" + localPort, configuration);
316
SSLMultiplexServerInvoker invoker = (SSLMultiplexServerInvoker) connector.getServerInvoker();
317
ServerSocketFactory svrSocketFactory = SSL.createServerSocketFactory("testpw", "testpw", "certificate/clientKeyStore", "certificate/clientTrustStore");
318
invoker.setServerSocketFactory(svrSocketFactory);
319
invoker.setClientConnectAddress(addr);
320
invoker.setClientConnectPort(port);
330
if (initialConnector != null)
332
// don't need the initial connector any longer
333
initialConnector.stop();
336
server.unregisterMBean(objConnector);
342
initialConnector = null;
361
server.unregisterMBean(new ObjectName("remoting:type=Detector,transport=jndi"));
362
registry.removeNotificationListener(this);
364
server.unregisterMBean(new ObjectName("remoting:type=NetworkRegistry"));
374
if (initialConnector != null)
376
initialConnector.stop();
377
server.unregisterMBean(objConnector);
378
initialConnector = null;
381
if (connector != null)
403
public static void main(String[] args)
407
Client test = new Client();
408
logger = Logger.getLogger(test.getClass());
409
logger.setLevel((Level) Level.DEBUG);
411
String host = InetAddress.getLocalHost().getHostAddress();
413
test.setLocalHost(host);
432
public static class ClientCallbackHandler implements InvokerCallbackHandler
435
public void handleCallback(Callback callback) throws HandleCallbackException