1 23 24 29 30 package com.sun.enterprise.admin.selfmanagement.event; 31 32 import javax.management.ObjectName ; 33 import javax.management.MBeanNotificationInfo ; 34 import static com.sun.enterprise.admin.selfmanagement.event.StatisticMonitor.NumericalType.*; 35 import com.sun.appserv.management.event.StatisticMonitorNotification; 36 import static com.sun.appserv.management.event.StatisticMonitorNotification.*; 37 import java.util.logging.Logger ; 38 import java.util.logging.Level ; 39 import com.sun.logging.LogDomains; 40 41 84 public class CounterStatisticMonitor extends StatisticMonitor implements CounterStatisticMonitorMBean { 85 86 91 92 97 private Number threshold[] = new Number [capacityIncrement]; 98 99 103 private Number modulus = INTEGER_ZERO; 104 105 109 private Number offset = INTEGER_ZERO; 110 111 116 private boolean notify = false; 117 118 125 private boolean differenceMode = false; 126 127 133 private Number initThreshold = INTEGER_ZERO; 134 135 140 private Number previousScanCounter[] = new Number [capacityIncrement]; 141 142 149 private boolean modulusExceeded[] = new boolean[capacityIncrement]; 150 151 158 private Number derivedGaugeExceeded[] = new Number [capacityIncrement]; 159 160 165 private boolean derivedGaugeValid[] = new boolean[capacityIncrement]; 166 167 173 private boolean eventAlreadyNotified[] = new boolean[capacityIncrement]; 174 175 180 private NumericalType type[] = new NumericalType[capacityIncrement]; 181 182 private static final String [] types = { 183 RUNTIME_ERROR, 184 OBSERVED_OBJECT_ERROR, 185 OBSERVED_ATTRIBUTE_ERROR, 186 OBSERVED_ATTRIBUTE_TYPE_ERROR, 187 THRESHOLD_ERROR, 188 THRESHOLD_VALUE_EXCEEDED 189 }; 190 191 private static final MBeanNotificationInfo [] notifsInfo = { 192 new MBeanNotificationInfo ( 193 types, 194 "com.sun.appserv.management.event.StatisticMonitorNotification", 195 "Notifications sent by the CounterStatisticMonitor MBean") 196 }; 197 198 199 protected static Logger _logger = LogDomains.getLogger(LogDomains.SELF_MANAGEMENT_LOGGER); 202 203 208 209 212 public CounterStatisticMonitor() { 213 } 214 215 220 221 229 public void preDeregister() throws Exception { 230 231 super.preDeregister(); 234 if ( _logger.isLoggable(Level.FINER) ) 235 _logger.log(Level.FINER,"Reset the threshold values"); 236 237 238 synchronized (this) { 241 for (int i = 0; i < elementCount; i++) { 242 threshold[i] = initThreshold; 243 } 244 } 245 } 246 247 250 public synchronized void start() { 251 for (int i = 0; i < elementCount; i++) { 254 threshold[i] = initThreshold; 255 modulusExceeded[i] = false; 256 eventAlreadyNotified[i] = false; 257 previousScanCounter[i] = null; 258 } 259 doStart(); 260 } 261 262 265 public synchronized void stop() { 266 doStop(); 267 } 268 269 272 281 public synchronized Number getDerivedGauge(ObjectName object) { 282 return (Number ) super.getDerivedGauge(object); 283 } 284 285 295 public synchronized long getDerivedGaugeTimeStamp(ObjectName object) { 296 return super.getDerivedGaugeTimeStamp(object); 297 } 298 299 309 public synchronized Number getThreshold(ObjectName object) { 310 int index = indexOf(object); 311 if (index != -1) 312 return threshold[index]; 313 else 314 return null; 315 } 316 317 324 public synchronized Number getInitThreshold() { 325 return initThreshold; 326 } 327 328 341 public synchronized void setInitThreshold(Number value) 342 throws IllegalArgumentException { 343 344 if (value == null) { 345 throw new IllegalArgumentException ("Null threshold"); 346 } 347 if (value.longValue() < 0L) { 348 throw new IllegalArgumentException ("Negative threshold"); 349 } 350 351 initThreshold = value; 352 353 for (int i = 0; i < elementCount; i++) { 354 resetAlreadyNotified(i, THRESHOLD_ERROR_NOTIFIED); 355 356 threshold[i] = value; 359 modulusExceeded[i] = false; 360 eventAlreadyNotified[i] = false; 361 } 362 } 363 364 373 @Deprecated 374 public synchronized Number getDerivedGauge() { 375 return (Number ) derivedGauge[0]; 376 } 377 378 387 @Deprecated 388 public synchronized long getDerivedGaugeTimeStamp() { 389 return derivedGaugeTimestamp[0]; 390 } 391 392 402 @Deprecated 403 public synchronized Number getThreshold() { 404 return threshold[0]; 405 } 406 407 419 @Deprecated 420 public synchronized void setThreshold(Number value) 421 throws IllegalArgumentException { 422 setInitThreshold(value); 423 } 424 425 432 public synchronized Number getOffset() { 433 return offset; 434 } 435 436 446 public synchronized void setOffset(Number value) 447 throws IllegalArgumentException { 448 449 if (value == null) { 450 throw new IllegalArgumentException ("Null offset"); 451 } 452 if (value.longValue() < 0L) { 453 throw new IllegalArgumentException ("Negative offset"); 454 } 455 456 offset = value; 457 458 for (int i = 0; i < elementCount; i++) { 459 resetAlreadyNotified(i, THRESHOLD_ERROR_NOTIFIED); 460 } 461 } 462 463 470 public synchronized Number getModulus() { 471 return modulus; 472 } 473 474 484 public synchronized void setModulus(Number value) 485 throws IllegalArgumentException { 486 487 if (value == null) { 488 throw new IllegalArgumentException ("Null modulus"); 489 } 490 if (value.longValue() < 0L) { 491 throw new IllegalArgumentException ("Negative modulus"); 492 } 493 494 modulus = value; 495 496 for (int i = 0; i < elementCount; i++) { 497 resetAlreadyNotified(i, THRESHOLD_ERROR_NOTIFIED); 498 499 modulusExceeded[i] = false; 502 } 503 } 504 505 514 public synchronized boolean getNotify() { 515 return notify; 516 } 517 518 526 public synchronized void setNotify(boolean value) { 527 notify = value; 528 } 529 530 538 public synchronized boolean getDifferenceMode() { 539 return differenceMode; 540 } 541 542 549 public synchronized void setDifferenceMode(boolean value) { 550 differenceMode = value; 551 552 for (int i = 0; i < elementCount; i++) { 553 threshold[i] = initThreshold; 556 modulusExceeded[i] = false; 557 eventAlreadyNotified[i] = false; 558 previousScanCounter[i] = null; 559 } 560 } 561 562 567 public MBeanNotificationInfo [] getNotificationInfo() { 568 return notifsInfo; 569 } 570 571 576 577 589 private synchronized boolean updateDerivedGauge(Object scanCounter, 590 int index) { 591 592 boolean is_derived_gauge_valid; 593 594 if (differenceMode) { 597 598 if (previousScanCounter[index] != null) { 601 setDerivedGaugeWithDifference((Number )scanCounter, null, index); 602 603 if (((Number )derivedGauge[index]).longValue() < 0L) { 608 if (modulus.longValue() > 0L) { 609 setDerivedGaugeWithDifference((Number )scanCounter, 610 (Number )modulus, index); 611 } 612 threshold[index] = initThreshold; 613 eventAlreadyNotified[index] = false; 614 } 615 is_derived_gauge_valid = true; 616 } 617 else { 621 is_derived_gauge_valid = false; 622 } 623 previousScanCounter[index] = (Number )scanCounter; 624 } 625 else { 628 derivedGauge[index] = (Number )scanCounter; 629 is_derived_gauge_valid = true; 630 } 631 return is_derived_gauge_valid; 632 } 633 634 640 private StatisticMonitorNotification updateNotifications(int index) { 641 642 StatisticMonitorNotification n = null; 643 644 synchronized(this) { 645 if (!eventAlreadyNotified[index]) { 648 if (((Number )derivedGauge[index]).longValue() >= 649 threshold[index].longValue()) { 650 if (notify) { 651 n = new StatisticMonitorNotification(THRESHOLD_VALUE_EXCEEDED, 652 this, 653 0, 654 0, 655 "", 656 null, 657 null, 658 null, 659 threshold[index]); 660 } 661 if (!differenceMode) { 662 eventAlreadyNotified[index] = true; 663 } 664 } 665 } else { 666 if ( _logger.isLoggable(Level.INFO) ) { 667 _logger.log(Level.INFO,"The notification:" + 668 "\n\tNotification observed object = " + 669 getObservedObject(index) + 670 "\n\tNotification observed attribute = " + 671 getObservedAttribute() + 672 "\n\tNotification threshold level = " + 673 threshold[index] + 674 "\n\tNotification derived gauge = " + 675 derivedGauge[index] + 676 "\nhas already been sent"); 677 } 678 } 679 } 680 681 return n; 682 } 683 684 689 private synchronized void updateThreshold(int index) { 690 691 if (((Number )derivedGauge[index]).longValue() >= 695 threshold[index].longValue()) { 696 697 if (offset.longValue() > 0L) { 698 699 long threshold_value = threshold[index].longValue(); 703 while (((Number )derivedGauge[index]).longValue() >= 704 threshold_value) { 705 threshold_value += offset.longValue(); 706 } 707 708 switch(type[index]) { 711 case INTEGER: 712 threshold[index] = new Integer ((int)threshold_value); 713 break; 714 case BYTE: 715 threshold[index] = new Byte ((byte)threshold_value); 716 break; 717 case SHORT: 718 threshold[index] = new Short ((short)threshold_value); 719 break; 720 case LONG: 721 threshold[index] = new Long ((long)threshold_value); 722 break; 723 default: 724 if ( _logger.isLoggable(Level.WARNING) ) 726 _logger.log(Level.WARNING,"Threshold Type is Invalid"); 727 break; 728 } 729 730 if (!differenceMode) { 736 if (modulus.longValue() > 0L) { 737 if (threshold[index].longValue() > 738 modulus.longValue()) { 739 modulusExceeded[index] = true; 740 derivedGaugeExceeded[index] = 741 (Number ) derivedGauge[index]; 742 } 743 } 744 } 745 746 eventAlreadyNotified[index] = false; 749 } else { 750 modulusExceeded[index] = true; 751 derivedGaugeExceeded[index] = (Number ) derivedGauge[index]; 752 } 753 } 754 } 755 756 765 private synchronized void setDerivedGaugeWithDifference(Number scanCounter, 766 Number mod, 767 int index) { 768 774 775 long derived = 776 scanCounter.longValue() - previousScanCounter[index].longValue(); 777 if (mod != null) 778 derived += modulus.longValue(); 779 780 switch (type[index]) { 781 case INTEGER: derivedGauge[index] = new Integer ((int) derived); break; 782 case BYTE: derivedGauge[index] = new Byte ((byte) derived); break; 783 case SHORT: derivedGauge[index] = new Short ((short) derived); break; 784 case LONG: derivedGauge[index] = new Long (derived); break; 785 default: 786 if ( _logger.isLoggable(Level.WARNING) ) 788 _logger.log(Level.WARNING,"Threshold Type is Invalid"); 789 break; 790 } 791 } 792 793 798 799 805 boolean isComparableTypeValid(ObjectName object, 806 String attribute, 807 Comparable <?> value) { 808 int index = indexOf(object); 809 810 if (value instanceof Integer ) { 813 type[index] = INTEGER; 814 } else if (value instanceof Byte ) { 815 type[index] = BYTE; 816 } else if (value instanceof Short ) { 817 type[index] = SHORT; 818 } else if (value instanceof Long ) { 819 type[index] = LONG; 820 } else { 821 return false; 822 } 823 return true; 824 } 825 826 Comparable <?> getDerivedGaugeFromComparable(ObjectName object, 827 String attribute, 828 Comparable <?> value) { 829 int index = indexOf(object); 830 831 if (modulusExceeded[index]) { 834 if (((Number )derivedGauge[index]).longValue() < 835 derivedGaugeExceeded[index].longValue()) { 836 threshold[index] = initThreshold; 837 modulusExceeded[index] = false; 838 eventAlreadyNotified[index] = false; 839 } 840 } 841 842 derivedGaugeValid[index] = updateDerivedGauge(value, index); 850 851 return (Comparable <?>) derivedGauge[index]; 852 } 853 854 void onErrorNotification(StatisticMonitorNotification notification) { 855 int index = indexOf(notification.getObservedObject()); 856 synchronized(this) { 857 modulusExceeded[index] = false; 860 eventAlreadyNotified[index] = false; 861 previousScanCounter[index] = null; 862 } 863 } 864 865 StatisticMonitorNotification buildAlarmNotification(ObjectName object, 866 String attribute, 867 Comparable <?> value) { 868 int index = indexOf(object); 869 870 StatisticMonitorNotification alarm = null; 874 if (derivedGaugeValid[index]) { 875 alarm = updateNotifications(index); 876 updateThreshold(index); 877 } 878 return alarm; 879 } 880 881 895 synchronized boolean isThresholdTypeValid(ObjectName object, 896 String attribute, 897 Comparable <?> value) { 898 int index = indexOf(object); 899 Class <? extends Number > c = classForType(type[index]); 900 return (c.isInstance(threshold[index]) && 901 isValidForType(offset, c) && 902 isValidForType(modulus, c)); 903 } 904 905 910 synchronized void insertSpecificElementAt(int index) { 911 if (elementCount >= threshold.length) { 916 threshold = expandArray(threshold); 917 previousScanCounter = expandArray(previousScanCounter); 918 derivedGaugeExceeded = expandArray(derivedGaugeExceeded); 919 derivedGaugeValid = expandArray(derivedGaugeValid); 920 modulusExceeded = expandArray(modulusExceeded); 921 eventAlreadyNotified = expandArray(eventAlreadyNotified); 922 type = expandArray(type); 923 } 924 threshold[index] = INTEGER_ZERO; 925 previousScanCounter[index] = null; 926 derivedGaugeExceeded[index] = null; 927 derivedGaugeValid[index] = false; 928 modulusExceeded[index] = false; 929 eventAlreadyNotified[index] = false; 930 type[index] = INTEGER; 931 } 932 933 938 synchronized void removeSpecificElementAt(int index) { 939 removeElementAt(threshold, index); 944 removeElementAt(previousScanCounter, index); 945 removeElementAt(derivedGaugeExceeded, index); 946 removeElementAt(derivedGaugeValid, index); 947 removeElementAt(modulusExceeded, index); 948 removeElementAt(eventAlreadyNotified, index); 949 removeElementAt(type, index); 950 } 951 } 952 | Popular Tags |