1 16 17 package org.springframework.jmx.export.assembler; 18 19 import java.lang.reflect.Method ; 20 import java.lang.reflect.Modifier ; 21 import java.util.Arrays ; 22 import java.util.Enumeration ; 23 import java.util.HashMap ; 24 import java.util.Map ; 25 import java.util.Properties ; 26 27 import org.springframework.beans.factory.BeanClassLoaderAware; 28 import org.springframework.beans.factory.InitializingBean; 29 import org.springframework.util.ClassUtils; 30 import org.springframework.util.StringUtils; 31 32 60 public class InterfaceBasedMBeanInfoAssembler extends AbstractConfigurableMBeanInfoAssembler 61 implements BeanClassLoaderAware, InitializingBean { 62 63 66 private Class [] managedInterfaces; 67 68 71 private Properties interfaceMappings; 72 73 private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader(); 74 75 78 private Map resolvedInterfaceMappings; 79 80 81 89 public void setManagedInterfaces(Class [] managedInterfaces) { 90 if (managedInterfaces != null) { 91 for (int x = 0; x < managedInterfaces.length; x++) { 92 if (!managedInterfaces[x].isInterface()) { 93 throw new IllegalArgumentException ( 94 "Management interface [" + managedInterfaces[x].getName() + "] is no interface"); 95 } 96 } 97 } 98 this.managedInterfaces = managedInterfaces; 99 } 100 101 108 public void setInterfaceMappings(Properties mappings) { 109 this.interfaceMappings = mappings; 110 } 111 112 public void setBeanClassLoader(ClassLoader beanClassLoader) { 113 this.beanClassLoader = beanClassLoader; 114 } 115 116 117 public void afterPropertiesSet() { 118 if (this.interfaceMappings != null) { 119 this.resolvedInterfaceMappings = resolveInterfaceMappings(this.interfaceMappings); 120 } 121 } 122 123 128 private Map resolveInterfaceMappings(Properties mappings) { 129 Map resolvedMappings = new HashMap (mappings.size()); 130 for (Enumeration en = mappings.propertyNames(); en.hasMoreElements();) { 131 String beanKey = (String ) en.nextElement(); 132 String [] classNames = StringUtils.commaDelimitedListToStringArray(mappings.getProperty(beanKey)); 133 Class [] classes = resolveClassNames(classNames, beanKey); 134 resolvedMappings.put(beanKey, classes); 135 } 136 return resolvedMappings; 137 } 138 139 145 private Class [] resolveClassNames(String [] classNames, String beanKey) { 146 Class [] classes = new Class [classNames.length]; 147 for (int x = 0; x < classes.length; x++) { 148 Class cls = ClassUtils.resolveClassName(classNames[x].trim(), this.beanClassLoader); 149 if (!cls.isInterface()) { 150 throw new IllegalArgumentException ( 151 "Class [" + classNames[x] + "] mapped to bean key [" + beanKey + "] is no interface"); 152 } 153 classes[x] = cls; 154 } 155 return classes; 156 } 157 158 159 168 protected boolean includeReadAttribute(Method method, String beanKey) { 169 return isPublicInInterface(method, beanKey); 170 } 171 172 181 protected boolean includeWriteAttribute(Method method, String beanKey) { 182 return isPublicInInterface(method, beanKey); 183 } 184 185 194 protected boolean includeOperation(Method method, String beanKey) { 195 return isPublicInInterface(method, beanKey); 196 } 197 198 206 private boolean isPublicInInterface(Method method, String beanKey) { 207 return ((method.getModifiers() & Modifier.PUBLIC) > 0) && isDeclaredInInterface(method, beanKey); 208 } 209 210 214 private boolean isDeclaredInInterface(Method method, String beanKey) { 215 Class [] ifaces = null; 216 217 if (this.resolvedInterfaceMappings != null) { 218 ifaces = (Class []) this.resolvedInterfaceMappings.get(beanKey); 219 } 220 221 if (ifaces == null) { 222 ifaces = this.managedInterfaces; 223 if (ifaces == null) { 224 ifaces = ClassUtils.getAllInterfacesForClass(method.getDeclaringClass()); 225 } 226 } 227 228 if (ifaces != null) { 229 for (int i = 0; i < ifaces.length; i++) { 230 Method [] methods = ifaces[i].getMethods(); 231 for (int j = 0; j < methods.length; j++) { 232 Method ifaceMethod = methods[j]; 233 if (ifaceMethod.getName().equals(method.getName()) && 234 Arrays.equals(ifaceMethod.getParameterTypes(), method.getParameterTypes())) { 235 return true; 236 } 237 } 238 } 239 } 240 241 return false; 242 } 243 244 } 245 | Popular Tags |