| 1 24 25 26 27 28 package javax.management.modelmbean; 29 30 31 32 import java.lang.reflect.Method ; 33 import java.lang.reflect.InvocationTargetException ; 34 35 import java.util.Date ; 36 import java.util.HashMap ; 37 import java.util.HashSet ; 38 import java.util.Iterator ; 39 import java.util.Map ; 40 import java.util.Set ; 41 42 import java.io.PrintStream ; 43 import java.io.FileOutputStream ; 44 45 import javax.management.Attribute ; 46 import javax.management.AttributeList ; 47 import javax.management.AttributeChangeNotification ; 48 import javax.management.AttributeChangeNotificationFilter ; 49 import javax.management.AttributeNotFoundException ; 50 import javax.management.Descriptor ; 51 import javax.management.DescriptorAccess ; 52 import javax.management.InstanceNotFoundException ; 53 import javax.management.InvalidAttributeValueException ; 54 import javax.management.ListenerNotFoundException ; 55 import javax.management.MBeanAttributeInfo ; 56 import javax.management.MBeanConstructorInfo ; 57 import javax.management.MBeanException ; 58 import javax.management.MBeanInfo ; 59 import javax.management.MBeanNotificationInfo ; 60 import javax.management.MBeanOperationInfo ; 61 import javax.management.MBeanRegistration ; 62 import javax.management.MBeanRegistrationException ; 63 import javax.management.MBeanServer ; 64 import javax.management.MBeanServerFactory ; 65 import javax.management.Notification ; 66 import javax.management.NotificationBroadcasterSupport ; 67 import javax.management.NotificationFilter ; 68 import javax.management.NotificationListener ; 69 import javax.management.ObjectName ; 70 import javax.management.ReflectionException ; 71 import javax.management.RuntimeErrorException ; 72 import javax.management.RuntimeMBeanException ; 73 import javax.management.RuntimeOperationsException ; 74 import javax.management.ServiceNotFoundException ; 75 import javax.management.NotificationEmitter ; 76 import javax.management.loading.ClassLoaderRepository ; 77 78 import sun.reflect.misc.MethodUtil; 79 import sun.reflect.misc.ReflectUtil; 80 81 import com.sun.jmx.trace.Trace; 82 83 114 115 public class RequiredModelMBean 116 implements ModelMBean , MBeanRegistration , NotificationEmitter { 117 118 119 120 121 ModelMBeanInfo modelMBeanInfo; 122 123 125 private NotificationBroadcasterSupport generalBroadcaster = null; 126 127 128 private NotificationBroadcasterSupport attributeBroadcaster = null; 129 130 132 private Object managedResource = null; 133 134 private static final String currClass = "RequiredModelMBean"; 135 136 137 private boolean registered = false; 138 private transient MBeanServer server = null; 139 140 141 142 143 144 159 public RequiredModelMBean() 160 throws MBeanException , RuntimeOperationsException { 161 162 if (tracing()) 163 trace("RequiredModelMBean()","Entry and Exit"); 164 165 modelMBeanInfo = createDefaultModelMBeanInfo(); 166 } 167 168 188 public RequiredModelMBean(ModelMBeanInfo mbi) 189 throws MBeanException , RuntimeOperationsException { 190 191 if (tracing()) 192 trace("RequiredModelMBean(MBeanInfo)","Entry"); 193 194 setModelMBeanInfo(mbi); 195 196 if (tracing()) 197 trace("RequiredModelMBean(MBeanInfo)","Exit"); 198 } 199 200 201 202 203 204 205 238 public void setModelMBeanInfo(ModelMBeanInfo mbi) 239 throws MBeanException , RuntimeOperationsException { 240 241 if (tracing()) 242 trace("setModelMBeanInfo(ModelMBeanInfo)","Entry"); 243 244 if (mbi == null) { 245 if (tracing()) 246 trace("setModelMBeanInfo(ModelMBeanInfo)", 247 "ModelMBeanInfo is null: Raising exception."); 248 final RuntimeException x = new 249 IllegalArgumentException ("ModelMBeanInfo must not be null"); 250 final String exceptionText = 251 "Exception occured trying to initialize the " + 252 "ModelMBeanInfo of the RequiredModelMBean"; 253 throw new RuntimeOperationsException (x,exceptionText); 254 } 255 256 if (registered) { 257 if (tracing()) 258 trace("setModelMBeanInfo(ModelMBeanInfo)", 259 "RequiredMBean is registered: Raising exception."); 260 final String exceptionText = 261 "Exception occured trying to set the " + 262 "ModelMBeanInfo of the RequiredModelMBean"; 263 final RuntimeException x = new IllegalStateException ( 264 "cannot call setModelMBeanInfo while ModelMBean is registered"); 265 throw new RuntimeOperationsException (x,exceptionText); 266 } 267 268 if (tracing()) { 269 trace("setModelMBeanInfo(ModelMBeanInfo)", 270 "Setting ModelMBeanInfo to " + printModelMBeanInfo(mbi)); 271 trace("setModelMBeanInfo(ModelMBeanInfo)", 272 "ModelMBeanInfo notifications has " + 273 (mbi.getNotifications()).length + " elements"); 274 } 275 276 modelMBeanInfo = (ModelMBeanInfo )mbi.clone(); 277 278 if (tracing()) 279 trace("setModelMBeanInfo(ModelMBeanInfo)","set mbeanInfo to: "+ 280 printModelMBeanInfo(modelMBeanInfo)); 281 282 if (tracing()) 283 trace("setModelMBeanInfo(ModelMBeanInfo)","Exit"); 284 } 285 286 287 307 public void setManagedResource(Object mr, String mr_type) 308 throws MBeanException , RuntimeOperationsException , 309 InstanceNotFoundException , InvalidTargetObjectTypeException { 310 if (tracing()) 311 trace("setManagedResource(Object,String)","Entry"); 312 313 if ((mr_type == null) || 316 (! mr_type.equalsIgnoreCase("objectReference"))) { 317 if (tracing()) 318 trace("setManagedResource(Object,String)", 319 "Managed Resouce Type is not supported: " + mr_type); 320 321 throw new InvalidTargetObjectTypeException (mr_type); 322 } 323 324 if (tracing()) 325 trace("setManagedResource(Object,String)", 326 "Managed Resouce is valid"); 327 328 managedResource = mr; 329 330 if (tracing()) 331 trace("setManagedResource(Object, String)", "Exit"); 332 } 333 334 354 public void load() 355 throws MBeanException , RuntimeOperationsException , 356 InstanceNotFoundException { 357 final ServiceNotFoundException x = new ServiceNotFoundException ( 358 "Persistence not supported for this MBean"); 359 throw new MBeanException (x, x.getMessage()); 360 } 361 362 395 public void store() 396 throws MBeanException , RuntimeOperationsException , 397 InstanceNotFoundException { 398 final ServiceNotFoundException x = new ServiceNotFoundException ( 399 "Persistence not supported for this MBean"); 400 throw new MBeanException (x, x.getMessage()); 401 } 402 403 404 405 406 407 429 private Object resolveForCacheValue(Descriptor descr) 430 throws MBeanException , RuntimeOperationsException { 431 if (tracing()) 432 trace("resolveForCacheValue(Descriptor)","Entry"); 433 434 Object response = null; 435 boolean resetValue = false, returnCachedValue = true; 436 long currencyPeriod = 0; 437 438 if (descr == null) { 439 if (tracing()) 440 trace("resolveForCacheValue(Descriptor)", 441 "Input Descriptor is null"); 442 return response; 443 } 444 445 if (tracing()) 446 trace("resolveForCacheValue(Descriptor)","descriptor is " + 447 descr.toString()); 448 449 final Descriptor mmbDescr = modelMBeanInfo.getMBeanDescriptor(); 450 if (mmbDescr == null) { 451 if (tracing()) 452 trace("resolveForCacheValue(Descriptor)", 453 "MBean Descriptor is null"); 454 } 456 457 Object objExpTime = descr.getFieldValue("currencyTimeLimit"); 458 459 String expTime; 460 if (objExpTime != null) { 461 expTime = objExpTime.toString(); 462 } else { 463 expTime = null; 464 } 465 466 if ((expTime == null) && (mmbDescr != null)) { 467 objExpTime = mmbDescr.getFieldValue("currencyTimeLimit"); 468 if (objExpTime != null) { 469 expTime = objExpTime.toString(); 470 } else { 471 expTime = null; 472 } 473 } 474 475 if (expTime != null) { 476 if (tracing()) 477 trace("resolveForCacheValue(Descriptor)", 478 "currencyTimeLimit: " + expTime); 479 480 currencyPeriod = ((new Long (expTime)).longValue()) * 1000; 482 if (currencyPeriod < 0) { 483 484 returnCachedValue = false; 485 resetValue = true; 486 if (tracing()) 487 trace("resolveForCacheValue(Descriptor)", 488 currencyPeriod + ": never Cached"); 489 } else if (currencyPeriod == 0) { 490 491 returnCachedValue = true; 492 resetValue = false; 493 if (tracing()) 494 trace("resolveForCacheValue(Descriptor)", 495 "always valid Cache"); 496 } else { 497 Object objtStamp = 498 descr.getFieldValue("lastUpdatedTimeStamp"); 499 500 String tStamp; 501 if (objtStamp != null) tStamp = objtStamp.toString(); 502 else tStamp = null; 503 504 if (tracing()) 505 trace("resolveForCacheValue(Descriptor)", 506 "lastUpdatedTimeStamp: " + tStamp); 507 508 if (tStamp == null) 509 tStamp = "0"; 510 511 long lastTime = (new Long (tStamp)).longValue(); 512 513 if (tracing()) 514 trace("resolveForCacheValue(Descriptor)", 515 " currencyPeriod:" + currencyPeriod + 516 " lastUpdatedTimeStamp:" + lastTime); 517 518 long now = (new Date ()).getTime(); 519 520 if (now < (lastTime + currencyPeriod)) { 521 returnCachedValue = true; 522 resetValue = false; 523 if (tracing()) 524 trace("resolveForCacheValue(Descriptor)", 525 " timed valid Cache for " + now + " < " + 526 (lastTime + currencyPeriod)); 527 } else { 528 returnCachedValue = false; 529 resetValue = true; 530 if (tracing()) 531 trace("resolveForCacheValue(Descriptor)", 532 "timed expired cache for " + now + " > " + 533 (lastTime + currencyPeriod)); 534 } 535 } 536 537 if (tracing()) 538 trace("resolveForCacheValue(Descriptor)", 539 "returnCachedValue:" + returnCachedValue + 540 " resetValue: " + resetValue); 541 542 if (returnCachedValue == true) { 543 Object currValue = descr.getFieldValue("value"); 544 if (currValue != null) { 545 546 response = currValue; 547 548 if (tracing()) 549 trace("resolveForCacheValue(Descriptor)", 550 "valid Cache value: " + currValue); 551 552 } else { 553 response = null; 554 if (tracing()) 555 trace("resolveForCacheValue(Descriptor)", 556 "no Cached value"); 557 } 558 } 559 560 if (resetValue == true) { 561 562 descr.removeField("lastUpdatedTimeStamp"); 563 descr.removeField("value"); 564 response = null; 565 modelMBeanInfo.setDescriptor(descr,null); 566 if (tracing()) 567 trace("resolveForCacheValue(Descriptor)", 568 "reset cached value to null"); 569 } 570 } 571 572 if (tracing()) 573 trace("resolveForCache(Descriptor)","Exit"); 574 575 return response; 576 } 577 578 586 public MBeanInfo getMBeanInfo() { 587 588 if (tracing()) 589 trace("getMBeanInfo()","Entry and Exit"); 590 591 if (modelMBeanInfo == null) { 592 if (tracing()) 593 trace("getMBeanInfo()","modelMBeanInfo is null"); 594 modelMBeanInfo = createDefaultModelMBeanInfo(); 595 } 597 598 if (tracing()) { 599 trace("getMBeanInfo()","ModelMBeanInfo is " + 600 modelMBeanInfo.getClassName() + " for " + 601 modelMBeanInfo.getDescription()); 602 trace("getMBeanInfo()",printModelMBeanInfo(modelMBeanInfo)); 603 } 604 605 return((MBeanInfo ) ((ModelMBeanInfo )modelMBeanInfo).clone()); 606 } 607 608 private String printModelMBeanInfo(ModelMBeanInfo info) { 609 final StringBuffer retStr = new StringBuffer (); 610 if (info == null) { 611 if (tracing()) 612 trace("printModelMBeanInfo(ModelMBeanInfo)", 613 "ModelMBeanInfo to print is null, " + 614 "printing local ModelMBeanInfo"); 615 info = modelMBeanInfo; 616 } 617 618 retStr.append("\nMBeanInfo for ModelMBean is:"); 619 retStr.append("\nCLASSNAME: \t"+ info.getClassName()); 620 retStr.append("\nDESCRIPTION: \t"+ info.getDescription()); 621 622 623 try { 624 retStr.append("\nMBEAN DESCRIPTOR: \t"+ 625 info.getMBeanDescriptor()); 626 } catch (Exception e) { 627 retStr.append("\nMBEAN DESCRIPTOR: \t" + " is invalid"); 628 } 629 630 retStr.append("\nATTRIBUTES"); 631 632 final MBeanAttributeInfo [] attrInfo = info.getAttributes(); 633 if ((attrInfo != null) && (attrInfo.length>0)) { 634 for (int i=0; i<attrInfo.length; i++) { 635 final ModelMBeanAttributeInfo attInfo = 636 (ModelMBeanAttributeInfo )attrInfo[i]; 637 retStr.append(" ** NAME: \t"+ attInfo.getName()); 638 retStr.append(" DESCR: \t"+ attInfo.getDescription()); 639 retStr.append(" TYPE: \t"+ attInfo.getType() + 640 " READ: \t"+ attInfo.isReadable() + 641 " WRITE: \t"+ attInfo.isWritable()); 642 retStr.append(" DESCRIPTOR: " + 643 attInfo.getDescriptor().toString()); 644 } 645 } else { 646 retStr.append(" ** No attributes **"); 647 } 648 649 retStr.append("\nCONSTRUCTORS"); 650 final MBeanConstructorInfo [] constrInfo = info.getConstructors(); 651 if ((constrInfo != null) && (constrInfo.length > 0 )) { 652 for (int i=0; i<constrInfo.length; i++) { 653 final ModelMBeanConstructorInfo ctorInfo = 654 (ModelMBeanConstructorInfo )constrInfo[i]; 655 retStr.append(" ** NAME: \t"+ ctorInfo.getName()); 656 retStr.append(" DESCR: \t"+ 657 ctorInfo.getDescription()); 658 retStr.append(" PARAM: \t"+ 659 ctorInfo.getSignature().length + 660 " parameter(s)"); 661 retStr.append(" DESCRIPTOR: " + 662 ctorInfo.getDescriptor().toString()); 663 } 664 } else { 665 retStr.append(" ** No Constructors **"); 666 } 667 668 retStr.append("\nOPERATIONS"); 669 final MBeanOperationInfo [] opsInfo = info.getOperations(); 670 if ((opsInfo != null) && (opsInfo.length>0)) { 671 for (int i=0; i<opsInfo.length; i++) { 672 final ModelMBeanOperationInfo operInfo = 673 (ModelMBeanOperationInfo )opsInfo[i]; 674 retStr.append(" ** NAME: \t"+ operInfo.getName()); 675 retStr.append(" DESCR: \t"+ operInfo.getDescription()); 676 retStr.append(" PARAM: \t"+ 677 operInfo.getSignature().length + 678 " parameter(s)"); 679 retStr.append(" DESCRIPTOR: " + 680 operInfo.getDescriptor().toString()); 681 } 682 } else { 683 retStr.append(" ** No operations ** "); 684 } 685 686 retStr.append("\nNOTIFICATIONS"); 687 688 MBeanNotificationInfo [] notifInfo = info.getNotifications(); 689 if ((notifInfo != null) && (notifInfo.length>0)) { 690 for (int i=0; i<notifInfo.length; i++) { 691 final ModelMBeanNotificationInfo nInfo = 692 (ModelMBeanNotificationInfo )notifInfo[i]; 693 retStr.append(" ** NAME: \t"+ nInfo.getName()); 694 retStr.append(" DESCR: \t"+ nInfo.getDescription()); 695 retStr.append(" DESCRIPTOR: " + 696 nInfo.getDescriptor().toString()); 697 } 698 } else { 699 retStr.append(" ** No notifications **"); 700 } 701 702 retStr.append(" ** ModelMBean: End of MBeanInfo ** "); 703 704 return retStr.toString(); 705 } 706 707 private void echo(String txt) { 708 trace("echo(txt)",txt); 709 } 710 711 712 784 819 public Object invoke(String opName, Object [] opArgs, String [] sig) 820 throws MBeanException , ReflectionException { 821 final boolean tracing = tracing(); 822 823 if (tracing) 824 trace("invoke(String, Object[], String[])","Entry"); 825 826 if (opName == null) { 827 final RuntimeException x = 828 new IllegalArgumentException ("Method name must not be null"); 829 throw new RuntimeOperationsException (x, 830 "An exception occured while trying to " + 831 "invoke a method on a RequiredModelMBean"); 832 } 833 834 String opClassName = null; 835 String opMethodName; 836 837 int opSplitter = opName.lastIndexOf("."); 839 if (opSplitter > 0) { 840 opClassName = opName.substring(0,opSplitter); 841 opMethodNam
|