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.
23
package org.jboss.test.remoting.detection.metadata;
25
import junit.framework.TestCase;
26
import org.apache.log4j.Level;
27
import org.jboss.logging.XLevel;
28
import org.jboss.remoting.Client;
29
import org.jboss.remoting.InvocationRequest;
30
import org.jboss.remoting.InvokerLocator;
31
import org.jboss.remoting.ServerInvocationHandler;
32
import org.jboss.remoting.ServerInvoker;
33
import org.jboss.remoting.callback.InvokerCallbackHandler;
34
import org.jboss.remoting.detection.ServerInvokerMetadata;
35
import org.jboss.remoting.detection.multicast.MulticastDetector;
36
import org.jboss.remoting.network.NetworkInstance;
37
import org.jboss.remoting.network.NetworkNotification;
38
import org.jboss.remoting.network.NetworkRegistry;
39
import org.jboss.remoting.transport.Connector;
40
import org.jboss.test.remoting.TestUtil;
41
import org.w3c.dom.Document;
43
import javax.management.MBeanServer;
44
import javax.management.MBeanServerFactory;
45
import javax.management.Notification;
46
import javax.management.NotificationListener;
47
import javax.management.ObjectName;
48
import javax.xml.parsers.DocumentBuilderFactory;
49
import java.io.ByteArrayInputStream;
50
import java.net.InetAddress;
51
import java.security.AccessController;
52
import java.security.PrivilegedAction;
53
import java.util.HashSet;
54
import java.util.List;
55
import java.util.Random;
58
* Just tests that detector A sees detector B when B comes online then off.
59
* Also checks to make sure the detection message contains the proper data.
61
* @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
63
public class MetadataTestCase extends TestCase implements NotificationListener
65
private static int secret = Math.abs(new Random().nextInt(2000));
67
private HashSet subSystems = new HashSet();
69
public MetadataTestCase(String name)
74
public void testDetectors() throws Exception
76
org.apache.log4j.BasicConfigurator.configure();
77
org.apache.log4j.Category.getRoot().setLevel(Level.DEBUG);
78
org.apache.log4j.Category.getInstance("org.jboss.remoting").setLevel(XLevel.TRACE);
79
org.apache.log4j.Category.getInstance("org.jgroups").setLevel(Level.FATAL);
81
MulticastDetector detector1 = new MulticastDetector();
82
MulticastDetector detector2 = new MulticastDetector();
84
Connector connector1 = new Connector();
85
Connector connector2 = new Connector();
89
NetworkRegistry reg1 = setupServers(detector1, connector1);
90
// need to register with the mbean server for notifications
91
List mbeanServers = (List) AccessController.doPrivileged( new PrivilegedAction()
95
return MBeanServerFactory.findMBeanServer(null);
99
MBeanServer mbeanSvr = (MBeanServer) mbeanServers.get(0);
100
mbeanSvr.addNotificationListener(new ObjectName("remoting:type=NetworkRegistry"),
103
NetworkRegistry reg2 = setupServers(detector2, connector2);
105
// Need to allow heartbeat so have detection
106
Thread.currentThread().sleep(2000);
108
//Should now have an entry for both of the registries
109
int reg1Count = reg1.getServers().length;
110
int reg2Count = reg2.getServers().length;
111
System.out.println("registry 1: " + reg1Count);
112
System.out.println("registry 2: " + reg2Count);
114
if(reg1Count >= 1 && reg2Count >= 1)
116
System.out.println("PASSED - both registries have found detectors.");
120
System.out.println("FAILED - registries not populated with remote detectors.");
124
assertTrue(reg1Count >= 1 && reg2Count >= 1);
126
// Verify the Connectors created by this test have been detected.
127
checkForConnector(reg1);
128
checkForConnector(reg2);
130
// now check to make sure got the subsystem as expected
131
assertTrue(subSystems.contains("MOCK"));
135
// stop the 2nd detector, so see if 1st one detects it is missing
136
if (connector1 != null)
139
connector1.destroy();
142
if (connector2 != null)
145
connector2.destroy();
148
if (detector1 != null) detector1.stop();
149
if (detector2 != null) detector2.stop();
152
//connector2.destroy();
156
private synchronized NetworkRegistry setupServers(MulticastDetector detector, Connector connector)
158
NetworkRegistry registry = null;
159
System.setProperty("jboss.identity", String.valueOf(System.currentTimeMillis()));
160
System.out.println("jboss.identity = " + System.getProperty("jboss.identity"));
164
MBeanServer server = MBeanServerFactory.createMBeanServer();
166
//registry = NetworkRegistry.getInstance();
167
registry = TestNetworkRegistry.createNetworkRegistry();
168
server.registerMBean(registry, new ObjectName("remoting:type=NetworkRegistry"));
170
//int port = Math.abs(new Random().nextInt(2000));
171
int port = TestUtil.getRandomPort();
172
System.out.println("port = " + port);
174
String host = InetAddress.getLocalHost().getHostAddress();
175
String bindAddr = System.getProperty("jrunit.bind_addr", host);
176
InvokerLocator locator = new InvokerLocator("socket://" + bindAddr + ":" + port);
178
StringBuffer buf = new StringBuffer();
179
buf.append("<?xml version=\"1.0\"?>\n");
180
buf.append("<handlers>\n");
181
buf.append(" <handler subsystem=\"mock\">" + TestInvocationHandler.class.getName() + "</handler>\n");
182
buf.append("</handlers>\n");
183
Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(buf.toString().getBytes()));
184
connector.setInvokerLocator(locator.getLocatorURI());
185
connector.setConfiguration(xml.getDocumentElement());
186
ObjectName obj = new ObjectName("jboss.remoting:type=Connector,transport=" + locator.getProtocol());
187
server.registerMBean(connector, obj);
188
//connector.create();
191
//Need to set new domain for identity
192
server.registerMBean(detector, new ObjectName("remoting:type=JNDIDetector"));
194
// set config info for detector and start it.
205
public void handleNotification(Notification notification, Object o)
207
System.out.println("Received notification: " + notification);
208
if(notification instanceof NetworkNotification)
210
NetworkNotification netNot = (NetworkNotification) notification;
211
ServerInvokerMetadata[] serverMetadata = netNot.getServerInvokers();
212
for (int i = 0; i < serverMetadata.length; i++)
214
String[] ss = serverMetadata[i].getSubSystems();
215
for (int j = 0; j < ss.length; j++)
217
subSystems.add(ss[j]);
223
private boolean checkForConnector(NetworkRegistry registry)
225
boolean found = false;
226
NetworkInstance[] servers1 = registry.getServers();
227
for (int i = 0; i < servers1.length; i++)
229
InvokerLocator[] locators = servers1[i].getLocators();
230
for (int j = 0; j < locators.length; j++)
234
Client client = new Client(locators[j]);
236
if (secret == ((Integer) client.invoke("abc")).intValue())
239
System.out.println("FOUND: " + locators[j]);
253
private static class TestNetworkRegistry extends NetworkRegistry
255
public static NetworkRegistry createNetworkRegistry()
257
return new TestNetworkRegistry();
261
public static class TestInvocationHandler implements ServerInvocationHandler
263
public void addListener(InvokerCallbackHandler callbackHandler) {}
264
public Object invoke(final InvocationRequest invocation) throws Throwable
266
return new Integer(secret);
268
public void removeListener(InvokerCallbackHandler callbackHandler) {}
269
public void setMBeanServer(MBeanServer server) {}
270
public void setInvoker(ServerInvoker invoker) {}