1 25 package org.objectweb.carol.util.mbean; 26 27 import java.util.List ; 28 29 import javax.management.MBeanServer ; 30 import javax.management.MBeanServerFactory ; 31 import javax.management.MalformedObjectNameException ; 32 import javax.management.ObjectName ; 33 34 import org.apache.commons.logging.Log; 35 36 import org.objectweb.carol.util.configuration.ConfigurationException; 37 import org.objectweb.carol.util.configuration.ProtocolConfigurationImplMBean; 38 39 45 public class MBeanUtils { 46 47 50 private static MBeanServer mbeanServer = null; 51 52 55 private MBeanUtils() { 56 57 } 58 59 65 protected static void initMBeanServer(Log logger, String idMbeanServer) throws ConfigurationException { 66 List mbeanServers = MBeanServerFactory.findMBeanServer(idMbeanServer); 67 if (mbeanServers.size() == 0) { 68 throw new ConfigurationException("No MBean Servers were found with id '" + idMbeanServer + "'"); 69 } 70 if (mbeanServers.size() > 1) { 71 if (logger.isDebugEnabled()) { 72 logger.debug("Take first MBeanServer of the list"); 73 } 74 } 75 mbeanServer = (MBeanServer ) mbeanServers.get(0); 76 } 77 78 79 87 public static void registerProtocolConfigurationMBean(ProtocolConfigurationImplMBean protocolConfiguration, Log logger, String idMbeanServer, String serverName) throws ConfigurationException { 88 89 if (mbeanServer == null) { 91 initMBeanServer(logger, idMbeanServer); 92 } 93 StringBuffer sb = new StringBuffer (mbeanServer.getDefaultDomain()); 94 sb.append(":j2eeType=JNDIResource"); 95 sb.append(",name="); 96 sb.append(protocolConfiguration.getName()); 97 sb.append(",J2EEServer="); 98 sb.append(serverName); 99 ObjectName on = null; 100 try { 101 on = new ObjectName (sb.toString()); 102 } catch (MalformedObjectNameException e) { 103 throw new ConfigurationException("Cannot build ObjectName for configuration '" + protocolConfiguration.getName() + "'", e); 104 } 105 106 protocolConfiguration.setobjectName(on.toString()); 108 109 try { 110 mbeanServer.registerMBean(protocolConfiguration, on); 111 } catch (Exception e) { 112 throw new ConfigurationException("Cannot register MBean '" + on + "' in MBeanServer", e); 113 } 114 115 } 116 117 } 118 | Popular Tags |