1 17 18 19 package org.apache.tomcat.util.modeler; 20 21 22 import java.lang.reflect.Method ; 23 import java.util.HashMap ; 24 import java.util.Map ; 25 26 import javax.management.AttributeNotFoundException ; 27 import javax.management.DynamicMBean ; 28 import javax.management.InstanceNotFoundException ; 29 import javax.management.MBeanAttributeInfo ; 30 import javax.management.MBeanConstructorInfo ; 31 import javax.management.MBeanException ; 32 import javax.management.MBeanInfo ; 33 import javax.management.MBeanNotificationInfo ; 34 import javax.management.MBeanOperationInfo ; 35 import javax.management.ReflectionException ; 36 import javax.management.RuntimeOperationsException ; 37 import javax.management.ServiceNotFoundException ; 38 40 41 48 49 public class ManagedBean implements java.io.Serializable 50 { 51 private static final String BASE_MBEAN = "org.apache.tomcat.util.modeler.BaseModelMBean"; 52 static final Object [] NO_ARGS_PARAM=new Object [0]; 54 static final Class [] NO_ARGS_PARAM_SIG=new Class [0]; 55 56 57 61 transient MBeanInfo info = null; 62 private Map attributes = new HashMap (); 64 private Map operations = new HashMap (); 66 67 protected String className = BASE_MBEAN; 68 protected String description = null; 70 protected String domain = null; 71 protected String group = null; 72 protected String name = null; 73 74 protected NotificationInfo notifications[] = new NotificationInfo[0]; 76 protected String type = null; 77 78 81 public ManagedBean() { 82 AttributeInfo ai=new AttributeInfo(); 83 ai.setName("modelerType"); 84 ai.setDescription("Type of the modeled resource. Can be set only once"); 85 ai.setType("java.lang.String"); 86 ai.setWriteable(false); 87 addAttribute(ai); 88 } 89 90 92 93 96 public AttributeInfo[] getAttributes() { 97 AttributeInfo result[] = new AttributeInfo[attributes.size()]; 98 attributes.values().toArray(result); 99 return result; 100 } 101 102 103 109 public String getClassName() { 110 return (this.className); 111 } 112 113 public void setClassName(String className) { 114 this.className = className; 115 this.info = null; 116 } 117 118 119 126 127 130 public String getDescription() { 131 return (this.description); 132 } 133 134 public void setDescription(String description) { 135 this.description = description; 136 this.info = null; 137 } 138 139 140 144 public String getDomain() { 145 return (this.domain); 146 } 147 148 public void setDomain(String domain) { 149 this.domain = domain; 150 } 151 152 153 158 163 166 public String getGroup() { 167 return (this.group); 168 } 169 170 public void setGroup(String group) { 171 this.group = group; 172 } 173 174 175 179 public String getName() { 180 return (this.name); 181 } 182 183 public void setName(String name) { 184 this.name = name; 185 this.info = null; 186 } 187 188 189 192 public NotificationInfo[] getNotifications() { 193 return (this.notifications); 194 } 195 196 197 200 public OperationInfo[] getOperations() { 201 OperationInfo[] result = new OperationInfo[operations.size()]; 202 operations.values().toArray(result); 203 return result; 204 } 205 206 207 212 public String getType() { 213 return (this.type); 214 } 215 216 public void setType(String type) { 217 this.type = type; 218 this.info = null; 219 } 220 221 222 224 225 230 public void addAttribute(AttributeInfo attribute) { 231 attributes.put(attribute.getName(), attribute); 232 } 233 234 235 240 253 254 260 264 265 270 public void addNotification(NotificationInfo notification) { 271 272 synchronized (notifications) { 273 NotificationInfo results[] = 274 new NotificationInfo[notifications.length + 1]; 275 System.arraycopy(notifications, 0, results, 0, 276 notifications.length); 277 results[notifications.length] = notification; 278 notifications = results; 279 this.info = null; 280 } 281 282 } 283 284 285 290 public void addOperation(OperationInfo operation) { 291 operations.put(operation.getName(), operation); 292 } 293 294 295 311 public DynamicMBean createMBean() 312 throws InstanceNotFoundException , 313 MBeanException , RuntimeOperationsException { 314 315 return (createMBean(null)); 316 317 } 318 319 320 339 public DynamicMBean createMBean(Object instance) 340 throws InstanceNotFoundException , 341 MBeanException , RuntimeOperationsException { 342 343 BaseModelMBean mbean = null; 344 345 if(getClassName().equals(BASE_MBEAN)) { 347 mbean = new BaseModelMBean(); 349 } else { 350 Class clazz = null; 351 Exception ex = null; 352 try { 353 clazz = Class.forName(getClassName()); 354 } catch (Exception e) { 355 } 356 357 if( clazz==null ) { 358 try { 359 ClassLoader cl= Thread.currentThread().getContextClassLoader(); 360 if ( cl != null) 361 clazz= cl.loadClass(getClassName()); 362 } catch (Exception e) { 363 ex=e; 364 } 365 } 366 367 if( clazz==null) { 368 throw new MBeanException 369 (ex, "Cannot load ModelMBean class " + getClassName()); 370 } 371 try { 372 mbean = (BaseModelMBean) clazz.newInstance(); 374 } catch (RuntimeOperationsException e) { 375 throw e; 376 } catch (Exception e) { 377 throw new MBeanException 378 (e, "Cannot instantiate ModelMBean of class " + 379 getClassName()); 380 } 381 } 382 383 mbean.setManagedBean(this); 384 385 try { 387 if (instance != null) 388 mbean.setManagedResource(instance, "ObjectReference"); 389 } catch (InstanceNotFoundException e) { 390 throw e; 391 } 392 return (mbean); 393 394 } 395 396 397 401 MBeanInfo getMBeanInfo() { 402 403 if (info != null) 405 return (info); 406 407 AttributeInfo attrs[] = getAttributes(); 409 MBeanAttributeInfo attributes[] = 410 new MBeanAttributeInfo [attrs.length]; 411 for (int i = 0; i < attrs.length; i++) 412 attributes[i] = attrs[i].createAttributeInfo(); 413 414 OperationInfo opers[] = getOperations(); 415 MBeanOperationInfo operations[] = 416 new MBeanOperationInfo [opers.length]; 417 for (int i = 0; i < opers.length; i++) 418 operations[i] = opers[i].createOperationInfo(); 419 420 421 427 NotificationInfo notifs[] = getNotifications(); 428 MBeanNotificationInfo notifications[] = 429 new MBeanNotificationInfo [notifs.length]; 430 for (int i = 0; i < notifs.length; i++) 431 notifications[i] = notifs[i].createNotificationInfo(); 432 433 434 info = new MBeanInfo (getClassName(), 436 getDescription(), 437 attributes, 438 new MBeanConstructorInfo [] {}, 439 operations, 440 notifications); 441 453 return (info); 454 455 } 456 457 458 461 public String toString() { 462 463 StringBuffer sb = new StringBuffer ("ManagedBean["); 464 sb.append("name="); 465 sb.append(name); 466 sb.append(", className="); 467 sb.append(className); 468 sb.append(", description="); 469 sb.append(description); 470 if (group != null) { 471 sb.append(", group="); 472 sb.append(group); 473 } 474 sb.append(", type="); 475 sb.append(type); 476 sb.append("]"); 477 return (sb.toString()); 478 479 } 480 481 Method getGetter(String aname, BaseModelMBean mbean, Object resource) 482 throws AttributeNotFoundException , MBeanException , ReflectionException { 483 Method m=null; 486 if( m==null ) { 487 AttributeInfo attrInfo = (AttributeInfo)attributes.get(aname); 488 if (attrInfo == null) 490 throw new AttributeNotFoundException (" Cannot find attribute " + aname + " for " + resource); 491 492 String getMethod = attrInfo.getGetMethod(); 493 if (getMethod == null) 494 throw new AttributeNotFoundException ("Cannot find attribute " + aname + " get method name"); 495 496 Object object = null; 497 NoSuchMethodException exception = null; 498 try { 499 object = mbean; 500 m = object.getClass().getMethod(getMethod, NO_ARGS_PARAM_SIG); 501 } catch (NoSuchMethodException e) { 502 exception = e;; 503 } 504 if( m== null && resource != null ) { 505 try { 506 object = resource; 507 m = object.getClass().getMethod(getMethod, NO_ARGS_PARAM_SIG); 508 exception=null; 509 } catch (NoSuchMethodException e) { 510 exception = e; 511 } 512 } 513 if( exception != null ) 514 throw new ReflectionException (exception, 515 "Cannot find getter method " + getMethod); 516 } 518 519 return m; 520 } 521 522 public Method getSetter(String aname, BaseModelMBean bean, Object resource) 523 throws AttributeNotFoundException , MBeanException , ReflectionException { 524 Method m=null; 528 if( m==null ) { 529 AttributeInfo attrInfo = (AttributeInfo)attributes.get(aname); 530 if (attrInfo == null) 531 throw new AttributeNotFoundException (" Cannot find attribute " + aname); 532 533 String setMethod = attrInfo.getSetMethod(); 535 if (setMethod == null) 536 throw new AttributeNotFoundException ("Cannot find attribute " + aname + " set method name"); 537 538 String argType=attrInfo.getType(); 539 540 Class signature[] = new Class [] { BaseModelMBean.getAttributeClass( argType ) }; 541 542 Object object = null; 543 NoSuchMethodException exception = null; 544 try { 545 object = this; 546 m = object.getClass().getMethod(setMethod, signature); 547 } catch (NoSuchMethodException e) { 548 exception = e;; 549 } 550 if( m== null && resource != null ) { 551 try { 552 object = resource; 553 m = object.getClass().getMethod(setMethod, signature); 554 exception=null; 555 } catch (NoSuchMethodException e) { 556 exception = e; 557 } 558 } 559 if( exception != null ) 560 throw new ReflectionException (exception, 561 "Cannot find setter method " + setMethod + 562 " " + resource); 563 } 565 566 return m; 567 } 568 569 public Method getInvoke(String aname, Object [] params, String [] signature, BaseModelMBean bean, Object resource) 570 throws MBeanException , ReflectionException { 571 Method method = null; 572 if (method == null) { 573 if (params == null) 574 params = new Object [0]; 575 if (signature == null) 576 signature = new String [0]; 577 if (params.length != signature.length) 578 throw new RuntimeOperationsException ( 579 new IllegalArgumentException ( 580 "Inconsistent arguments and signature"), 581 "Inconsistent arguments and signature"); 582 583 OperationInfo opInfo = (OperationInfo)operations.get(aname); 586 if (opInfo == null) 587 throw new MBeanException (new ServiceNotFoundException ( 588 "Cannot find operation " + aname), 589 "Cannot find operation " + aname); 590 591 Class types[] = new Class [signature.length]; 594 for (int i = 0; i < signature.length; i++) { 595 types[i] = BaseModelMBean.getAttributeClass(signature[i]); 596 } 597 598 Object object = null; 602 Exception exception = null; 603 try { 604 object = this; 605 method = object.getClass().getMethod(aname, types); 606 } catch (NoSuchMethodException e) { 607 exception = e; 608 ; 609 } 610 try { 611 if ((method == null) && (resource != null)) { 612 object = resource; 613 method = object.getClass().getMethod(aname, types); 614 } 615 } catch (NoSuchMethodException e) { 616 exception = e; 617 } 618 if (method == null) { 619 throw new ReflectionException (exception, "Cannot find method " 620 + aname + " with this signature"); 621 } 622 } 624 return method; 625 } 626 627 628 } 629 | Popular Tags |