1 22 package org.jboss.mx.capability; 23 24 import org.jboss.mx.metadata.AttributeOperationResolver; 25 import org.jboss.mx.metadata.MethodMapper; 26 27 import org.jboss.mx.server.ServerConstants; 28 import org.jboss.mx.util.PropertyAccess; 29 30 import javax.management.DynamicMBean ; 31 import javax.management.IntrospectionException ; 32 import javax.management.MBeanAttributeInfo ; 33 import javax.management.MBeanInfo ; 34 import javax.management.MBeanOperationInfo ; 35 import javax.management.Descriptor ; 36 import javax.management.modelmbean.ModelMBeanAttributeInfo ; 37 import java.lang.reflect.Method ; 38 39 45 public class DispatcherFactory 46 implements ServerConstants 47 { 48 49 53 public static DynamicMBean create(MBeanInfo info, Object resource) throws IntrospectionException 54 { 55 return create(info, resource, new AttributeOperationResolver(info)); 56 } 57 58 61 public static DynamicMBean create(MBeanInfo info, Object resource, AttributeOperationResolver resolver) throws IntrospectionException 62 { 63 if (null == info) 64 { 65 throw new IllegalArgumentException ("info cannot be null"); 66 } 67 68 if (null == resolver) 69 { 70 throw new IllegalArgumentException ("resolver cannot be null"); 71 } 72 73 if (null == resource) 74 { 75 throw new IllegalArgumentException ("resource cannot be null"); 76 } 77 78 MethodMapper mmap = new MethodMapper(resource.getClass()); 79 ReflectedMBeanDispatcher dispatcher = new ReflectedMBeanDispatcher(info, resolver, resource); 80 81 String flag = PropertyAccess.getProperty(OPTIMIZE_REFLECTED_DISPATCHER, "false"); 82 if (flag.equalsIgnoreCase("true")) 83 { 84 dispatcher = OptimizedMBeanDispatcher.create(info, resource ); 87 } 88 89 MBeanAttributeInfo [] attributes = info.getAttributes(); 90 for (int i = 0; i < attributes.length; i++) 91 { 92 MBeanAttributeInfo attribute = attributes[i]; 93 Method getter = null; 94 Method setter = null; 95 96 if (attribute.isReadable()) 97 { 98 if (attribute instanceof ModelMBeanAttributeInfo ) 99 { 100 ModelMBeanAttributeInfo mmbAttribute = (ModelMBeanAttributeInfo ) attribute; 101 Descriptor desc = mmbAttribute.getDescriptor(); 102 if (desc != null && desc.getFieldValue("getMethod") != null) 103 { 104 getter = mmap.lookupGetter(mmbAttribute); 105 if (getter == null) 106 { 107 throw new IntrospectionException ("no getter method found for attribute: " + attribute.getName()); 108 } 109 } 110 } 111 else 112 { 113 getter = mmap.lookupGetter(attribute); 114 if (getter == null) 115 { 116 throw new IntrospectionException ("no getter method found for attribute: " + attribute.getName()); 117 } 118 } 119 } 120 121 if (attribute.isWritable()) 122 { 123 if (attribute instanceof ModelMBeanAttributeInfo ) 124 { 125 ModelMBeanAttributeInfo mmbAttribute = (ModelMBeanAttributeInfo ) attribute; 126 Descriptor desc = mmbAttribute.getDescriptor(); 127 if (desc != null && desc.getFieldValue("setMethod") != null) 128 { 129 setter = mmap.lookupSetter(mmbAttribute); 130 if (setter == null) 131 { 132 throw new IntrospectionException ("no setter method found for attribute: " + attribute.getName()); 133 } 134 } 135 } 136 else 137 { 138 setter = mmap.lookupSetter(attribute); 139 if (setter == null) 140 { 141 throw new IntrospectionException ("no setter method found for attribute: " + attribute.getName()); 142 } 143 } 144 } 145 146 dispatcher.bindAttributeAt(i, getter, setter); 147 } 148 149 MBeanOperationInfo [] operations = info.getOperations(); 150 for (int i = 0; i < operations.length; i++) 151 { 152 MBeanOperationInfo operation = operations[i]; 153 Method method = mmap.lookupOperation(operation); 154 if (method == null) 155 { 156 throw new IntrospectionException ("no method found for operation: " + operation.getName()); } 158 159 dispatcher.bindOperationAt(i, method); 160 } 161 162 return dispatcher; 163 } 164 } 165 | Popular Tags |