1 8 9 package mx4j.server; 10 11 import java.lang.reflect.Method ; 12 import java.util.HashMap ; 13 import java.util.Map ; 14 import javax.management.MBeanAttributeInfo ; 15 import javax.management.MBeanOperationInfo ; 16 import javax.management.ReflectionException ; 17 18 import mx4j.util.MethodTernaryTree; 19 20 26 public class CachingReflectionMBeanInvoker extends ReflectionMBeanInvoker 27 { 28 private final Map attributes = new HashMap (); 29 private final Map attributeNames = new HashMap (); 30 private final MethodTernaryTree operations = new MethodTernaryTree(); 31 private final MethodTernaryTree methods = new MethodTernaryTree(); 32 33 protected MBeanOperationInfo getStandardOperationInfo(MBeanMetaData metadata, String method, String [] signature) 34 { 35 MBeanOperationInfo oper = null; 36 synchronized (operations) 37 { 38 oper = (MBeanOperationInfo )operations.get(method, signature); 39 } 40 if (oper != null) return oper; 41 42 MBeanOperationInfo info = super.getStandardOperationInfo(metadata, method, signature); 44 if (info != null) 45 { 46 synchronized (operations) 47 { 48 operations.put(method, signature, oper); 49 } 50 } 51 return info; 52 } 53 54 protected MBeanAttributeInfo getStandardAttributeInfo(MBeanMetaData metadata, String attribute, boolean forWrite) 55 { 56 MBeanAttributeInfo attr = null; 57 synchronized (attributes) 58 { 59 attr = (MBeanAttributeInfo )attributes.get(attribute); 60 } 61 62 if (attr == null) 63 { 64 attr = super.getStandardAttributeInfo(metadata, attribute, forWrite); 65 if (attr == null) return null; 66 67 synchronized (attributes) 68 { 69 attributes.put(attribute, attr); 70 } 71 } 72 73 if (forWrite && attr.isWritable()) return attr; 74 if (!forWrite && attr.isReadable()) return attr; 75 76 return null; 77 } 78 79 protected String getMethodForAttribute(MBeanAttributeInfo attribute, boolean getter) 80 { 81 AttributeName attributeName = null; 82 String name = attribute.getName(); 83 synchronized (attributeNames) 84 { 85 attributeName = (AttributeName)attributeNames.get(name); 86 } 87 if (attributeName == null) 88 { 89 attributeName = new AttributeName(super.getMethodForAttribute(attribute, true), super.getMethodForAttribute(attribute, false)); 90 synchronized (attributeNames) 91 { 92 attributeNames.put(name, attributeName); 93 } 94 } 95 96 if (getter) return attributeName.getter; 97 return attributeName.setter; 98 } 99 100 protected Method getStandardManagementMethod(MBeanMetaData metadata, String name, String [] signature) throws ReflectionException 101 { 102 Method method = null; 103 synchronized (methods) 104 { 105 method = (Method )methods.get(name, signature); 106 } 107 if (method != null) return method; 108 109 method = super.getStandardManagementMethod(metadata, name, signature); 111 synchronized (methods) 112 { 113 methods.put(name, signature, method); 114 } 115 return method; 116 } 117 118 private static class AttributeName 119 { 120 private final String getter; 121 private final String setter; 122 123 public AttributeName(String getter, String setter) 124 { 125 this.getter = getter; 126 this.setter = setter; 127 } 128 } 129 } 130 | Popular Tags |