KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > detection > jndi > JNDIDetectorTest2


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.remoting.detection.jndi;
8
9 import org.apache.log4j.Level;
10 import org.jboss.dtf.DistributedTestCase;
11 import org.jboss.remoting.InvokerLocator;
12 import org.jboss.remoting.network.NetworkRegistry;
13 import org.jboss.remoting.transport.Connector;
14 import org.w3c.dom.Document JavaDoc;
15
16 import javax.management.MBeanServer JavaDoc;
17 import javax.management.MBeanServerFactory JavaDoc;
18 import javax.management.ObjectName JavaDoc;
19 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
20 import java.io.ByteArrayInputStream JavaDoc;
21 import java.util.Random JavaDoc;
22
23 /**
24  * Just tests that detector A sees detector B when B comes online then off.
25  *
26  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
27  */

28 public class JNDIDetectorTest2 extends DistributedTestCase
29 {
30    // Setup for JNDI Server
31
private int detectorPort = 1099;
32    private String JavaDoc detectorHost = "localhost";
33    private String JavaDoc contextFactory = "org.jnp.interfaces.NamingContextFactory";
34    private String JavaDoc urlPackage = "org.jboss.naming:org.jnp.interfaces";
35
36    public JNDIDetectorTest2(String JavaDoc name)
37    {
38       super(name);
39    }
40
41    public JNDIDetectorTest2(String JavaDoc name, int port, String JavaDoc host)
42    {
43       super(name);
44       this.detectorPort = port;
45       this.detectorHost = host;
46    }
47
48    public JNDIDetectorTest2(int instances, int port, String JavaDoc host)
49    {
50       super("JNDIDetectorTest2");
51       this.detectorPort = port;
52       this.detectorHost = host;
53       init(instances);
54    }
55
56
57    public int getDetectorPort()
58    {
59       return detectorPort;
60    }
61
62    public void setDetectorPort(int detectorPort)
63    {
64       this.detectorPort = detectorPort;
65    }
66
67    public String JavaDoc getDetectorHost()
68    {
69       return detectorHost;
70    }
71
72    public void setDetectorHost(String JavaDoc detectorHost)
73    {
74       this.detectorHost = detectorHost;
75    }
76
77    public void testDetectors()
78    {
79       try
80       {
81
82          JNDIDetector detector = new JNDIDetector();
83          detector.setCleanDetectionNumber(2);
84
85          Connector connector = new Connector();
86
87          NetworkRegistry reg = setupServers(detector, connector);
88
89          startup();
90
91          // sleep for a few seconds so the 1st detector can discover 2nd one down
92
Thread.currentThread().sleep(8000);
93
94          // stop the 2nd detector, so see if 1st one detects it is missing
95
connector.stop();
96          //connector.destroy();
97
connector = null;
98          detector.stop();
99          detector = null;
100
101          Thread.currentThread().sleep(20000);
102
103          shutdown();
104
105       }
106       catch(Exception JavaDoc e)
107       {
108          e.printStackTrace();
109       }
110    }
111
112
113    private synchronized NetworkRegistry setupServers(JNDIDetector detector, Connector connector)
114    {
115       NetworkRegistry registry = null;
116       System.setProperty("jboss.identity", String.valueOf(System.currentTimeMillis()));
117       System.out.println("jboss.identity = " + System.getProperty("jboss.identity"));
118
119       try
120       {
121          MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
122
123          //registry = NetworkRegistry.getInstance();
124
registry = TestNetworkRegistry.createNetworkRegistry();
125          server.registerMBean(registry, new ObjectName JavaDoc("remoting:type=NetworkRegistry"));
126
127          int port = Math.abs(new Random JavaDoc().nextInt(2000));
128          System.out.println("port = " + port);
129
130          InvokerLocator locator = new InvokerLocator("socket://localhost:" + port);
131          StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
132          buf.append("<?xml version=\"1.0\"?>\n");
133          buf.append("<handlers>\n");
134          buf.append(" <handler subsystem=\"mock\">org.jboss.remoting.transport.mock.MockServerInvocationHandler</handler>\n");
135          buf.append("</handlers>\n");
136          Document JavaDoc xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream JavaDoc(buf.toString().getBytes()));
137          connector.setInvokerLocator(locator.getLocatorURI());
138          connector.setConfiguration(xml.getDocumentElement());
139          ObjectName JavaDoc obj = new ObjectName JavaDoc("jboss.remoting:type=Connector,transport=" + locator.getProtocol());
140          server.registerMBean(connector, obj);
141          //connector.create();
142
connector.start();
143
144          //Need to set new domain for identity
145
server.registerMBean(detector, new ObjectName JavaDoc("remoting:type=JNDIDetector"));
146
147          // set config info for detector and start it.
148
detector.setPort(detectorPort);
149          detector.setHost(detectorHost);
150          detector.setContextFactory(contextFactory);
151          detector.setURLPackage(urlPackage);
152          detector.start();
153       }
154       catch(Exception JavaDoc e)
155       {
156          e.printStackTrace();
157       }
158
159       return registry;
160    }
161
162    public static void main(String JavaDoc[] args)
163    {
164       org.apache.log4j.BasicConfigurator.configure();
165       org.apache.log4j.Category.getRoot().setLevel(Level.DEBUG);
166
167       JNDIDetectorTest2 test = null;
168       if(args.length == 2)
169       {
170          String JavaDoc sPort = args[0];
171          int port = Integer.parseInt(sPort);
172          String JavaDoc host = args[1];
173          test = new JNDIDetectorTest2("JNDIDetectorTest2", port, host);
174       }
175       else if(args.length == 3)
176       {
177          String JavaDoc sInstances = args[0];
178          int instances = Integer.parseInt(sInstances);
179          String JavaDoc sPort = args[1];
180          int port = Integer.parseInt(sPort);
181          String JavaDoc host = args[2];
182          test = new JNDIDetectorTest2(instances, port, host);
183       }
184       else
185       {
186          test = new JNDIDetectorTest2("JNDIDetectorTest2");
187       }
188       test.testDetectors();
189
190       System.err.println("JNDIDetectorTest2 DONE");
191
192       System.exit(0);
193
194    }
195
196    public static class TestNetworkRegistry extends NetworkRegistry
197    {
198       public static NetworkRegistry createNetworkRegistry()
199       {
200          return new TestNetworkRegistry();
201       }
202    }
203
204 }
205
Popular Tags