KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > detection > util > DetectorUtil


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.util;
8
9 import javax.management.MBeanServer JavaDoc;
10 import javax.management.MBeanServerFactory JavaDoc;
11 import javax.management.ObjectName JavaDoc;
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 /**
24  * A simple utility class that will periodically print out remoting servers
25  * running based on detections. Can specify to either use multicast or jndi
26  * detection type.
27  *
28  * @author <a HREF="mailto:telrod@vocalocity.net">Tom Elrod</a>
29  * @version $Revision: 1.2 $
30  */

31 public class DetectorUtil
32 {
33    /**
34     * multicast *
35     */

36    public static final String JavaDoc TYPE_MULTICAST = "multicast";
37    /**
38     * jndi *
39     */

40    public static final String JavaDoc TYPE_JNDI = "jndi";
41    /**
42     * 2410 *
43     */

44    public static final int DEFAULT_PORT = 2410;
45    /**
46     * localhost *
47     */

48    public static final String JavaDoc DEFAULT_HOST = "localhost";
49
50    private String JavaDoc contextFactory = "org.jnp.interfaces.NamingContextFactory";
51    private String JavaDoc urlPackage = "org.jboss.naming:org.jnp.interfaces";
52
53    private String JavaDoc type = TYPE_MULTICAST;
54    private int port = DEFAULT_PORT;
55    private String JavaDoc host = DEFAULT_HOST;
56
57    public DetectorUtil()
58    {
59    }
60
61    public DetectorUtil(String JavaDoc type)
62    {
63       this(type, DEFAULT_PORT, DEFAULT_HOST);
64    }
65
66    public DetectorUtil(String JavaDoc type, int port)
67    {
68       this(type, port, DEFAULT_HOST);
69    }
70
71    public DetectorUtil(String JavaDoc type, int port, String JavaDoc 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 JavaDoc server = MBeanServerFactory.createMBeanServer();
90
91          NetworkRegistry registry = NetworkRegistry.getInstance();
92          server.registerMBean(registry, new ObjectName JavaDoc("remoting:type=NetworkRegistry"));
93
94          InvokerLocator locator = new InvokerLocator("socket://localhost");
95          //ClassLoader clsLoader = Thread.currentThread().getContextClassLoader();
96
//SocketServerInvoker invokerSvr = new SocketServerInvoker(clsLoader, locator);
97
//ObjectName objName = new ObjectName("jboss.remoting:type=SocketServerInvoker");
98
//server.registerMBean(invokerSvr, objName);
99
//invokerSvr.start();
100

101          Connector connector = new Connector();
102          connector.setInvokerLocator(locator.getLocatorURI());
103          ObjectName JavaDoc obj = new ObjectName JavaDoc("jboss.remoting:type=Connector,transport=" + locator.getProtocol());
104          server.registerMBean(connector, obj);
105          //connector.create();
106
connector.start();
107
108          Detector detector = null;
109          ObjectName JavaDoc 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 JavaDoc("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 JavaDoc("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 JavaDoc ex)
146       {
147          System.err.println("Error creating and starting DetectorUtil.");
148          ex.printStackTrace();
149       }
150    }
151
152    public static void main(String JavaDoc[] args)
153    {
154       DetectorUtil test = null;
155
156       if(args.length == 1)
157       {
158          String JavaDoc type = args[0];
159          test = new DetectorUtil(type);
160       }
161       else if(args.length == 2)
162       {
163          String JavaDoc 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 JavaDoc type = args[0];
170          int port = Integer.parseInt(args[1]);
171          String JavaDoc 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 JavaDoc getHost()
189    {
190       return host;
191    }
192
193    private int getPort()
194    {
195       return port;
196    }
197
198    private String JavaDoc getTransport()
199    {
200       return type;
201    }
202
203
204 }
205
Popular Tags