1 7 package org.jboss.remoting.detection.util; 8 9 import javax.management.MBeanServer ; 10 import javax.management.MBeanServerFactory ; 11 import javax.management.ObjectName ; 12 import org.apache.log4j.Level; 13 import org.jboss.logging.Logger; 14 import org.jboss.remoting.InvokerLocator; 15 import org.jboss.remoting.detection.Detector; 16 import org.jboss.remoting.detection.jndi.JNDIDetector; 17 import org.jboss.remoting.detection.multicast.MulticastDetector; 18 import org.jboss.remoting.network.NetworkInstance; 19 import org.jboss.remoting.network.NetworkRegistry; 20 import org.jboss.remoting.transport.Connector; 21 22 23 31 public class DetectorUtil 32 { 33 36 public static final String TYPE_MULTICAST = "multicast"; 37 40 public static final String TYPE_JNDI = "jndi"; 41 44 public static final int DEFAULT_PORT = 2410; 45 48 public static final String DEFAULT_HOST = "localhost"; 49 50 private String contextFactory = "org.jnp.interfaces.NamingContextFactory"; 51 private String urlPackage = "org.jboss.naming:org.jnp.interfaces"; 52 53 private String type = TYPE_MULTICAST; 54 private int port = DEFAULT_PORT; 55 private String host = DEFAULT_HOST; 56 57 public DetectorUtil() 58 { 59 } 60 61 public DetectorUtil(String type) 62 { 63 this(type, DEFAULT_PORT, DEFAULT_HOST); 64 } 65 66 public DetectorUtil(String type, int port) 67 { 68 this(type, port, DEFAULT_HOST); 69 } 70 71 public DetectorUtil(String type, int port, String host) 72 { 73 this.type = type; 74 this.port = port; 75 this.host = host; 76 } 77 78 public void start() 79 { 80 try 81 { 82 83 org.apache.log4j.BasicConfigurator.configure(); 84 org.apache.log4j.Category.getRoot().setLevel(Level.DEBUG); 85 Logger log = Logger.getLogger(getClass()); 86 87 System.setProperty("jboss.identity", String.valueOf(System.currentTimeMillis())); 88 89 MBeanServer server = MBeanServerFactory.createMBeanServer(); 90 91 NetworkRegistry registry = NetworkRegistry.getInstance(); 92 server.registerMBean(registry, new ObjectName ("remoting:type=NetworkRegistry")); 93 94 InvokerLocator locator = new InvokerLocator("socket://localhost"); 95 101 Connector connector = new Connector(); 102 connector.setInvokerLocator(locator.getLocatorURI()); 103 ObjectName obj = new ObjectName ("jboss.remoting:type=Connector,transport=" + locator.getProtocol()); 104 server.registerMBean(connector, obj); 105 connector.start(); 107 108 Detector detector = null; 109 ObjectName objName = null; 110 111 if(type.equals(TYPE_MULTICAST)) 112 { 113 MulticastDetector mdet = new MulticastDetector(); 114 mdet.setPort(port); 115 detector = mdet; 116 objName = new ObjectName ("remoting:type=Detector,transport=multicast"); 117 } 118 else if(type.equals(TYPE_JNDI)) 119 { 120 JNDIDetector jdet = new JNDIDetector(); 121 jdet.setPort(port); 122 jdet.setHost(host); 123 jdet.setContextFactory(contextFactory); 124 jdet.setURLPackage(urlPackage); 125 detector = jdet; 126 objName = new ObjectName ("remoting:type=Detector,transport=jndi"); 127 } 128 129 server.registerMBean(detector, objName); 130 detector.start(); 131 System.err.println("Starting Detector"); 132 133 while(true) 134 { 135 Thread.currentThread().sleep(3000); 136 NetworkInstance[] instances = registry.getServers(); 137 for(int x = 0; x < instances.length; x++) 138 { 139 log.debug(instances[x]); 140 } 141 } 142 143 144 } 145 catch(Exception ex) 146 { 147 System.err.println("Error creating and starting DetectorUtil."); 148 ex.printStackTrace(); 149 } 150 } 151 152 public static void main(String [] args) 153 { 154 DetectorUtil test = null; 155 156 if(args.length == 1) 157 { 158 String type = args[0]; 159 test = new DetectorUtil(type); 160 } 161 else if(args.length == 2) 162 { 163 String type = args[0]; 164 int port = Integer.parseInt(args[1]); 165 test = new DetectorUtil(type, port); 166 } 167 else if(args.length == 3) 168 { 169 String type = args[0]; 170 int port = Integer.parseInt(args[1]); 171 String host = args[2]; 172 test = new DetectorUtil(type, port, host); 173 } 174 else 175 { 176 test = new DetectorUtil(); 177 System.out.println("Using default type (" + test.getTransport() + 178 ") and default port (" + test.getPort() + ") and " + 179 "default host (" + test.getHost() + ")" + 180 "\nCan enter type (multicast, jndi), port, and/or host via command line."); 181 } 182 183 test.start(); 184 185 186 } 187 188 private String getHost() 189 { 190 return host; 191 } 192 193 private int getPort() 194 { 195 return port; 196 } 197 198 private String getTransport() 199 { 200 return type; 201 } 202 203 204 } 205 | Popular Tags |