KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > bus > management > jmx > MBServerConnectorFactory


1 package org.objectweb.celtix.bus.management.jmx;
2
3 import java.io.IOException JavaDoc;
4 import java.rmi.registry.LocateRegistry JavaDoc;
5 import java.util.Map JavaDoc;
6 import java.util.Properties JavaDoc;
7 import java.util.logging.Level JavaDoc;
8 import java.util.logging.Logger JavaDoc;
9
10 import javax.management.MBeanServer JavaDoc;
11 import javax.management.MBeanServerFactory JavaDoc;
12 import javax.management.remote.JMXConnectorServer JavaDoc;
13 import javax.management.remote.JMXConnectorServerFactory JavaDoc;
14 import javax.management.remote.JMXServiceURL JavaDoc;
15
16 import org.objectweb.celtix.common.logging.LogUtils;
17
18
19
20 /**
21  * Deal with the MBeanServer Connections
22  *
23  */

24 public final class MBServerConnectorFactory {
25         
26     public static final String JavaDoc DEFAULT_SERVICE_URL = "service:jmx:rmi:///jndi/rmi://localhost:9913/jmxrmi";
27     
28     private static final Logger JavaDoc LOG = LogUtils.getL7dLogger(MBServerConnectorFactory.class);
29
30     private static MBServerConnectorFactory factory;
31     private static MBeanServer JavaDoc server;
32
33     private static String JavaDoc serviceUrl = DEFAULT_SERVICE_URL;
34
35     private static Map JavaDoc environment;
36
37     private static boolean threaded;
38
39     private static boolean daemon;
40
41     private static JMXConnectorServer JavaDoc connectorServer;
42     
43     private MBServerConnectorFactory() {
44         
45     }
46     
47     private int getURLLocalHostPort(String JavaDoc url) {
48         int portStart = url.indexOf("localhost") + 10;
49         int portEnd;
50         int port = 0;
51         if (portStart > 0) {
52             portEnd = indexNotOfNumber(url, portStart);
53             if (portEnd > portStart) {
54                 final String JavaDoc portString = url.substring(portStart, portEnd);
55                 port = Integer.parseInt(portString);
56             }
57         }
58         return port;
59     }
60     
61     private static int indexNotOfNumber(String JavaDoc str, int index) {
62         int i = 0;
63         for (i = index; i < str.length(); i++) {
64             if (str.charAt(i) < '0' || str.charAt(i) > '9') {
65                 return i;
66             }
67         }
68         return -1;
69     }
70     
71     public static MBServerConnectorFactory getInstance() {
72         if (factory == null) {
73             factory = new MBServerConnectorFactory();
74         }
75         return factory;
76     }
77     
78     public void setMBeanServer(MBeanServer JavaDoc ms) {
79         server = ms;
80     }
81
82     public void setServiceUrl(String JavaDoc url) {
83         serviceUrl = url;
84     }
85
86     public void setEnvironment(Properties JavaDoc env) {
87         environment = env;
88     }
89    
90     public void setEnvironment(Map JavaDoc env) {
91         environment = env;
92     }
93     
94     public void setThreaded(boolean fthread) {
95         threaded = fthread;
96     }
97     
98     public void setDaemon(boolean fdaemon) {
99         daemon = fdaemon;
100     }
101
102
103     @SuppressWarnings JavaDoc("unchecked")
104     public void createConnector() throws IOException JavaDoc {
105         
106         if (server == null) {
107             server = MBeanServerFactory.createMBeanServer();
108         }
109         
110         // Create the JMX service URL.
111
JMXServiceURL JavaDoc url = new JMXServiceURL JavaDoc(serviceUrl);
112         
113         // if the URL is localhost, start up an Registry
114
if (serviceUrl.indexOf("localhost") > -1
115             && url.getProtocol().compareToIgnoreCase("rmi") == 0) {
116             try {
117                 int port = getURLLocalHostPort(serviceUrl);
118                 try {
119                     LocateRegistry.createRegistry(port);
120                 } catch (Exception JavaDoc ex) {
121                     // the registry may had been created
122
LocateRegistry.getRegistry(port);
123                 }
124                
125             } catch (Exception JavaDoc ex) {
126                 LOG.log(Level.SEVERE, "CREATE_REGISTRY_FAULT_MSG", new Object JavaDoc[]{ex});
127             }
128         }
129         
130         // Create the connector server now.
131
connectorServer =
132             JMXConnectorServerFactory.newJMXConnectorServer(url, environment, server);
133
134        
135         if (threaded) {
136              // Start the connector server asynchronously (in a separate thread).
137
Thread JavaDoc connectorThread = new Thread JavaDoc() {
138                 public void run() {
139                     try {
140                         connectorServer.start();
141                     } catch (IOException JavaDoc ex) {
142                         LOG.log(Level.SEVERE, "START_CONNECTOR_FAILURE_MSG", new Object JavaDoc[]{ex});
143                     }
144                 }
145             };
146             
147             connectorThread.setName("JMX Connector Thread [" + serviceUrl + "]");
148             connectorThread.setDaemon(daemon);
149             connectorThread.start();
150         } else {
151              // Start the connector server in the same thread.
152
connectorServer.start();
153         }
154
155         if (LOG.isLoggable(Level.INFO)) {
156             LOG.info("JMX connector server started: " + connectorServer);
157         }
158     }
159
160     public void destroy() throws IOException JavaDoc {
161         connectorServer.stop();
162         if (LOG.isLoggable(Level.INFO)) {
163             LOG.info("JMX connector server stoped: " + connectorServer);
164         }
165     }
166
167 }
168
Popular Tags