KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > jmx > JmxConfigurator


1 package org.jgroups.jmx;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5 import org.jgroups.stack.ProtocolStack;
6 import org.jgroups.util.Util;
7
8 import javax.management.*;
9 import java.util.Vector JavaDoc;
10 import java.util.Set JavaDoc;
11 import java.util.Iterator JavaDoc;
12
13 /**
14  * @author Bela Ban
15  * @version $Id: JmxConfigurator.java,v 1.10 2006/08/09 13:02:21 belaban Exp $
16  */

17 public class JmxConfigurator {
18     static final Log log=LogFactory.getLog(JmxConfigurator.class);
19
20     /**
21      * Registers an already created channel with the MBeanServer. Creates an org.jgroups.jmx.JChannel which
22      * delegates to the org.jgroups.JChannel and registers it. Optionally, this method will also try to
23      * create one MBean proxy for each protocol in the channel's protocol stack, and register it as well.
24      * @param channel
25      * @param server
26      * @param domain Has to be a JMX ObjectName of the domain, e.g. DefaultDomain:name=JGroups
27      * @param register_protocols
28      * @return org.jgroups.jmx.JChannel for the specified org.jgroups.JChannel
29      */

30     public static org.jgroups.jmx.JChannel registerChannel(org.jgroups.JChannel channel,
31                                                            MBeanServer server, String JavaDoc domain, String JavaDoc cluster_name,
32                                                            boolean register_protocols) throws Exception JavaDoc {
33         if(cluster_name == null)
34             cluster_name=channel != null? channel.getClusterName() : null;
35         if(cluster_name == null)
36             cluster_name="null";
37         if(register_protocols) {
38             String JavaDoc tmp=domain + ":type=protocol,cluster=" +cluster_name;
39             registerProtocols(server, channel, tmp);
40         }
41         return registerChannel(channel, server, domain + ":type=channel,cluster=" +cluster_name);
42     }
43
44     /**
45      * Registers an already created channel with the MBeanServer. Creates an org.jgroups.jmx.JChannel which
46      * delegates to the org.jgroups.JChannel and registers it.
47      * @param channel
48      * @param server
49      * @param name The JMX ObjectName
50      * @return org.jgroups.jmx.JChannel for the specified org.jgroups.JChannel
51      */

52     public static org.jgroups.jmx.JChannel registerChannel(org.jgroups.JChannel channel,
53                                                            MBeanServer server, String JavaDoc name) throws Exception JavaDoc {
54         JChannel retval=new JChannel(channel);
55         server.registerMBean(retval, new ObjectName(name));
56         return retval;
57     }
58     
59     
60     
61     public static void unregisterChannel(MBeanServer server, ObjectName name) throws Exception JavaDoc {
62         if(server != null)
63             server.unregisterMBean(name);
64     }
65
66     public static void unregisterChannel(MBeanServer server, String JavaDoc name) throws Exception JavaDoc {
67         if(server != null)
68             server.unregisterMBean(new ObjectName(name));
69     }
70
71
72     public static org.jgroups.jmx.JChannelFactory registerChannelFactory(org.jgroups.JChannelFactory factory,
73                                                                          MBeanServer server, String JavaDoc name) throws Exception JavaDoc {
74         JChannelFactory retval=new JChannelFactory(factory);
75         server.registerMBean(retval, new ObjectName(name));
76         return retval;
77     }
78
79     
80
81
82     /**
83      * Takes all protocols of an existing stack, creates corresponding MBean proxies and registers them with
84      * the MBean server
85      * @param channel
86      * @param prefix
87      */

88     public static void registerProtocols(MBeanServer server, org.jgroups.JChannel channel, String JavaDoc prefix) throws Exception JavaDoc {
89         ProtocolStack stack=channel.getProtocolStack();
90         Vector JavaDoc protocols=stack.getProtocols();
91         org.jgroups.stack.Protocol prot;
92         org.jgroups.jmx.Protocol p=null;
93         for(int i=0; i < protocols.size(); i++) {
94             prot=(org.jgroups.stack.Protocol)protocols.get(i);
95             try {
96                 p=findProtocol(prot);
97             }
98             catch(ClassNotFoundException JavaDoc e) {
99                 p=null;
100             }
101             catch(Throwable JavaDoc e) {
102                 log.error("failed creating a JMX wrapper instance for " + prot, e);
103                 p=null;
104             }
105             if(p == null)
106                 p=new org.jgroups.jmx.Protocol(prot);
107             ObjectName prot_name=new ObjectName(prefix + ",protocol=" + prot.getName());
108             server.registerMBean(p, prot_name);
109         }
110     }
111
112     public static void unregisterProtocols(MBeanServer server, org.jgroups.JChannel channel, String JavaDoc channel_name) {
113         ProtocolStack stack=channel.getProtocolStack();
114         Vector JavaDoc protocols=stack.getProtocols();
115         org.jgroups.stack.Protocol prot;
116         ObjectName prot_name=null;
117         for(int i=0; i < protocols.size(); i++) {
118             prot=(org.jgroups.stack.Protocol)protocols.get(i);
119             try {
120                 prot_name=new ObjectName(channel_name + ",protocol=" + prot.getName());
121                 server.unregisterMBean(prot_name);
122             }
123             catch(Throwable JavaDoc e) {
124                 log.error("failed to unregister " + prot_name, e);
125             }
126         }
127     }
128
129     /**
130      * Unregisters object_name and everything under it
131      * @param object_name
132      */

133     public static void unregister(MBeanServer server, String JavaDoc object_name) throws Exception JavaDoc {
134         Set JavaDoc mbeans=server.queryNames(new ObjectName(object_name), null);
135         if(mbeans != null) {
136             ObjectName name;
137             for(Iterator JavaDoc it=mbeans.iterator(); it.hasNext();) {
138                 name=(ObjectName)it.next();
139                 server.unregisterMBean(name);
140             }
141         }
142     }
143
144
145     protected static Protocol findProtocol(org.jgroups.stack.Protocol prot) throws ClassNotFoundException JavaDoc, IllegalAccessException JavaDoc, InstantiationException JavaDoc {
146         Protocol p;
147         String JavaDoc prot_name=prot.getClass().getName();
148         String JavaDoc clname=prot_name.replaceFirst("org.jgroups.", "org.jgroups.jmx.");
149         Class JavaDoc cl=Util.loadClass(clname, JmxConfigurator.class);
150         if(cl != null) {
151             p=(Protocol)cl.newInstance();
152             p.attachProtocol(prot);
153             return p;
154         }
155         return null;
156     }
157 }
158
Popular Tags