1 8 9 package mx4j; 10 11 import java.lang.reflect.InvocationTargetException ; 12 import java.lang.reflect.Method ; 13 import java.util.Arrays ; 14 import javax.management.Attribute ; 15 import javax.management.AttributeList ; 16 import javax.management.AttributeNotFoundException ; 17 import javax.management.DynamicMBean ; 18 import javax.management.InvalidAttributeValueException ; 19 import javax.management.MBeanAttributeInfo ; 20 import javax.management.MBeanConstructorInfo ; 21 import javax.management.MBeanException ; 22 import javax.management.MBeanInfo ; 23 import javax.management.MBeanNotificationInfo ; 24 import javax.management.MBeanOperationInfo ; 25 import javax.management.MBeanParameterInfo ; 26 import javax.management.ReflectionException ; 27 import javax.management.RuntimeErrorException ; 28 import javax.management.RuntimeMBeanException ; 29 30 import mx4j.util.Utils; 31 32 74 public abstract class AbstractDynamicMBean implements DynamicMBean 75 { 76 private MBeanInfo info; 77 private Object resource; 78 79 84 protected AbstractDynamicMBean() 85 { 86 } 87 88 93 public Object getAttribute(String attribute) throws AttributeNotFoundException , MBeanException , ReflectionException 94 { 95 if (attribute == null) throw new AttributeNotFoundException ("Attribute " + attribute + " not found"); 96 97 Object resource = null; 98 MBeanInfo info = null; 99 synchronized (this) 100 { 101 resource = getResourceOrThis(); 102 info = getMBeanInfo(); 103 } 104 105 MBeanAttributeInfo [] attrs = info.getAttributes(); 106 if (attrs == null || attrs.length == 0) throw new AttributeNotFoundException ("No attributes defined for this MBean"); 107 108 for (int i = 0; i < attrs.length; ++i) 109 { 110 MBeanAttributeInfo attr = attrs[i]; 111 if (attr == null) continue; 112 113 if (attribute.equals(attr.getName())) 114 { 115 if (!attr.isReadable()) throw new ReflectionException (new NoSuchMethodException ("No getter defined for attribute: " + attribute)); 116 117 String prefix = null; 119 if (attr.isIs()) 120 prefix = "is"; 121 else 122 prefix = "get"; 123 124 try 125 { 126 return invoke(resource, prefix + attr.getName(), new Class [0], new Object [0]); 127 } 128 catch (InvalidAttributeValueException x) 129 { 130 throw new ReflectionException (x); 131 } 132 } 133 } 134 135 throw new AttributeNotFoundException ("Attribute " + attribute + " not found"); 136 } 137 138 141 public AttributeList getAttributes(String [] attributes) 142 { 143 AttributeList list = new AttributeList (); 144 145 if (attributes != null) 146 { 147 for (int i = 0; i < attributes.length; ++i) 148 { 149 String attribute = attributes[i]; 150 try 151 { 152 Object result = getAttribute(attribute); 153 list.add(new Attribute (attribute, result)); 154 } 155 catch (AttributeNotFoundException ignored) 156 { 157 } 158 catch (MBeanException ignored) 159 { 160 } 161 catch (ReflectionException ignored) 162 { 163 } 164 } 165 } 166 167 return list; 168 } 169 170 177 public synchronized MBeanInfo getMBeanInfo() 178 { 179 if (info == null) setMBeanInfo(createMBeanInfo()); 180 return info; 181 } 182 183 188 public Object invoke(String method, Object [] arguments, String [] params) throws MBeanException , ReflectionException 189 { 190 if (method == null) throw new IllegalArgumentException ("Method name cannot be null"); 191 if (arguments == null) arguments = new Object [0]; 192 if (params == null) params = new String [0]; 193 194 Object resource = null; 195 MBeanInfo info = null; 196 synchronized (this) 197 { 198 resource = getResourceOrThis(); 199 info = getMBeanInfo(); 200 } 201 202 MBeanOperationInfo [] opers = info.getOperations(); 203 if (opers == null || opers.length == 0) throw new ReflectionException (new NoSuchMethodException ("No operations defined for this MBean")); 204 205 for (int i = 0; i < opers.length; ++i) 206 { 207 MBeanOperationInfo oper = opers[i]; 208 if (oper == null) continue; 209 210 if (method.equals(oper.getName())) 211 { 212 MBeanParameterInfo [] parameters = oper.getSignature(); 213 if (params.length != parameters.length) continue; 214 215 String [] signature = new String [parameters.length]; 216 for (int j = 0; j < signature.length; ++j) 217 { 218 MBeanParameterInfo param = parameters[j]; 219 if (param == null) 220 signature[j] = null; 221 else 222 signature[j] = param.getType(); 223 } 224 225 if (Utils.arrayEquals(params, signature)) 226 { 227 try 229 { 230 Class [] classes = Utils.loadClasses(resource.getClass().getClassLoader(), signature); 231 return invoke(resource, method, classes, arguments); 232 } 233 catch (ClassNotFoundException x) 234 { 235 throw new ReflectionException (x); 236 } 237 catch (InvalidAttributeValueException x) 238 { 239 throw new ReflectionException (x); 240 } 241 } 242 } 243 } 244 245 throw new ReflectionException (new NoSuchMethodException ("Operation " + method + " with signature " + Arrays.asList(params) + " is not defined for this MBean")); 246 } 247 248 253 public void setAttribute(Attribute attribute) throws AttributeNotFoundException , InvalidAttributeValueException , MBeanException , ReflectionException 254 { 255 if (attribute == null) throw new AttributeNotFoundException ("Attribute " + attribute + " not found"); 256 257 Object resource = null; 258 MBeanInfo info = null; 259 synchronized (this) 260 { 261 resource = getResourceOrThis(); 262 info = getMBeanInfo(); 263 } 264 265 MBeanAttributeInfo [] attrs = info.getAttributes(); 266 if (attrs == null || attrs.length == 0) throw new AttributeNotFoundException ("No attributes defined for this MBean"); 267 268 for (int i = 0; i < attrs.length; ++i) 269 { 270 MBeanAttributeInfo attr = attrs[i]; 271 if (attr == null) continue; 272 273 if (attribute.getName().equals(attr.getName())) 274 { 275 if (!attr.isWritable()) throw new ReflectionException (new NoSuchMethodException ("No setter defined for attribute: " + attribute)); 276 277 try 278 { 279 String signature = attr.getType(); 280 Class cls = Utils.loadClass(resource.getClass().getClassLoader(), signature); 281 invoke(resource, "set" + attr.getName(), new Class []{cls}, new Object []{attribute.getValue()}); 282 return; 283 } 284 catch (ClassNotFoundException x) 285 { 286 throw new ReflectionException (x); 287 } 288 } 289 } 290 291 throw new AttributeNotFoundException ("Attribute " + attribute + " not found"); 292 } 293 294 297 public AttributeList setAttributes(AttributeList attributes) 298 { 299 AttributeList list = new AttributeList (); 300 301 if (attributes != null) 302 { 303 for (int i = 0; i < attributes.size(); ++i) 304 { 305 Attribute attribute = (Attribute )attributes.get(i); 306 try 307 { 308 setAttribute(attribute); 309 list.add(attribute); 310 } 311 catch (AttributeNotFoundException ignored) 312 { 313 } 314 catch (InvalidAttributeValueException ignored) 315 { 316 } 317 catch (MBeanException ignored) 318 { 319 } 320 catch (ReflectionException ignored) 321 { 322 } 323 } 324 } 325 326 return list; 327 } 328 329 334 protected Object invoke(String name, Class [] params, Object [] args) throws InvalidAttributeValueException , MBeanException , ReflectionException 335 { 336 Object resource = getResourceOrThis(); 337 return invoke(resource, name, params, args); 338 } 339 340 349 protected Object invoke(Object resource, String name, Class [] params, Object [] args) throws InvalidAttributeValueException , MBeanException , ReflectionException 350 { 351 try 352 { 353 Class cls = resource.getClass(); 354 Method method = findMethod(cls, name, params); 355 return invokeMethod(method, resource, args); 356 } 357 catch (NoSuchMethodException x) 358 { 359 throw new ReflectionException (x); 360 } 361 catch (IllegalAccessException x) 362 { 363 throw new ReflectionException (x); 364 } 365 catch (IllegalArgumentException x) 366 { 367 throw new InvalidAttributeValueException (x.toString()); 368 } 369 catch (InvocationTargetException x) 370 { 371 Throwable t = x.getTargetException(); 372 if (t instanceof RuntimeException ) 373 throw new RuntimeMBeanException ((RuntimeException )t); 374 else if (t instanceof Exception ) throw new MBeanException ((Exception )t); 375 throw new RuntimeErrorException ((Error )t); 376 } 377 } 378 379 387 protected Method findMethod(Class cls, String name, Class [] params) throws NoSuchMethodException 388 { 389 return cls.getMethod(name, params); 390 } 391 392 399 protected Object invokeMethod(Method method, Object resource, Object [] args) throws IllegalAccessException , IllegalArgumentException , InvocationTargetException 400 { 401 return method.invoke(resource, args); 402 } 403 404 private Object getResourceOrThis() 405 { 406 Object resource = getResource(); 407 if (resource == null) resource = this; 408 return resource; 409 } 410 411 416 protected synchronized Object getResource() 417 { 418 return resource; 419 } 420 421 426 public synchronized void setResource(Object resource) 427 { 428 this.resource = resource; 429 } 430 431 437 protected synchronized void setMBeanInfo(MBeanInfo info) 438 { 439 this.info = info; 440 } 441 442 454 protected MBeanInfo createMBeanInfo() 455 { 456 MBeanAttributeInfo [] attrs = createMBeanAttributeInfo(); 457 MBeanConstructorInfo [] ctors = createMBeanConstructorInfo(); 458 MBeanOperationInfo [] opers = createMBeanOperationInfo(); 459 MBeanNotificationInfo [] notifs = createMBeanNotificationInfo(); 460 String className = getMBeanClassName(); 461 String description = getMBeanDescription(); 462 return new MBeanInfo (className, description, attrs, ctors, opers, notifs); 463 } 464 465 468 protected MBeanAttributeInfo [] createMBeanAttributeInfo() 469 { 470 return null; 471 } 472 473 476 protected MBeanConstructorInfo [] createMBeanConstructorInfo() 477 { 478 return null; 479 } 480 481 484 protected MBeanOperationInfo [] createMBeanOperationInfo() 485 { 486 return null; 487 } 488 489 492 protected MBeanNotificationInfo [] createMBeanNotificationInfo() 493 { 494 return null; 495 } 496 497 501 protected String getMBeanClassName() 502 { 503 return getClass().getName(); 504 } 505 506 509 protected String getMBeanDescription() 510 { 511 return null; 512 } 513 } 514 | Popular Tags |