1 16 17 package org.springframework.jmx.support; 18 19 import java.beans.PropertyDescriptor ; 20 import java.lang.management.ManagementFactory ; 21 import java.lang.reflect.Method ; 22 import java.util.Hashtable ; 23 import java.util.List ; 24 25 import javax.management.DynamicMBean ; 26 import javax.management.MBeanParameterInfo ; 27 import javax.management.MBeanServer ; 28 import javax.management.MBeanServerFactory ; 29 import javax.management.MalformedObjectNameException ; 30 import javax.management.ObjectName ; 31 32 import org.apache.commons.logging.Log; 33 import org.apache.commons.logging.LogFactory; 34 35 import org.springframework.core.JdkVersion; 36 import org.springframework.jmx.MBeanServerNotFoundException; 37 import org.springframework.util.ClassUtils; 38 import org.springframework.util.ObjectUtils; 39 import org.springframework.util.StringUtils; 40 41 50 public abstract class JmxUtils { 51 52 56 public static final String IDENTITY_OBJECT_NAME_KEY = "identity"; 57 58 61 private static final String MBEAN_SUFFIX = "MBean"; 62 63 64 private static final Log logger = LogFactory.getLog(JmxUtils.class); 65 66 67 76 public static MBeanServer locateMBeanServer() throws MBeanServerNotFoundException { 77 return locateMBeanServer(null); 78 } 79 80 92 public static MBeanServer locateMBeanServer(String agentId) throws MBeanServerNotFoundException { 93 List servers = MBeanServerFactory.findMBeanServer(agentId); 94 95 MBeanServer server = null; 96 if (servers != null && servers.size() > 0) { 97 if (servers.size() > 1 && logger.isWarnEnabled()) { 99 logger.warn("Found more than one MBeanServer instance" + 100 (agentId != null ? " with agent id [" + agentId + "]" : "") + 101 ". Returning first from list."); 102 } 103 server = (MBeanServer ) servers.get(0); 104 } 105 106 if (server == null && agentId == null && JdkVersion.isAtLeastJava15()) { 107 try { 109 server = ManagementFactory.getPlatformMBeanServer(); 110 } 111 catch (SecurityException ex) { 112 throw new MBeanServerNotFoundException("No specific MBeanServer found, " + 113 "and not allowed to obtain the Java platform MBeanServer", ex); 114 } 115 } 116 117 if (server == null) { 118 throw new MBeanServerNotFoundException( 119 "Unable to locate an MBeanServer instance" + 120 (agentId != null ? " with agent id [" + agentId + "]" : "")); 121 } 122 123 if (logger.isDebugEnabled()) { 124 logger.debug("Found MBeanServer: " + server); 125 } 126 return server; 127 } 128 129 136 public static Class [] parameterInfoToTypes(MBeanParameterInfo [] paramInfo) throws ClassNotFoundException { 137 Class [] types = null; 138 if (paramInfo != null && paramInfo.length > 0) { 139 types = new Class [paramInfo.length]; 140 for (int x = 0; x < paramInfo.length; x++) { 141 types[x] = ClassUtils.forName(paramInfo[x].getType()); 142 } 143 } 144 return types; 145 } 146 147 154 public static String [] getMethodSignature(Method method) { 155 Class [] types = method.getParameterTypes(); 156 String [] signature = new String [types.length]; 157 for (int x = 0; x < types.length; x++) { 158 signature[x] = types[x].getName(); 159 } 160 return signature; 161 } 162 163 173 public static String getAttributeName(PropertyDescriptor property, boolean useStrictCasing) { 174 if (useStrictCasing) { 175 return StringUtils.capitalize(property.getName()); 176 } 177 else { 178 return property.getName(); 179 } 180 } 181 182 191 public static boolean isMBean(Class beanClass) { 192 if (beanClass == null) { 193 return false; 194 } 195 if (DynamicMBean .class.isAssignableFrom(beanClass)) { 196 return true; 197 } 198 Class cls = beanClass; 199 while (cls != null && cls != Object .class) { 200 if (hasMBeanInterface(cls)) { 201 return true; 202 } 203 cls = cls.getSuperclass(); 204 } 205 return false; 206 } 207 208 218 public static Class getClassToExpose(Object managedBean) { 219 return ClassUtils.getUserClass(managedBean); 220 } 221 222 232 public static Class getClassToExpose(Class beanClass) { 233 return ClassUtils.getUserClass(beanClass); 234 } 235 236 243 private static boolean hasMBeanInterface(Class clazz) { 244 Class [] implementedInterfaces = clazz.getInterfaces(); 245 String mbeanInterfaceName = clazz.getName() + MBEAN_SUFFIX; 246 for (int x = 0; x < implementedInterfaces.length; x++) { 247 Class iface = implementedInterfaces[x]; 248 if (iface.getName().equals(mbeanInterfaceName)) { 249 return true; 250 } 251 } 252 return false; 253 } 254 255 269 public static ObjectName appendIdentityToObjectName(ObjectName objectName, Object managedResource) 270 throws MalformedObjectNameException { 271 272 Hashtable keyProperties = objectName.getKeyPropertyList(); 273 keyProperties.put(IDENTITY_OBJECT_NAME_KEY, ObjectUtils.getIdentityHexString(managedResource)); 274 return ObjectNameManager.getInstance(objectName.getDomain(), keyProperties); 275 } 276 277 } 278 | Popular Tags |