1 22 package org.jboss.cache.jmx; 23 24 import org.jboss.cache.config.Configuration; 25 import org.jboss.cache.interceptors.Interceptor; 26 27 import javax.management.MBeanServer ; 28 import javax.management.MBeanServerFactory ; 29 import javax.management.ObjectName ; 30 import java.util.ArrayList ; 31 import java.util.List ; 32 33 40 public class JmxUtil 41 { 42 public static final String MBEAN_CLASS_SUFFIX = "MBean"; 43 public static final String MBEAN_KEY = ",cache-interceptor="; 44 public static final String JBOSS_CACHE_DOMAIN = "jboss.cache"; 45 public static final String PREFIX = JBOSS_CACHE_DOMAIN + ":service=Cache,clusterName="; 46 private static final String JBOSS_SERVER_DOMAIN = "jboss"; 47 public static final String CACHE_TYPE_KEY = "cacheType="; 48 public static final String PLAIN_CACHE_TYPE = "cacheType=Cache"; 49 50 public static void registerCacheMBean(MBeanServer server, CacheJmxWrapperMBean cache, String cacheObjectName) 51 throws Exception 52 { 53 ObjectName on = new ObjectName (cacheObjectName); 54 if (!server.isRegistered(on)) 55 { 56 server.registerMBean(cache, on); 57 } 58 } 59 60 67 public static void registerInterceptors(MBeanServer server, List <Interceptor> interceptors, String cacheObjectName) 68 throws Exception 69 { 70 if (server == null || interceptors == null || cacheObjectName == null) 71 { 72 return; 73 } 74 75 Interceptor interceptor = null; 76 77 for (int i = 0; i < interceptors.size(); i++) 78 { 79 interceptor = interceptors.get(i); 80 boolean mbeanExists = true; 81 try 82 { 83 Class.forName(interceptor.getClass().getName() + MBEAN_CLASS_SUFFIX); 85 } 86 catch (Throwable e) 87 { 88 mbeanExists = false; 90 } 91 92 String className = interceptor.getClass().getName(); 95 String serviceName = cacheObjectName + MBEAN_KEY + className.substring(className.lastIndexOf('.') + 1); 96 97 ObjectName objName = new ObjectName (serviceName); 98 if (!server.isRegistered(objName)) 99 { 100 if (mbeanExists) 101 { 103 server.registerMBean(interceptor, objName); 104 } 105 } 109 } 110 } 111 112 public static String getCacheObjectName(org.jboss.cache.CacheSPI cache) 113 { 114 return getCacheObjectName(cache.getConfiguration(), cache.getClass().getName()); 116 } 117 118 public static String getCacheObjectName(Configuration config, String cacheSPIClass) 119 { 120 String tmpName = config.getServiceName(); 122 if (tmpName == null) 123 { 124 if (config.getClusterName() == null) 125 { 126 tmpName = PREFIX + cacheSPIClass + System.currentTimeMillis(); 127 } 128 else 129 { 130 tmpName = PREFIX + config.getClusterName(); 131 } 132 } 133 134 return tmpName; 135 } 136 137 public static void unregisterCacheMBean(MBeanServer server, String cacheObjectName) 138 throws Exception 139 { 140 server.unregisterMBean(new ObjectName (cacheObjectName)); 141 } 142 143 150 public static void unregisterInterceptors(MBeanServer server, List <Interceptor> interceptors, String cacheObjectName) 151 throws Exception 152 { 153 if (server == null || interceptors == null || cacheObjectName == null) 154 { 155 return; 156 } 157 158 Interceptor interceptor = null; 159 160 for (int i = 0; i < interceptors.size(); i++) 161 { 162 interceptor = interceptors.get(i); 163 164 String className = interceptor.getClass().getName(); 167 String serviceName = cacheObjectName + MBEAN_KEY + className.substring(className.lastIndexOf('.') + 1); 168 169 ObjectName objName = new ObjectName (serviceName); 170 if (server.isRegistered(objName)) 171 { 172 server.unregisterMBean(objName); 173 } 174 } 175 } 176 177 public static MBeanServer getMBeanServer() 178 { 179 ArrayList servers = MBeanServerFactory.findMBeanServer(null); 180 if (servers == null || servers.size() == 0) 181 { 182 return null; 183 } 184 185 for (int i = 0; i < servers.size(); i++) 187 { 188 MBeanServer server = (MBeanServer ) servers.get(i); 189 if (server.getDefaultDomain().equalsIgnoreCase(JBOSS_SERVER_DOMAIN)) 190 { 191 return server; 192 } 193 } 194 195 return (MBeanServer ) servers.get(0); 197 198 } 199 } 200 | Popular Tags |