1 7 8 package javax.management.openmbean; 9 10 import com.sun.jmx.mbeanserver.MXBeanLookup; 11 import com.sun.jmx.mbeanserver.OpenConverter; 12 import java.lang.reflect.InvocationHandler ; 13 import java.lang.reflect.Method ; 14 import java.lang.reflect.Proxy ; 15 16 88 public class CompositeDataInvocationHandler implements InvocationHandler { 89 99 public CompositeDataInvocationHandler(CompositeData compositeData) { 100 this(compositeData, null); 101 } 102 103 118 CompositeDataInvocationHandler(CompositeData compositeData, 119 MXBeanLookup lookup) { 120 if (compositeData == null) 121 throw new IllegalArgumentException ("compositeData"); 122 this.compositeData = compositeData; 123 this.lookup = lookup; 124 } 125 126 132 public CompositeData getCompositeData() { 133 assert compositeData != null; 134 return compositeData; 135 } 136 137 public Object invoke(Object proxy, Method method, Object [] args) 138 throws Throwable { 139 final String methodName = method.getName(); 140 141 if (method.getDeclaringClass() == Object .class) { 143 if (methodName.equals("toString") && args == null) 144 return "Proxy[" + compositeData + "]"; 145 else if (methodName.equals("hashCode") && args == null) 146 return compositeData.hashCode() + 0x43444948; 147 else if (methodName.equals("equals") && args.length == 1 148 && method.getParameterTypes()[0] == Object .class) 149 return equals(proxy, args[0]); 150 else { 151 157 return method.invoke(this, args); 158 } 159 } 160 161 String propertyName = OpenConverter.propertyName(method); 162 if (propertyName == null) { 163 throw new IllegalArgumentException ("Method is not getter: " + 164 method.getName()); 165 } 166 Object openValue; 167 if (compositeData.containsKey(propertyName)) 168 openValue = compositeData.get(propertyName); 169 else { 170 String decap = OpenConverter.decapitalize(propertyName); 171 if (compositeData.containsKey(decap)) 172 openValue = compositeData.get(decap); 173 else { 174 final String msg = 175 "No CompositeData item " + propertyName + 176 (decap.equals(propertyName) ? "" : " or " + decap) + 177 " to match " + methodName; 178 throw new IllegalArgumentException (msg); 179 } 180 } 181 OpenConverter converter = 182 OpenConverter.toConverter(method.getGenericReturnType()); 183 return converter.fromOpenValue(lookup, openValue); 184 } 185 186 209 private boolean equals(Object proxy, Object other) { 210 if (other == null) 211 return false; 212 213 final Class proxyClass = proxy.getClass(); 214 final Class otherClass = other.getClass(); 215 if (proxyClass != otherClass) 216 return false; 217 InvocationHandler otherih = Proxy.getInvocationHandler(other); 218 if (!(otherih instanceof CompositeDataInvocationHandler )) 219 return false; 220 CompositeDataInvocationHandler othercdih = 221 (CompositeDataInvocationHandler ) otherih; 222 return compositeData.equals(othercdih.compositeData); 223 } 224 225 private final CompositeData compositeData; 226 private final MXBeanLookup lookup; 227 } 228 | Popular Tags |