~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/deadlock/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
 
/*
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.
6
 
*
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.
11
 
*
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.
16
 
*
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.
21
 
*/
22
 
package org.jboss.test.remoting.detection.jndi.deadlock;
23
 
 
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;
37
 
 
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;
50
 
import java.util.Map;
51
 
 
52
 
/**
53
 
 * @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
54
 
 * @author Michael Voss
55
 
 */
56
 
public class Client implements NotificationListener, Runnable
57
 
{
58
 
 
59
 
   private static Logger logger;
60
 
   private String localHost = "";
61
 
   private String localPort;
62
 
   private static NetworkRegistry registry;
63
 
 
64
 
 
65
 
   private MBeanServer server;
66
 
   private ObjectName objConnector = null;
67
 
 
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;
74
 
 
75
 
   private void setLocalHost(String host)
76
 
   {
77
 
      jndiAddress = localHost = host;
78
 
   }
79
 
 
80
 
 
81
 
   private void setUp()
82
 
   {
83
 
 
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);
88
 
      /*
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);
92
 
*/
93
 
      try
94
 
      {
95
 
         Thread s = new Thread(this);
96
 
         Runtime.getRuntime().addShutdownHook(s);
97
 
      }
98
 
      catch (Exception e)
99
 
      {
100
 
         e.printStackTrace();
101
 
         System.exit(1);
102
 
      }
103
 
 
104
 
      registry = NetworkRegistry.getInstance();
105
 
 
106
 
      try
107
 
      {
108
 
         server = MBeanServerFactory.createMBeanServer();
109
 
         server.registerMBean(registry, new ObjectName("remoting:type=NetworkRegistry"));
110
 
      }
111
 
      catch (Exception e)
112
 
      {
113
 
         e.printStackTrace();
114
 
      }
115
 
 
116
 
      try
117
 
      {
118
 
         localPort = FindFreePort();
119
 
         InvokerLocator locator = new InvokerLocator("multiplex://" + localHost + ":" + localPort);
120
 
 
121
 
         //used until a server is found
122
 
         initialConnector = new Connector(locator.getLocatorURI());
123
 
 
124
 
         try
125
 
         {
126
 
            objConnector = new ObjectName("jboss.remoting:type=Connector,transport=" + locator.getProtocol());
127
 
            server.registerMBean(initialConnector, objConnector);
128
 
         }
129
 
         catch (Exception e)
130
 
         {
131
 
            e.printStackTrace();
132
 
         }
133
 
 
134
 
         initialConnector.create();
135
 
         initialConnector.start();
136
 
 
137
 
      }
138
 
      catch (Exception e)
139
 
      {
140
 
         e.printStackTrace();
141
 
         System.exit(1);
142
 
      }
143
 
 
144
 
 
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
152
 
 
153
 
 
154
 
      try
155
 
      {
156
 
         server.registerMBean(jdet, new ObjectName("remoting:type=Detector,transport=jndi"));
157
 
      }
158
 
      catch (Exception e)
159
 
      {
160
 
         e.printStackTrace();
161
 
      }
162
 
 
163
 
      try
164
 
      {
165
 
         jdet.start();
166
 
         registry.addNotificationListener(this, null, null);
167
 
      }
168
 
      catch (Exception ex)
169
 
      {
170
 
         ex.printStackTrace();
171
 
         System.exit(1);
172
 
      }
173
 
 
174
 
      //a third person tries to connect to the server
175
 
      try
176
 
      {
177
 
         Socket s = new Socket(localHost, 1001);
178
 
         s.close();
179
 
      }
180
 
      catch (UnknownHostException e)
181
 
      {
182
 
         e.printStackTrace();
183
 
      }
184
 
      catch (IOException e)
185
 
      {
186
 
         e.printStackTrace();
187
 
      }
188
 
   }
189
 
 
190
 
 
191
 
   public void handleNotification(Notification notification, Object handback)
192
 
   {
193
 
 
194
 
      if (notification instanceof NetworkNotification)
195
 
      {
196
 
         NetworkNotification networkNotification = (NetworkNotification) notification;
197
 
 
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++)
202
 
            {
203
 
               try
204
 
               {
205
 
                  //for this test make sure it's not a client
206
 
                  if (networkNotification.getLocator()[x].getPort() == 1001)
207
 
                  {
208
 
                     System.out.println("-+-Discovered server '" + locators[x].getLocatorURI() + "'-+-");
209
 
                     init(locators[x]);
210
 
                  }
211
 
               }
212
 
               catch (Exception ignored)
213
 
               {
214
 
               }
215
 
            }
216
 
 
217
 
         }
218
 
         else if (NetworkNotification.SERVER_REMOVED.equals(networkNotification.getType()))
219
 
         { // notification
220
 
 
221
 
            InvokerLocator[] locators = networkNotification.getLocator();
222
 
            for (int x = 0; x < locators.length; x++)
223
 
            {
224
 
 
225
 
               try
226
 
               {
227
 
                  //for this test make sure it's not a client
228
 
                  if (networkNotification.getLocator()[x].getPort() == 1001)
229
 
                  {
230
 
                     System.out.println("-!-Server '" + locators[x].getLocatorURI() + "' has gone-!-");
231
 
                  }
232
 
               }
233
 
               catch (Throwable throwable)
234
 
               {
235
 
                  throwable.printStackTrace();
236
 
               }
237
 
 
238
 
            }
239
 
         }
240
 
 
241
 
 
242
 
      }
243
 
 
244
 
   }
245
 
 
246
 
 
247
 
   private static String FindFreePort()
248
 
   {
249
 
 
250
 
      ServerSocket socket = null;
251
 
      try
252
 
      {
253
 
         socket = new ServerSocket(0);
254
 
         return new Integer(socket.getLocalPort()).toString();
255
 
      }
256
 
      catch (IOException e)
257
 
      {
258
 
         e.printStackTrace();
259
 
      }
260
 
      finally
261
 
      {
262
 
         if (socket != null)
263
 
         {
264
 
            try
265
 
            {
266
 
               socket.close();
267
 
            }
268
 
            catch (IOException e)
269
 
            {
270
 
            }
271
 
         }
272
 
      }
273
 
      return null;
274
 
 
275
 
   }
276
 
 
277
 
 
278
 
   private synchronized void init(InvokerLocator locator)
279
 
   {
280
 
      localPort = FindFreePort();
281
 
 
282
 
      InvokerLocator newLocator = initServer(locator);//Test#1: commented out;   Test#2: not commented out
283
 
 
284
 
      try
285
 
      {
286
 
 
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();
291
 
 
292
 
         Map configuration = new HashMap();
293
 
 
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");
300
 
 
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");
307
 
 
308
 
/*
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");
314
 
 
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");
320
 
*/
321
 
         
322
 
         if (client != null)
323
 
         {
324
 
            System.out.println("answer: " + client.invoke("abc"));
325
 
            client.disconnect();
326
 
         }
327
 
         
328
 
         client = new org.jboss.remoting.Client(locator, "sample", configuration);
329
 
         client.connect();
330
 
         System.out.println("answer: " + client.invoke("def"));
331
 
         client.disconnect();
332
 
         
333
 
         client = new org.jboss.remoting.Client(locator, "sample", configuration);
334
 
         client.connect();
335
 
         System.out.println("answer: " + client.invoke("ghi"));
336
 
         
337
 
         client.disconnect();
338
 
         client = new org.jboss.remoting.Client(newLocator, "sample", configuration);
339
 
         client.connect();
340
 
         System.out.println("answer: " + client.invoke("jkl"));
341
 
         
342
 
         //initServer(locator);//Test#1: not commented out;   Test#2: commented out
343
 
 
344
 
         InvokerLocator clientLocator = new InvokerLocator("sslmultiplex://" + localHost + ":" + localPort);
345
 
         ClientCallbackHandler handler = new ClientCallbackHandler();
346
 
         client.addListener(handler, clientLocator);
347
 
         System.out.println("successful");
348
 
      }
349
 
      catch (Throwable e)
350
 
      {
351
 
         e.printStackTrace();
352
 
 
353
 
      }
354
 
 
355
 
 
356
 
   }
357
 
 
358
 
 
359
 
   private InvokerLocator initServer(InvokerLocator remoteLocator)
360
 
   {
361
 
      //building connector for found server
362
 
      Map configuration = new HashMap();
363
 
      String addr = remoteLocator.getHost();
364
 
      int port = remoteLocator.getPort();
365
 
 
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();
370
 
 
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");
377
 
 
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");
384
 
 
385
 
/*
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");
391
 
 
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");
397
 
*/
398
 
 
399
 
      InvokerLocator newLocator = null;
400
 
      try
401
 
      {
402
 
         newLocator = new InvokerLocator("sslmultiplex://" + localHost + ":" + localPort);
403
 
         connector = new Connector(newLocator.getLocatorURI(), configuration);
404
 
 
405
 
         connector.create();
406
 
 
407
 
         try
408
 
         {
409
 
            SSLMultiplexServerInvoker invoker = (SSLMultiplexServerInvoker) connector.getServerInvoker();
410
 
 
411
 
//            ServerSocketFactory svrSocketFactory = SSL.createServerSocketFactory("testpw", "testpw", "certificate/clientKeyStore", "certificate/clientTrustStore");
412
 
            ServerSocketFactory svrSocketFactory = SSL.createServerSocketFactory("unit-tests-server", "unit-tests-client", clientKeyStorePath, clientTrustStorePath);
413
 
 
414
 
            invoker.setServerSocketFactory(svrSocketFactory);
415
 
            invoker.setClientConnectAddress(addr);
416
 
            invoker.setClientConnectPort(port);
417
 
         }
418
 
         catch (Exception e)
419
 
         {
420
 
            e.printStackTrace();
421
 
         }
422
 
 
423
 
         connector.addInvocationHandler("sample", new MockServerInvocationHandler());
424
 
         connector.start();
425
 
 
426
 
 
427
 
         if (initialConnector != null)
428
 
         {
429
 
            // don't need the initial connector any longer
430
 
            initialConnector.stop();
431
 
            try
432
 
            {
433
 
               server.unregisterMBean(objConnector);
434
 
            }
435
 
            catch (Exception e)
436
 
            {
437
 
               e.printStackTrace();
438
 
            }
439
 
            initialConnector = null;
440
 
 
441
 
         }
442
 
      }
443
 
      catch (Exception e)
444
 
      {
445
 
         e.printStackTrace();
446
 
 
447
 
      }
448
 
      
449
 
      return newLocator;
450
 
 
451
 
   }
452
 
 
453
 
 
454
 
   public void run()
455
 
   {
456
 
 
457
 
      try
458
 
      {
459
 
         jdet.stop();
460
 
         server.unregisterMBean(new ObjectName("remoting:type=Detector,transport=jndi"));
461
 
         registry.removeNotificationListener(this);
462
 
         registry = null;
463
 
         server.unregisterMBean(new ObjectName("remoting:type=NetworkRegistry"));
464
 
 
465
 
      }
466
 
      catch (Exception e)
467
 
      {
468
 
      }
469
 
 
470
 
      try
471
 
      {
472
 
 
473
 
         if (initialConnector != null)
474
 
         {
475
 
            initialConnector.stop();
476
 
            server.unregisterMBean(objConnector);
477
 
            initialConnector = null;
478
 
         }
479
 
 
480
 
         if (connector != null)
481
 
         {
482
 
            connector.stop();
483
 
            connector = null;
484
 
         }
485
 
 
486
 
         if (client != null)
487
 
         {
488
 
            client.disconnect();
489
 
            client = null;
490
 
         }
491
 
 
492
 
      }
493
 
      catch (Exception e)
494
 
      {
495
 
         e.printStackTrace();
496
 
      }
497
 
 
498
 
 
499
 
   }
500
 
 
501
 
 
502
 
   public static void main(String[] args)
503
 
   {
504
 
      try
505
 
      {
506
 
         Client test = new Client();
507
 
         logger = Logger.getLogger(test.getClass());
508
 
         logger.setLevel((Level) Level.DEBUG);
509
 
 
510
 
         String host = InetAddress.getLocalHost().getHostAddress();
511
 
 
512
 
         test.setLocalHost(host);
513
 
         test.setUp();
514
 
 
515
 
         while (true)
516
 
         {
517
 
            Thread.sleep(1000);
518
 
         }
519
 
 
520
 
      }
521
 
      catch (Exception e)
522
 
      {
523
 
         e.printStackTrace();
524
 
         System.exit(1);
525
 
      }
526
 
 
527
 
 
528
 
   }
529
 
 
530
 
 
531
 
   public static class ClientCallbackHandler implements InvokerCallbackHandler
532
 
   {
533
 
 
534
 
      public void handleCallback(Callback callback) throws HandleCallbackException
535
 
      {
536
 
 
537
 
      }
538
 
 
539
 
   }
540
 
}
541