001: package org.objectweb.celtix.bus.management.jmx;
002:
003: import java.io.IOException;
004: import java.rmi.registry.LocateRegistry;
005: import java.util.Map;
006: import java.util.Properties;
007: import java.util.logging.Level;
008: import java.util.logging.Logger;
009:
010: import javax.management.MBeanServer;
011: import javax.management.MBeanServerFactory;
012: import javax.management.remote.JMXConnectorServer;
013: import javax.management.remote.JMXConnectorServerFactory;
014: import javax.management.remote.JMXServiceURL;
015:
016: import org.objectweb.celtix.common.logging.LogUtils;
017:
018: /**
019: * Deal with the MBeanServer Connections
020: *
021: */
022: public final class MBServerConnectorFactory {
023:
024: public static final String DEFAULT_SERVICE_URL = "service:jmx:rmi:///jndi/rmi://localhost:9913/jmxrmi";
025:
026: private static final Logger LOG = LogUtils
027: .getL7dLogger(MBServerConnectorFactory.class);
028:
029: private static MBServerConnectorFactory factory;
030: private static MBeanServer server;
031:
032: private static String serviceUrl = DEFAULT_SERVICE_URL;
033:
034: private static Map environment;
035:
036: private static boolean threaded;
037:
038: private static boolean daemon;
039:
040: private static JMXConnectorServer connectorServer;
041:
042: private MBServerConnectorFactory() {
043:
044: }
045:
046: private int getURLLocalHostPort(String url) {
047: int portStart = url.indexOf("localhost") + 10;
048: int portEnd;
049: int port = 0;
050: if (portStart > 0) {
051: portEnd = indexNotOfNumber(url, portStart);
052: if (portEnd > portStart) {
053: final String portString = url.substring(portStart,
054: portEnd);
055: port = Integer.parseInt(portString);
056: }
057: }
058: return port;
059: }
060:
061: private static int indexNotOfNumber(String str, int index) {
062: int i = 0;
063: for (i = index; i < str.length(); i++) {
064: if (str.charAt(i) < '0' || str.charAt(i) > '9') {
065: return i;
066: }
067: }
068: return -1;
069: }
070:
071: public static MBServerConnectorFactory getInstance() {
072: if (factory == null) {
073: factory = new MBServerConnectorFactory();
074: }
075: return factory;
076: }
077:
078: public void setMBeanServer(MBeanServer ms) {
079: server = ms;
080: }
081:
082: public void setServiceUrl(String url) {
083: serviceUrl = url;
084: }
085:
086: public void setEnvironment(Properties env) {
087: environment = env;
088: }
089:
090: public void setEnvironment(Map env) {
091: environment = env;
092: }
093:
094: public void setThreaded(boolean fthread) {
095: threaded = fthread;
096: }
097:
098: public void setDaemon(boolean fdaemon) {
099: daemon = fdaemon;
100: }
101:
102: @SuppressWarnings("unchecked")
103: public void createConnector() throws IOException {
104:
105: if (server == null) {
106: server = MBeanServerFactory.createMBeanServer();
107: }
108:
109: // Create the JMX service URL.
110: JMXServiceURL url = new JMXServiceURL(serviceUrl);
111:
112: // if the URL is localhost, start up an Registry
113: if (serviceUrl.indexOf("localhost") > -1
114: && url.getProtocol().compareToIgnoreCase("rmi") == 0) {
115: try {
116: int port = getURLLocalHostPort(serviceUrl);
117: try {
118: LocateRegistry.createRegistry(port);
119: } catch (Exception ex) {
120: // the registry may had been created
121: LocateRegistry.getRegistry(port);
122: }
123:
124: } catch (Exception ex) {
125: LOG.log(Level.SEVERE, "CREATE_REGISTRY_FAULT_MSG",
126: new Object[] { ex });
127: }
128: }
129:
130: // Create the connector server now.
131: connectorServer = JMXConnectorServerFactory
132: .newJMXConnectorServer(url, environment, server);
133:
134: if (threaded) {
135: // Start the connector server asynchronously (in a separate thread).
136: Thread connectorThread = new Thread() {
137: public void run() {
138: try {
139: connectorServer.start();
140: } catch (IOException ex) {
141: LOG.log(Level.SEVERE,
142: "START_CONNECTOR_FAILURE_MSG",
143: new Object[] { ex });
144: }
145: }
146: };
147:
148: connectorThread.setName("JMX Connector Thread ["
149: + serviceUrl + "]");
150: connectorThread.setDaemon(daemon);
151: connectorThread.start();
152: } else {
153: // Start the connector server in the same thread.
154: connectorServer.start();
155: }
156:
157: if (LOG.isLoggable(Level.INFO)) {
158: LOG
159: .info("JMX connector server started: "
160: + connectorServer);
161: }
162: }
163:
164: public void destroy() throws IOException {
165: connectorServer.stop();
166: if (LOG.isLoggable(Level.INFO)) {
167: LOG.info("JMX connector server stoped: " + connectorServer);
168: }
169: }
170:
171: }
|