1 8 9 package mx4j.server; 10 11 import java.lang.reflect.InvocationTargetException ; 12 import java.lang.reflect.Method ; 13 14 import javax.management.Attribute ; 15 import javax.management.AttributeNotFoundException ; 16 import javax.management.InvalidAttributeValueException ; 17 import javax.management.JMRuntimeException ; 18 import javax.management.MBeanAttributeInfo ; 19 import javax.management.MBeanException ; 20 import javax.management.MBeanOperationInfo ; 21 import javax.management.MBeanParameterInfo ; 22 import javax.management.ReflectionException ; 23 import javax.management.RuntimeErrorException ; 24 import javax.management.RuntimeMBeanException ; 25 import javax.management.RuntimeOperationsException ; 26 27 import mx4j.util.Utils; 28 import mx4j.ImplementationException; 29 30 36 public class ReflectionMBeanInvoker implements MBeanInvoker 37 { 38 41 protected static final String [] EMPTY_PARAMS = new String [0]; 42 45 protected static final Object [] EMPTY_ARGS = new Object [0]; 46 47 public Object invoke(MBeanMetaData metadata, String method, String [] params, Object [] args) throws MBeanException , ReflectionException  48 { 49 MBeanOperationInfo oper = getStandardOperationInfo(metadata, method, params); 50 if (oper != null) 51 { 52 try 53 { 54 return doInvoke(metadata, method, params, args); 55 } 56 catch (BadArgumentException x) 57 { 58 throw new RuntimeOperationsException (x.nested); 59 } 60 } 61 else 62 { 63 throw new ReflectionException (new NoSuchMethodException ("Operation " + method + " does not belong to the management interface")); 64 } 65 } 66 67 public Object getAttribute(MBeanMetaData metadata, String attribute) throws MBeanException , AttributeNotFoundException , ReflectionException  68 { 69 MBeanAttributeInfo attr = getStandardAttributeInfo(metadata, attribute, false); 70 if (attr != null) 71 { 72 String methodName = getMethodForAttribute(attr, true); 73 try 74 { 75 return doInvoke(metadata, methodName, EMPTY_PARAMS, EMPTY_ARGS); 76 } 77 catch (BadArgumentException x) 78 { 79 throw new ImplementationException(); 81 } 82 } 83 else 84 { 85 throw new AttributeNotFoundException (attribute); 86 } 87 } 88 89 public void setAttribute(MBeanMetaData metadata, Attribute attribute) throws MBeanException , AttributeNotFoundException , InvalidAttributeValueException , ReflectionException  90 { 91 String name = attribute.getName(); 92 MBeanAttributeInfo attr = getStandardAttributeInfo(metadata, name, true); 93 if (attr != null) 94 { 95 String methodName = getMethodForAttribute(attr, false); 96 try 97 { 98 doInvoke(metadata, methodName, new String []{attr.getType()}, new Object []{attribute.getValue()}); 99 } 100 catch (BadArgumentException x) 101 { 102 throw new InvalidAttributeValueException ("Invalid value for attribute " + name + ": " + attribute.getValue()); 103 } 104 } 105 else 106 { 107 throw new AttributeNotFoundException (name); 108 } 109 } 110 111 115 protected Object doInvoke(MBeanMetaData metadata, String method, String [] signature, Object [] args) throws ReflectionException , MBeanException , BadArgumentException 116 { 117 try 118 { 119 return invokeImpl(metadata, method, signature, args); 120 } 121 catch (ReflectionException x) 122 { 123 throw x; 124 } 125 catch (MBeanException x) 126 { 127 throw x; 128 } 129 catch (BadArgumentException x) 130 { 131 throw x; 132 } 133 catch (Throwable t) 134 { 135 if (t instanceof Error ) throw new RuntimeErrorException ((Error )t); 136 if (t instanceof JMRuntimeException ) throw (JMRuntimeException )t; 137 if (t instanceof RuntimeException ) throw new RuntimeMBeanException ((RuntimeException )t); 138 throw new MBeanException ((Exception )t); 139 } 140 } 141 142 147 protected Object invokeImpl(MBeanMetaData metadata, String method, String [] signature, Object [] args) throws Throwable  148 { 149 Method m = getStandardManagementMethod(metadata, method, signature); 150 try 151 { 152 return m.invoke(metadata.getMBean(), args); 153 } 154 catch (IllegalAccessException x) 155 { 156 throw new ReflectionException (x); 157 } 158 catch (IllegalArgumentException x) 159 { 160 throw new BadArgumentException(x); 161 } 162 catch (InvocationTargetException x) 163 { 164 throw x.getTargetException(); 165 } 166 } 167 168 172 protected MBeanOperationInfo getStandardOperationInfo(MBeanMetaData metadata, String method, String [] signature) 173 { 174 MBeanOperationInfo [] opers = metadata.getMBeanInfo().getOperations(); 175 if (opers != null) 176 { 177 for (int i = 0; i < opers.length; ++i) 178 { 179 MBeanOperationInfo oper = opers[i]; 180 String name = oper.getName(); 181 if (method.equals(name)) 182 { 183 MBeanParameterInfo [] params = oper.getSignature(); 185 if (signature.length == params.length) 186 { 187 boolean match = true; 188 for (int j = 0; j < params.length; ++j) 189 { 190 MBeanParameterInfo param = params[j]; 191 if (!signature[j].equals(param.getType())) 192 { 193 match = false; 194 break; 195 } 196 } 197 if (match) return oper; 198 } 199 } 200 } 201 } 202 return null; 203 } 204 205 209 protected MBeanAttributeInfo getStandardAttributeInfo(MBeanMetaData metadata, String attribute, boolean forWrite) 210 { 211 MBeanAttributeInfo [] attrs = metadata.getMBeanInfo().getAttributes(); 212 if (attrs != null) 213 { 214 for (int i = 0; i < attrs.length; ++i) 215 { 216 MBeanAttributeInfo attr = attrs[i]; 217 String name = attr.getName(); 218 if (attribute.equals(name)) 219 { 220 if (forWrite && attr.isWritable()) return attr; 221 if (!forWrite && attr.isReadable()) return attr; 222 } 223 } 224 } 225 return null; 226 } 227 228 231 protected String getMethodForAttribute(MBeanAttributeInfo attribute, boolean forRead) 232 { 233 String name = attribute.getName(); 234 String attributeName = null; 235 if (forRead) 236 { 237 String prefix = attribute.isIs() ? "is" : "get"; 238 attributeName = prefix + name; 239 } 240 else 241 { 242 attributeName = "set" + name; 243 } 244 return attributeName; 245 } 246 247 250 protected Method getStandardManagementMethod(MBeanMetaData metadata, String name, String [] signature) throws ReflectionException  251 { 252 try 253 { 254 Class [] params = Utils.loadClasses(metadata.getClassLoader(), signature); 255 Method method = metadata.getMBeanInterface().getMethod(name, params); 256 return method; 257 } 258 catch (ClassNotFoundException x) 259 { 260 throw new ReflectionException (x); 261 } 262 catch (NoSuchMethodException x) 263 { 264 throw new ReflectionException (x); 265 } 266 } 267 268 private static class BadArgumentException extends Exception  269 { 270 private final IllegalArgumentException nested; 271 272 private BadArgumentException(IllegalArgumentException nested) 273 { 274 this.nested = nested; 275 } 276 } 277 } 278 | Popular Tags |