1 17 package org.apache.servicemix.jbi.management; 18 19 import java.io.IOException ; 20 import java.lang.reflect.Method ; 21 import java.rmi.RemoteException ; 22 import java.util.List ; 23 24 import javax.management.JMException ; 25 import javax.management.MBeanServer ; 26 import javax.management.MBeanServerFactory ; 27 import javax.management.MalformedObjectNameException ; 28 import javax.management.ObjectName ; 29 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 import org.apache.servicemix.jbi.jmx.ConnectorServerFactoryBean; 33 import org.apache.servicemix.jbi.jmx.RmiRegistryFactoryBean; 34 35 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean; 36 37 38 43 class MBeanServerContext { 44 45 48 public static final String DEFAULT_DOMAIN="org.apache.activemq"; 49 private final static Log log=LogFactory.getLog(ManagementContext.class); 50 private MBeanServer beanServer; 51 private String jmxDomainName=DEFAULT_DOMAIN; 52 private boolean useMBeanServer=true; 53 private boolean createMBeanServer=true; 54 private boolean locallyCreateMBeanServer=false; 55 private boolean createConnector=true; 56 private boolean findTigerMbeanServer=false; 57 private int connectorPort=1099; 58 private String connectorPath="/jmxrmi"; 59 private AtomicBoolean started=new AtomicBoolean(false); 60 private ConnectorServerFactoryBean connectorServerFactoryBean; 61 private RmiRegistryFactoryBean rmiRegistryFactoryBean; 62 63 public MBeanServerContext(){ 64 this(null); 65 } 66 67 public MBeanServerContext(MBeanServer server){ 68 this.beanServer=server; 69 } 70 71 public void start() throws IOException { 72 if (started.compareAndSet(false, true)) { 74 getMBeanServer(); 75 if (createConnector) { 76 try { 77 rmiRegistryFactoryBean = new RmiRegistryFactoryBean(); 78 rmiRegistryFactoryBean.setPort(connectorPort); 79 rmiRegistryFactoryBean.afterPropertiesSet(); 80 } catch (Exception e) { 81 log.warn("Failed to start rmi registry: " + e.getMessage()); 82 if (log.isDebugEnabled()) { 83 log.debug("Failed to start rmi registry", e); 84 } 85 } 86 try { 87 connectorServerFactoryBean = new ConnectorServerFactoryBean(); 88 connectorServerFactoryBean.setObjectName("connector:name=rmi"); 90 connectorServerFactoryBean.setServer(getMBeanServer()); 92 String serviceUrl = "service:jmx:rmi:///jndi/rmi://localhost:" + connectorPort + connectorPath; 93 connectorServerFactoryBean.setServiceUrl(serviceUrl); 94 connectorServerFactoryBean.afterPropertiesSet(); 95 } catch (Exception e) { 96 log.warn("Failed to start jmx connector: " + e.getMessage()); 97 if (log.isDebugEnabled()) { 98 log.debug("Failed to create jmx connector", e); 99 } 100 } 101 } 102 } 103 } 104 105 public void stop() throws IOException { 106 if (started.compareAndSet(true, false)) { 107 if (connectorServerFactoryBean != null) { 108 try { 109 connectorServerFactoryBean.destroy(); 110 } catch (Exception e) { 111 log.warn("Failed to stop jmx connector: " + e.getMessage()); 112 if (log.isDebugEnabled()) { 113 log.debug("Failed to stop jmx connector", e); 114 } 115 } finally { 116 connectorServerFactoryBean = null; 117 } 118 } 119 if (rmiRegistryFactoryBean != null) { 120 try { 121 rmiRegistryFactoryBean.destroy(); 122 } catch (RemoteException e) { 123 log.warn("Failed to stop rmi registry: " + e.getMessage()); 124 if (log.isDebugEnabled()) { 125 log.debug("Failed to stop rmi registry", e); 126 } 127 } finally { 128 rmiRegistryFactoryBean = null; 129 } 130 } 131 if (locallyCreateMBeanServer && beanServer != null) { 132 List list = MBeanServerFactory.findMBeanServer(null); 134 if (list != null && !list.isEmpty() && list.contains(beanServer)) { 135 MBeanServerFactory.releaseMBeanServer(beanServer); 136 } 137 } 138 } 139 } 140 141 144 public String getJmxDomainName(){ 145 return jmxDomainName; 146 } 147 148 152 public void setJmxDomainName(String jmxDomainName){ 153 this.jmxDomainName=jmxDomainName; 154 } 155 156 161 public MBeanServer getMBeanServer(){ 162 if(this.beanServer==null){ 163 this.beanServer=findMBeanServer(); 164 } 165 return beanServer; 166 } 167 168 173 public void setMBeanServer(MBeanServer beanServer){ 174 this.beanServer=beanServer; 175 } 176 177 180 public boolean isUseMBeanServer(){ 181 return useMBeanServer; 182 } 183 184 188 public void setUseMBeanServer(boolean useMBeanServer){ 189 this.useMBeanServer=useMBeanServer; 190 } 191 192 195 public boolean isCreateMBeanServer(){ 196 return createMBeanServer; 197 } 198 199 203 public void setCreateMBeanServer(boolean enableJMX){ 204 this.createMBeanServer=enableJMX; 205 } 206 207 public boolean isFindTigerMbeanServer() { 208 return findTigerMbeanServer; 209 } 210 211 214 public void setFindTigerMbeanServer(boolean findTigerMbeanServer) { 215 this.findTigerMbeanServer = findTigerMbeanServer; 216 } 217 218 225 public ObjectName createCustomComponentMBeanName(String type,String name){ 226 ObjectName result=null; 227 String tmp=jmxDomainName+":"+"type="+sanitizeString(type)+",name="+sanitizeString(name); 228 try{ 229 result=new ObjectName (tmp); 230 }catch(MalformedObjectNameException e){ 231 log.error("Couldn't create ObjectName from: "+type+" , "+name); 232 } 233 return result; 234 } 235 236 242 private static String sanitizeString(String in){ 243 String result=null; 244 if(in!=null){ 245 result=in.replace(':','_'); 246 result=result.replace('/','_'); 247 result=result.replace('\\','_'); 248 } 249 return result; 250 } 251 252 261 public static ObjectName getSystemObjectName(String domainName,String containerName,Class theClass) 262 throws MalformedObjectNameException ,NullPointerException { 263 String tmp=domainName+":"+"type="+theClass.getName()+",name="+getRelativeName(containerName,theClass); 264 return new ObjectName (tmp); 265 } 266 267 private static String getRelativeName(String containerName,Class theClass){ 268 String name=theClass.getName(); 269 int index=name.lastIndexOf("."); 270 if(index>=0&&(index+1)<name.length()){ 271 name=name.substring(index+1); 272 } 273 return containerName+"."+name; 274 } 275 276 282 public void unregisterMBean(ObjectName name) throws JMException { 283 if(beanServer!=null&&beanServer.isRegistered(name)){ 284 beanServer.unregisterMBean(name); 285 } 286 } 287 288 protected synchronized MBeanServer findMBeanServer(){ 289 MBeanServer result=null; 290 try{ 292 if(useMBeanServer){ 293 if (findTigerMbeanServer) { 294 result = findTigerMBeanServer(); 295 } 296 if (result == null) { 297 List list=MBeanServerFactory.findMBeanServer(null); 300 if(list!=null&&list.size()>0){ 301 result=(MBeanServer ) list.get(0); 302 } 303 } 304 } 305 if(result==null&&createMBeanServer){ 306 result=createMBeanServer(); 307 } 308 }catch(NoClassDefFoundError e){ 309 log.error("Could not load MBeanServer",e); 310 }catch(Throwable e){ 311 log.error("Failed to initialize MBeanServer",e); 313 } 314 return result; 315 } 316 317 public static MBeanServer findTigerMBeanServer() { 318 String name = "java.lang.management.ManagementFactory"; 319 Class type = loadClass(name, ManagementContext.class.getClassLoader()); 320 if (type != null) { 321 try { 322 Method method = type.getMethod("getPlatformMBeanServer", new Class [0]); 323 if (method != null) { 324 Object answer = method.invoke(null, new Object [0]); 325 if (answer instanceof MBeanServer ) { 326 return (MBeanServer ) answer; 327 } 328 else { 329 log.warn("Could not cast: " + answer + " into an MBeanServer. There must be some classloader strangeness in town"); 330 } 331 } 332 else { 333 log.warn("Method getPlatformMBeanServer() does not appear visible on type: " + type.getName()); 334 } 335 } 336 catch (Exception e) { 337 log.warn("Failed to call getPlatformMBeanServer() due to: " + e, e); 338 } 339 } 340 else { 341 log.trace("Class not found: " + name + " so probably running on Java 1.4"); 342 } 343 return null; 344 } 345 346 private static Class loadClass(String name, ClassLoader loader) { 347 try { 348 return loader.loadClass(name); 349 } 350 catch (ClassNotFoundException e) { 351 try { 352 return Thread.currentThread().getContextClassLoader().loadClass(name); 353 } 354 catch (ClassNotFoundException e1) { 355 return null; 356 } 357 } 358 } 359 360 366 protected MBeanServer createMBeanServer() throws MalformedObjectNameException ,IOException { 367 MBeanServer mbeanServer=MBeanServerFactory.createMBeanServer(jmxDomainName); 368 locallyCreateMBeanServer=true; 369 return mbeanServer; 370 } 371 372 public String getConnectorPath(){ 373 return connectorPath; 374 } 375 376 public void setConnectorPath(String connectorPath){ 377 this.connectorPath=connectorPath; 378 } 379 380 public int getConnectorPort(){ 381 return connectorPort; 382 } 383 384 public void setConnectorPort(int connectorPort){ 385 this.connectorPort=connectorPort; 386 } 387 388 public boolean isCreateConnector(){ 389 return createConnector; 390 } 391 392 public void setCreateConnector(boolean createConnector){ 393 this.createConnector=createConnector; 394 } 395 } 396 | Popular Tags |