1 22 package org.jboss.mx.mxbean; 23 24 import java.lang.reflect.InvocationHandler ; 25 import java.lang.reflect.Method ; 26 import java.lang.reflect.Proxy ; 27 import java.lang.reflect.Type ; 28 29 import javax.management.openmbean.CompositeData ; 30 import javax.management.openmbean.InvalidKeyException ; 31 32 import org.jboss.util.UnreachableStatementException; 33 34 40 public class CompositeDataInvocationHandler implements InvocationHandler 41 { 42 43 private CompositeData compositeData; 44 45 50 public CompositeDataInvocationHandler(CompositeData compositeData) 51 { 52 if (compositeData == null) 53 throw new IllegalArgumentException ("Null compositeData"); 54 this.compositeData = compositeData; 55 } 56 57 62 public CompositeData getCompositeData() 63 { 64 return compositeData; 65 } 66 67 public Object invoke(Object proxy, Method method, Object [] args) throws Throwable 68 { 69 if (Object .class.equals(method.getDeclaringClass())) 70 return handleObjectInvocation(method.getName(), args); 71 72 Object value = getValue(method); 73 74 Type returnType = method.getGenericReturnType(); 75 return MXBeanUtils.reconstruct(returnType, value, method); 76 } 77 78 private Object getValue(Method method) 79 { 80 String key = MXBeanUtils.getCompositeDataKey(method); 81 if (key == null) 82 throw new IllegalArgumentException ("Unsupported method '" + method + "'; it must be a property getter."); 83 try 84 { 85 return compositeData.get(key); 86 } 87 catch (InvalidKeyException e) 88 { 89 throw new IllegalArgumentException ("Unsupported method '" + method + "'; it must be a property getter for one of the item names of the composite data: " + compositeData, e); 90 } 91 } 92 93 private Object handleObjectInvocation(String name, Object [] args) throws Throwable 94 { 95 if ("equals".equals(name)) 96 { 97 Object object = args[0]; 98 if (object == null || object instanceof Proxy == false) 99 return false; 100 InvocationHandler handler = Proxy.getInvocationHandler(object); 101 if (handler == this) 102 return true; 103 if (handler == null || handler instanceof CompositeDataInvocationHandler == false) 104 return false; 105 106 CompositeDataInvocationHandler other = (CompositeDataInvocationHandler) handler; 107 return getCompositeData().equals(other.getCompositeData()); 108 } 109 else if ("hashCode".equals(name)) 110 return getCompositeData().hashCode(); 111 else if ("toString".equals(name)) 112 return getCompositeData().toString(); 113 throw new UnreachableStatementException(); 114 } 115 } 116 | Popular Tags |