~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/RestartTestServer.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;
 
23
 
 
24
import org.jboss.jrunit.extensions.ServerTestCase;
 
25
import org.jboss.logging.Logger;
 
26
import org.jboss.remoting.InvocationRequest;
 
27
import org.jboss.remoting.InvokerLocator;
 
28
import org.jboss.remoting.ServerInvocationHandler;
 
29
import org.jboss.remoting.ServerInvoker;
 
30
import org.jboss.remoting.callback.InvokerCallbackHandler;
 
31
import org.jboss.remoting.detection.jndi.JNDIDetector;
 
32
import org.jboss.remoting.samples.detection.jndi.SimpleJNDIServer;
 
33
import org.jboss.remoting.transport.Connector;
 
34
import org.jboss.remoting.util.SecurityUtility;
 
35
import org.jnp.server.Main;
 
36
 
 
37
import javax.management.MBeanServer;
 
38
import javax.management.MBeanServerFactory;
 
39
import javax.management.ObjectName;
 
40
 
 
41
import java.io.InputStream;
 
42
import java.io.OutputStream;
 
43
import java.lang.reflect.Method;
 
44
import java.net.InetAddress;
 
45
import java.net.ServerSocket;
 
46
import java.net.Socket;
 
47
import java.security.AccessController;
 
48
import java.security.PrivilegedActionException;
 
49
import java.security.PrivilegedExceptionAction;
 
50
import java.util.HashMap;
 
51
import java.util.Map;
 
52
 
 
53
/**
 
54
 * @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
 
55
 * @author <a href="mailto:mazz@jboss.com">John Mazzitelli</a>
 
56
 * @author <a href="mailto:ron.sigal@jboss.com">Ron Sigal</a>
 
57
 */
 
58
public class RestartTestServer extends ServerTestCase
 
59
{
 
60
   public static int syncPort = 6402;
 
61
   
 
62
   protected static Logger log = Logger.getLogger(RestartTestServer.class);
 
63
   protected static String transport;
 
64
   protected static String host;
 
65
   protected static int port = 5402;
 
66
   protected static Thread serverThread;
 
67
   
 
68
   protected int detectorPort = 1099;
 
69
   protected String contextFactory = "org.jnp.interfaces.NamingContextFactory";
 
70
   protected String urlPackage = "org.jboss.naming:org.jnp.interfaces";
 
71
   protected Main jserver;
 
72
   protected JNDIDetector detector;
 
73
   protected Connector connector;
 
74
 
 
75
   public void setUp() throws Exception
 
76
   {
 
77
      host = InetAddress.getLocalHost().getHostName();
 
78
      final String locatorURI = getTransport() + "://" + host + ":" + port;
 
79
      log.info("This server's endpoint will be: " + locatorURI);
 
80
      
 
81
      serverThread = new Thread()
 
82
      {
 
83
         public void run()
 
84
         {
 
85
            try
 
86
            {
 
87
               setupJNDI();
 
88
               setupDetector();
 
89
               setupServer(locatorURI);
 
90
               ServerSocket ss = new ServerSocket(syncPort, 0, InetAddress.getLocalHost());
 
91
               log.info("bound to: " + InetAddress.getLocalHost());
 
92
               Socket s = ss.accept();
 
93
               InputStream is = s.getInputStream();
 
94
               OutputStream os = s.getOutputStream();
 
95
               
 
96
               // Indicate server is started.
 
97
               os.write(3);
 
98
               log.info("indicated server started");
 
99
               
 
100
               is.read();
 
101
               log.info("got request to shut down server");
 
102
               shutdownServer(locatorURI);
 
103
               log.info("shut down server");
 
104
               
 
105
               is.read();
 
106
               log.info("got request to restart server");
 
107
               setupServer(locatorURI);
 
108
               os.write(7);
 
109
               log.info("restarted server");
 
110
               
 
111
               is.read();
 
112
               log.info("got request to shut down server");
 
113
               shutdownServer(locatorURI);
 
114
               shutdownDetector();
 
115
               shutdownJNDI();
 
116
               log.info("shut down server");
 
117
            }
 
118
            catch (Exception e)
 
119
            {
 
120
               log.error(e);
 
121
               e.printStackTrace();
 
122
            }
 
123
         }
 
124
      };
 
125
      
 
126
      serverThread.start();
 
127
   }
 
128
   
 
129
   protected void setupJNDI() throws Exception
 
130
   {
 
131
      Object namingBean = null;
 
132
      Class namingBeanImplClass = null;
 
133
      try
 
134
      {
 
135
         namingBeanImplClass = Class.forName("org.jnp.server.NamingBeanImpl");
 
136
         namingBean = namingBeanImplClass.newInstance();
 
137
         Method startMethod = namingBeanImplClass.getMethod("start", new Class[] {});
 
138
         setSystemProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
 
139
         startMethod.invoke(namingBean, new Object[] {});
 
140
      }
 
141
      catch (Exception e)
 
142
      {
 
143
         SimpleJNDIServer.println("Cannot find NamingBeanImpl: must be running jdk 1.4");
 
144
      };
 
145
      
 
146
      String host = InetAddress.getLocalHost().getHostAddress();
 
147
 
 
148
      jserver = new Main();
 
149
      if (namingBean != null)
 
150
      {
 
151
         Class namingBeanClass = Class.forName("org.jnp.server.NamingBean");
 
152
         Method setNamingInfoMethod = jserver.getClass().getMethod("setNamingInfo", new Class[] {namingBeanClass});
 
153
         setNamingInfoMethod.invoke(jserver, new Object[] {namingBean});
 
154
      }
 
155
      jserver.setPort(detectorPort);
 
156
      jserver.setBindAddress(host);
 
157
      jserver.setRmiPort(31000);
 
158
      jserver.start();
 
159
      System.out.println("Started JNDI server on " + host + ":" + port);
 
160
   }
 
161
   
 
162
   protected void shutdownJNDI()
 
163
   {
 
164
      jserver.stop();
 
165
   }
 
166
 
 
167
   protected void setupDetector() throws Exception
 
168
   {
 
169
      // we need an MBeanServer to store our network registry and multicast detector services
 
170
      MBeanServer server = MBeanServerFactory.createMBeanServer();
 
171
 
 
172
      String detectorHost = InetAddress.getLocalHost().getHostName();
 
173
 
 
174
      detector = new JNDIDetector(getConfiguration());
 
175
      // set config info for detector and start it.
 
176
      detector.setPort(detectorPort);
 
177
      detector.setHost(detectorHost);
 
178
      detector.setContextFactory(contextFactory);
 
179
      detector.setURLPackage(urlPackage);
 
180
 
 
181
      server.registerMBean(detector, new ObjectName("remoting:type=JNDIDetector"));
 
182
      detector.start();
 
183
      log.info("JNDIDetector has been created and is listening for new NetworkRegistries to come online");
 
184
   }
 
185
   
 
186
   protected void shutdownDetector() throws Exception
 
187
   {
 
188
      detector.stop();
 
189
   }
 
190
 
 
191
   /**
 
192
    * Sets up our JBoss/Remoting server by creating our Connector on the given locator URI
 
193
    * and installing our invocation handler that will handle incoming messages.
 
194
    *
 
195
    * @param locatorURI defines our server endpoing
 
196
    * @throws Exception
 
197
    */
 
198
   protected void setupServer(String locatorURI) throws Exception
 
199
   {
 
200
      InvokerLocator locator = new InvokerLocator(locatorURI);
 
201
      log.info("Starting remoting server with locator uri of: " + locatorURI);
 
202
      connector = new Connector(locator, getConfiguration());
 
203
      connector.create();
 
204
      SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
 
205
      connector.addInvocationHandler("sample", invocationHandler);
 
206
      connector.start();
 
207
   }
 
208
   
 
209
   
 
210
   protected void shutdownServer(String locatorURI)
 
211
   {
 
212
      connector.stop();
 
213
      log.info("Stopped remoting server with locator uri of: " + locatorURI);
 
214
   }
 
215
 
 
216
   
 
217
   protected String getTransport()
 
218
   {
 
219
      return "socket";
 
220
   }
 
221
   
 
222
   
 
223
   /**
 
224
    * @return configuration map for Connector and JNDIDetector
 
225
    */
 
226
   protected Map getConfiguration()
 
227
   {
 
228
      return new HashMap();
 
229
   }
 
230
   
 
231
   
 
232
   public static void main(String[] args)
 
233
   {
 
234
      try
 
235
      {
 
236
         RestartTestServer server = new RestartTestServer();
 
237
         server.setUp();
 
238
         serverThread.join();
 
239
      }
 
240
      catch (Exception e)
 
241
      {
 
242
         log.error(e);
 
243
         e.printStackTrace();
 
244
      }
 
245
   }
 
246
   
 
247
 
 
248
   /**
 
249
    * Simple invocation handler implementation.  This is the handler that processes incoming messages from clients.
 
250
    */
 
251
   public static class SampleInvocationHandler implements ServerInvocationHandler
 
252
   {
 
253
      /**
 
254
       * This is the method that is called when a new message comes in from a client.
 
255
       *
 
256
       * @param invocation the incoming client invocation, encapsulates the message object
 
257
       * @return the response object we send back to the client.
 
258
       * @throws Throwable
 
259
       */
 
260
      public Object invoke(InvocationRequest invocation) throws Throwable
 
261
      {
 
262
         // Print out the invocation request
 
263
         String msg = invocation.getParameter().toString();
 
264
 
 
265
         log.info("RECEIVED A CLIENT MESSAGE: " + msg);
 
266
 
 
267
         String response = "Server received your message that said [" + msg + "]";
 
268
 
 
269
         if(msg.indexOf("Welcome") > -1)
 
270
         {
 
271
            response = "Received your welcome message.  Thank you!";
 
272
         }
 
273
 
 
274
         log.info("Returning the following message back to the client: " + response);
 
275
 
 
276
         return response;
 
277
      }
 
278
 
 
279
      /**
 
280
       * Adds a callback handler that will listen for callbacks from the server invoker handler.
 
281
       *
 
282
       * @param callbackHandler
 
283
       */
 
284
      public void addListener(InvokerCallbackHandler callbackHandler)
 
285
      {
 
286
         // NO OP as we do not handle callback listeners in this example
 
287
      }
 
288
 
 
289
      /**
 
290
       * Removes the callback handler that was listening for callbacks from the server invoker handler.
 
291
       *
 
292
       * @param callbackHandler
 
293
       */
 
294
      public void removeListener(InvokerCallbackHandler callbackHandler)
 
295
      {
 
296
         // NO OP as we do not handle callback listeners in this example
 
297
      }
 
298
 
 
299
      /**
 
300
       * set the mbean server that the handler can reference
 
301
       *
 
302
       * @param server
 
303
       */
 
304
      public void setMBeanServer(MBeanServer server)
 
305
      {
 
306
         // NO OP as we do not need a reference to the MBeanServer for this handler
 
307
      }
 
308
 
 
309
      /**
 
310
       * set the invoker that owns this handler
 
311
       *
 
312
       * @param invoker
 
313
       */
 
314
      public void setInvoker(ServerInvoker invoker)
 
315
      {
 
316
         // NO OP as we do not need a reference back to the server invoker
 
317
      }
 
318
   }
 
319
   
 
320
   static private void setSystemProperty(final String name, final String value)
 
321
   {
 
322
      if (SecurityUtility.skipAccessControl())
 
323
      {
 
324
         System.setProperty(name, value);
 
325
         return;
 
326
      }
 
327
      
 
328
      try
 
329
      {
 
330
         AccessController.doPrivileged( new PrivilegedExceptionAction()
 
331
         {
 
332
            public Object run() throws Exception
 
333
            {
 
334
               return System.setProperty(name, value);
 
335
            }
 
336
         });
 
337
      }
 
338
      catch (PrivilegedActionException e)
 
339
      {
 
340
         throw (RuntimeException) e.getCause();
 
341
      }
 
342
   }
 
343
}