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.
22
package org.jboss.remoting.detection.util;
24
import java.security.AccessController;
25
import java.security.PrivilegedActionException;
26
import java.security.PrivilegedExceptionAction;
28
import javax.management.MBeanServer;
29
import javax.management.MBeanServerFactory;
30
import javax.management.ObjectName;
31
import org.apache.log4j.Level;
32
import org.jboss.logging.Logger;
33
import org.jboss.remoting.InvokerLocator;
34
import org.jboss.remoting.detection.Detector;
35
import org.jboss.remoting.detection.jndi.JNDIDetector;
36
import org.jboss.remoting.detection.multicast.MulticastDetector;
37
import org.jboss.remoting.network.NetworkInstance;
38
import org.jboss.remoting.network.NetworkRegistry;
39
import org.jboss.remoting.transport.Connector;
40
import org.jboss.remoting.util.SecurityUtility;
44
* A simple utility class that will periodically print out remoting servers
45
* running based on detections. Can specify to either use multicast or jndi
48
* @author <a href="mailto:telrod@vocalocity.net">Tom Elrod</a>
49
* @version $Revision: 5001 $
51
public class DetectorUtil
56
public static final String TYPE_MULTICAST = "multicast";
60
public static final String TYPE_JNDI = "jndi";
64
public static final int DEFAULT_PORT = 2410;
68
public static final String DEFAULT_HOST = "localhost";
70
private String contextFactory = "org.jnp.interfaces.NamingContextFactory";
71
private String urlPackage = "org.jboss.naming:org.jnp.interfaces";
73
private String type = TYPE_MULTICAST;
74
private int port = DEFAULT_PORT;
75
private String host = DEFAULT_HOST;
81
public DetectorUtil(String type)
83
this(type, DEFAULT_PORT, DEFAULT_HOST);
86
public DetectorUtil(String type, int port)
88
this(type, port, DEFAULT_HOST);
91
public DetectorUtil(String type, int port, String host)
103
org.apache.log4j.BasicConfigurator.configure();
104
org.apache.log4j.Category.getRoot().setLevel(Level.DEBUG);
105
Logger log = Logger.getLogger(getClass());
107
setSystemProperty("jboss.identity", String.valueOf(System.currentTimeMillis()));
109
MBeanServer server = createMBeanServer();
110
NetworkRegistry registry = NetworkRegistry.getInstance();
111
registerMBean(server, registry, new ObjectName("remoting:type=NetworkRegistry"));
113
InvokerLocator locator = new InvokerLocator("socket://localhost");
114
//ClassLoader clsLoader = Thread.currentThread().getContextClassLoader();
115
//SocketServerInvoker invokerSvr = new SocketServerInvoker(clsLoader, locator);
116
//ObjectName objName = new ObjectName("jboss.remoting:type=SocketServerInvoker");
117
//server.registerMBean(invokerSvr, objName);
118
//invokerSvr.start();
120
Connector connector = new Connector();
121
connector.setInvokerLocator(locator.getLocatorURI());
122
ObjectName obj = new ObjectName("jboss.remoting:type=Connector,transport=" + locator.getProtocol());
123
registerMBean(server, connector, obj);
124
//connector.create();
127
Detector detector = null;
128
ObjectName objName = null;
130
if(type.equals(TYPE_MULTICAST))
132
MulticastDetector mdet = new MulticastDetector();
135
objName = new ObjectName("remoting:type=Detector,transport=multicast");
137
else if(type.equals(TYPE_JNDI))
139
JNDIDetector jdet = new JNDIDetector();
142
jdet.setContextFactory(contextFactory);
143
jdet.setURLPackage(urlPackage);
145
objName = new ObjectName("remoting:type=Detector,transport=jndi");
148
registerMBean(server, detector, objName);
150
System.err.println("Starting Detector");
154
Thread.currentThread().sleep(3000);
155
NetworkInstance[] instances = registry.getServers();
156
for(int x = 0; x < instances.length; x++)
158
log.debug(instances[x]);
166
System.err.println("Error creating and starting DetectorUtil.");
167
ex.printStackTrace();
171
public static void main(String[] args)
173
DetectorUtil test = null;
177
String type = args[0];
178
test = new DetectorUtil(type);
180
else if(args.length == 2)
182
String type = args[0];
183
int port = Integer.parseInt(args[1]);
184
test = new DetectorUtil(type, port);
186
else if(args.length == 3)
188
String type = args[0];
189
int port = Integer.parseInt(args[1]);
190
String host = args[2];
191
test = new DetectorUtil(type, port, host);
195
test = new DetectorUtil();
196
System.out.println("Using default type (" + test.getTransport() +
197
") and default port (" + test.getPort() + ") and " +
198
"default host (" + test.getHost() + ")" +
199
"\nCan enter type (multicast, jndi), port, and/or host via command line.");
207
private String getHost()
212
private int getPort()
217
private String getTransport()
222
static private MBeanServer createMBeanServer() throws Exception
224
if (SecurityUtility.skipAccessControl())
226
return MBeanServerFactory.createMBeanServer();
231
return (MBeanServer) AccessController.doPrivileged( new PrivilegedExceptionAction()
233
public Object run() throws Exception
235
return MBeanServerFactory.createMBeanServer();
239
catch (PrivilegedActionException e)
241
throw (Exception) e.getCause();
245
static private void registerMBean(final MBeanServer server, final Object o, final ObjectName name)
248
if (SecurityUtility.skipAccessControl())
250
server.registerMBean(o, name);
256
AccessController.doPrivileged( new PrivilegedExceptionAction()
258
public Object run() throws Exception
260
server.registerMBean(o, name);
265
catch (PrivilegedActionException e)
267
throw (Exception) e.getCause();
271
static private void setSystemProperty(final String name, final String value)
273
if (SecurityUtility.skipAccessControl())
275
System.setProperty(name, value);
281
AccessController.doPrivileged( new PrivilegedExceptionAction()
283
public Object run() throws Exception
285
return System.setProperty(name, value);
289
catch (PrivilegedActionException e)
291
throw (RuntimeException) e.getCause();