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 ; 10 import java.util.Set ; 11 import java.util.Iterator ; 12 13 17 public class JmxConfigurator { 18 static final Log log=LogFactory.getLog(JmxConfigurator.class); 19 20 30 public static org.jgroups.jmx.JChannel registerChannel(org.jgroups.JChannel channel, 31 MBeanServer server, String domain, String cluster_name, 32 boolean register_protocols) throws Exception { 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 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 52 public static org.jgroups.jmx.JChannel registerChannel(org.jgroups.JChannel channel, 53 MBeanServer server, String name) throws Exception { 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 { 62 if(server != null) 63 server.unregisterMBean(name); 64 } 65 66 public static void unregisterChannel(MBeanServer server, String name) throws Exception { 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 name) throws Exception { 74 JChannelFactory retval=new JChannelFactory(factory); 75 server.registerMBean(retval, new ObjectName(name)); 76 return retval; 77 } 78 79 80 81 82 88 public static void registerProtocols(MBeanServer server, org.jgroups.JChannel channel, String prefix) throws Exception { 89 ProtocolStack stack=channel.getProtocolStack(); 90 Vector 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 e) { 99 p=null; 100 } 101 catch(Throwable 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 channel_name) { 113 ProtocolStack stack=channel.getProtocolStack(); 114 Vector 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 e) { 124 log.error("failed to unregister " + prot_name, e); 125 } 126 } 127 } 128 129 133 public static void unregister(MBeanServer server, String object_name) throws Exception { 134 Set mbeans=server.queryNames(new ObjectName(object_name), null); 135 if(mbeans != null) { 136 ObjectName name; 137 for(Iterator 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 , IllegalAccessException , InstantiationException { 146 Protocol p; 147 String prot_name=prot.getClass().getName(); 148 String clname=prot_name.replaceFirst("org.jgroups.", "org.jgroups.jmx."); 149 Class 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 |