1 17 package org.apache.servicemix.web.jmx; 18 19 import org.apache.activemq.Service; 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 23 import javax.jms.JMSException ; 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 java.util.List ; 31 32 37 public class ManagementContext implements Service { 38 41 public static String DEFAULT_DOMAIN = "org.apache.activemq"; 42 43 private final static Log log = LogFactory.getLog(ManagementContext.class); 44 45 private MBeanServer beanServer; 46 private String jmxDomainName = DEFAULT_DOMAIN; 47 private boolean useMBeanServer = true; 48 private boolean createMBeanServer = true; 49 private boolean locallyCreateMBeanServer = false; 50 51 public ManagementContext() { 52 this(null); 53 } 54 55 public ManagementContext(MBeanServer server) { 56 this.beanServer = server; 57 } 58 59 public void start() throws JMSException { 60 getMBeanServer(); 62 } 63 64 public void stop() throws JMSException { 65 if (locallyCreateMBeanServer && beanServer != null) { 66 List list = MBeanServerFactory.findMBeanServer(null); 68 if (list != null && !list.isEmpty() && list.contains(beanServer)) { 69 MBeanServerFactory.releaseMBeanServer(beanServer); 70 } 71 } 72 } 73 74 77 public String getJmxDomainName() { 78 return jmxDomainName; 79 } 80 81 85 public void setJmxDomainName(String jmxDomainName) { 86 this.jmxDomainName = jmxDomainName; 87 } 88 89 94 public MBeanServer getMBeanServer() { 95 if (this.beanServer == null) { 96 this.beanServer = findMBeanServer(); 97 } 98 return beanServer; 99 } 100 101 104 public boolean isUseMBeanServer() { 105 return useMBeanServer; 106 } 107 108 112 public void setUseMBeanServer(boolean useMBeanServer) { 113 this.useMBeanServer = useMBeanServer; 114 } 115 116 119 public boolean isCreateMBeanServer() { 120 return createMBeanServer; 121 } 122 123 127 public void setCreateMBeanServer(boolean enableJMX) { 128 this.createMBeanServer = enableJMX; 129 } 130 131 139 public ObjectName createCustomComponentMBeanName(String type, String name) { 140 ObjectName result = null; 141 String tmp = jmxDomainName + ":" + "type=" + sanitizeString(type) + ",name=" + sanitizeString(name); 142 try { 143 result = new ObjectName (tmp); 144 } 145 catch (MalformedObjectNameException e) { 146 log.error("Couldn't create ObjectName from: " + type + " , " + name); 147 } 148 return result; 149 } 150 151 157 private static String sanitizeString(String in) { 158 String result = null; 159 if (in != null) { 160 result = in.replace(':', '_'); 161 result = result.replace('/', '_'); 162 result = result.replace('\\', '_'); 163 } 164 return result; 165 } 166 167 176 public static ObjectName getSystemObjectName(String domainName, String containerName, Class theClass) 177 throws MalformedObjectNameException , NullPointerException { 178 String tmp = domainName + ":" + "type=" + theClass.getName() + ",name=" 179 + getRelativeName(containerName, theClass); 180 return new ObjectName (tmp); 181 } 182 183 private static String getRelativeName(String containerName, Class theClass) { 184 String name = theClass.getName(); 185 int index = name.lastIndexOf("."); 186 if (index >= 0 && (index + 1) < name.length()) { 187 name = name.substring(index + 1); 188 } 189 return containerName + "." + name; 190 } 191 192 198 public void unregisterMBean(ObjectName name) throws JMException { 199 if (beanServer != null && beanServer.isRegistered(name)) { 200 beanServer.unregisterMBean(name); 201 } 202 } 203 204 protected synchronized MBeanServer findMBeanServer() { 205 MBeanServer result = null; 206 try { 208 if (useMBeanServer) { 209 List list = MBeanServerFactory.findMBeanServer(null); 212 if (list != null && list.size() > 0) { 213 result = (MBeanServer ) list.get(0); 214 } 215 } 216 217 if (result == null && createMBeanServer) { 218 result = MBeanServerFactory.createMBeanServer(jmxDomainName); 219 locallyCreateMBeanServer = true; 220 } 221 } 222 catch (NoClassDefFoundError e) { 223 log.error("Couldnot load MBeanServer", e); 224 } 225 catch (Throwable e) { 226 log.error("Failed to initialize MBeanServer", e); 228 } 229 return result; 230 } 231 } 232 | Popular Tags |