1 6 package org.logicalcobwebs.proxool.admin.jmx; 7 8 import org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF; 9 import org.logicalcobwebs.proxool.ProxoolFacade; 10 import org.logicalcobwebs.proxool.ProxoolConstants; 11 import org.logicalcobwebs.proxool.ProxoolException; 12 import org.apache.commons.logging.Log; 13 import org.apache.commons.logging.LogFactory; 14 15 import javax.management.MBeanServer ; 16 import javax.management.MBeanServerFactory ; 17 import javax.management.ObjectName ; 18 import javax.management.MalformedObjectNameException ; 19 import java.util.Properties ; 20 import java.util.ArrayList ; 21 import java.util.StringTokenizer ; 22 import java.util.List ; 23 24 31 public class ProxoolJMXHelper { 32 private static final Log LOG = LogFactory.getLog(ProxoolJMXHelper.class); 33 34 private ProxoolJMXHelper() { 35 } 36 37 43 public static void registerPool(String alias, Properties poolPropeties) throws ProxoolException { 44 ConnectionPoolDefinitionIF connectionPoolDefinition = 45 ProxoolFacade.getConnectionPoolDefinition(alias); 46 String [] agentIds = getAgentIds(poolPropeties); 47 ArrayList servers = null; 48 for (int i = 0; i < agentIds.length; i++) { 49 servers = MBeanServerFactory.findMBeanServer(agentIds[i]); 50 if (servers == null || servers.size() < 1) { 51 LOG.error("Could not register pool " + connectionPoolDefinition.getAlias() + " for JMX instrumentation" 52 + " because lookup of MBeanServer using agent id " + agentIds[i] + " failed."); 53 } else { 54 MBeanServer mBeanServer = (MBeanServer ) servers.get(0); 55 ConnectionPoolMBean poolMBean = new ConnectionPoolMBean(alias, poolPropeties); 56 57 try { 58 mBeanServer.registerMBean(poolMBean, getObjectName(connectionPoolDefinition.getAlias())); 59 LOG.info("Registered JMX MBean for pool " + connectionPoolDefinition.getAlias() + " in agent " + agentIds[i]); 60 } catch (Exception e) { 61 LOG.error("Registration of JMX MBean for pool " + connectionPoolDefinition.getAlias() 62 + "in agent " + agentIds[i] + " failed.", e); 63 } 64 } 65 } 66 } 67 68 73 public static void unregisterPool(String alias, Properties poolPropeties) { 74 String [] agentIds = getAgentIds(poolPropeties); 75 ArrayList servers = null; 76 for (int i = 0; i < agentIds.length; i++) { 77 servers = MBeanServerFactory.findMBeanServer(agentIds[i]); 78 if (servers == null || servers.size() < 1) { 79 LOG.error("Could not unregister MBean for pool " + alias 80 + " because lookup of MBeanServer using agent id " + agentIds[i] + " failed."); 81 } else { 82 MBeanServer mBeanServer = (MBeanServer ) servers.get(0); 83 try { 84 mBeanServer.unregisterMBean(getObjectName(alias)); 85 LOG.info("Unregistered JMX MBean for pool " + alias + " in agent " + agentIds[i]); 86 } catch (Exception e) { 87 LOG.error("Unregistration of JMX MBean for pool " + alias + "in agent " 88 + agentIds[i] + " failed.", e); 89 } 90 } 91 } 92 } 93 94 100 public static ObjectName getObjectName(String alias) throws MalformedObjectNameException { 101 return new ObjectName ("Proxool:type=Pool, name=" + alias); 102 } 103 104 private static String [] getAgentIds(Properties poolPropeties) { 105 String idString = poolPropeties.getProperty(ProxoolConstants.JMX_AGENT_PROPERTY); 106 if (idString == null || idString.trim().equals("")) { 107 return new String []{null}; 109 } else { 110 StringTokenizer tokenizer = new StringTokenizer (idString, ","); 111 List tokens = new ArrayList (3); 112 while (tokenizer.hasMoreElements()) { 113 tokens.add(tokenizer.nextToken().trim()); 114 } 115 return (String []) tokens.toArray(new String [tokens.size()]); 116 } 117 } 118 119 128 public static String getValidIdentifier(String propertyName) { 129 if (propertyName.indexOf("-") == -1) { 130 return propertyName; 131 } else { 132 StringBuffer buffer = new StringBuffer (propertyName); 133 int index = -1; 134 while ((index = buffer.toString().indexOf("-")) > -1) { 135 buffer.deleteCharAt(index); 136 buffer.setCharAt(index, Character.toUpperCase(buffer.charAt(index))); 137 } 138 return buffer.toString(); 139 } 140 } 141 } 142 143 | Popular Tags |