1 package org.objectweb.celtix.bus.management.jmx; 2 3 import java.io.IOException ; 4 import java.rmi.registry.LocateRegistry ; 5 import java.util.Map ; 6 import java.util.Properties ; 7 import java.util.logging.Level ; 8 import java.util.logging.Logger ; 9 10 import javax.management.MBeanServer ; 11 import javax.management.MBeanServerFactory ; 12 import javax.management.remote.JMXConnectorServer ; 13 import javax.management.remote.JMXConnectorServerFactory ; 14 import javax.management.remote.JMXServiceURL ; 15 16 import org.objectweb.celtix.common.logging.LogUtils; 17 18 19 20 24 public final class MBServerConnectorFactory { 25 26 public static final String DEFAULT_SERVICE_URL = "service:jmx:rmi:///jndi/rmi://localhost:9913/jmxrmi"; 27 28 private static final Logger LOG = LogUtils.getL7dLogger(MBServerConnectorFactory.class); 29 30 private static MBServerConnectorFactory factory; 31 private static MBeanServer server; 32 33 private static String serviceUrl = DEFAULT_SERVICE_URL; 34 35 private static Map environment; 36 37 private static boolean threaded; 38 39 private static boolean daemon; 40 41 private static JMXConnectorServer connectorServer; 42 43 private MBServerConnectorFactory() { 44 45 } 46 47 private int getURLLocalHostPort(String 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 portString = url.substring(portStart, portEnd); 55 port = Integer.parseInt(portString); 56 } 57 } 58 return port; 59 } 60 61 private static int indexNotOfNumber(String 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 ms) { 79 server = ms; 80 } 81 82 public void setServiceUrl(String url) { 83 serviceUrl = url; 84 } 85 86 public void setEnvironment(Properties env) { 87 environment = env; 88 } 89 90 public void setEnvironment(Map 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 ("unchecked") 104 public void createConnector() throws IOException { 105 106 if (server == null) { 107 server = MBeanServerFactory.createMBeanServer(); 108 } 109 110 JMXServiceURL url = new JMXServiceURL (serviceUrl); 112 113 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 ex) { 121 LocateRegistry.getRegistry(port); 123 } 124 125 } catch (Exception ex) { 126 LOG.log(Level.SEVERE, "CREATE_REGISTRY_FAULT_MSG", new Object []{ex}); 127 } 128 } 129 130 connectorServer = 132 JMXConnectorServerFactory.newJMXConnectorServer(url, environment, server); 133 134 135 if (threaded) { 136 Thread connectorThread = new Thread () { 138 public void run() { 139 try { 140 connectorServer.start(); 141 } catch (IOException ex) { 142 LOG.log(Level.SEVERE, "START_CONNECTOR_FAILURE_MSG", new Object []{ex}); 143 } 144 } 145 }; 146 147 connectorThread.setName("JMX Connector Thread [" + serviceUrl + "]"); 148 connectorThread.setDaemon(daemon); 149 connectorThread.start(); 150 } else { 151 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 { 161 connectorServer.stop(); 162 if (LOG.isLoggable(Level.INFO)) { 163 LOG.info("JMX connector server stoped: " + connectorServer); 164 } 165 } 166 167 } 168 | Popular Tags |