1 22 package org.jboss.mx.interceptor; 23 24 import java.lang.reflect.Method ; 25 26 import javax.management.Descriptor ; 27 import javax.management.InvalidAttributeValueException ; 28 import javax.management.MBeanException ; 29 import javax.management.ServiceNotFoundException ; 30 import javax.management.modelmbean.InvalidTargetObjectTypeException ; 31 32 import org.jboss.mx.modelmbean.ModelMBeanConstants; 33 import org.jboss.mx.server.Invocation; 34 import org.jboss.mx.server.MBeanInvoker; 35 36 42 public class AttributeDispatcher 43 extends ReflectedDispatcher 44 { 45 private Method getter; 46 private Method setter; 47 48 public AttributeDispatcher(Method getter, Method setter, boolean dynamic) 49 { 50 super(dynamic); 51 setName("Attribute Dispatcher"); 52 this.getter = getter; 53 this.setter = setter; 54 } 55 56 62 public Object invoke(Invocation invocation) throws Throwable 63 { 64 Object target = invocation.getTarget(); 65 66 Object value = null; 67 Object [] args = invocation.getArgs(); 68 if( args == null ) 70 { 71 Method getMethod = getter; 72 if (dynamic) 73 { 74 Descriptor d = invocation.getDescriptor(); 75 if (d != null) 76 { 77 Object descriptorTarget = d.getFieldValue(ModelMBeanConstants.TARGET_OBJECT); 78 if (descriptorTarget != null) 79 { 80 String targetType = (String ) d.getFieldValue(ModelMBeanConstants.TARGET_TYPE); 81 if (ModelMBeanConstants.OBJECT_REF.equalsIgnoreCase(targetType) == false) 82 throw new InvalidTargetObjectTypeException ("Target type is " + targetType); 83 target = descriptorTarget; 84 } 85 String getMethodString = (String ) d.getFieldValue(ModelMBeanConstants.GET_METHOD); 86 if (getMethodString != null && (getMethod == null || getMethodString.equals(getMethod.getName()) == false)) 87 { 88 MBeanInvoker invoker = invocation.getInvoker(); 89 Object object = invoker.invoke(getMethodString, new Object [0], new String [0]); 90 checkAssignable(getMethodString, invocation.getAttributeTypeClass(), object); 91 return object; 92 } 93 } 94 } 95 if (target == null) 96 throw new MBeanException (new ServiceNotFoundException ("No Target")); 97 try 98 { 99 value = getMethod.invoke(target, args); 100 } 101 catch (Throwable t) 102 { 103 handleInvocationExceptions(t); 104 return null; 105 } 106 } 107 else 109 { 110 Method setMethod = setter; 111 if (dynamic) 112 { 113 Descriptor d = invocation.getDescriptor(); 114 if (d != null) 115 { 116 Object descriptorTarget = d.getFieldValue(ModelMBeanConstants.TARGET_OBJECT); 117 if (descriptorTarget != null) 118 { 119 String targetType = (String ) d.getFieldValue(ModelMBeanConstants.TARGET_TYPE); 120 if (ModelMBeanConstants.OBJECT_REF.equalsIgnoreCase(targetType) == false) 121 throw new InvalidTargetObjectTypeException ("Target type is " + targetType); 122 target = descriptorTarget; 123 } 124 String setMethodString = (String ) d.getFieldValue(ModelMBeanConstants.SET_METHOD); 125 if (setMethodString != null && (setMethod == null || setMethodString.equals(setMethod.getName()) == false)) 126 { 127 MBeanInvoker invoker = invocation.getInvoker(); 128 return invoker.invoke(setMethodString, new Object [] { args[0] }, new String [] { invocation.getAttributeType() }); 129 } 130 } 131 } 132 if (target == null) 133 throw new MBeanException (new ServiceNotFoundException ("No Target")); 134 try 135 { 136 value = setMethod.invoke(target, args); 137 } 138 catch (Throwable t) 139 { 140 handleInvocationExceptions(t); 141 return null; 142 } 143 } 144 return value; 145 } 146 147 protected void checkAssignable(String context, Class clazz, Object value) throws InvalidAttributeValueException , ClassNotFoundException 148 { 149 if (value != null && clazz.isAssignableFrom(value.getClass()) == false) 150 throw new InvalidAttributeValueException (context + " has class " + value.getClass() + " loaded from " + value.getClass().getClassLoader() + 151 " that is not assignable to attribute class " + clazz + " loaded from " + clazz.getClassLoader()); 152 } 153 } 154 | Popular Tags |