1 23 24 29 30 package com.sun.enterprise.admin.selfmanagement.event; 31 32 import com.sun.jmx.mbeanserver.GetPropertyAction; 33 import java.beans.BeanInfo ; 34 import java.beans.Introspector ; 35 import java.beans.PropertyDescriptor ; 36 import java.io.IOException ; 37 import java.lang.reflect.Array ; 38 import java.lang.reflect.InvocationTargetException ; 39 import java.security.AccessControlContext ; 40 import java.security.AccessController ; 41 import java.security.PrivilegedAction ; 42 import java.util.ArrayList ; 43 import java.util.List ; 44 import java.util.concurrent.ExecutorService ; 45 import java.util.concurrent.Executors ; 46 import java.util.concurrent.LinkedBlockingQueue ; 47 import java.util.concurrent.ScheduledExecutorService ; 48 import java.util.concurrent.ScheduledFuture ; 49 import java.util.concurrent.ThreadFactory ; 50 import java.util.concurrent.ThreadPoolExecutor ; 51 import java.util.concurrent.TimeUnit ; 52 import java.util.concurrent.atomic.AtomicInteger ; 53 import javax.management.AttributeNotFoundException ; 54 import javax.management.InstanceNotFoundException ; 55 import javax.management.IntrospectionException ; 56 import javax.management.MBeanAttributeInfo ; 57 import javax.management.MBeanException ; 58 import javax.management.MBeanInfo ; 59 import javax.management.MBeanRegistration ; 60 import javax.management.MBeanServer ; 61 import javax.management.MBeanServerConnection ; 62 import javax.management.NotificationBroadcasterSupport ; 63 import javax.management.ObjectName ; 64 import javax.management.ReflectionException ; 65 import com.sun.appserv.management.event.StatisticMonitorNotification; 66 import static com.sun.appserv.management.event.StatisticMonitorNotification.*; 67 import javax.management.openmbean.CompositeData ; 68 import java.util.logging.Logger ; 69 import java.util.logging.Level ; 70 import com.sun.logging.LogDomains; 71 72 83 public abstract class StatisticMonitor 84 extends NotificationBroadcasterSupport 85 implements StatisticMonitorMBean, MBeanRegistration { 86 87 92 93 96 private List <ObjectName > observedObjects = new ArrayList <ObjectName >(); 97 98 101 private String observedAttribute; 102 103 107 private long granularityPeriod = 10000; 108 109 113 private boolean isActive = false; 114 115 119 private long sequenceNumber = 0; 120 121 125 private boolean isComplexTypeAttribute = false; 126 127 130 private String firstAttribute; 131 132 135 private List <String > remainingAttributes = new ArrayList <String >(); 136 137 140 private AccessControlContext acc; 141 142 145 private static final ScheduledExecutorService scheduler = 146 Executors.newSingleThreadScheduledExecutor( 147 new DaemonThreadFactory("Scheduler")); 148 149 152 private static final int maximumPoolSize; 153 154 157 private static final ExecutorService executor; 158 159 protected static Logger _logger = LogDomains.getLogger(LogDomains.SELF_MANAGEMENT_LOGGER); 162 163 static { 164 final String maximumPoolSizeSysProp = "jmx.x.monitor.maximum.pool.size"; 165 final String maximumPoolSizeStr = (String ) AccessController.doPrivileged( 166 new GetPropertyAction(maximumPoolSizeSysProp)); 167 if (maximumPoolSizeStr == null || 168 maximumPoolSizeStr.trim().length() == 0) { 169 maximumPoolSize = 10; 170 } else { 171 int maximumPoolSizeTmp = 10; 172 try { 173 maximumPoolSizeTmp = Integer.parseInt(maximumPoolSizeStr); 174 } catch (NumberFormatException e) { 175 if ( _logger.isLoggable(Level.WARNING) ) { 176 _logger.log(Level.WARNING,"Wrong value for " + maximumPoolSizeSysProp + " system property: " + e); 177 _logger.log(Level.WARNING,maximumPoolSizeSysProp + " defaults to 10."); 178 } 179 maximumPoolSizeTmp = 10; 180 } 181 if (maximumPoolSizeTmp < 1) { 182 maximumPoolSize = 1; 183 } else { 184 maximumPoolSize = maximumPoolSizeTmp; 185 } 186 } 187 executor = new ThreadPoolExecutor ( 188 maximumPoolSize, 189 maximumPoolSize, 190 60L, 191 TimeUnit.SECONDS, 192 new LinkedBlockingQueue <Runnable >(), 193 new DaemonThreadFactory("Executor")); 194 } 197 198 201 private MonitorTask monitorTask = new MonitorTask(); 202 203 206 private Runnable schedulerTask = new SchedulerTask(monitorTask); 207 208 211 private ScheduledFuture <?> schedulerFuture; 212 213 218 219 224 protected final static int capacityIncrement = 16; 225 226 231 protected int elementCount = 0; 232 233 237 @Deprecated 238 protected int alreadyNotified = 0; 239 240 251 protected int alreadyNotifieds[] = new int[capacityIncrement]; 252 253 260 protected MBeanServer server; 261 262 265 269 protected static final int RESET_FLAGS_ALREADY_NOTIFIED = 0; 270 271 277 protected static final int OBSERVED_OBJECT_ERROR_NOTIFIED = 1; 278 279 285 protected static final int OBSERVED_ATTRIBUTE_ERROR_NOTIFIED = 2; 286 287 294 protected static final int OBSERVED_ATTRIBUTE_TYPE_ERROR_NOTIFIED = 4; 295 296 303 protected static final int RUNTIME_ERROR_NOTIFIED = 8; 304 305 310 311 316 static final int THRESHOLD_ERROR_NOTIFIED = 16; 317 318 324 Object derivedGauge[] = new Object [capacityIncrement]; 325 326 332 long derivedGaugeTimestamp[] = new long[capacityIncrement]; 333 334 338 enum NumericalType { BYTE, SHORT, INTEGER, LONG, FLOAT, DOUBLE }; 339 340 343 static final Integer INTEGER_ZERO = new Integer (0); 344 345 350 351 365 public ObjectName preRegister(MBeanServer server, ObjectName name) 366 throws Exception { 367 if ( _logger.isLoggable(Level.FINER) ) 368 _logger.log(Level.FINER,"Initialize reference on the MBean Server"); 369 this.server = server; 370 return name; 371 } 372 373 380 public void postRegister (Boolean registrationDone) { 381 } 382 383 391 public void preDeregister() throws Exception { 392 if ( _logger.isLoggable(Level.FINER) ) 393 _logger.log(Level.FINER,"Stop the statistic monitor"); 394 stop(); 397 } 398 399 405 public void postDeregister() { 406 } 407 408 411 public abstract void start(); 412 413 416 public abstract void stop(); 417 418 421 431 @Deprecated 432 public synchronized ObjectName getObservedObject() { 433 if (observedObjects.isEmpty()) { 434 return null; 435 } else { 436 return observedObjects.get(0); 437 } 438 } 439 440 452 @Deprecated 453 public synchronized void setObservedObject(ObjectName object) 454 throws IllegalArgumentException { 455 while (!observedObjects.isEmpty()) { 456 removeObservedObject(observedObjects.get(0)); 457 } 458 addObservedObject(object); 459 } 460 461 470 public synchronized void addObservedObject(ObjectName object) 471 throws IllegalArgumentException { 472 473 if (object == null) { 474 throw new IllegalArgumentException ("Null observed object"); 475 } 476 477 if (observedObjects.contains(object)) { 480 return; 481 } 482 483 observedObjects.add(object); 486 487 if (elementCount >= alreadyNotifieds.length) { 490 alreadyNotifieds = expandArray(alreadyNotifieds); 491 derivedGauge = expandArray(derivedGauge); 492 derivedGaugeTimestamp = expandArray(derivedGaugeTimestamp); 493 } 494 alreadyNotifieds[elementCount] = RESET_FLAGS_ALREADY_NOTIFIED; 495 updateDeprecatedAlreadyNotified(); 496 derivedGauge[elementCount] = null; 497 derivedGaugeTimestamp[elementCount] = System.currentTimeMillis(); 498 499 insertSpecificElementAt(elementCount); 502 503 elementCount++; 506 } 507 508 515 public synchronized void removeObservedObject(ObjectName object) { 516 if (object == null) 519 return; 520 521 int index = observedObjects.indexOf(object); 522 if (index >= 0) { 523 observedObjects.remove(index); 524 525 removeElementAt(alreadyNotifieds, index); 528 updateDeprecatedAlreadyNotified(); 529 removeElementAt(derivedGauge, index); 530 removeElementAt(derivedGaugeTimestamp, index); 531 532 removeSpecificElementAt(index); 535 536 elementCount--; 539 } 540 } 541 542 551 public synchronized boolean containsObservedObject(ObjectName object) { 552 return observedObjects.contains(object); 553 } 554 555 562 public synchronized ObjectName [] getObservedObjects() { 563 return observedObjects.toArray(new ObjectName [0]); 564 } 565 566 574 public String getObservedAttribute() { 575 return observedAttribute; 576 } 577 578 588 public void setObservedAttribute(String attribute) 589 throws IllegalArgumentException { 590 591 if (attribute == null) { 592 throw new IllegalArgumentException ("Null observed attribute"); 593 } 594 595 synchronized(this) { 598 observedAttribute = attribute; 599 600 firstAttribute = null; 604 remainingAttributes.clear(); 605 isComplexTypeAttribute = false; 606 607 for (int i = 0; i < elementCount; i++) { 608 resetAlreadyNotified(i, 609 OBSERVED_ATTRIBUTE_ERROR_NOTIFIED | 610 OBSERVED_ATTRIBUTE_TYPE_ERROR_NOTIFIED); 611 } 612 } 613 } 614 615 623 public synchronized long getGranularityPeriod() { 624 return granularityPeriod; 625 } 626 627 637 public synchronized void setGranularityPeriod(long period) 638 throws IllegalArgumentException { 639 640 if (period <= 0) { 641 throw new IllegalArgumentException ("Nonpositive granularity " + 642 "period"); 643 } 644 granularityPeriod = period; 645 646 if (isActive()) { 649 schedulerFuture.cancel(false); 650 schedulerFuture = scheduler.schedule(schedulerTask, 651 period, 652 TimeUnit.MILLISECONDS); 653 } 654 } 655 656 665 668 public synchronized boolean isActive() { 669 return isActive; 670 } 671 672 677 678 681 void doStart() { 682 if ( _logger.isLoggable(Level.FINER) ) 683 _logger.log(Level.FINER,"Start the statistic monitor"); 684 685 synchronized(this) { 686 if (isActive()) { 687 if ( _logger.isLoggable(Level.WARNING) ) 688 _logger.log(Level.WARNING,"The StatisticMonitor is already active"); 689 return; 690 } 691 692 isActive = true; 693 694 firstAttribute = null; 698 remainingAttributes.clear(); 699 isComplexTypeAttribute = false; 700 701 acc = AccessController.getContext(); 705 706 schedulerFuture = scheduler.schedule(schedulerTask, 709 getGranularityPeriod(), 710 TimeUnit.MILLISECONDS); 711 } 712 } 713 714 717 void doStop() { 718 if ( _logger.isLoggable(Level.FINER) ) 719 _logger.log(Level.FINER,"Stop the StatisticMonitor"); 720 721 synchronized(this) { 722 if (!isActive()) { 723 if ( _logger.isLoggable(Level.WARNING) ) 724 _logger.log(Level.WARNING,"StatisticMonitor is not active"); 725 return; 726 } 727 728 isActive = false; 729 730 schedulerFuture.cancel(false); 733 734 acc = null; 737 738 firstAttribute = null; 742 remainingAttributes.clear(); 743 isComplexTypeAttribute = false; 744 } 745 } 746 747 758 synchronized Object getDerivedGauge(ObjectName object) { 759 int index = indexOf(object); 760 if (index != -1) { 761 return derivedGauge[index]; 762 } 763 else 764 return null; 765 } 766 767 779 synchronized long getDerivedGaugeTimeStamp(ObjectName object) { 780 int index = indexOf(object); 781 if (index != -1) 782 return derivedGaugeTimestamp[index]; 783 else 784 return 0; 785 } 786 787 Object getAttribute(MBeanServerConnection mbsc, 788 ObjectName object, 789 String attribute) 790 throws AttributeNotFoundException , 791 InstanceNotFoundException , 792 MBeanException , 793 ReflectionException , 794 IOException { 795 if (firstAttribute == null) { 798 if (attribute.indexOf('.') != -1) { 799 MBeanInfo mbi; 800 try { 801 mbi = mbsc.getMBeanInfo(object); 802 } catch (IntrospectionException e) { 803 throw new IllegalArgumentException (e); 804 } 805 MBeanAttributeInfo mbaiArray[] = mbi.getAttributes(); 806 for (MBeanAttributeInfo mbai : mbaiArray) { 807 if (attribute.equals(mbai.getName())) { 808 firstAttribute = attribute; 809 break; 810 } 811 } 812 if (firstAttribute == null) { 813 String tokens[] = attribute.split("\\.", -1); 814 firstAttribute = tokens[0]; 815 for (int i = 1; i < tokens.length; i++) 816 remainingAttributes.add(tokens[i]); 817 isComplexTypeAttribute = true; 818 } 819 } else { 820 firstAttribute = attribute; 821 } 822 } 823 return mbsc.getAttribute(object, firstAttribute); 824 } 825 826 Comparable <?> getComparableFromAttribute(ObjectName object, 827 String attribute, 828 Object value) 829 throws AttributeNotFoundException { 830 if (isComplexTypeAttribute) { 831 Object v = value; 832 for (String attr : remainingAttributes) 833 v = introspect(object, attr, v); 834 return (Comparable <?>) v; 835 } else { 836 return (Comparable <?>) value; 837 } 838 } 839 840 Object introspect(ObjectName object, 841 String attribute, 842 Object value) 843 throws AttributeNotFoundException { 844 try { 845 if (value.getClass().isArray() && attribute.equals("length")) { 846 return Array.getLength(value); 847 } else if (value instanceof CompositeData ) { 848 return ((CompositeData ) value).get(attribute); 849 } else { 850 BeanInfo bi = Introspector.getBeanInfo(value.getClass()); 853 PropertyDescriptor [] pds = bi.getPropertyDescriptors(); 854 for (PropertyDescriptor pd : pds) 855 if (pd.getName().equals(attribute)) { 856 return pd.getReadMethod().invoke(value); 857 } 858 throw new AttributeNotFoundException ( 859 "Could not find the getter method for the property " + 860 attribute + " using the Java Beans introspector"); 861 } 862 } catch (InvocationTargetException e) { 863 throw new IllegalArgumentException (e); 864 } catch (AttributeNotFoundException e) { 865 throw e; 866 } catch (Exception e) { 867 throw (AttributeNotFoundException ) 868 new AttributeNotFoundException (e.getMessage()).initCause(e); 869 } 870 } 871 872 boolean isComparableTypeValid(ObjectName object, 873 String attribute, 874 Comparable <?> value) { 875 return true; 876 } 877 878 String buildErrorNotification(ObjectName object, 879 String attribute, 880 Comparable <?> value) { 881 return null; 882 } 883 884 void onErrorNotification(StatisticMonitorNotification notification) { 885 } 886 887 Comparable <?> getDerivedGaugeFromComparable(ObjectName object, 888 String attribute, 889 Comparable <?> value) { 890 return (Comparable <?>) value; 891 } 892 893 StatisticMonitorNotification buildAlarmNotification(ObjectName object, 894 String attribute, 895 Comparable <?> value){ 896 return null; 897 } 898 899 boolean isThresholdTypeValid(ObjectName object, 900 String attribute, 901 Comparable <?> value) { 902 return true; 903 } 904 905 static Class <? extends Number > classForType(NumericalType type) { 906 switch (type) { 907 case BYTE: 908 return Byte .class; 909 case SHORT: 910 return Short .class; 911 case INTEGER: 912 return Integer .class; 913 case LONG: 914 return Long .class; 915 case FLOAT: 916 return Float .class; 917 case DOUBLE: 918 return Double .class; 919 default: 920 throw new IllegalArgumentException ( 921 "Unsupported numerical type"); 922 } 923 } 924 925 static boolean isValidForType(Object value, Class <? extends Number > c) { 926 return ((value == INTEGER_ZERO) || c.isInstance(value)); 927 } 928 929 935 synchronized ObjectName getObservedObject(int index) 936 throws ArrayIndexOutOfBoundsException { 937 return observedObjects.get(index); 938 } 939 940 943 synchronized void updateDeprecatedAlreadyNotified() { 944 if (elementCount > 0) 945 alreadyNotified = alreadyNotifieds[0]; 946 else 947 alreadyNotified = 0; 948 } 949 950 955 synchronized void setAlreadyNotified(int index, int mask) { 956 alreadyNotifieds[index] |= mask; 957 if (index == 0) 958 updateDeprecatedAlreadyNotified(); 959 } 960 961 966 synchronized void resetAlreadyNotified(int index, int mask) { 967 alreadyNotifieds[index] &= ~mask; 968 if (index == 0) 969 updateDeprecatedAlreadyNotified(); 970 } 971 972 synchronized boolean alreadyNotified(int index, int mask) { 973 return ((alreadyNotifieds[index] & mask) != 0); 974 } 975 976 981 synchronized void resetAllAlreadyNotified(int index) { 982 alreadyNotifieds[index] = RESET_FLAGS_ALREADY_NOTIFIED; 983 if (index == 0) 984 updateDeprecatedAlreadyNotified(); 985 } 986 987 990 int[] expandArray(int[] array) { 991 int[] newArray = new int[array.length + capacityIncrement]; 992 System.arraycopy(array, 0, newArray, 0, array.length); 993 return newArray; 994 } 995 996 999 long[] expandArray(long[] array) { 1000 long[] newArray = new long[array.length + capacityIncrement]; 1001 System.arraycopy(array, 0, newArray, 0, array.length); 1002 return newArray; 1003 } 1004 1005 1008 boolean[] expandArray(boolean[] array) { 1009 boolean[] newArray = new boolean[array.length + capacityIncrement]; 1010 System.arraycopy(array, 0, newArray, 0, array.length); 1011 return newArray; 1012 } 1013 1014 1017 Number [] expandArray(Number [] array) { 1018 Number [] newArray = new Number [array.length + capacityIncrement]; 1019 System.arraycopy(array, 0, newArray, 0, array.length); 1020 return newArray; 1021 } 1022 1023 1026 String [] expandArray(String [] array) { 1027 String [] newArray = new String [array.length + capacityIncrement]; 1028 System.arraycopy(array, 0, newArray, 0, array.length); 1029 return newArray; 1030 } 1031 1032 1035 NumericalType[] expandArray(NumericalType[] array) { 1036 NumericalType[] newArray = 1037 new NumericalType[array.length + capacityIncrement]; 1038 System.arraycopy(array, 0, newArray, 0, array.length); 1039 return newArray; 1040 } 1041 1042 1045 Object [] expandArray(Object [] array) { 1046 Object [] newArray = new Object [array.length + capacityIncrement]; 1047 System.arraycopy(array, 0, newArray, 0, array.length); 1048 return newArray; 1049 } 1050 1051 1055 synchronized void removeElementAt(int[] array, int index) { 1056 if (index < 0 || index >= elementCount) 1057 return; 1058 int j = elementCount - index - 1; 1059 if (j > 0) { 1060 System.arraycopy(array, index + 1, array, index, j); 1061 } 1062 } 1063 1064 1068 synchronized void removeElementAt(long[] array, int index) { 1069 if (index < 0 || index >= elementCount) 1070 return; 1071 int j = elementCount - index - 1; 1072 if (j > 0) { 1073 System.arraycopy(array, index + 1, array, index, j); 1074 } 1075 } 1076 1077 1081 synchronized void removeElementAt(boolean[] array, int index) { 1082 if (index < 0 || index >= elementCount) 1083 return; 1084 int j = elementCount - index - 1; 1085 if (j > 0) { 1086 System.arraycopy(array, index + 1, array, index, j); 1087 } 1088 } 1089 1090 1094 synchronized void removeElementAt(Object [] array, int index) { 1095 if (index < 0 || index >= elementCount) 1096 return; 1097 int j = elementCount - index - 1; 1098 if (j > 0) { 1099 System.arraycopy(array, index + 1, array, index, j); 1100 } 1101 array[elementCount - 1] = null; 1102 } 1103 1104 1108 synchronized int indexOf(ObjectName object) { 1109 return observedObjects.indexOf(object); 1110 } 1111 1112 1120 void insertSpecificElementAt(int index) {} 1121 1122 1130 void removeSpecificElementAt(int index) {} 1131 1132 1137 1138 1154 private void sendNotification(String type, long timeStamp, String msg, 1155 Object derGauge, Object trigger, 1156 ObjectName object, boolean onError) { 1157 if ( _logger.isLoggable(Level.FINER) ) 1158 _logger.log(Level.FINER,"send notification: " + 1159 "\n\tNotification observed object = " + object + 1160 "\n\tNotification observed attribute = " + observedAttribute + 1161 "\n\tNotification derived gauge = " + derGauge); 1162 1163 long seqno; 1164 synchronized (this) { 1165 seqno = sequenceNumber++; 1166 } 1167 1168 StatisticMonitorNotification mn = 1169 new StatisticMonitorNotification(type, 1170 this, 1171 seqno, 1172 timeStamp, 1173 msg, 1174 object, 1175 observedAttribute, 1176 derGauge, 1177 trigger); 1178 if (onError) 1179 onErrorNotification(mn); 1180 sendNotification(mn); 1181 } 1182 1183 1188 private void monitor(int index) { 1189 1190 String notifType = null; 1191 String msg = null; 1192 Object derGauge = null; 1193 Object trigger = null; 1194 ObjectName object = null; 1195 Comparable <?> value = null; 1196 StatisticMonitorNotification alarm = null; 1197 1198 synchronized(this) { 1199 if (!isActive()) 1200 return; 1201 1202 object = getObservedObject(index); 1209 String attribute = getObservedAttribute(); 1210 if (object == null || attribute == null) { 1211 return; 1212 } 1213 1214 Object attributeValue = null; 1219 try { 1220 attributeValue = getAttribute(server, object, attribute); 1221 if (attributeValue == null) 1222 if (alreadyNotified(index, 1223 OBSERVED_ATTRIBUTE_TYPE_ERROR_NOTIFIED)) 1224 return; 1225 else { 1226 notifType = OBSERVED_ATTRIBUTE_TYPE_ERROR; 1227 setAlreadyNotified( 1228 index, 1229 OBSERVED_ATTRIBUTE_TYPE_ERROR_NOTIFIED); 1230 msg = "The observed attribute value is null."; 1231 if ( _logger.isLoggable(Level.WARNING) ) 1232 _logger.log(Level.WARNING,msg); 1233 } 1234 } catch (NullPointerException np_ex) { 1235 if (alreadyNotified(index, RUNTIME_ERROR_NOTIFIED)) 1236 return; 1237 else { 1238 notifType = RUNTIME_ERROR; 1239 setAlreadyNotified(index, RUNTIME_ERROR_NOTIFIED); 1240 msg = 1241 "The monitor must be registered in the MBean " + 1242 "server or an MBeanServerConnection must be " + 1243 "explicitly supplied."; 1244 if ( _logger.isLoggable(Level.WARNING) ) { 1245 _logger.log(Level.WARNING,msg); 1246 _logger.log(Level.WARNING, np_ex.toString()); 1247 } 1248 } 1249 } catch (InstanceNotFoundException inf_ex) { 1250 if (alreadyNotified(index, OBSERVED_OBJECT_ERROR_NOTIFIED)) 1251 return; 1252 else { 1253 notifType = OBSERVED_OBJECT_ERROR; 1254 setAlreadyNotified(index, OBSERVED_OBJECT_ERROR_NOTIFIED); 1255 msg = 1256 "The observed object must be accessible in " + 1257 "the MBeanServerConnection."; 1258 if ( _logger.isLoggable(Level.WARNING) ) { 1259 _logger.log(Level.WARNING,msg); 1260 _logger.log(Level.WARNING, inf_ex.toString()); 1261 } 1262 } 1263 } catch (AttributeNotFoundException anf_ex) { 1264 if (alreadyNotified(index, OBSERVED_ATTRIBUTE_ERROR_NOTIFIED)) 1265 return; 1266 else { 1267 notifType = OBSERVED_ATTRIBUTE_ERROR; 1268 setAlreadyNotified(index, 1269 OBSERVED_ATTRIBUTE_ERROR_NOTIFIED); 1270 msg = 1271 "The observed attribute must be accessible in " + 1272 "the observed object."; 1273 if ( _logger.isLoggable(Level.WARNING) ) { 1274 _logger.log(Level.WARNING,msg); 1275 _logger.log(Level.WARNING, anf_ex.toString()); 1276 } 1277 1278 } 1279 } catch (MBeanException mb_ex) { 1280 if (alreadyNotified(index, RUNTIME_ERROR_NOTIFIED)) 1281 return; 1282 else { 1283 notifType = RUNTIME_ERROR; 1284 setAlreadyNotified(index, RUNTIME_ERROR_NOTIFIED); 1285 msg = mb_ex.getMessage(); 1286 if ( _logger.isLoggable(Level.WARNING) ) { 1287 _logger.log(Level.WARNING,msg); 1288 _logger.log(Level.WARNING, mb_ex.toString()); 1289 } 1290 } 1291 } catch (ReflectionException ref_ex) { 1292 if (alreadyNotified(index, RUNTIME_ERROR_NOTIFIED)) { 1293 return; 1294 } else { 1295 notifType = RUNTIME_ERROR; 1296 setAlreadyNotified(index, 1297 RUNTIME_ERROR_NOTIFIED); 1298 msg = ref_ex.getMessage(); 1299 if ( _logger.isLoggable(Level.WARNING) ) { 1300 _logger.log(Level.WARNING,msg); 1301 _logger.log(Level.WARNING, ref_ex.toString()); 1302 } 1303 } 1304 } catch (IOException io_ex) { 1305 if (alreadyNotified(index, RUNTIME_ERROR_NOTIFIED)) 1306 return; 1307 else { 1308 notifType = RUNTIME_ERROR; 1309 setAlreadyNotified(index, RUNTIME_ERROR_NOTIFIED); 1310 msg = io_ex.getMessage(); 1311 if ( _logger.isLoggable(Level.WARNING) ) { 1312 _logger.log(Level.WARNING,msg); 1313 _logger.log(Level.WARNING, io_ex.toString()); 1314 } 1315 } 1316 } catch (RuntimeException rt_ex) { 1317 if (alreadyNotified(index, RUNTIME_ERROR_NOTIFIED)) 1318 return; 1319 else { 1320 notifType = RUNTIME_ERROR; 1321 setAlreadyNotified(index, RUNTIME_ERROR_NOTIFIED); 1322 msg = rt_ex.getMessage(); 1323 if ( _logger.isLoggable(Level.WARNING) ) { 1324 _logger.log(Level.WARNING,msg); 1325 _logger.log(Level.WARNING, rt_ex.toString()); 1326 } 1327 } 1328 } 1329 1330 if (msg == null) { 1334 try { 1335 value = getComparableFromAttribute(object, 1336 attribute, 1337 attributeValue); 1338 } catch (AttributeNotFoundException e) { 1339 if (alreadyNotified(index, 1340 OBSERVED_ATTRIBUTE_ERROR_NOTIFIED)) 1341 return; 1342 else { 1343 notifType = OBSERVED_ATTRIBUTE_ERROR; 1344 setAlreadyNotified(index, 1345 OBSERVED_ATTRIBUTE_ERROR_NOTIFIED); 1346 msg = 1347 "The observed attribute must be accessible in " + 1348 "the observed object."; 1349 if ( _logger.isLoggable(Level.WARNING) ) { 1350 _logger.log(Level.WARNING,msg); 1351 _logger.log(Level.WARNING, e.toString()); 1352 } 1353 1354 } 1355 } catch (RuntimeException e) { 1356 if (alreadyNotified(index, RUNTIME_ERROR_NOTIFIED)) 1357 return; 1358 else { 1359 notifType = RUNTIME_ERROR; 1360 setAlreadyNotified(index, RUNTIME_ERROR_NOTIFIED); 1361 msg = e.getMessage(); 1362 if ( _logger.isLoggable(Level.WARNING) ) { 1363 _logger.log(Level.WARNING,msg); 1364 _logger.log(Level.WARNING, e.toString()); 1365 } 1366 } 1367 } 1368 } 1369 1370 if (msg == null) { 1374 if (!isComparableTypeValid(object, attribute, value)) { 1375 if (alreadyNotified(index, 1376 OBSERVED_ATTRIBUTE_TYPE_ERROR_NOTIFIED)) 1377 return; 1378 else { 1379 notifType = OBSERVED_ATTRIBUTE_TYPE_ERROR; 1380 setAlreadyNotified( 1381 index, 1382 OBSERVED_ATTRIBUTE_TYPE_ERROR_NOTIFIED); 1383 msg = "The observed attribute type is not valid."; 1384 if ( _logger.isLoggable(Level.WARNING) ) { 1385 _logger.log(Level.WARNING,msg); 1386 } 1387 } 1388 } 1389 } 1390 1391 if (msg == null) { 1394 if (!isThresholdTypeValid(object, attribute, value)) { 1395 if (alreadyNotified(index, THRESHOLD_ERROR_NOTIFIED)) 1396 return; 1397 else { 1398 notifType = THRESHOLD_ERROR; 1399 setAlreadyNotified(index, THRESHOLD_ERROR_NOTIFIED); 1400 msg = "The threshold type is not valid."; 1401 if ( _logger.isLoggable(Level.WARNING) ) { 1402 _logger.log(Level.WARNING,msg); 1403 } 1404 } 1405 } 1406 } 1407 1408 if (msg == null) { 1412 msg = buildErrorNotification(object, attribute, value); 1413 if (msg != null) { 1414 if (alreadyNotified(index, RUNTIME_ERROR_NOTIFIED)) 1415 return; 1416 else { 1417 notifType = RUNTIME_ERROR; 1418 setAlreadyNotified(index, RUNTIME_ERROR_NOTIFIED); 1419 if ( _logger.isLoggable(Level.WARNING) ) { 1420 _logger.log(Level.WARNING,msg); 1421 } 1422 } 1423 } 1424 } 1425 1426 if (msg == null) { 1430 resetAllAlreadyNotified(index); 1433 1434 derGauge = getDerivedGaugeFromComparable(object, 1437 attribute, 1438 value); 1439 derivedGauge[index] = derGauge; 1440 derivedGaugeTimestamp[index] = System.currentTimeMillis(); 1441 1442 alarm = buildAlarmNotification(object, 1445 attribute, 1446 (Comparable <?>) derGauge); 1447 } 1448 } 1449 1450 if (msg != null) 1453 sendNotification(notifType, 1454 System.currentTimeMillis(), 1455 msg, 1456 derGauge, 1457 trigger, 1458 object, 1459 true); 1460 1461 if (alarm != null && alarm.getType() != null) { 1464 sendNotification(alarm.getType(), 1465 System.currentTimeMillis(), 1466 alarm.getMessage(), 1467 derGauge, 1468 alarm.getTrigger(), 1469 object, 1470 false); 1471 } 1472 } 1473 1474 1480 private static class SchedulerTask implements Runnable { 1481 1482 private Runnable task = null; 1483 1484 1489 1490 public SchedulerTask(Runnable task) { 1491 this.task = task; 1492 } 1493 1494 1499 1500 public void run() { 1501 executor.submit(task); 1502 } 1503 } 1504 1505 1511 private class MonitorTask implements Runnable { 1512 1513 1518 1519 public MonitorTask() { 1520 } 1521 1522 1527 1528 public void run() { 1529 synchronized(StatisticMonitor.this) { 1530 AccessController.doPrivileged(new PrivilegedAction () { 1531 public Object run() { 1532 if (StatisticMonitor.this.isActive()) 1533 for (int i = 0; i < StatisticMonitor.this.elementCount; i++) 1534 StatisticMonitor.this.monitor(i); 1535 return null; 1536 } 1537 }, StatisticMonitor.this.acc); 1538 StatisticMonitor.this.schedulerFuture = 1539 scheduler.schedule(StatisticMonitor.this.schedulerTask, 1540 StatisticMonitor.this.getGranularityPeriod(), 1541 TimeUnit.MILLISECONDS); 1542 } 1543 } 1544 } 1545 1546 1559 private static class DaemonThreadFactory implements ThreadFactory { 1560 final ThreadGroup group; 1561 final AtomicInteger threadNumber = new AtomicInteger (1); 1562 final String namePrefix; 1563 final String nameSuffix = "]"; 1564 1565 public DaemonThreadFactory(String poolName) { 1566 SecurityManager s = System.getSecurityManager(); 1567 group = (s != null) ? s.getThreadGroup() : 1568 Thread.currentThread().getThreadGroup(); 1569 namePrefix = "JMX Monitor " + poolName + " Pool [Thread-"; 1570 } 1571 1572 public Thread newThread(Runnable r) { 1573 Thread t = new Thread (group, 1574 r, 1575 namePrefix + 1576 threadNumber.getAndIncrement() + 1577 nameSuffix, 1578 0); 1579 t.setDaemon(true); 1580 if (t.getPriority() != Thread.NORM_PRIORITY) 1581 t.setPriority(Thread.NORM_PRIORITY); 1582 return t; 1583 } 1584 } 1585} 1586 | Popular Tags |