~ubuntu-branches/ubuntu/raring/libjboss-remoting-java/raring

« back to all changes in this revision

Viewing changes to tests/org/jboss/test/remoting/detection/jndi/deadlock3/Client.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.jboss.test.remoting.detection.jndi.deadlock3;
2
 
 
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;
16
 
 
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;
29
 
import java.util.Map;
30
 
 
31
 
/**
32
 
 * @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
33
 
 */
34
 
public class Client implements NotificationListener, Runnable
35
 
{
36
 
 
37
 
   private static Logger logger;
38
 
   private String localHost = "";
39
 
   private String localPort;
40
 
   private static NetworkRegistry registry;
41
 
 
42
 
 
43
 
   private MBeanServer server;
44
 
   private ObjectName objConnector = null;
45
 
 
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;
52
 
 
53
 
   private void setLocalHost(String host)
54
 
   {
55
 
      jndiAddress = localHost = host;
56
 
   }
57
 
 
58
 
 
59
 
   private void setUp()
60
 
   {
61
 
 
62
 
      try
63
 
      {
64
 
         Thread s = new Thread(this);
65
 
         Runtime.getRuntime().addShutdownHook(s);
66
 
      }
67
 
      catch (Exception e)
68
 
      {
69
 
         e.printStackTrace();
70
 
         System.exit(1);
71
 
      }
72
 
 
73
 
      registry = NetworkRegistry.getInstance();
74
 
 
75
 
      try
76
 
      {
77
 
         server = MBeanServerFactory.createMBeanServer();
78
 
         server.registerMBean(registry, new ObjectName("remoting:type=NetworkRegistry"));
79
 
      }
80
 
      catch (Exception e)
81
 
      {
82
 
         e.printStackTrace();
83
 
      }
84
 
 
85
 
      try
86
 
      {
87
 
         localPort = FindFreePort();
88
 
         InvokerLocator locator = new InvokerLocator("multiplex://" + localHost + ":" + localPort);
89
 
 
90
 
         //used until a server is found
91
 
         initialConnector = new Connector(locator.getLocatorURI());
92
 
 
93
 
         try
94
 
         {
95
 
            objConnector = new ObjectName("jboss.remoting:type=Connector,transport=" + locator.getProtocol());
96
 
            server.registerMBean(initialConnector, objConnector);
97
 
         }
98
 
         catch (Exception e)
99
 
         {
100
 
            e.printStackTrace();
101
 
         }
102
 
 
103
 
         initialConnector.create();
104
 
         initialConnector.start();
105
 
 
106
 
      }
107
 
      catch (Exception e)
108
 
      {
109
 
         e.printStackTrace();
110
 
         System.exit(1);
111
 
      }
112
 
 
113
 
 
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
121
 
 
122
 
 
123
 
      try
124
 
      {
125
 
         server.registerMBean(jdet, new ObjectName("remoting:type=Detector,transport=jndi"));
126
 
      }
127
 
      catch (Exception e)
128
 
      {
129
 
         e.printStackTrace();
130
 
      }
131
 
 
132
 
      try
133
 
      {
134
 
         jdet.start();
135
 
         registry.addNotificationListener(this, null, null);
136
 
      }
137
 
      catch (Exception ex)
138
 
      {
139
 
         ex.printStackTrace();
140
 
         System.exit(1);
141
 
      }
142
 
 
143
 
      //a third person tries to connect to the server
144
 
      try
145
 
      {
146
 
         Socket s = new Socket(localHost, 1001);
147
 
         s.close();
148
 
      }
149
 
      catch (UnknownHostException e)
150
 
      {
151
 
         e.printStackTrace();
152
 
      }
153
 
      catch (IOException e)
154
 
      {
155
 
         e.printStackTrace();
156
 
      }
157
 
   }
158
 
 
159
 
 
160
 
   public void handleNotification(Notification notification, Object handback)
161
 
   {
162
 
 
163
 
      if (notification instanceof NetworkNotification)
164
 
      {
165
 
         NetworkNotification networkNotification = (NetworkNotification) notification;
166
 
 
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++)
171
 
            {
172
 
               try
173
 
               {
174
 
                  //for this test make sure it's not a client
175
 
                  if (networkNotification.getLocator()[x].getPort() == 1001)
176
 
                  {
177
 
                     System.out.println("-+-Discovered server '" + locators[x].getLocatorURI() + "'-+-");
178
 
                     init(locators[x]);
179
 
                  }
180
 
               }
181
 
               catch (Exception ignored)
182
 
               {
183
 
               }
184
 
            }
185
 
 
186
 
         }
187
 
         else if (NetworkNotification.SERVER_REMOVED.equals(networkNotification.getType()))
188
 
         { // notification
189
 
 
190
 
            InvokerLocator[] locators = networkNotification.getLocator();
191
 
            for (int x = 0; x < locators.length; x++)
192
 
            {
193
 
 
194
 
               try
195
 
               {
196
 
                  //for this test make sure it's not a client
197
 
                  if (networkNotification.getLocator()[x].getPort() == 1001)
198
 
                  {
199
 
                     System.out.println("-!-Server '" + locators[x].getLocatorURI() + "' has gone-!-");
200
 
                  }
201
 
               }
202
 
               catch (Throwable throwable)
203
 
               {
204
 
                  throwable.printStackTrace();
205
 
               }
206
 
 
207
 
            }
208
 
         }
209
 
 
210
 
 
211
 
      }
212
 
 
213
 
   }
214
 
 
215
 
 
216
 
   private static String FindFreePort()
217
 
   {
218
 
 
219
 
      ServerSocket socket = null;
220
 
      try
221
 
      {
222
 
         socket = new ServerSocket(0);
223
 
         return new Integer(socket.getLocalPort()).toString();
224
 
      }
225
 
      catch (IOException e)
226
 
      {
227
 
         e.printStackTrace();
228
 
      }
229
 
      finally
230
 
      {
231
 
         if (socket != null)
232
 
         {
233
 
            try
234
 
            {
235
 
               socket.close();
236
 
            }
237
 
            catch (IOException e)
238
 
            {
239
 
            }
240
 
         }
241
 
      }
242
 
      return null;
243
 
 
244
 
   }
245
 
 
246
 
 
247
 
   private synchronized void init(InvokerLocator locator)
248
 
   {
249
 
      localPort = FindFreePort();
250
 
 
251
 
      initServer(locator);//Test#1: commented out;   Test#2: not commented out
252
 
 
253
 
      try
254
 
      {
255
 
 
256
 
         Map configuration = new HashMap();
257
 
         configuration.put(MultiplexInvokerConstants.MULTIPLEX_BIND_HOST_KEY, localHost);
258
 
         configuration.put(MultiplexInvokerConstants.MULTIPLEX_BIND_PORT_KEY, localPort);
259
 
 
260
 
 
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");
265
 
 
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");
270
 
 
271
 
         client = new org.jboss.remoting.Client(locator, "sample", configuration);
272
 
         client.connect();
273
 
 
274
 
         //initServer(locator);//Test#1: not commented out;   Test#2: commented out
275
 
 
276
 
         InvokerLocator clientLocator = new InvokerLocator("sslmultiplex://" + localHost + ":" + localPort);
277
 
         ClientCallbackHandler handler = new ClientCallbackHandler();
278
 
         client.addListener(handler, clientLocator);
279
 
         System.out.println("successful");
280
 
      }
281
 
      catch (Throwable e)
282
 
      {
283
 
         e.printStackTrace();
284
 
 
285
 
      }
286
 
 
287
 
 
288
 
   }
289
 
 
290
 
 
291
 
   private void initServer(InvokerLocator remoteLocator)
292
 
   {
293
 
      //building connector for found server
294
 
      Map configuration = new HashMap();
295
 
      String addr = remoteLocator.getHost();
296
 
      int port = remoteLocator.getPort();
297
 
 
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");
302
 
 
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");
307
 
 
308
 
      try
309
 
      {
310
 
         connector = new Connector("sslmultiplex://" + localHost + ":" + localPort, configuration);
311
 
 
312
 
         connector.create();
313
 
 
314
 
         try
315
 
         {
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);
321
 
         }
322
 
         catch (Exception e)
323
 
         {
324
 
            e.printStackTrace();
325
 
         }
326
 
 
327
 
         connector.start();
328
 
 
329
 
 
330
 
         if (initialConnector != null)
331
 
         {
332
 
            // don't need the initial connector any longer
333
 
            initialConnector.stop();
334
 
            try
335
 
            {
336
 
               server.unregisterMBean(objConnector);
337
 
            }
338
 
            catch (Exception e)
339
 
            {
340
 
               e.printStackTrace();
341
 
            }
342
 
            initialConnector = null;
343
 
 
344
 
         }
345
 
      }
346
 
      catch (Exception e)
347
 
      {
348
 
         e.printStackTrace();
349
 
 
350
 
      }
351
 
 
352
 
   }
353
 
 
354
 
 
355
 
   public void run()
356
 
   {
357
 
 
358
 
      try
359
 
      {
360
 
         jdet.stop();
361
 
         server.unregisterMBean(new ObjectName("remoting:type=Detector,transport=jndi"));
362
 
         registry.removeNotificationListener(this);
363
 
         registry = null;
364
 
         server.unregisterMBean(new ObjectName("remoting:type=NetworkRegistry"));
365
 
 
366
 
      }
367
 
      catch (Exception e)
368
 
      {
369
 
      }
370
 
 
371
 
      try
372
 
      {
373
 
 
374
 
         if (initialConnector != null)
375
 
         {
376
 
            initialConnector.stop();
377
 
            server.unregisterMBean(objConnector);
378
 
            initialConnector = null;
379
 
         }
380
 
 
381
 
         if (connector != null)
382
 
         {
383
 
            connector.stop();
384
 
            connector = null;
385
 
         }
386
 
 
387
 
         if (client != null)
388
 
         {
389
 
            client.disconnect();
390
 
            client = null;
391
 
         }
392
 
 
393
 
      }
394
 
      catch (Exception e)
395
 
      {
396
 
         e.printStackTrace();
397
 
      }
398
 
 
399
 
 
400
 
   }
401
 
 
402
 
 
403
 
   public static void main(String[] args)
404
 
   {
405
 
      try
406
 
      {
407
 
         Client test = new Client();
408
 
         logger = Logger.getLogger(test.getClass());
409
 
         logger.setLevel((Level) Level.DEBUG);
410
 
 
411
 
         String host = InetAddress.getLocalHost().getHostAddress();
412
 
 
413
 
         test.setLocalHost(host);
414
 
         test.setUp();
415
 
 
416
 
         while (true)
417
 
         {
418
 
            Thread.sleep(1000);
419
 
         }
420
 
 
421
 
      }
422
 
      catch (Exception e)
423
 
      {
424
 
         e.printStackTrace();
425
 
         System.exit(1);
426
 
      }
427
 
 
428
 
 
429
 
   }
430
 
 
431
 
 
432
 
   public static class ClientCallbackHandler implements InvokerCallbackHandler
433
 
   {
434
 
 
435
 
      public void handleCallback(Callback callback) throws HandleCallbackException
436
 
      {
437
 
 
438
 
      }
439
 
 
440
 
   }
441
 
}
442