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

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/detection/jndi/deadlock/Server.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
 
 
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;
 
39
 
 
40
import javax.management.MBeanServer;
 
41
import javax.management.MBeanServerFactory;
 
42
import javax.management.ObjectName;
 
43
import javax.net.ServerSocketFactory;
 
44
 
 
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;
 
52
import java.util.Map;
 
53
 
 
54
/**
 
55
 * @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
 
56
 * @author Michael Voss
 
57
 */
 
58
public class Server implements Runnable
 
59
{
 
60
   private static Connector connector = null;
 
61
   private static Logger logger;
 
62
   private MBeanServer server;
 
63
   private JNDIDetector detector;
 
64
 
 
65
   private String jndiAddress = null;
 
66
   private int jndiPort = 2410;
 
67
   private String port = "1001";
 
68
 
 
69
 
 
70
   private void init()
 
71
   {
 
72
      String localHost = "";
 
73
      try
 
74
      {
 
75
         jndiAddress = localHost = InetAddress.getLocalHost().getHostAddress();
 
76
      }
 
77
      catch (UnknownHostException uhe)
 
78
      {
 
79
         uhe.printStackTrace();
 
80
         System.exit(1);
 
81
      }
 
82
 
 
83
      try
 
84
      {
 
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();
 
89
 
 
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");
 
97
 
 
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");
 
104
 
 
105
         configuration.put(SSLSocketBuilder.REMOTING_SOCKET_USE_CLIENT_MODE, "false");
 
106
 
 
107
/*
 
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");
 
113
 
 
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");
 
120
*/
 
121
          connector = new Connector(configuration);
 
122
 
 
123
         InvokerLocator locator = new InvokerLocator("sslmultiplex://" + localHost + ":" + port);
 
124
         connector.setInvokerLocator(locator.getLocatorURI());
 
125
         connector.create();
 
126
 
 
127
         try
 
128
         {
 
129
 
 
130
//            ServerSocketFactory svrSocketFactory = SSL.createServerSocketFactory("testpw", "testpw", "certificate/serverKeyStore", "certificate/serverTrustStore");
 
131
            ServerSocketFactory svrSocketFactory = SSL.createServerSocketFactory("unit-tests-server", "unit-tests-client", serverKeyStorePath, serverTrustStorePath);
 
132
 
 
133
            SSLMultiplexServerInvoker socketSvrInvoker = (SSLMultiplexServerInvoker) connector.getServerInvoker();
 
134
            socketSvrInvoker.setServerSocketFactory(svrSocketFactory);
 
135
         }
 
136
         catch (Exception e)
 
137
         {
 
138
            e.printStackTrace();
 
139
         }
 
140
 
 
141
         /*connector = new Connector();
 
142
            InvokerLocator locator = new InvokerLocator("multiplex://"+localHost+":"+port);
 
143
            connector.setInvokerLocator(locator.getLocatorURI());
 
144
            connector.create(); */
 
145
 
 
146
         try
 
147
         {
 
148
            connector.addInvocationHandler("sample", new SampleInvocationHandler());
 
149
         }
 
150
         catch (Exception e)
 
151
         {
 
152
            e.printStackTrace();
 
153
            System.exit(1);
 
154
         }
 
155
         connector.start();
 
156
 
 
157
         server.registerMBean(connector, new ObjectName("jboss.remoting:type=Connector"));
 
158
 
 
159
 
 
160
      }
 
161
      catch (Exception e)
 
162
      {
 
163
         e.printStackTrace();
 
164
      }
 
165
 
 
166
 
 
167
   }
 
168
 
 
169
 
 
170
   private void registerJNDI()
 
171
   {
 
172
 
 
173
      detector = new JNDIDetector();
 
174
 
 
175
      try
 
176
      {
 
177
         server.registerMBean(detector, new ObjectName("remoting:type=JNDIDetector"));
 
178
      }
 
179
      catch (Exception ignored)
 
180
      {
 
181
      }
 
182
 
 
183
      detector.setPort(jndiPort);
 
184
      detector.setHost(jndiAddress);
 
185
      try
 
186
      {
 
187
         detector.start();
 
188
      }
 
189
      catch (Exception e)
 
190
      {
 
191
         e.printStackTrace();
 
192
      }
 
193
 
 
194
   }
 
195
 
 
196
 
 
197
   private void setUp() throws Exception
 
198
   {
 
199
 
 
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);
 
204
 
 
205
      try
 
206
      {
 
207
         Thread t = new Thread(this);
 
208
         Runtime.getRuntime().addShutdownHook(t);
 
209
      }
 
210
      catch (Exception ignored)
 
211
      {
 
212
      }
 
213
 
 
214
      logger = Logger.getLogger(getClass());
 
215
      logger.setLevel((Level) Level.DEBUG);
 
216
 
 
217
      server = MBeanServerFactory.createMBeanServer();
 
218
      startJNDIServer();
 
219
      init();
 
220
      registerJNDI();
 
221
 
 
222
 
 
223
   }
 
224
 
 
225
   private void startJNDIServer() throws Exception
 
226
   {
 
227
      Object namingBean = null;
 
228
      Class namingBeanImplClass = null;
 
229
      try
 
230
      {
 
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[] {});
 
236
      }
 
237
      catch (Exception e)
 
238
      {
 
239
         SimpleJNDIServer.println("Cannot find NamingBeanImpl: must be running jdk 1.4");
 
240
      }
 
241
      
 
242
      String host = InetAddress.getLocalHost().getHostAddress();
 
243
 
 
244
      Main jserver = new Main();
 
245
      if (namingBean != null)
 
246
      {
 
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});
 
250
      }
 
251
      jserver.setPort(2410);
 
252
      jserver.setBindAddress(host);
 
253
      jserver.setRmiPort(31000);
 
254
      jserver.start();
 
255
 
 
256
   }
 
257
 
 
258
 
 
259
   public void run()
 
260
   {
 
261
      try
 
262
      {
 
263
 
 
264
         server.unregisterMBean(new ObjectName("remoting:type=JNDIDetector"));
 
265
         if (detector != null)
 
266
         {
 
267
            try
 
268
            {
 
269
               detector.stop();
 
270
            }
 
271
            catch (Exception ignored)
 
272
            {
 
273
            }
 
274
         }
 
275
         if (connector != null)
 
276
         {
 
277
            try
 
278
            {
 
279
               connector.stop();
 
280
            }
 
281
            catch (Exception ignored)
 
282
            {
 
283
            }
 
284
 
 
285
            connector = null;
 
286
            server.unregisterMBean(new ObjectName("jboss.remoting:type=Connector"));
 
287
            Thread.sleep(1000);
 
288
 
 
289
         }
 
290
      }
 
291
      catch (Exception e)
 
292
      {
 
293
      }
 
294
 
 
295
   }
 
296
 
 
297
 
 
298
   public static void main(String[] args)
 
299
   {
 
300
 
 
301
      Server server = new Server();
 
302
      try
 
303
      {
 
304
         server.setUp();
 
305
 
 
306
         while (true)
 
307
         {
 
308
            Thread.sleep(1000);
 
309
         }
 
310
      }
 
311
      catch (Exception e)
 
312
      {
 
313
         e.printStackTrace();
 
314
      }
 
315
   }
 
316
 
 
317
 
 
318
   public static class SampleInvocationHandler implements ServerInvocationHandler
 
319
   {
 
320
 
 
321
      public Object invoke(InvocationRequest invocation) throws Throwable
 
322
      {
 
323
         System.out.println("invoke(): " + invocation.getParameter());
 
324
         logger.info("invoke(): " + invocation.getParameter());
 
325
         return invocation.getParameter();
 
326
//         return null;
 
327
      }
 
328
 
 
329
 
 
330
      public void addListener(InvokerCallbackHandler callbackHandler)
 
331
      {
 
332
 
 
333
      }
 
334
 
 
335
 
 
336
      public void removeListener(InvokerCallbackHandler callbackHandler)
 
337
      {
 
338
 
 
339
      }
 
340
 
 
341
 
 
342
      public void setMBeanServer(MBeanServer server)
 
343
      {
 
344
 
 
345
      }
 
346
 
 
347
      public void setInvoker(ServerInvoker invoker)
 
348
      {
 
349
 
 
350
      }
 
351
 
 
352
   }
 
353
   
 
354
   static private void setSystemProperty(final String name, final String value)
 
355
   {
 
356
      if (SecurityUtility.skipAccessControl())
 
357
      {
 
358
         System.setProperty(name, value);
 
359
         return;
 
360
      }
 
361
      
 
362
      try
 
363
      {
 
364
         AccessController.doPrivileged( new PrivilegedExceptionAction()
 
365
         {
 
366
            public Object run() throws Exception
 
367
            {
 
368
               return System.setProperty(name, value);
 
369
            }
 
370
         });
 
371
      }
 
372
      catch (PrivilegedActionException e)
 
373
      {
 
374
         throw (RuntimeException) e.getCause();
 
375
      }
 
376
   }
 
377
}
 
378