1 25 26 package org.objectweb.easybeans.jmx; 27 28 import java.util.List ; 29 30 import javax.management.AttributeNotFoundException ; 31 import javax.management.InstanceNotFoundException ; 32 import javax.management.MBeanException ; 33 import javax.management.MBeanServer ; 34 import javax.management.MBeanServerFactory ; 35 import javax.management.MalformedObjectNameException ; 36 import javax.management.ObjectName ; 37 import javax.management.ReflectionException ; 38 39 43 public final class MBeanServerHelper { 44 45 48 private static MBeanServer mbeanServer = null; 49 50 53 private static String idMbeanServer = null; 54 55 58 private static final String DEFAULT_DOMAIN_NAME = "EasyBeans"; 59 60 63 private MBeanServerHelper() { 64 65 } 66 67 72 protected static MBeanServer getMBeanServerServer() throws JMXRemoteException { 73 if (getInternalMBeanServer() != null) { 74 return getInternalMBeanServer(); 75 } 76 throw new JMXRemoteException("No running MBeanServer was found."); 77 } 78 79 82 private static MBeanServer getInternalMBeanServer() { 83 List mbeanServers = MBeanServerFactory.findMBeanServer(null); 84 if (mbeanServers.size() > 0) { 85 return (MBeanServer ) mbeanServers.get(0); 86 } 87 return null; 88 } 89 90 94 public static synchronized void startMBeanServer() throws MBeanServerException { 95 mbeanServer = getInternalMBeanServer(); 96 if (mbeanServer == null) { 98 mbeanServer = MBeanServerFactory.createMBeanServer(DEFAULT_DOMAIN_NAME); 100 } 101 102 ObjectName mbeanServerDelegate = null; 103 try { 104 mbeanServerDelegate = new ObjectName ("JMImplementation:type=MBeanServerDelegate"); 105 } catch (MalformedObjectNameException e) { 106 throw new MBeanServerException("Cannot build an objectName", e); 107 } catch (NullPointerException e) { 108 throw new MBeanServerException("Cannot build an objectName", e); 109 } 110 111 try { 112 idMbeanServer = (String ) mbeanServer.getAttribute(mbeanServerDelegate, "MBeanServerId"); 113 } catch (AttributeNotFoundException e) { 114 throw new MBeanServerException("Cannot get an attribute on MBeanserver.", e); 115 } catch (InstanceNotFoundException e) { 116 throw new MBeanServerException("Cannot get an attribute on MBeanserver.", e); 117 } catch (MBeanException e) { 118 throw new MBeanServerException("Cannot get an attribute on MBeanserver.", e); 119 } catch (ReflectionException e) { 120 throw new MBeanServerException("Cannot get an attribute on MBeanserver.", e); 121 } 122 } 123 124 127 protected static String getIdMbeanServer() { 128 return idMbeanServer; 129 } 130 131 } 132 | Popular Tags |