KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > proxool > admin > jmx > ProxoolJMXHelper


1 /*
2  * This software is released under a licence similar to the Apache Software Licence.
3  * See org.logicalcobwebs.proxool.package.html for details.
4  * The latest version is available at http://proxool.sourceforge.net
5  */

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 JavaDoc;
16 import javax.management.MBeanServerFactory JavaDoc;
17 import javax.management.ObjectName JavaDoc;
18 import javax.management.MalformedObjectNameException JavaDoc;
19 import java.util.Properties JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.StringTokenizer JavaDoc;
22 import java.util.List JavaDoc;
23
24 /**
25  * Utilities for Proxool JMX instrumentation.
26  * @version $Revision: 1.7 $, $Date: 2006/01/18 14:39:56 $
27  * @author Christian Nedregaard (christian_nedregaard@email.com)
28  * @author $Author: billhorsman $ (current maintainer)
29  * @since Proxool 0.8
30  */

31 public class ProxoolJMXHelper {
32     private static final Log LOG = LogFactory.getLog(ProxoolJMXHelper.class);
33
34     private ProxoolJMXHelper() {
35     }
36
37     /**
38      * Create and register a {@link org.logicalcobwebs.proxool.admin.jmx.ConnectionPoolMBean} to the given agents.
39      * Will log errors instead of throwing exceptions if one or more of the registrations fails.
40      * @param poolPropeties the complete pool properties.
41      * @throws org.logicalcobwebs.proxool.ProxoolException if the pool can not be found.
42      */

43     public static void registerPool(String JavaDoc alias, Properties JavaDoc poolPropeties) throws ProxoolException {
44         ConnectionPoolDefinitionIF connectionPoolDefinition =
45                 ProxoolFacade.getConnectionPoolDefinition(alias);
46         String JavaDoc[] agentIds = getAgentIds(poolPropeties);
47         ArrayList JavaDoc 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 JavaDoc mBeanServer = (MBeanServer JavaDoc) 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 JavaDoc 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     /**
69      * Unregister a {@link org.logicalcobwebs.proxool.admin.jmx.ConnectionPoolMBean} from the given agents.
70      * Will log errors instead of throwing exceptions if one or more of the unregistrations fails.
71      * @param poolPropeties the complete pool properties.
72      */

73     public static void unregisterPool(String JavaDoc alias, Properties JavaDoc poolPropeties) {
74         String JavaDoc[] agentIds = getAgentIds(poolPropeties);
75         ArrayList JavaDoc 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 JavaDoc mBeanServer = (MBeanServer JavaDoc) 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 JavaDoc e) {
87                     LOG.error("Unregistration of JMX MBean for pool " + alias + "in agent "
88                             + agentIds[i] + " failed.", e);
89                 }
90             }
91         }
92     }
93
94     /**
95      * Get the prefered JMX object name for a Proxool pool.
96      * @param alias the alias of the pool.
97      * @return the generated object name.
98      * @throws javax.management.MalformedObjectNameException if the creation of the object name fails.
99      */

100     public static ObjectName JavaDoc getObjectName(String JavaDoc alias) throws MalformedObjectNameException JavaDoc {
101         return new ObjectName JavaDoc("Proxool:type=Pool, name=" + alias);
102     }
103
104     private static String JavaDoc[] getAgentIds(Properties JavaDoc poolPropeties) {
105         String JavaDoc idString = poolPropeties.getProperty(ProxoolConstants.JMX_AGENT_PROPERTY);
106         if (idString == null || idString.trim().equals("")) {
107             // null agent id means 'default agent'
108
return new String JavaDoc[]{null};
109         } else {
110             StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(idString, ",");
111             List JavaDoc tokens = new ArrayList JavaDoc(3);
112             while (tokenizer.hasMoreElements()) {
113                 tokens.add(tokenizer.nextToken().trim());
114             }
115             return (String JavaDoc[]) tokens.toArray(new String JavaDoc[tokens.size()]);
116         }
117     }
118
119     /**
120      * Generate a valid JMX identifier attribute name from a Proxool property name.
121      * This basically involves changing all occurences of <code>-&lt;char&gt;</code> to
122      * <code>&lt;uppercase char&gt;</code>.<br>
123      * <code>driver-properties</code> will for instance become
124      * <code>driverProperties</code>.
125      * @param propertyName the name to be converted.
126      * @return the converted attribute name.
127      */

128     public static String JavaDoc getValidIdentifier(String JavaDoc propertyName) {
129         if (propertyName.indexOf("-") == -1) {
130             return propertyName;
131         } else {
132             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc (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 /*
144  Revision history:
145  $Log: ProxoolJMXHelper.java,v $
146  Revision 1.7 2006/01/18 14:39:56 billhorsman
147  Unbundled Jakarta's Commons Logging.
148
149  Revision 1.6 2003/05/06 23:15:56 chr32
150  Moving JMX classes back in from sandbox.
151
152  Revision 1.1 2003/03/07 16:35:18 billhorsman
153  moved jmx stuff into sandbox until it is tested
154
155  Revision 1.4 2003/03/05 15:14:15 billhorsman
156  fix for jdk 1.2
157
158  Revision 1.3 2003/03/03 11:12:00 billhorsman
159  fixed licence
160
161  Revision 1.2 2003/02/26 19:05:03 chr32
162  Added utility methods.
163  Fixed mutiple servers bug.
164
165  Revision 1.1 2003/02/24 18:01:29 chr32
166  Rewrite and renamed: ProxoolJMXHelper.
167
168  Revision 1.1 2003/02/24 01:14:17 chr32
169  Init rev (unfinished).
170
171 */
Popular Tags