1 25 26 package org.objectweb.easybeans.jmx; 27 28 import static org.objectweb.easybeans.jmx.MBeanServerHelper.getMBeanServerServer; 29 30 import java.io.IOException ; 31 import java.net.MalformedURLException ; 32 import java.util.HashMap ; 33 import java.util.Map ; 34 35 import javax.management.InstanceAlreadyExistsException ; 36 import javax.management.InstanceNotFoundException ; 37 import javax.management.MBeanRegistrationException ; 38 import javax.management.MalformedObjectNameException ; 39 import javax.management.NotCompliantMBeanException ; 40 import javax.management.ObjectName ; 41 import javax.management.remote.JMXConnectorServer ; 42 import javax.management.remote.JMXConnectorServerFactory ; 43 import javax.management.remote.JMXServiceURL ; 44 45 import org.objectweb.easybeans.component.itf.RegistryComponent; 46 import org.objectweb.easybeans.log.JLog; 47 import org.objectweb.easybeans.log.JLogFactory; 48 49 54 public final class JMXRemoteHelper { 55 56 59 private static JLog logger = JLogFactory.getLog(JMXRemoteHelper.class); 60 61 64 private static JMXConnectorServer jmxConnectorServer = null; 65 66 69 private static final String PREFIX_URL = "service:jmx:rmi:///jndi/"; 70 71 74 private static final String DEFAULT_RMI = "rmi://localhost:1099"; 75 76 79 private static final String SUFFIX_URL = "/EasyBeansConnector"; 80 81 84 private static final String DEFAULT_NAME_CONNECTOR = "connectors:name=JMXRemoteConnector"; 85 86 87 90 private JMXRemoteHelper() { 91 92 } 93 94 95 100 private static void init(final RegistryComponent registryComponent) throws JMXRemoteException { 101 102 Map <String , String > environment = new HashMap <String , String >(); 104 JMXServiceURL jmxServiceURL = null; 105 106 StringBuilder sb = new StringBuilder (PREFIX_URL); 108 if (registryComponent != null) { 109 sb.append(registryComponent.getProviderURL()); 110 } else { 111 sb.append(DEFAULT_RMI); 112 } 113 sb.append(SUFFIX_URL); 114 String url = sb.toString(); 115 116 try { 117 jmxServiceURL = new JMXServiceURL (url); 118 } catch (MalformedURLException e) { 119 throw new JMXRemoteException("Cannot create jmxservice url with url '" + url + "'.", e); 120 } 121 environment.put("jmx.remote.jndi.rebind", "true"); 122 try { 123 jmxConnectorServer = JMXConnectorServerFactory.newJMXConnectorServer(jmxServiceURL, environment, null); 124 } catch (IOException e) { 125 throw new JMXRemoteException("Cannot create new JMX Connector", e); 126 } 127 logger.info("Creating JMXRemote connector with URl ''{0}''", url); 128 129 } 130 131 136 public static synchronized void startConnector(final RegistryComponent registryComponent) throws JMXRemoteException { 137 if (jmxConnectorServer == null) { 139 init(registryComponent); 140 } 141 142 ObjectName connectorServerName = getConnectorObjectName(); 143 try { 145 getMBeanServerServer().registerMBean(jmxConnectorServer, connectorServerName); 146 } catch (InstanceAlreadyExistsException e) { 147 throw new JMXRemoteException("Cannot register Mbean with the name '" + connectorServerName + "'", e); 148 } catch (MBeanRegistrationException e) { 149 throw new JMXRemoteException("Cannot register Mbean with the name '" + connectorServerName + "'", e); 150 } catch (NotCompliantMBeanException e) { 151 throw new JMXRemoteException("Cannot register Mbean with the name '" + connectorServerName + "'", e); 152 } 153 154 try { 156 jmxConnectorServer.start(); 157 } catch (IOException e) { 158 throw new JMXRemoteException("Cannot start the jmx connector", e); 159 } 160 } 161 162 166 public static synchronized void stopConnector() throws JMXRemoteException { 167 if (jmxConnectorServer != null) { 169 ObjectName connectorServerName = getConnectorObjectName(); 170 try { 171 jmxConnectorServer.stop(); 173 getMBeanServerServer().unregisterMBean(connectorServerName); 175 jmxConnectorServer = null; 177 } catch (InstanceNotFoundException e) { 178 throw new JMXRemoteException("Cannot unregister JMX Connector with name '" + connectorServerName + "'", e); 179 } catch (MBeanRegistrationException e) { 180 throw new JMXRemoteException("Cannot unregister JMX Connector with name '" + connectorServerName + "'", e); 181 } catch (IOException e) { 182 throw new JMXRemoteException("Cannot Stop JMX Connector with name '" + connectorServerName + "'", e); 183 } 184 } 185 } 186 187 188 192 private static ObjectName getConnectorObjectName() throws JMXRemoteException { 193 ObjectName connectorServerName = null; 194 String objName = null; 195 try { 196 objName = DEFAULT_NAME_CONNECTOR; 197 connectorServerName = new ObjectName (objName); 198 } catch (MalformedObjectNameException e) { 199 throw new JMXRemoteException("Cannot create ObjectName with name '" + objName + "'", e); 200 } catch (NullPointerException e) { 201 throw new JMXRemoteException("Cannot create ObjectName with name '" + objName + "'", e); 202 } 203 return connectorServerName; 204 } 205 } 206 | Popular Tags |