001: package org.jgroups.jmx;
002:
003: import org.apache.commons.logging.Log;
004: import org.apache.commons.logging.LogFactory;
005: import org.jgroups.stack.ProtocolStack;
006: import org.jgroups.util.Util;
007:
008: import javax.management.*;
009: import java.util.Vector;
010: import java.util.Set;
011: import java.util.Iterator;
012:
013: /**
014: * @author Bela Ban
015: * @version $Id: JmxConfigurator.java,v 1.10 2006/08/09 13:02:21 belaban Exp $
016: */
017: public class JmxConfigurator {
018: static final Log log = LogFactory.getLog(JmxConfigurator.class);
019:
020: /**
021: * Registers an already created channel with the MBeanServer. Creates an org.jgroups.jmx.JChannel which
022: * delegates to the org.jgroups.JChannel and registers it. Optionally, this method will also try to
023: * create one MBean proxy for each protocol in the channel's protocol stack, and register it as well.
024: * @param channel
025: * @param server
026: * @param domain Has to be a JMX ObjectName of the domain, e.g. DefaultDomain:name=JGroups
027: * @param register_protocols
028: * @return org.jgroups.jmx.JChannel for the specified org.jgroups.JChannel
029: */
030: public static org.jgroups.jmx.JChannel registerChannel(
031: org.jgroups.JChannel channel, MBeanServer server,
032: String domain, String cluster_name,
033: boolean register_protocols) throws Exception {
034: if (cluster_name == null)
035: cluster_name = channel != null ? channel.getClusterName()
036: : null;
037: if (cluster_name == null)
038: cluster_name = "null";
039: if (register_protocols) {
040: String tmp = domain + ":type=protocol,cluster="
041: + cluster_name;
042: registerProtocols(server, channel, tmp);
043: }
044: return registerChannel(channel, server, domain
045: + ":type=channel,cluster=" + cluster_name);
046: }
047:
048: /**
049: * Registers an already created channel with the MBeanServer. Creates an org.jgroups.jmx.JChannel which
050: * delegates to the org.jgroups.JChannel and registers it.
051: * @param channel
052: * @param server
053: * @param name The JMX ObjectName
054: * @return org.jgroups.jmx.JChannel for the specified org.jgroups.JChannel
055: */
056: public static org.jgroups.jmx.JChannel registerChannel(
057: org.jgroups.JChannel channel, MBeanServer server,
058: String name) throws Exception {
059: JChannel retval = new JChannel(channel);
060: server.registerMBean(retval, new ObjectName(name));
061: return retval;
062: }
063:
064: public static void unregisterChannel(MBeanServer server,
065: ObjectName name) throws Exception {
066: if (server != null)
067: server.unregisterMBean(name);
068: }
069:
070: public static void unregisterChannel(MBeanServer server, String name)
071: throws Exception {
072: if (server != null)
073: server.unregisterMBean(new ObjectName(name));
074: }
075:
076: public static org.jgroups.jmx.JChannelFactory registerChannelFactory(
077: org.jgroups.JChannelFactory factory, MBeanServer server,
078: String name) throws Exception {
079: JChannelFactory retval = new JChannelFactory(factory);
080: server.registerMBean(retval, new ObjectName(name));
081: return retval;
082: }
083:
084: /**
085: * Takes all protocols of an existing stack, creates corresponding MBean proxies and registers them with
086: * the MBean server
087: * @param channel
088: * @param prefix
089: */
090: public static void registerProtocols(MBeanServer server,
091: org.jgroups.JChannel channel, String prefix)
092: throws Exception {
093: ProtocolStack stack = channel.getProtocolStack();
094: Vector protocols = stack.getProtocols();
095: org.jgroups.stack.Protocol prot;
096: org.jgroups.jmx.Protocol p = null;
097: for (int i = 0; i < protocols.size(); i++) {
098: prot = (org.jgroups.stack.Protocol) protocols.get(i);
099: try {
100: p = findProtocol(prot);
101: } catch (ClassNotFoundException e) {
102: p = null;
103: } catch (Throwable e) {
104: log.error("failed creating a JMX wrapper instance for "
105: + prot, e);
106: p = null;
107: }
108: if (p == null)
109: p = new org.jgroups.jmx.Protocol(prot);
110: ObjectName prot_name = new ObjectName(prefix + ",protocol="
111: + prot.getName());
112: server.registerMBean(p, prot_name);
113: }
114: }
115:
116: public static void unregisterProtocols(MBeanServer server,
117: org.jgroups.JChannel channel, String channel_name) {
118: ProtocolStack stack = channel.getProtocolStack();
119: Vector protocols = stack.getProtocols();
120: org.jgroups.stack.Protocol prot;
121: ObjectName prot_name = null;
122: for (int i = 0; i < protocols.size(); i++) {
123: prot = (org.jgroups.stack.Protocol) protocols.get(i);
124: try {
125: prot_name = new ObjectName(channel_name + ",protocol="
126: + prot.getName());
127: server.unregisterMBean(prot_name);
128: } catch (Throwable e) {
129: log.error("failed to unregister " + prot_name, e);
130: }
131: }
132: }
133:
134: /**
135: * Unregisters object_name and everything under it
136: * @param object_name
137: */
138: public static void unregister(MBeanServer server, String object_name)
139: throws Exception {
140: Set mbeans = server.queryNames(new ObjectName(object_name),
141: null);
142: if (mbeans != null) {
143: ObjectName name;
144: for (Iterator it = mbeans.iterator(); it.hasNext();) {
145: name = (ObjectName) it.next();
146: server.unregisterMBean(name);
147: }
148: }
149: }
150:
151: protected static Protocol findProtocol(
152: org.jgroups.stack.Protocol prot)
153: throws ClassNotFoundException, IllegalAccessException,
154: InstantiationException {
155: Protocol p;
156: String prot_name = prot.getClass().getName();
157: String clname = prot_name.replaceFirst("org.jgroups.",
158: "org.jgroups.jmx.");
159: Class cl = Util.loadClass(clname, JmxConfigurator.class);
160: if (cl != null) {
161: p = (Protocol) cl.newInstance();
162: p.attachProtocol(prot);
163: return p;
164: }
165: return null;
166: }
167: }
|