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 opMethodName = opName.substring(opSplitter+1); 842 } else 843 opMethodName = opName; 844 845 847 opSplitter = opMethodName.indexOf("("); 848 if (opSplitter > 0) 849 opMethodName = opMethodName.substring(0,opSplitter); 850 851 if (tracing) 852 trace("invoke(String, Object[], String[])", 853 "Finding operation " + opName + " as " + opMethodName); 854 855 ModelMBeanOperationInfo opInfo = 856 modelMBeanInfo.getOperation(opMethodName); 857 if (opInfo == null) { 858 final String msg = 859 "Operation " + opName + " not in ModelMBeanInfo"; 860 throw new MBeanException (new ServiceNotFoundException (msg), msg); 861 } 862 863 final Descriptor opDescr = opInfo.getDescriptor(); 864 if (opDescr == null) { 865 final String msg = "Operation descriptor null"; 866 throw new MBeanException (new ServiceNotFoundException (msg), msg); 867 } 868 869 final Object cached = resolveForCacheValue(opDescr); 870 if (cached != null) { 871 if (tracing) 872 trace("invoke(String, Object[], String[])", 873 "Returning cached value"); 874 return cached; 875 } 876 877 if (opClassName == null) 878 opClassName = (String ) opDescr.getFieldValue("class"); 879 881 opMethodName = (String ) opDescr.getFieldValue("name"); 882 if (opMethodName == null) { 883 final String msg = 884 "Method descriptor must include `name' field"; 885 throw new MBeanException (new ServiceNotFoundException (msg), msg); 886 } 887 888 final String targetTypeField = (String ) 889 opDescr.getFieldValue("targetType"); 890 if (targetTypeField != null 891 && !targetTypeField.equalsIgnoreCase("objectReference")) { 892 final String msg = 893 "Target type must be objectReference: " + targetTypeField; 894 throw new MBeanException (new InvalidTargetObjectTypeException (msg), 895 msg); 896 } 897 898 final Object targetObjectField = opDescr.getFieldValue("targetObject"); 899 if (tracing && targetObjectField != null) 900 trace("invoke(String, Object[], String[])", 901 "Found target object in descriptor"); 902 903 906 Method method; 907 Object targetObject; 908 909 method = findRMMBMethod(opMethodName, targetObjectField, 910 opClassName, sig); 911 912 if (method != null) 913 targetObject = this; 914 else { 915 if (tracing) 916 trace("invoke(String, Object[], String[])", 917 "looking for method in managedResource class"); 918 if (targetObjectField != null) 919 targetObject = targetObjectField; 920 else { 921 targetObject = managedResource; 922 if (targetObject == null) { 923 final String msg = 924 "managedResource for invoke " + opName + 925 " is null"; 926 Exception snfe = new ServiceNotFoundException (msg); 927 throw new MBeanException (snfe); 928 } 929 } 930 931 final Class targetClass; 932 933 if (opClassName != null) { 934 try { 935 final ClassLoader targetClassLoader = 936 targetObject.getClass().getClassLoader(); 937 targetClass = Class.forName(opClassName, false, 938 targetClassLoader); 939 } catch (ClassNotFoundException e) { 940 final String msg = 941 "class for invoke " + opName + " not found"; 942 throw new ReflectionException (e, msg); 943 } 944 } else 945 targetClass = targetObject.getClass(); 946 947 method = resolveMethod(targetClass, opMethodName, sig); 948 } 949 950 if (tracing) 951 trace("invoke(String, Object[], String[])", 952 "found " + opMethodName + ", now invoking"); 953 954 final Object result = 955 invokeMethod(opName, method, targetObject, opArgs); 956 957 if (tracing) 958 trace("invoke(String, Object[], String[])", 959 "successfully invoked method"); 960 961 if (result != null) 962 cacheResult(opInfo, opDescr, result); 963 964 return result; 965 } 966 967 private static Method resolveMethod(Class targetClass, 968 String opMethodName, 969 String [] sig) 970 throws ReflectionException { 971 final boolean tracing = tracing(); 972 973 if (tracing) 974 trace("resolveMethod", 975 "resolving " + targetClass + "." + opMethodName); 976 977 final Class [] argClasses; 978 979 if (sig == null) 980 argClasses = null; 981 else { 982 final ClassLoader targetClassLoader = targetClass.getClassLoader(); 983 argClasses = new Class [sig.length]; 984 for (int i = 0; i < sig.length; i++) { 985 if (tracing) 986 trace("resolveMethod", "resolve type " + sig[i]); 987 argClasses[i] = (Class ) primitiveClassMap.get(sig[i]); 988 if (argClasses[i] == null) { 989 try { 990 argClasses[i] = 991 Class.forName(sig[i], false, targetClassLoader); 992 } catch (ClassNotFoundException e) { 993 if (tracing) 994 trace("resolveMethod", "class not found"); 995 final String msg = "Parameter class not found"; 996 throw new ReflectionException (e, msg); 997 } 998 } 999 } 1000 } 1001 1002 try { 1003 return targetClass.getMethod(opMethodName, argClasses); 1004 } catch (NoSuchMethodException e) { 1005 final String msg = 1006 "Target method not found: " + targetClass.getName() + "." + 1007 opMethodName; 1008 throw new ReflectionException (e, msg); 1009 } 1010 } 1011 1012 1014 private static final Class [] primitiveClasses = { 1015 int.class, long.class, boolean.class, double.class, 1016 float.class, short.class, byte.class, char.class, 1017 }; 1018 private static final Map primitiveClassMap = 1019 new HashMap (); 1020 static { 1021 for (int i = 0; i < primitiveClasses.length; i++) { 1022 final Class c = primitiveClasses[i]; 1023 primitiveClassMap.put(c.getName(), c); 1024 } 1025 } 1026 1027 1030 private static Method findRMMBMethod(String opMethodName, 1031 Object targetObjectField, 1032 String opClassName, 1033 String [] sig) { 1034 if (tracing()) 1035 trace("invoke(String, Object[], String[])", 1036 "looking for method in RequiredModelMBean class"); 1037 if (!isRMMBMethodName(opMethodName)) 1038 return null; 1039 if (targetObjectField != null) 1040 return null; 1041 final Class rmmbClass = RequiredModelMBean .class; 1042 final Class targetClass; 1043 if (opClassName == null) 1044 targetClass = rmmbClass; 1045 else { 1046 try { 1047 final ClassLoader targetClassLoader = 1048 rmmbClass.getClassLoader(); 1049 targetClass = Class.forName(opClassName, false, 1050 targetClassLoader); 1051 if (!rmmbClass.isAssignableFrom(targetClass)) 1052 return null; 1053 } catch (ClassNotFoundException e) { 1054 return null; 1055 } 1056 } 1057 try { 1058 return resolveMethod(targetClass, opMethodName, sig); 1059 } catch (ReflectionException e) { 1060 return null; 1061 } 1062 } 1063 1064 1068 private Object invokeMethod(String opName, Method method, 1069 Object targetObject, Object [] opArgs) 1070 throws MBeanException , ReflectionException { 1071 try { 1072 ReflectUtil.checkPackageAccess(method.getDeclaringClass()); 1073 return MethodUtil.invoke(method, targetObject, opArgs); 1074 } catch (RuntimeErrorException ree) { 1075 throw new RuntimeOperationsException (ree, 1076 "RuntimeException occured in RequiredModelMBean "+ 1077 "while trying to invoke operation " + opName); 1078 } catch (RuntimeException re) { 1079 throw new RuntimeOperationsException (re, 1080 "RuntimeException occured in RequiredModelMBean "+ 1081 "while trying to invoke operation " + opName); 1082 } catch (IllegalAccessException iae) { 1083 throw new ReflectionException (iae, 1084 "IllegalAccessException occured in " + 1085 "RequiredModelMBean while trying to " + 1086 "invoke operation " + opName); 1087 } catch (InvocationTargetException ite) { 1088 Throwable mmbTargEx = ite.getTargetException(); 1089 if (mmbTargEx instanceof RuntimeException ) { 1090 throw new MBeanException ((RuntimeException )mmbTargEx, 1091 "RuntimeException thrown in RequiredModelMBean "+ 1092 "while trying to invoke operation " + opName); 1093 } else if (mmbTargEx instanceof Error ) { 1094 throw new RuntimeErrorException ((Error )mmbTargEx, 1095 "Error occured in RequiredModelMBean while trying "+ 1096 "to invoke operation " + opName); 1097 } else if (mmbTargEx instanceof ReflectionException ) { 1098 throw (ReflectionException ) mmbTargEx; 1099 } else { 1100 throw new MBeanException ((Exception )mmbTargEx, 1101 "Exception thrown in RequiredModelMBean "+ 1102 "while trying to invoke operation " + opName); 1103 } 1104 } catch (Error err) { 1105 throw new RuntimeErrorException (err, 1106 "Error occured in RequiredModelMBean while trying "+ 1107 "to invoke operation " + opName); 1108 } catch (Exception e) { 1109 throw new ReflectionException (e, 1110 "Exception occured in RequiredModelMBean while " + 1111 "trying to invoke operation " + opName); 1112 } 1113 } 1114 1115 1121 private void cacheResult(ModelMBeanOperationInfo opInfo, 1122 Descriptor opDescr, Object result) 1123 throws MBeanException { 1124 1125 Descriptor mmbDesc = 1126 modelMBeanInfo.getMBeanDescriptor(); 1127 1128 Object objctl = 1129 opDescr.getFieldValue("currencyTimeLimit"); 1130 String ctl; 1131 if (objctl != null) { 1132 ctl = objctl.toString(); 1133 } else { 1134 ctl = null; 1135 } 1136 if ((ctl == null) && (mmbDesc != null)) { 1137 objctl = 1138 mmbDesc.getFieldValue("currencyTimeLimit"); 1139 if (objctl != null) { 1140 ctl = objctl.toString(); 1141 } else { 1142 ctl = null; 1143 } 1144 } 1145 if ((ctl != null) && !(ctl.equals("-1"))) { 1146 opDescr.setField("value", result); 1147 opDescr.setField("lastUpdatedTimeStamp", 1148 (new Long ((new Date ()).getTime())).toString()); 1149 1150 modelMBeanInfo.setDescriptor(opDescr, 1151 "operation"); 1152 if (tracing()) { 1153 trace("invoke(String,Object[],Object[])", 1154 "new descriptor is " + 1155 opDescr.toString()); 1156 } 1157 } 1158 } 1159 1160 1174 private static Set rmmbMethodNames; 1175 private static synchronized boolean isRMMBMethodName(String name) { 1176 if (rmmbMethodNames == null) { 1177 try { 1178 Set names = new HashSet (); 1179 Method [] methods = RequiredModelMBean .class.getMethods(); 1180 for (int i = 0; i < methods.length; i++) 1181 names.add(methods[i].getName()); 1182 rmmbMethodNames = names; 1183 } catch (Exception e) { 1184 return true; 1185 } 1188 } 1189 return rmmbMethodNames.contains(name); 1190 } 1191 1192 1282 public Object getAttribute(String attrName) 1283 throws AttributeNotFoundException , MBeanException , 1284 ReflectionException { 1285 if (attrName == null) 1286 throw new RuntimeOperationsException (new 1287 IllegalArgumentException ("attributeName must not be null"), 1288 "Exception occured trying to get attribute of a " + 1289 "RequiredModelMBean"); 1290 1291 if (tracing()) 1292 trace("getAttribute(String)","Entry with" + attrName); 1293 1294 1295 ModelMBeanAttributeInfo attrInfo=null; 1296 Descriptor attrDescr=null; 1297 Object response = null; 1298 1299 try { 1300 if (modelMBeanInfo == null) 1301 throw new AttributeNotFoundException ( 1302 "getAttribute failed: ModelMBeanInfo not found for "+ 1303 attrName); 1304 1305 attrInfo = modelMBeanInfo.getAttribute(attrName); 1306 Descriptor mmbDesc = modelMBeanInfo.getMBeanDescriptor(); 1307 1308 if (attrInfo == null) 1309 throw new AttributeNotFoundException ("getAttribute failed:"+ 1310 " ModelMBeanAttributeInfo not found for " + attrName); 1311 1312 attrDescr = attrInfo.getDescriptor(); 1313 if (attrDescr != null) { 1314 if (!attrInfo.isReadable()) 1315 throw new AttributeNotFoundException ( 1316 "getAttribute failed: " + attrName + 1317 " is not readable "); 1318 1319 response = resolveForCacheValue(attrDescr); 1320 1321 1322 if (tracing()) 1323 trace("getAttribute(String)","*** cached value is " + 1324 response); 1325 1326 if (response == null) { 1327 1328 if (tracing()) 1329 trace("getAttribute(String)", 1330 "**** cached value is null" + 1331 " - " + "getting getMethod"); 1332 String attrGetMethod = 1333 (String )(attrDescr.getFieldValue("getMethod")); 1334 1335 if (attrGetMethod != null) { 1336 1337 if (tracing()) 1338 trace("getAttribute(String)", 1339 "invoking a getMethod for " + attrName); 1340 1341 Object getResponse = 1342 invoke(attrGetMethod, new Object [] {}, 1343 new String [] {}); 1344 1345 if (getResponse != null) { 1346 if (tracing()) 1348 trace("getAttribute(String)", 1349 "got a non-null response from getMethod\n"); 1350 1351 response = getResponse; 1352 1353 Object objctl = 1355 attrDescr.getFieldValue("currencyTimeLimit"); 1356 1357 String ctl; 1358 if (objctl != null) ctl = objctl.toString(); 1359 else ctl = null; 1360 1361 if ((ctl == null) && (mmbDesc != null)) { 1362 objctl = mmbDesc. 1363 getFieldValue("currencyTimeLimit"); 1364 if (objctl != null) ctl = objctl.toString(); 1365 else ctl = null; 1366 } 1367 1368 if ((ctl != null) && !(ctl.equals("-1"))) { 1369 if (tracing()) 1370 trace("getAttribute(String)", 1371 "setting cached value and "+ 1372 "lastUpdatedTime in descriptor"); 1373 1374 attrDescr.setField("value", response); 1375 final String stamp = 1376 (new Long ((new Date ()).getTime())). 1377 toString(); 1378 attrDescr.setField("lastUpdatedTimeStamp", 1379 stamp); 1380 attrInfo.setDescriptor(attrDescr); 1381 modelMBeanInfo.setDescriptor(attrDescr, 1382 "attribute"); 1383 if (tracing()) { 1384 trace("getAttribute(String)", 1385 "new descriptor is " + 1386 attrDescr.toString()); 1387 trace("getAttribute(String)","local: "+ 1388 "AttributeInfo descriptor is "+ 1389 attrInfo.getDescriptor(). 1390 toString()); 1391 final String attStr = modelMBeanInfo. 1392 getDescriptor(attrName,"attribute"). 1393 toString(); 1394 trace("getAttribute(String)", 1395 "modelMBeanInfo: " + 1396 "AttributeInfo descriptor is " + 1397 attStr); 1398 } 1399 } 1400 } else { 1401 if (tracing()) 1403 trace("getAttribute(String)", 1404 "got a null response from getMethod\n"); 1405 response = null; 1406 } 1407 } else { 1408 String qualifier=""; 1410 response = attrDescr.getFieldValue("value"); 1411 if (response == null) { 1412 qualifier="default "; 1413 response = attrDescr.getFieldValue("default"); 1414 } 1415 if (tracing()) 1416 trace("getAttribute(String)", 1417 "could not find getMethod for " + 1418 attrName + ", returning descriptor " + 1419 qualifier + "value"); 1420 } 1422 } 1423 1424 String respType = attrInfo.getType(); 1426 if (response != null) { 1427 String responseClass = response.getClass().getName(); 1428 if (!respType.equals(responseClass)) { 1429 boolean wrongType = false; 1430 boolean primitiveType = false; 1431 boolean correspondingTypes = false; 1432 for (int i = 0; i < primitiveTypes.length; i++) { 1433 if (respType.equals(primitiveTypes[i])) { 1434 primitiveType = true; 1435 if (responseClass.equals(primitiveWrappers[i])) 1436 correspondingTypes = true; 1437 break; 1438 } 1439 } 1440 if (primitiveType) { 1441 if (!correspondingTypes) 1443 wrongType = true; 1444 } else { 1445 boolean subtype; 1447 try { 1448 ClassLoader cl = 1449 response.getClass().getClassLoader(); 1450 Class c = Class.forName(respType, true, cl); 1451 subtype = c.isInstance(response); 1452 } catch (Exception e) { 1453 subtype = false; 1454 if (tracing()) 1455 traceX("getAttribute(String)", e); 1456 } 1457 if (!subtype) 1458 wrongType = true; 1459 } 1460 if (wrongType) { 1461 if (tracing()) 1462 trace("getAttribute(String)", 1463 "Wrong response type '" + respType + "'"); 1464 throw new MBeanException ( 1467 new InvalidAttributeValueException ( 1468 "Wrong value type received for get attribute"), 1469 "An exception occurred while trying to get an " + 1470 "attribute value through a RequiredModelMBean"); 1471 } 1472 } 1473 } 1474 } else { 1475 if (tracing()) 1476 trace("getAttribute(String)","getMethod failed " + 1477 attrName + " not in attributeDescriptor\n"); 1478 throw new MBeanException (new 1479 InvalidAttributeValueException ( 1480 "Unable to resolve attribute value, " + 1481 "no getMethod defined in descriptor for attribute"), 1482 "An exception occured while trying to get an "+ 1483 "attribute value through a RequiredModelMBean"); 1484 } 1485 1486 } catch (MBeanException mbe) { 1487 throw mbe; 1488 } catch (AttributeNotFoundException t) { 1489 throw t; 1490 } catch (Exception e) { 1491 if (tracing()) 1492 trace("getAttribute(String)","getMethod failed with " + 1493 e.getMessage() + " exception type " + 1494 (e.getClass()).toString()); 1495 throw new MBeanException (e,"An exception occured while trying "+ 1496 "to get an attribute value: " + e.getMessage()); 1497 } 1498 1499 if (tracing()) 1500 trace("getAttribute(String)","Exit"); 1501 1502 return response; 1503 } 1504 1505 1521 public AttributeList getAttributes(String [] attrNames) { 1522 if (tracing()) 1523 trace("getAttributes(String[])","Entry"); 1524 1525 AttributeList responseList = null; 1526 if (attrNames == null) 1527 throw new RuntimeOperationsException (new 1528 IllegalArgumentException ("attributeNames must not be null"), 1529 "Exception occured trying to get attributes of a "+ 1530 "RequiredModelMBean"); 1531 1532 responseList = new AttributeList (); 1533 for (int i = 0; i < attrNames.length; i++) { 1534 try { 1535 responseList.add(new Attribute (attrNames[i], 1536 getAttribute(attrNames[i]))); 1537 } catch (Exception e) { 1538 error("getAttributes(String[])","Failed to get \"" + 1541 attrNames[i] + "\": " + e); 1542 traceX("getAttributes(String[])",e); 1543 } 1544 } 1545 1546 if (tracing()) 1547 trace("getAttributes(String[])","Exit"); 1548 1549 return responseList; 1550 } 1551 1552 1624 public void setAttribute(Attribute attribute) 1625 throws AttributeNotFoundException , InvalidAttributeValueException , 1626 MBeanException , ReflectionException { 1627 1628 if (tracing()) 1629 trace("setAttribute()","Entry"); 1630 1631 if (attribute == null) 1632 throw new RuntimeOperationsException (new 1633 IllegalArgumentException ("attribute must not be null"), 1634 "Exception occured trying to set an attribute of a "+ 1635 "RequiredModelMBean"); 1636 1637 1638 1639 1640 1641 1642 String attrName = attribute.getName(); 1643 Object attrValue = attribute.getValue(); 1644 boolean updateDescriptor = false; 1645 1646 ModelMBeanAttributeInfo attrInfo = 1647 modelMBeanInfo.getAttribute(attrName); 1648 1649 if (attrInfo == null) 1650 throw new AttributeNotFoundException ("setAttribute failed: " + 1651 attrName + " is not found "); 1652 1653 Descriptor mmbDesc = modelMBeanInfo.getMBeanDescriptor(); 1654 Descriptor attrDescr = attrInfo.getDescriptor(); 1655 1656 if (attrDescr != null) { 1657 if (!attrInfo.isWritable()) 1658 throw new AttributeNotFoundException ("setAttribute failed: " 1659 + attrName + " is not writable "); 1660 1661 Object setResponse = null; 1662 String attrSetMethod = (String ) 1663 (attrDescr.getFieldValue("setMethod")); 1664 String attrType = (String )(attrInfo.getType()); 1665 Object currValue = "Unknown"; 1666 1667 try { 1668 currValue = this.getAttribute(attrName); 1669 } catch (Throwable t) { 1670 } 1672 1673 Attribute oldAttr = new Attribute (attrName, currValue); 1674 1675 1676 if (attrSetMethod == null) { 1677 if (attrValue != null) { 1678 try { 1679 final Class clazz = loadClass(attrType); 1680 if (! clazz.isInstance(attrValue)) throw new 1681 InvalidAttributeValueException (clazz.getName() + 1682 " expected, " + 1683 attrValue.getClass().getName() + 1684 " received."); 1685 } catch (ClassNotFoundException x) { 1686 error("setAttribute","Class " + attrType + 1687 " for attribute " + attrName + " not found: " + 1688 x); 1689 debug("setAttribute",x); 1690 } 1691 } 1692 updateDescriptor = true; 1693 } else { 1694 setResponse = invoke(attrSetMethod, 1695 (new Object [] {attrValue}), 1696 (new String [] {attrType}) ); 1697 } 1698 1699 1700 Object objctl = attrDescr.getFieldValue("currencyTimeLimit"); 1701 String ctl; 1702 if (objctl != null) ctl = objctl.toString(); 1703 else ctl = null; 1704 1705 if ((ctl == null) && (mmbDesc != null)) { 1706 objctl = mmbDesc.getFieldValue("currencyTimeLimit"); 1707 if (objctl != null) ctl = objctl.toString(); 1708 else ctl = null; 1709 } 1710 1711 final boolean updateCache = ((ctl != null) && !(ctl.equals("-1"))); 1712 if (updateCache || updateDescriptor) { 1713 if (tracing()) 1714 trace("setAttribute()","setting cached value of " + 1715 attrName + " to " + attrValue); 1716 1717 attrDescr.setField("value", attrValue); 1718 1719 if (updateCache) { 1720 final String currtime = 1721 (new Long ((new Date ()).getTime())).toString(); 1722 1723 attrDescr.setField("lastUpdatedTimeStamp", currtime); 1724 } 1725 1726 attrInfo.setDescriptor(attrDescr); 1727 1728 modelMBeanInfo.setDescriptor(attrDescr,"attribute"); 1729 if (tracing()) { 1730 trace("setAttribute()","new descriptor is " + 1731 attrDescr.toString()); 1732 trace("setAttribute()","AttributeInfo descriptor is " + 1733 attrInfo.getDescriptor().toString()); 1734 trace("setAttribute()","AttributeInfo descriptor is " + 1735 modelMBeanInfo.getDescriptor(attrName,"attribute") 1736 .toString()); 1737 } 1738 1739 } 1740 1741 if (tracing()) 1742 trace("setAttribute()","sending sendAttributeNotification"); 1743 1744 sendAttributeChangeNotification(oldAttr,attribute); 1745 1746 } else { 1748 if (tracing()) 1749 trace("setAttribute(String)","setMethod failed "+attrName+ 1750 " not in attributeDescriptor\n"); 1751 1752 throw new InvalidAttributeValueException ( 1753 "Unable to resolve attribute value, "+ 1754 "no defined in descriptor for attribute"); 1755 } 1757 if (tracing()) 1758 trace("setAttribute(Attribute)","Exit"); 1759 1760 } 1761 1762 1778 public AttributeList setAttributes(AttributeList attributes) { 1779 1780 if (tracing()) 1781 trace("setAttributes(AttributeList)","Entry"); 1782 1783 if (attributes == null) 1784 throw new RuntimeOperationsException (new 1785 IllegalArgumentException ("attributes must not be null"), 1786 "Exception occured trying to set attributes of a "+ 1787 "RequiredModelMBean"); 1788 1789 final AttributeList responseList = new AttributeList (); 1790 1791 for (Iterator i = attributes.iterator(); i.hasNext();) { 1793 final Attribute attr = (Attribute ) i.next(); 1794 try { 1795 setAttribute(attr); 1796 responseList.add(attr); 1797 } catch (Exception excep) { 1798 responseList.remove(attr); 1799 } 1800 } 1801 1802 return responseList; 1803 } 1804 1805 1806 1807 private ModelMBeanInfo createDefaultModelMBeanInfo() { 1808 return(new ModelMBeanInfoSupport ((this.getClass().getName()), 1809 "Default ModelMBean", null, null, null, null)); 1810 } 1811 1812 1813 1814 1815 1816 1817 private synchronized void writeToLog(String logFileName, 1818 String logEntry) throws Exception { 1819 1820 PrintStream logOut = null; 1821 FileOutputStream fos = null; 1822 if (tracing()) trace("writeToLog()","Notification Logging to " + 1823 logFileName + ": " + logEntry); 1824 if ((logFileName == null) || (logEntry == null)) { 1825 if (tracing()) trace("writeToLog()","Bad input parameters"); 1826 return; 1827 } 1828 1829 try { 1830 fos = new FileOutputStream (logFileName, true); 1831 logOut = new PrintStream (fos); 1832 logOut.println(logEntry); 1833 logOut.close(); 1834 if (tracing()) trace("writeToLog()", 1835 "Successfully opened log " + logFileName); 1836 } catch (Exception e) { 1837 if (tracing()) 1838 trace("writeToLog","Exception " + e.toString() + 1839 " trying to write to the Notification log file " + 1840 logFileName); 1841 throw e; 1842 } 1843 } 1844 1845 1846 1865 public void addNotificationListener(NotificationListener listener, 1866 NotificationFilter filter, 1867 Object handback) 1868 throws java.lang.IllegalArgumentException { 1869 final String ftag = "addNotificationListener(NotificationListener, NotificationFilter, Object)"; 1870 if (tracing()) 1871 trace(ftag,"Entry"); 1872 1873 if (listener == null) 1874 throw new IllegalArgumentException ( 1875 "notification listener must not be null"); 1876 1877 if (generalBroadcaster == null) 1878 generalBroadcaster = new NotificationBroadcasterSupport (); 1879 1880 generalBroadcaster.addNotificationListener(listener, filter, 1881 handback); 1882 if (tracing()) { 1883 trace(ftag,"NotificationListener added"); 1884 trace(ftag,"Exit"); 1885 } 1886 } 1887 1888 1900 public void removeNotificationListener(NotificationListener listener) 1901 throws ListenerNotFoundException { 1902 if (listener == null) 1903 throw new ListenerNotFoundException ( 1904 "Notification listener is null"); 1905 1906 final String ftag="removeNotificationListener(NotificationListener)"; 1907 1908 if (tracing()) trace(ftag,"Entry"); 1909 1910 if (generalBroadcaster == null) 1911 throw new ListenerNotFoundException ( 1912 "No notification listeners registered"); 1913 1914 1915 generalBroadcaster.removeNotificationListener(listener); 1916 1917 if (tracing()) trace(ftag,"Exit"); 1918 1919 } 1920 1921 public void removeNotificationListener(NotificationListener listener, 1922 NotificationFilter filter, 1923 Object handback) 1924 throws ListenerNotFoundException { 1925 1926 if (listener == null) 1927 throw new ListenerNotFoundException ( 1928 "Notification listener is null"); 1929 1930 final String ftag="removeNotificationListener(NotificationListener, NotificationFilter, Object)"; 1931 1932 if (tracing()) trace(ftag,"Entry"); 1933 1934 if (generalBroadcaster == null) 1935 throw new ListenerNotFoundException ( 1936 "No notification listeners registered"); 1937 1938 1939 generalBroadcaster.removeNotificationListener(listener,filter, 1940 handback); 1941 1942 if (tracing()) trace(ftag,"Exit"); 1943 1944 } 1945 1946 public void sendNotification(Notification ntfyObj) 1947 throws MBeanException , RuntimeOperationsException { 1948 if (tracing()) 1949 trace("sendNotification(Notification)","Entry"); 1950 1951 if (ntfyObj == null) 1952 throw new RuntimeOperationsException (new 1953 IllegalArgumentException ("notification object must not be "+ 1954 "null"), 1955 "Exception occured trying to send a notification from a "+ 1956 "RequiredModelMBean"); 1957 1958 1959 Descriptor ntfyDesc = 1961 modelMBeanInfo.getDescriptor(ntfyObj.getType(),"notification"); 1962 Descriptor mmbDesc = modelMBeanInfo.getMBeanDescriptor(); 1963 1964 if (ntfyDesc != null) { 1965 String logging = (String ) ntfyDesc.getFieldValue("log"); 1966 1967 if (logging == null) { 1968 if (mmbDesc != null) 1969 logging = (String ) mmbDesc.getFieldValue("log"); 1970 } 1971 1972 if ((logging != null) && 1973 (logging.equalsIgnoreCase("t") || 1974 logging.equalsIgnoreCase("true"))) { 1975 1976 String logfile = (String ) ntfyDesc.getFieldValue("logfile"); 1977 if (logfile == null) { 1978 if (mmbDesc != null) 1979 logfile = (String )mmbDesc.getFieldValue("logfile"); 1980 } 1981 if (logfile != null) { 1982 try { 1983 writeToLog(logfile,"LogMsg: " + 1984 ((new Date (ntfyObj.getTimeStamp())).toString())+ 1985 " " + ntfyObj.getType() + " " + 1986 ntfyObj.getMessage() + " Severity = " + 1987 (String )ntfyDesc.getFieldValue("severity")); 1988 } catch (Exception e) { 1989 error("sendNotification(Notification)", 1990 "Failed to log " + ntfyObj.getType() + 1991 " notification: " + e); 1992 traceX("sendNotification(Notification)",e); 1993 } 1994 } 1995 } 1996 } 1997 if (generalBroadcaster != null) { 1998 generalBroadcaster.sendNotification(ntfyObj); 1999 } 2000 2001 if (tracing()) trace("sendNotification(Notification)", 2002 "sendNotification sent provided notification object"); 2003 2004 if (tracing()) 2005 trace("sendNotification(Notification)","Exit"); 2006 2007 } 2008 2009 2010 public void sendNotification(String ntfyText) 2011 throws MBeanException , RuntimeOperationsException { 2012 if (tracing()) 2013 trace("sendNotification(String)","Entry"); 2014 2015 if (ntfyText == null) 2016 throw new RuntimeOperationsException (new 2017 IllegalArgumentException ("notification message must not "+ 2018 "be null"), 2019 "Exception occured trying to send a text notification "+ 2020 "from a ModelMBean"); 2021 2022 Notification myNtfyObj = new Notification ("jmx.modelmbean.generic", 2023 this, 1, ntfyText); 2024 sendNotification(myNtfyObj); 2025 if (tracing()) trace("sendNotification(string)", 2026 "Notification sent"); 2027 2028 if (tracing()) 2029 trace("sendNotification(String)","Exit"); 2030 } 2031 2032 2036 private static final 2037 boolean hasNotification(final ModelMBeanInfo info, 2038 final String notifName) { 2039 try { 2040 if (info == null) return false; 2041 else return (info.getNotification(notifName)!=null); 2042 } catch (MBeanException x) { 2043 return false; 2044 } catch (RuntimeOperationsException r) { 2045 return false; 2046 } 2047 } 2048 2049 2053 private static final ModelMBeanNotificationInfo makeGenericInfo() { 2054 final Descriptor genericDescriptor = new DescriptorSupport ( new 2055 String [] { 2056 "name=GENERIC", 2057 "descriptorType=notification", 2058 "log=T", 2059 "severity=6", 2060 "displayName=jmx.modelmbean.generic"} ); 2061 2062 return new ModelMBeanNotificationInfo (new 2063 String [] {"jmx.modelmbean.generic"}, 2064 "GENERIC", 2065 "A text notification has been issued by the managed resource", 2066 genericDescriptor); 2067 } 2068 2069 2073 private static final 2074 ModelMBeanNotificationInfo makeAttributeChangeInfo() { 2075 final Descriptor attributeDescriptor = new DescriptorSupport (new 2076 String [] { 2077 "name=ATTRIBUTE_CHANGE", 2078 "descriptorType=notification", 2079 "log=T", 2080 "severity=6", 2081 "displayName=jmx.attribute.change"}); 2082 2083 return new ModelMBeanNotificationInfo (new 2084 String [] {"jmx.attribute.change"}, 2085 "ATTRIBUTE_CHANGE", 2086 "Signifies that an observed MBean attribute value has changed", 2087 attributeDescriptor ); 2088 } 2089 2090 2107 public MBeanNotificationInfo [] getNotificationInfo() { 2108 if (tracing()) 2109 trace("getNotificationInfo()","Entry"); 2110 2111 2114 final boolean hasGeneric = hasNotification(modelMBeanInfo,"GENERIC"); 2117 2118 final boolean hasAttributeChange = 2122 hasNotification(modelMBeanInfo,"ATTRIBUTE_CHANGE"); 2123 2124 final ModelMBeanNotificationInfo [] currInfo = 2127 (ModelMBeanNotificationInfo [])modelMBeanInfo.getNotifications(); 2128 2129 final int len = ((currInfo==null?0:currInfo.length) + 2134 (hasGeneric?0:1) + (hasAttributeChange?0:1)); 2135 2136 final ModelMBeanNotificationInfo [] respInfo = 2139 new ModelMBeanNotificationInfo [len]; 2140 2141 2144 int inserted=0; 2148 if (!hasGeneric) 2149 respInfo[inserted++] = makeGenericInfo(); 2152 2153 2154 if (!hasAttributeChange) 2155 respInfo[inserted++] = makeAttributeChangeInfo(); 2158 2159 final int count = currInfo.length; 2162 final int offset = inserted; 2163 for (int j=0; j < count; j++) { 2164 respInfo[offset+j] = (ModelMBeanNotificationInfo )(currInfo[j]); 2165 } 2166 2167 if (tracing()) 2168 trace("getNotificationInfo()","Exit"); 2169 2170 return respInfo; 2171 } 2172 2173 2174 public void addAttributeChangeNotificationListener(NotificationListener 2175 inlistener, 2176 String 2177 inAttributeName, 2178 Object inhandback) 2179 throws MBeanException , RuntimeOperationsException , 2180 IllegalArgumentException { 2181 final String ftag="addAttributeChangeNotificationListener(NotificationListener, String, Object)"; 2182 2183 if (tracing()) trace(ftag,"Entry"); 2184 2185 if (inlistener == null) 2186 throw new IllegalArgumentException ( 2187 "Listener to be registered must not be null"); 2188 2189 2190 if (attributeBroadcaster == null) 2191 attributeBroadcaster = new NotificationBroadcasterSupport (); 2192 2193 AttributeChangeNotificationFilter currFilter = 2194 new AttributeChangeNotificationFilter (); 2195 2196 MBeanAttributeInfo [] attrInfo = modelMBeanInfo.getAttributes(); 2197 boolean found = false; 2198 if (inAttributeName == null) { 2199 if ((attrInfo != null) && (attrInfo.length>0)) { 2200 for (int i=0; i<attrInfo.length; i++) { 2201 currFilter.enableAttribute(attrInfo[i].getName()); 2202 } 2203 } 2204 } else { 2205 if ((attrInfo != null) && (attrInfo.length>0)) { 2206 for (int i=0; i<attrInfo.length; i++) { 2207 if (inAttributeName.equals(attrInfo[i].getName())) { 2208 found = true; 2209 currFilter.enableAttribute(inAttributeName); 2210 break; 2211 } 2212 } 2213 if (!found) { 2214 throw new RuntimeOperationsException (new 2215 IllegalArgumentException ( 2216 "The attribute name does not exist"), 2217 "Exception occured trying to add an "+ 2218 "AttributeChangeNotification listener"); 2219 } 2220 } 2221 } 2222 2223 if (tracing()) 2224 trace(ftag, "Set attribute change filter to " + 2225 ((currFilter.getEnabledAttributes()).firstElement()).toString()); 2226 2227 attributeBroadcaster.addNotificationListener(inlistener,currFilter, 2228 inhandback); 2229 if (tracing()) trace("addAttributeChangeNotificationListener", 2230 "added for " + inAttributeName); 2231 2232 if (tracing()) trace(ftag,"Exit"); 2233 } 2234 2235 public void removeAttributeChangeNotificationListener(NotificationListener inlistener, String inAttributeName) 2236 throws MBeanException , RuntimeOperationsException , 2237 ListenerNotFoundException { 2238 if (inlistener == null) throw new 2239 ListenerNotFoundException ("Notification listener is null"); 2240 2241 final String ftag = "removeAttributeChangeNotificationListener(NotificationListener, String)"; 2242 2243 if (tracing()) trace(ftag,"Entry"); 2244 2245 2246 if (attributeBroadcaster == null) 2247 throw new ListenerNotFoundException ( 2248 "No attribute change notification listeners registered"); 2249 2250 2251 MBeanAttributeInfo [] attrInfo = modelMBeanInfo.getAttributes(); 2252 boolean found = false; 2253 if ((attrInfo != null) && (attrInfo.length>0)) { 2254 for (int i=0; i<attrInfo.length; i++) { 2255 if (attrInfo[i].getName().equals(inAttributeName)) { 2256 found = true; 2257 break; 2258 } 2259 } 2260 } 2261 2262 if ((!found) && (inAttributeName != null)) { 2263 throw new RuntimeOperationsException (new 2264 IllegalArgumentException ("Invalid attribute name"), 2265 "Exception occured trying to remove "+ 2266 "attribute change notification listener"); 2267 } 2268 2269 2270 2273 2274 attributeBroadcaster.removeNotificationListener(inlistener); 2275 2276 2277 if (tracing()) trace(ftag,"Exit"); 2278 2279 } 2280 2281 2282 public void sendAttributeChangeNotification(AttributeChangeNotification 2283 ntfyObj) 2284 throws MBeanException , RuntimeOperationsException { 2285 final String ftag = 2286 "sendAttributeChangeNotification(AttributeChangeNotification)"; 2287 2288 if (tracing()) trace(ftag,"Entry"); 2289 2290 if (ntfyObj == null) 2291 throw new RuntimeOperationsException (new 2292 IllegalArgumentException ( 2293 "attribute change notification object must not be null"), 2294 "Exception occured trying to send "+ 2295 "attribute change notification of a ModelMBean"); 2296 2297 Object oldv = ntfyObj.getOldValue(); 2298 Object newv = ntfyObj.getNewValue(); 2299 2300 if (oldv == null) oldv = "null"; 2301 if (newv == null) newv = "null"; 2302 2303 if (tracing()) 2304 trace(ftag,"Sending AttributeChangeNotification " + 2305 " with " + ntfyObj.getAttributeName() + 2306 ntfyObj.getAttributeType() + 2307 ntfyObj.getNewValue() + 2308 ntfyObj.getOldValue()); 2309 2310 Descriptor ntfyDesc = 2312 modelMBeanInfo.getDescriptor(ntfyObj.getType(),"notification"); 2313 Descriptor mmbDesc = modelMBeanInfo.getMBeanDescriptor(); 2314 2315 String logging, logfile; 2316 2317 if (ntfyDesc != null) { 2318 logging =(String ) ntfyDesc.getFieldValue("log"); 2319 if (logging == null) { 2320 if (mmbDesc != null) 2321 logging = (String ) mmbDesc.getFieldValue("log"); 2322 } 2323 if ((logging != null) && 2324 ( logging.equalsIgnoreCase("t") || 2325 logging.equalsIgnoreCase("true"))) { 2326 logfile = (String ) ntfyDesc.getFieldValue("logfile"); 2327 if (logfile == null) { 2328 if (mmbDesc != null) 2329 logfile = (String )mmbDesc.getFieldValue("logfile"); 2330 } 2331 2332 if (logfile != null) { 2333 try { 2334 writeToLog(logfile,"LogMsg: " + 2335 ((new Date (ntfyObj.getTimeStamp())).toString())+ 2336 " " + ntfyObj.getType() + " " + 2337 ntfyObj.getMessage() + 2338 " Name = " + ntfyObj.getAttributeName() + 2339 " Old value = " + oldv + 2340 " New value = " + newv); 2341 } catch (Exception e) { 2342 error(ftag,"Failed to log " + ntfyObj.getType() + 2343 " notification: " + e); 2344 traceX(ftag,e); 2345 } 2346 } 2347 } 2348 } else if (mmbDesc != null) { 2349 logging = (String ) mmbDesc.getFieldValue("log"); 2350 if ((logging != null) && 2351 ( logging.equalsIgnoreCase("t") || 2352 logging.equalsIgnoreCase("true") )) { 2353 logfile = (String ) mmbDesc.getFieldValue("logfile"); 2354 2355 if (logfile != null) { 2356 try { 2357 writeToLog(logfile,"LogMsg: " + 2358 ((new Date (ntfyObj.getTimeStamp())).toString())+ 2359 " " + ntfyObj.getType() + " " + 2360 ntfyObj.getMessage() + 2361 " Name = " + ntfyObj.getAttributeName() + 2362 " Old value = " + oldv + 2363 " New value = " + newv); 2364 } catch (Exception e) { 2365 error(ftag,"Failed to log " + ntfyObj.getType() + 2366 " notification: " + e); 2367 traceX(ftag,e); 2368 } 2369 } 2370 } 2371 } 2372 if (attributeBroadcaster != null) { 2373 attributeBroadcaster.sendNotification(ntfyObj); 2374 } 2375 2376 if (generalBroadcaster != null) { 2383 generalBroadcaster.sendNotification(ntfyObj); 2384 } 2385 if (tracing()) trace(ftag,"sent notification"); 2386 2387 if (tracing()) trace(ftag,"Exit"); 2388 } 2389 2390 2391 public void sendAttributeChangeNotification(Attribute inOldVal, 2392 Attribute inNewVal) 2393 throws MBeanException , RuntimeOperationsException { 2394 final String ftag = 2395 "sendAttributeChangeNotification(Attribute, Attribute)"; 2396 if (tracing()) trace(ftag,"Entry"); 2397 2398 if ((inOldVal == null) || (inNewVal == null)) 2400 throw new RuntimeOperationsException (new 2401 IllegalArgumentException ("Attribute object must not be null"), 2402 "Exception occured trying to send " + 2403 "attribute change notification of a ModelMBean"); 2404 2405 2406 if (!(inOldVal.getName().equals(inNewVal.getName()))) 2407 throw new RuntimeOperationsException (new 2408 IllegalArgumentException ("Attribute names are not the same"), 2409 "Exception occured trying to send " + 2410 "attribute change notification of a ModelMBean"); 2411 2412 2413 Object newVal = inNewVal.getValue(); 2414 Object oldVal = inOldVal.getValue(); 2415 String className = "unknown"; 2416 if (newVal != null) 2417 className = newVal.getClass().getName(); 2418 if (oldVal != null) 2419 className = oldVal.getClass().getName(); 2420 2421 AttributeChangeNotification myNtfyObj = new 2422 AttributeChangeNotification (this, 2423 1, 2424 ((new Date ()).getTime()), 2425 "AttributeChangeDetected", 2426 inOldVal.getName(), 2427 className, 2428 inOldVal.getValue(), 2429 inNewVal.getValue()); 2430 2431 sendAttributeChangeNotification(myNtfyObj); 2432 2433 if (tracing()) trace(ftag,"Exit"); 2434 2435 } 2436 2437 2447 protected ClassLoaderRepository getClassLoaderRepository() { 2448 return MBeanServerFactory.getClassLoaderRepository(server); 2449 } 2450 2451 private Class loadClass(String className) 2452 throws ClassNotFoundException { 2453 try { 2454 return Class.forName(className); 2455 } catch (ClassNotFoundException e) { 2456 final ClassLoaderRepository clr = 2457 getClassLoaderRepository(); 2458 if (clr == null) throw new ClassNotFoundException (className); 2459 return clr.loadClass(className); 2460 } 2461 } 2462 2463 2464 2465 2466 2467 2468 2498 public ObjectName preRegister(MBeanServer server, 2499 ObjectName name) 2500 throws java.lang.Exception { 2501 if (name == null) throw new NullPointerException ( 2505 "name of RequiredModelMBean to registered is null"); 2506 this.server = server; 2507 return name; 2508 } 2509 2510 2523 public void postRegister(Boolean registrationDone) { 2524 registered = registrationDone.booleanValue(); 2525 } 2526 2527 2541 public void preDeregister() throws java.lang.Exception { 2542 } 2543 2544 2553 public void postDeregister() { 2554 registered = false; 2555 this.server=null; 2556 } 2557 2558 private final static boolean tracing() { 2560 2561 return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_MODELMBEAN); 2562 } 2563 2564 private static void trace(String inClass, String inMethod, String inText) { 2565 Trace.send(Trace.LEVEL_TRACE, Trace.INFO_MODELMBEAN, inClass, 2566 inMethod, inText); 2567 } 2568 2569 private static void traceX(String inMethod, Throwable x) { 2570 Trace.send(Trace.LEVEL_TRACE, Trace.INFO_MODELMBEAN, currClass, 2571 inMethod, x); 2572 } 2573 2574 private static void trace(String inMethod, String inText) { 2575 trace(currClass, inMethod, inText); 2576 } 2577 2578 private static void error(String inMethod, String inText) { 2579 Trace.send(Trace.LEVEL_ERROR, Trace.INFO_MODELMBEAN, currClass, 2580 inMethod, inText); 2581 } 2582 2583 private static boolean debugging() { 2584 return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_MODELMBEAN); 2585 } 2586 2587 private static void debug(String inClass, String inMethod, String inText) { 2588 Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_MODELMBEAN, inClass, 2589 inMethod, inText); 2590 } 2591 2592 private static void debug(String inMethod, String inText) { 2593 debug(currClass, inMethod, inText); 2594 } 2595 2596 private static void debug(String inMethod, Throwable x) { 2597 Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_MODELMBEAN, currClass, 2598 inMethod, x); 2599 } 2600 2601 private static final String [] primitiveTypes; 2602 private static final String [] primitiveWrappers; 2603 static { 2604 primitiveTypes = new String [] { 2605 Boolean.TYPE.getName(), 2606 Byte.TYPE.getName(), 2607 Character.TYPE.getName(), 2608 Short.TYPE.getName(), 2609 Integer.TYPE.getName(), 2610 Long.TYPE.getName(), 2611 Float.TYPE.getName(), 2612 Double.TYPE.getName(), 2613 Void.TYPE.getName() 2614 }; 2615 primitiveWrappers = new String [] { 2616 Boolean .class.getName(), 2617 Byte .class.getName(), 2618 Character .class.getName(), 2619 Short .class.getName(), 2620 Integer .class.getName(), 2621 Long .class.getName(), 2622 Float .class.getName(), 2623 Double .class.getName(), 2624 Void .class.getName() 2625 }; 2626 } 2627} 2628 | Popular Tags |