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.
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.
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.
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.
22
package org.jboss.test.remoting.detection.jndi;
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;
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;
37
import java.io.InputStream;
38
import java.io.OutputStream;
39
import java.net.InetAddress;
40
import java.net.Socket;
42
import java.util.HashMap;
44
import junit.framework.TestCase;
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>
51
public class RestartTestClient extends TestCase implements NotificationListener
53
private static Logger log = Logger.getLogger(RestartTestClient.class);
55
private int detectorPort = 1099;
56
private String contextFactory = "org.jnp.interfaces.NamingContextFactory";
57
private String urlPackage = "org.jboss.naming:org.jnp.interfaces";
59
protected JNDIDetector detector;
60
protected int serversDetected;
61
protected boolean invocationSucceeded;
62
protected Object lock = new Object();
63
protected boolean notified;
67
* Sets up NetworkRegistry and JNDIDetector so we can listen for any additions
68
* or removals of remoting servers on the network.
72
public void setUp() throws Exception
74
// we need an MBeanServer to store our network registry and jndi detector services
75
MBeanServer server = MBeanServerFactory.createMBeanServer();
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");
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");
86
String detectorHost = InetAddress.getLocalHost().getHostName();
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);
96
server.registerMBean(detector, new ObjectName("remoting:type=JNDIDetector"));
98
for (int i = 0; i < 5; i++)
103
log.info("JNDIDetector has been created and is listening for new NetworkRegistries to come online");
108
log.info("unable to connect to JDNI: will try again");
115
public void tearDown() throws Exception
120
public void testDetections()
124
String host = InetAddress.getLocalHost().getHostName();
127
for (int i = 0; i < 5; i++)
131
s = new Socket(host, RestartTestServer.syncPort);
136
log.info("Unable to connect to " + host + ":" + RestartTestServer.syncPort);
137
log.info("Will try again");
142
catch (InterruptedException ignored) {}
145
InputStream is = s.getInputStream();
146
OutputStream os = s.getOutputStream();
148
// Wait until server has been started.
151
assertEquals(1, serversDetected);
152
assertTrue(invocationSucceeded);
153
log.info("PASSED first detection test");
154
invocationSucceeded = false;
156
// Tell server to shut down.
159
assertEquals(0, serversDetected);
160
log.info("PASSED second detection test");
162
// Tell server to restart.
165
assertEquals(1, serversDetected);
166
assertTrue(invocationSucceeded);
167
log.info("PASSED third detection test");
169
// Tell server test is over.
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.
184
* @param notification the notification object
185
* @param handback the handback object given to the broadcaster upon listener registration
187
public void handleNotification(Notification notification, Object handback)
191
// check to see if network notification
192
if(notification instanceof NetworkNotification)
194
log.info("GOT A NETWORK-REGISTRY NOTIFICATION: " + notification.getType());
196
NetworkNotification networkNotification = (NetworkNotification) notification;
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++)
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;
215
catch(Throwable throwable)
217
throwable.printStackTrace();
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++)
227
log.info("It has been detected that a server has gone down with a locator of: " + locators[x]);
241
protected void waitOnDetection() throws InterruptedException
260
protected void notifyDetection()
270
* Make call on remoting server based on locator uri provided.
272
* @param locatorURI the URI of the remote server we want to send the message to
275
public void makeInvocation(String locatorURI) throws Throwable
277
InvokerLocator locator = new InvokerLocator(locatorURI);
278
log.info("Sending welcome message to remoting server with locator uri of: " + locatorURI);
280
Client remotingClient = new Client(locator, getConfiguration());
281
remotingClient.connect();
282
Object response = remotingClient.invoke("Welcome Aboard!", null);
284
log.info("The newly discovered server sent this response to our welcome message: " + response);
286
remotingClient.disconnect();
292
* @return configuration map for JNDIDetector
294
protected Map getConfiguration()
296
return new HashMap();