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;
24
import org.apache.log4j.Level;
25
import org.apache.log4j.Logger;
26
import org.jboss.remoting.InvokerLocator;
27
import org.jboss.remoting.callback.Callback;
28
import org.jboss.remoting.callback.HandleCallbackException;
29
import org.jboss.remoting.callback.InvokerCallbackHandler;
30
import org.jboss.remoting.detection.jndi.JNDIDetector;
31
import org.jboss.remoting.network.NetworkNotification;
32
import org.jboss.remoting.network.NetworkRegistry;
33
import org.jboss.remoting.security.SSLSocketBuilder;
34
import org.jboss.remoting.transport.Connector;
35
import org.jboss.remoting.transport.sslmultiplex.SSLMultiplexServerInvoker;
36
import org.jboss.test.remoting.transport.mock.MockServerInvocationHandler;
38
import javax.management.MBeanServer;
39
import javax.management.MBeanServerFactory;
40
import javax.management.Notification;
41
import javax.management.NotificationListener;
42
import javax.management.ObjectName;
43
import javax.net.ServerSocketFactory;
44
import java.io.IOException;
45
import java.net.InetAddress;
46
import java.net.ServerSocket;
47
import java.net.Socket;
48
import java.net.UnknownHostException;
49
import java.util.HashMap;
53
* @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
54
* @author Michael Voss
56
public class Client implements NotificationListener, Runnable
59
private static Logger logger;
60
private String localHost = "";
61
private String localPort;
62
private static NetworkRegistry registry;
65
private MBeanServer server;
66
private ObjectName objConnector = null;
68
private JNDIDetector jdet = null;
69
private Connector initialConnector = null;
70
private Connector connector;
71
private org.jboss.remoting.Client client = null;
72
private String jndiAddress = null;
73
private int jndiPort = 2410;
75
private void setLocalHost(String host)
77
jndiAddress = localHost = host;
84
org.apache.log4j.BasicConfigurator.configure();
85
org.apache.log4j.Category.getRoot().setLevel(Level.OFF);
86
org.apache.log4j.Category.getInstance("org.jboss.remoting").setLevel(Level.OFF);
87
org.apache.log4j.Category.getInstance("org.jgroups").setLevel(Level.OFF);
89
org.apache.log4j.Category.getRoot().setLevel(Level.DEBUG);
90
org.apache.log4j.Category.getInstance("org.jboss.remoting").setLevel(Level.DEBUG);
91
org.apache.log4j.Category.getInstance("org.jgroups").setLevel(Level.FATAL);
95
Thread s = new Thread(this);
96
Runtime.getRuntime().addShutdownHook(s);
104
registry = NetworkRegistry.getInstance();
108
server = MBeanServerFactory.createMBeanServer();
109
server.registerMBean(registry, new ObjectName("remoting:type=NetworkRegistry"));
118
localPort = FindFreePort();
119
InvokerLocator locator = new InvokerLocator("multiplex://" + localHost + ":" + localPort);
121
//used until a server is found
122
initialConnector = new Connector(locator.getLocatorURI());
126
objConnector = new ObjectName("jboss.remoting:type=Connector,transport=" + locator.getProtocol());
127
server.registerMBean(initialConnector, objConnector);
134
initialConnector.create();
135
initialConnector.start();
145
jdet = new JNDIDetector();
146
jdet.setPort(jndiPort);
147
jdet.setHost(jndiAddress);
148
jdet.setContextFactory("org.jnp.interfaces.NamingContextFactory");
149
jdet.setURLPackage("org.jboss.naming:org.jnp.interfaces");
150
jdet.setCleanDetectionNumber(2147483647);//avoids that the server is detected as gone
151
//but it's no use if there are two or more servers
156
server.registerMBean(jdet, new ObjectName("remoting:type=Detector,transport=jndi"));
166
registry.addNotificationListener(this, null, null);
170
ex.printStackTrace();
174
//a third person tries to connect to the server
177
Socket s = new Socket(localHost, 1001);
180
catch (UnknownHostException e)
184
catch (IOException e)
191
public void handleNotification(Notification notification, Object handback)
194
if (notification instanceof NetworkNotification)
196
NetworkNotification networkNotification = (NetworkNotification) notification;
198
if (NetworkNotification.SERVER_ADDED.equals(networkNotification.getType()))
199
{ // notification is for new servers being added
200
InvokerLocator[] locators = networkNotification.getLocator();
201
for (int x = 0; x < locators.length; x++)
205
//for this test make sure it's not a client
206
if (networkNotification.getLocator()[x].getPort() == 1001)
208
System.out.println("-+-Discovered server '" + locators[x].getLocatorURI() + "'-+-");
212
catch (Exception ignored)
218
else if (NetworkNotification.SERVER_REMOVED.equals(networkNotification.getType()))
221
InvokerLocator[] locators = networkNotification.getLocator();
222
for (int x = 0; x < locators.length; x++)
227
//for this test make sure it's not a client
228
if (networkNotification.getLocator()[x].getPort() == 1001)
230
System.out.println("-!-Server '" + locators[x].getLocatorURI() + "' has gone-!-");
233
catch (Throwable throwable)
235
throwable.printStackTrace();
247
private static String FindFreePort()
250
ServerSocket socket = null;
253
socket = new ServerSocket(0);
254
return new Integer(socket.getLocalPort()).toString();
256
catch (IOException e)
268
catch (IOException e)
278
private synchronized void init(InvokerLocator locator)
280
localPort = FindFreePort();
282
InvokerLocator newLocator = initServer(locator);//Test#1: commented out; Test#2: not commented out
287
// String clientKeyStorePath = this.getClass().getResource("certificate/clientKeyStore").getFile();
288
// String clientTrustStorePath = this.getClass().getResource("certificate/clientTrustStore").getFile();
289
String clientKeyStorePath = this.getClass().getResource("../../../transport/socket/ssl/.keystore").getFile();
290
String clientTrustStorePath = this.getClass().getResource("../../../transport/socket/ssl/.trustStore").getFile();
292
Map configuration = new HashMap();
294
configuration.put(SSLSocketBuilder.REMOTING_KEY_STORE_TYPE, "JKS");
295
configuration.put(SSLSocketBuilder.REMOTING_KEY_STORE_FILE_PATH, clientKeyStorePath);
296
// configuration.put(SSLSocketBuilder.REMOTING_KEY_STORE_FILE_PATH, "certificate/clientKeyStore");
297
// configuration.put(SSLSocketBuilder.REMOTING_KEY_STORE_PASSWORD, "testpw");
298
configuration.put(SSLSocketBuilder.REMOTING_KEY_STORE_PASSWORD, "unit-tests-server");
299
configuration.put(SSLSocketBuilder.REMOTING_KEY_STORE_ALGORITHM, "SunX509");
301
configuration.put(SSLSocketBuilder.REMOTING_TRUST_STORE_TYPE, "JKS");
302
configuration.put(SSLSocketBuilder.REMOTING_TRUST_STORE_FILE_PATH, clientTrustStorePath);
303
// configuration.put(SSLSocketBuilder.REMOTING_TRUST_STORE_FILE_PATH, "certificate/clientTrustStore");
304
// configuration.put(SSLSocketBuilder.REMOTING_TRUST_STORE_PASSWORD, "testpw");
305
configuration.put(SSLSocketBuilder.REMOTING_TRUST_STORE_PASSWORD, "unit-tests-client");
306
configuration.put(SSLSocketBuilder.REMOTING_TRUST_STORE_ALGORITHM, "SunX509");
309
configuration.put(RemotingSSLSocketFactory.REMOTING_KEY_STORE_TYPE, "JKS");
310
configuration.put(RemotingSSLSocketFactory.REMOTING_KEY_STORE_FILE_PATH, clientKeyStorePath);
311
// configuration.put(RemotingSSLSocketFactory.REMOTING_KEY_STORE_FILE_PATH, "certificate/clientKeyStore");
312
configuration.put(RemotingSSLSocketFactory.REMOTING_KEY_STORE_PASSWORD, "testpw");
313
configuration.put(RemotingSSLSocketFactory.REMOTING_ALGORITHM, "SunX509");
315
configuration.put(RemotingSSLSocketFactory.REMOTING_TRUST_STORE_TYPE, "JKS");
316
configuration.put(RemotingSSLSocketFactory.REMOTING_TRUST_STORE_FILE_PATH, clientTrustStorePath);
317
// configuration.put(RemotingSSLSocketFactory.REMOTING_TRUST_STORE_FILE_PATH, "certificate/clientTrustStore");
318
configuration.put(RemotingSSLSocketFactory.REMOTING_TRUST_STORE_PASSWORD, "testpw");
319
configuration.put(RemotingSSLSocketFactory.REMOTING_TRUST_ALGORITHM, "SunX509");
324
System.out.println("answer: " + client.invoke("abc"));
328
client = new org.jboss.remoting.Client(locator, "sample", configuration);
330
System.out.println("answer: " + client.invoke("def"));
333
client = new org.jboss.remoting.Client(locator, "sample", configuration);
335
System.out.println("answer: " + client.invoke("ghi"));
338
client = new org.jboss.remoting.Client(newLocator, "sample", configuration);
340
System.out.println("answer: " + client.invoke("jkl"));
342
//initServer(locator);//Test#1: not commented out; Test#2: commented out
344
InvokerLocator clientLocator = new InvokerLocator("sslmultiplex://" + localHost + ":" + localPort);
345
ClientCallbackHandler handler = new ClientCallbackHandler();
346
client.addListener(handler, clientLocator);
347
System.out.println("successful");
359
private InvokerLocator initServer(InvokerLocator remoteLocator)
361
//building connector for found server
362
Map configuration = new HashMap();
363
String addr = remoteLocator.getHost();
364
int port = remoteLocator.getPort();
366
// String clientKeyStorePath = this.getClass().getResource("certificate/clientKeyStore").getFile();
367
// String clientTrustStorePath = this.getClass().getResource("certificate/clientTrustStore").getFile();
368
String clientKeyStorePath = this.getClass().getResource("../../../transport/socket/ssl/.keystore").getFile();
369
String clientTrustStorePath = this.getClass().getResource("../../../transport/socket/ssl/.trustStore").getFile();
371
configuration.put(SSLSocketBuilder.REMOTING_KEY_STORE_TYPE, "JKS");
372
configuration.put(SSLSocketBuilder.REMOTING_KEY_STORE_FILE_PATH, clientKeyStorePath);
373
// configuration.put(SSLSocketBuilder.REMOTING_KEY_STORE_FILE_PATH, "certificate/clientKeyStore");
374
// configuration.put(SSLSocketBuilder.REMOTING_KEY_STORE_PASSWORD, "testpw");
375
configuration.put(SSLSocketBuilder.REMOTING_KEY_STORE_PASSWORD, "unit-tests-server");
376
// configuration.put(SSLSocketBuilder.REMOTING_KEY_PASSWORD, "SunX509");
378
configuration.put(SSLSocketBuilder.REMOTING_TRUST_STORE_TYPE, "JKS");
379
configuration.put(SSLSocketBuilder.REMOTING_TRUST_STORE_FILE_PATH, clientTrustStorePath);
380
// configuration.put(SSLSocketBuilder.REMOTING_TRUST_STORE_FILE_PATH, "certificate/clientTrustStore");
381
// configuration.put(SSLSocketBuilder.REMOTING_TRUST_STORE_PASSWORD, "testpw");
382
configuration.put(SSLSocketBuilder.REMOTING_TRUST_STORE_PASSWORD, "unit-tests-client");
383
configuration.put(SSLSocketBuilder.REMOTING_TRUST_STORE_ALGORITHM, "SunX509");
386
configuration.put(RemotingSSLSocketFactory.REMOTING_KEY_STORE_TYPE, "JKS");
387
configuration.put(RemotingSSLSocketFactory.REMOTING_KEY_STORE_FILE_PATH, clientKeyStorePath);
388
// configuration.put(RemotingSSLSocketFactory.REMOTING_KEY_STORE_FILE_PATH, "certificate/clientKeyStore");
389
configuration.put(RemotingSSLSocketFactory.REMOTING_KEY_STORE_PASSWORD, "testpw");
390
configuration.put(RemotingSSLSocketFactory.REMOTING_ALGORITHM, "SunX509");
392
configuration.put(RemotingSSLSocketFactory.REMOTING_TRUST_STORE_TYPE, "JKS");
393
configuration.put(RemotingSSLSocketFactory.REMOTING_TRUST_STORE_FILE_PATH, clientTrustStorePath);
394
// configuration.put(RemotingSSLSocketFactory.REMOTING_TRUST_STORE_FILE_PATH, "certificate/clientTrustStore");
395
configuration.put(RemotingSSLSocketFactory.REMOTING_TRUST_STORE_PASSWORD, "testpw");
396
configuration.put(RemotingSSLSocketFactory.REMOTING_TRUST_ALGORITHM, "SunX509");
399
InvokerLocator newLocator = null;
402
newLocator = new InvokerLocator("sslmultiplex://" + localHost + ":" + localPort);
403
connector = new Connector(newLocator.getLocatorURI(), configuration);
409
SSLMultiplexServerInvoker invoker = (SSLMultiplexServerInvoker) connector.getServerInvoker();
411
// ServerSocketFactory svrSocketFactory = SSL.createServerSocketFactory("testpw", "testpw", "certificate/clientKeyStore", "certificate/clientTrustStore");
412
ServerSocketFactory svrSocketFactory = SSL.createServerSocketFactory("unit-tests-server", "unit-tests-client", clientKeyStorePath, clientTrustStorePath);
414
invoker.setServerSocketFactory(svrSocketFactory);
415
invoker.setClientConnectAddress(addr);
416
invoker.setClientConnectPort(port);
423
connector.addInvocationHandler("sample", new MockServerInvocationHandler());
427
if (initialConnector != null)
429
// don't need the initial connector any longer
430
initialConnector.stop();
433
server.unregisterMBean(objConnector);
439
initialConnector = null;
460
server.unregisterMBean(new ObjectName("remoting:type=Detector,transport=jndi"));
461
registry.removeNotificationListener(this);
463
server.unregisterMBean(new ObjectName("remoting:type=NetworkRegistry"));
473
if (initialConnector != null)
475
initialConnector.stop();
476
server.unregisterMBean(objConnector);
477
initialConnector = null;
480
if (connector != null)
502
public static void main(String[] args)
506
Client test = new Client();
507
logger = Logger.getLogger(test.getClass());
508
logger.setLevel((Level) Level.DEBUG);
510
String host = InetAddress.getLocalHost().getHostAddress();
512
test.setLocalHost(host);
531
public static class ClientCallbackHandler implements InvokerCallbackHandler
534
public void handleCallback(Callback callback) throws HandleCallbackException