~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/RestartTestClient.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.logging.Logger;
 
25
import org.jboss.remoting.Client;
 
26
import org.jboss.remoting.InvokerLocator;
 
27
import org.jboss.remoting.detection.jndi.JNDIDetector;
 
28
import org.jboss.remoting.network.NetworkNotification;
 
29
import org.jboss.remoting.network.NetworkRegistry;
 
30
 
 
31
import javax.management.MBeanServer;
 
32
import javax.management.MBeanServerFactory;
 
33
import javax.management.Notification;
 
34
import javax.management.NotificationListener;
 
35
import javax.management.ObjectName;
 
36
 
 
37
import java.io.InputStream;
 
38
import java.io.OutputStream;
 
39
import java.net.InetAddress;
 
40
import java.net.Socket;
 
41
import java.util.Map;
 
42
import java.util.HashMap;
 
43
 
 
44
import junit.framework.TestCase;
 
45
 
 
46
/**
 
47
 * @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
 
48
 * @author <a href="mailto:mazz@jboss.com">John Mazzitelli</a>
 
49
 * @author <a href="mailto:ron.sigal@jboss.com">Ron Sigal</a>
 
50
 */
 
51
public class RestartTestClient extends TestCase implements NotificationListener
 
52
{
 
53
   private static Logger log = Logger.getLogger(RestartTestClient.class);
 
54
   
 
55
   private int detectorPort = 1099;
 
56
   private String contextFactory = "org.jnp.interfaces.NamingContextFactory";
 
57
   private String urlPackage = "org.jboss.naming:org.jnp.interfaces";
 
58
   
 
59
   protected JNDIDetector detector;
 
60
   protected int serversDetected;
 
61
   protected boolean invocationSucceeded;
 
62
   protected Object lock = new Object();
 
63
   protected boolean notified;
 
64
 
 
65
 
 
66
   /**
 
67
    * Sets up NetworkRegistry and JNDIDetector so we can listen for any additions
 
68
    * or removals of remoting servers on the network.
 
69
    *
 
70
    * @throws Exception
 
71
    */
 
72
   public void setUp() throws Exception
 
73
   {
 
74
      // we need an MBeanServer to store our network registry and jndi detector services
 
75
      MBeanServer server = MBeanServerFactory.createMBeanServer();
 
76
 
 
77
      // the registry will house all remoting servers discovered
 
78
      NetworkRegistry registry = NetworkRegistry.getInstance();
 
79
      server.registerMBean(registry, new ObjectName("remoting:type=NetworkRegistry"));
 
80
      log.info("NetworkRegistry has been created");
 
81
 
 
82
      // register class as listener, so know when new server found
 
83
      registry.addNotificationListener(this, null, null);
 
84
      log.info("NetworkRegistry has added the client as a listener");
 
85
 
 
86
      String detectorHost = InetAddress.getLocalHost().getHostName();
 
87
      
 
88
      // jndi detector will detect new network registries that come online
 
89
      detector = new JNDIDetector(getConfiguration());
 
90
      // set config info for detector and start it.
 
91
      detector.setPort(detectorPort);
 
92
      detector.setHost(detectorHost);
 
93
      detector.setContextFactory(contextFactory);
 
94
      detector.setURLPackage(urlPackage);
 
95
 
 
96
      server.registerMBean(detector, new ObjectName("remoting:type=JNDIDetector"));
 
97
 
 
98
      for (int i = 0; i < 5; i++)
 
99
      {
 
100
         try
 
101
         {
 
102
            detector.start();
 
103
            log.info("JNDIDetector has been created and is listening for new NetworkRegistries to come online");
 
104
            break;
 
105
         }
 
106
         catch (Exception e)
 
107
         {
 
108
            log.info("unable to connect to JDNI: will try again");
 
109
            Thread.sleep(2000);
 
110
         }
 
111
      }
 
112
   }
 
113
 
 
114
   
 
115
   public void tearDown() throws Exception
 
116
   {
 
117
      detector.stop();
 
118
   }
 
119
   
 
120
   public void testDetections()
 
121
   {
 
122
      try
 
123
      {
 
124
         String host = InetAddress.getLocalHost().getHostName();
 
125
         
 
126
         Socket s = null;
 
127
         for (int i = 0; i < 5; i++)
 
128
         {
 
129
            try
 
130
            {
 
131
               s = new Socket(host, RestartTestServer.syncPort);
 
132
               break;
 
133
            }
 
134
            catch (Exception e)
 
135
            {
 
136
               log.info("Unable to connect to " + host + ":" + RestartTestServer.syncPort);
 
137
               log.info("Will try again");
 
138
               try
 
139
               {
 
140
                  Thread.sleep(2000);
 
141
               }
 
142
               catch (InterruptedException ignored) {}
 
143
            }
 
144
         }
 
145
         InputStream is = s.getInputStream();
 
146
         OutputStream os = s.getOutputStream();
 
147
         
 
148
         // Wait until server has been started.
 
149
         is.read();
 
150
         waitOnDetection();
 
151
         assertEquals(1, serversDetected);
 
152
         assertTrue(invocationSucceeded);
 
153
         log.info("PASSED first detection test");
 
154
         invocationSucceeded = false;
 
155
         
 
156
         // Tell server to shut down.
 
157
         os.write(5);
 
158
         waitOnDetection();
 
159
         assertEquals(0, serversDetected);
 
160
         log.info("PASSED second detection test");
 
161
         
 
162
         // Tell server to restart.
 
163
         os.write(7);
 
164
         waitOnDetection();
 
165
         assertEquals(1, serversDetected);
 
166
         assertTrue(invocationSucceeded);
 
167
         log.info("PASSED third detection test");
 
168
 
 
169
         // Tell server test is over.
 
170
         os.write(9);
 
171
      }
 
172
      catch (Exception e)
 
173
      {
 
174
         log.error(e);
 
175
         e.printStackTrace();
 
176
         fail();
 
177
      }
 
178
   }
 
179
 
 
180
   /**
 
181
    * Callback method from the broadcaster MBean this listener implementation is registered to. When a new server
 
182
    * is detected, a welcome message will immediately be sent to the newly discovered server.
 
183
    *
 
184
    * @param notification the notification object
 
185
    * @param handback     the handback object given to the broadcaster upon listener registration
 
186
    */
 
187
   public void handleNotification(Notification notification, Object handback)
 
188
   {
 
189
      try
 
190
      {
 
191
         // check to see if network notification
 
192
         if(notification instanceof NetworkNotification)
 
193
         {
 
194
            log.info("GOT A NETWORK-REGISTRY NOTIFICATION: " + notification.getType());
 
195
            
 
196
            NetworkNotification networkNotification = (NetworkNotification) notification;
 
197
            
 
198
            if(NetworkNotification.SERVER_ADDED.equals(networkNotification.getType()))
 
199
            { // notification is for new servers being added
 
200
               log.info("New server(s) have been detected - getting locators and sending welcome messages");
 
201
               InvokerLocator[] locators = networkNotification.getLocator();
 
202
               for(int x = 0; x < locators.length; x++)
 
203
               {
 
204
                  try
 
205
                  {
 
206
                     serversDetected++;
 
207
                     
 
208
                     // get the new found server's locator and invoke a call
 
209
                     InvokerLocator newServerLocator = locators[x];
 
210
                     log.info("detected: " + newServerLocator);
 
211
                     invocationSucceeded = false;
 
212
                     makeInvocation(newServerLocator.getLocatorURI());
 
213
                     invocationSucceeded = true;
 
214
                  }
 
215
                  catch(Throwable throwable)
 
216
                  {
 
217
                     throwable.printStackTrace();
 
218
                  }
 
219
               }
 
220
            }
 
221
            else if(NetworkNotification.SERVER_REMOVED.equals(networkNotification.getType()))
 
222
            { // notification is for old servers that have gone down
 
223
               InvokerLocator[] locators = networkNotification.getLocator();
 
224
               for(int x = 0; x < locators.length; x++)
 
225
               {
 
226
                  serversDetected--;
 
227
                  log.info("It has been detected that a server has gone down with a locator of: " + locators[x]);
 
228
               }
 
229
            }
 
230
         }
 
231
         
 
232
         return;
 
233
      }
 
234
      finally
 
235
      {
 
236
         notifyDetection();
 
237
      }
 
238
   }
 
239
   
 
240
   
 
241
   protected void waitOnDetection() throws InterruptedException
 
242
   {
 
243
      synchronized (lock)
 
244
      {  
 
245
         try
 
246
         {
 
247
            if (notified)
 
248
               return;
 
249
            
 
250
            lock.wait();
 
251
         }
 
252
         finally
 
253
         {
 
254
            notified = false;
 
255
         }
 
256
      }
 
257
   }
 
258
   
 
259
   
 
260
   protected void notifyDetection()
 
261
   {
 
262
      synchronized (lock)
 
263
      {
 
264
         notified = true;
 
265
         lock.notify();
 
266
      }
 
267
   }
 
268
 
 
269
   /**
 
270
    * Make call on remoting server based on locator uri provided.
 
271
    *
 
272
    * @param locatorURI the URI of the remote server we want to send the message to
 
273
    * @throws Throwable
 
274
    */
 
275
   public void makeInvocation(String locatorURI) throws Throwable
 
276
   {
 
277
      InvokerLocator locator = new InvokerLocator(locatorURI);
 
278
      log.info("Sending welcome message to remoting server with locator uri of: " + locatorURI);
 
279
 
 
280
      Client remotingClient = new Client(locator, getConfiguration());
 
281
      remotingClient.connect();
 
282
      Object response = remotingClient.invoke("Welcome Aboard!", null);
 
283
 
 
284
      log.info("The newly discovered server sent this response to our welcome message: " + response);
 
285
 
 
286
      remotingClient.disconnect();
 
287
      return;
 
288
   }
 
289
 
 
290
   
 
291
   /**
 
292
    * @return configuration map for JNDIDetector
 
293
    */
 
294
   protected Map getConfiguration()
 
295
   {
 
296
      return new HashMap();
 
297
   }
 
298
}