1 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 ; 15 16 import javax.management.MBeanServer ; 17 import javax.management.MBeanServerFactory ; 18 import javax.management.ObjectName ; 19 import javax.xml.parsers.DocumentBuilderFactory ; 20 import java.io.ByteArrayInputStream ; 21 import java.util.Random ; 22 23 28 public class JNDIDetectorTest2 extends DistributedTestCase 29 { 30 private int detectorPort = 1099; 32 private String detectorHost = "localhost"; 33 private String contextFactory = "org.jnp.interfaces.NamingContextFactory"; 34 private String urlPackage = "org.jboss.naming:org.jnp.interfaces"; 35 36 public JNDIDetectorTest2(String name) 37 { 38 super(name); 39 } 40 41 public JNDIDetectorTest2(String name, int port, String host) 42 { 43 super(name); 44 this.detectorPort = port; 45 this.detectorHost = host; 46 } 47 48 public JNDIDetectorTest2(int instances, int port, String 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 getDetectorHost() 68 { 69 return detectorHost; 70 } 71 72 public void setDetectorHost(String 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 Thread.currentThread().sleep(8000); 93 94 connector.stop(); 96 connector = null; 98 detector.stop(); 99 detector = null; 100 101 Thread.currentThread().sleep(20000); 102 103 shutdown(); 104 105 } 106 catch(Exception 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 server = MBeanServerFactory.createMBeanServer(); 122 123 registry = TestNetworkRegistry.createNetworkRegistry(); 125 server.registerMBean(registry, new ObjectName ("remoting:type=NetworkRegistry")); 126 127 int port = Math.abs(new Random ().nextInt(2000)); 128 System.out.println("port = " + port); 129 130 InvokerLocator locator = new InvokerLocator("socket://localhost:" + port); 131 StringBuffer buf = new StringBuffer (); 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 xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream (buf.toString().getBytes())); 137 connector.setInvokerLocator(locator.getLocatorURI()); 138 connector.setConfiguration(xml.getDocumentElement()); 139 ObjectName obj = new ObjectName ("jboss.remoting:type=Connector,transport=" + locator.getProtocol()); 140 server.registerMBean(connector, obj); 141 connector.start(); 143 144 server.registerMBean(detector, new ObjectName ("remoting:type=JNDIDetector")); 146 147 detector.setPort(detectorPort); 149 detector.setHost(detectorHost); 150 detector.setContextFactory(contextFactory); 151 detector.setURLPackage(urlPackage); 152 detector.start(); 153 } 154 catch(Exception e) 155 { 156 e.printStackTrace(); 157 } 158 159 return registry; 160 } 161 162 public static void main(String [] 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 sPort = args[0]; 171 int port = Integer.parseInt(sPort); 172 String host = args[1]; 173 test = new JNDIDetectorTest2("JNDIDetectorTest2", port, host); 174 } 175 else if(args.length == 3) 176 { 177 String sInstances = args[0]; 178 int instances = Integer.parseInt(sInstances); 179 String sPort = args[1]; 180 int port = Integer.parseInt(sPort); 181 String 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 |