1 23 24 29 30 package com.sun.enterprise.admin.selfmanagement.event; 31 32 import javax.management.MBeanNotificationInfo ; 33 import javax.management.ObjectName ; 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 93 public class GaugeStatisticMonitor extends StatisticMonitor implements GaugeStatisticMonitorMBean { 94 95 100 101 106 private Number highThreshold = INTEGER_ZERO; 107 108 113 private Number lowThreshold = INTEGER_ZERO; 114 115 121 private boolean notifyHigh = false; 122 123 129 private boolean notifyLow = false; 130 131 140 private boolean differenceMode = false; 141 142 147 private Number previousScanGauge[] = new Number [capacityIncrement]; 148 149 154 private int status[] = new int[capacityIncrement]; 155 156 161 private NumericalType type[] = new NumericalType[capacityIncrement]; 162 163 168 private boolean derivedGaugeValid[] = new boolean[capacityIncrement]; 169 170 private static final String [] types = { 171 RUNTIME_ERROR, 172 OBSERVED_OBJECT_ERROR, 173 OBSERVED_ATTRIBUTE_ERROR, 174 OBSERVED_ATTRIBUTE_TYPE_ERROR, 175 THRESHOLD_ERROR, 176 THRESHOLD_HIGH_VALUE_EXCEEDED, 177 THRESHOLD_LOW_VALUE_EXCEEDED 178 }; 179 180 private static final MBeanNotificationInfo [] notifsInfo = { 181 new MBeanNotificationInfo ( 182 types, 183 "com.sun.appserv.management.event.StatisticMonitorNotification", 184 "Notifications sent by the GaugeStatisticMonitor MBean") 185 }; 186 187 private static final int RISING = 0; 190 private static final int FALLING = 1; 191 private static final int RISING_OR_FALLING = 2; 192 193 protected static Logger _logger = LogDomains.getLogger(LogDomains.SELF_MANAGEMENT_LOGGER); 196 197 198 203 204 207 public GaugeStatisticMonitor() { 208 } 209 210 215 216 219 public synchronized void start() { 220 for (int i = 0; i < elementCount; i++) { 223 status[i] = RISING_OR_FALLING; 224 previousScanGauge[i] = null; 225 } 226 doStart(); 227 } 228 229 232 public synchronized void stop() { 233 doStop(); 234 } 235 236 239 249 public synchronized Number getDerivedGauge(ObjectName object) { 250 return (Number ) super.getDerivedGauge(object); 251 } 252 253 265 public synchronized long getDerivedGaugeTimeStamp(ObjectName object) { 266 return super.getDerivedGaugeTimeStamp(object); 267 } 268 269 278 @Deprecated 279 public synchronized Number getDerivedGauge() { 280 return (Number ) derivedGauge[0]; 281 } 282 283 292 @Deprecated 293 public synchronized long getDerivedGaugeTimeStamp() { 294 return derivedGaugeTimestamp[0]; 295 } 296 297 304 public synchronized Number getHighThreshold() { 305 return highThreshold; 306 } 307 308 315 public synchronized Number getLowThreshold() { 316 return lowThreshold; 317 } 318 319 334 public synchronized void setThresholds(Number highValue, Number lowValue) 335 throws IllegalArgumentException { 336 337 if ((highValue == null) || (lowValue == null)) { 338 throw new IllegalArgumentException ("Null threshold value"); 339 } 340 341 if (highValue.getClass() != lowValue.getClass()) { 342 throw new IllegalArgumentException ("Different type " + 343 "threshold values"); 344 } 345 346 if (isFirstStrictlyGreaterThanLast(lowValue, highValue, 347 highValue.getClass().getName())) { 348 throw new IllegalArgumentException ("High threshold less than " + 349 "low threshold"); 350 } 351 352 highThreshold = highValue; 353 lowThreshold = lowValue; 354 355 for (int i = 0; i < elementCount; i++) { 356 resetAlreadyNotified(i, THRESHOLD_ERROR_NOTIFIED); 357 358 status[i] = RISING_OR_FALLING; 361 } 362 } 363 364 373 public synchronized boolean getNotifyHigh() { 374 return notifyHigh; 375 } 376 377 385 public synchronized void setNotifyHigh(boolean value) { 386 notifyHigh = value; 387 } 388 389 398 public synchronized boolean getNotifyLow() { 399 return notifyLow; 400 } 401 402 410 public synchronized void setNotifyLow(boolean value) { 411 notifyLow = value; 412 } 413 414 422 public synchronized boolean getDifferenceMode() { 423 return differenceMode; 424 } 425 426 433 public synchronized void setDifferenceMode(boolean value) { 434 differenceMode = value; 435 436 for (int i = 0; i < elementCount; i++) { 439 status[i] = RISING_OR_FALLING; 440 previousScanGauge[i] = null; 441 } 442 } 443 444 449 public MBeanNotificationInfo [] getNotificationInfo() { 450 return notifsInfo; 451 } 452 453 458 459 471 private synchronized boolean updateDerivedGauge(Object scanGauge, 472 int index) { 473 474 boolean is_derived_gauge_valid; 475 476 if (differenceMode) { 479 480 if (previousScanGauge[index] != null) { 483 setDerivedGaugeWithDifference((Number )scanGauge, index); 484 is_derived_gauge_valid = true; 485 } 486 else { 490 is_derived_gauge_valid = false; 491 } 492 previousScanGauge[index] = (Number )scanGauge; 493 } 494 else { 497 derivedGauge[index] = (Number )scanGauge; 498 is_derived_gauge_valid = true; 499 } 500 501 return is_derived_gauge_valid; 502 } 503 504 510 private StatisticMonitorNotification updateNotifications(int index) { 511 512 StatisticMonitorNotification n = null; 513 514 synchronized(this) { 518 if (status[index] == RISING_OR_FALLING) { 519 if (isFirstGreaterThanLast((Number )derivedGauge[index], 520 highThreshold, 521 type[index])) { 522 if (notifyHigh) { 523 n = new StatisticMonitorNotification( 524 THRESHOLD_HIGH_VALUE_EXCEEDED, 525 this, 526 0, 527 0, 528 "", 529 null, 530 null, 531 null, 532 highThreshold); 533 } 534 status[index] = FALLING; 535 } else if (isFirstGreaterThanLast(lowThreshold, 536 (Number )derivedGauge[index], 537 type[index])) { 538 if (notifyLow) { 539 n = new StatisticMonitorNotification( 540 THRESHOLD_LOW_VALUE_EXCEEDED, 541 this, 542 0, 543 0, 544 "", 545 null, 546 null, 547 null, 548 lowThreshold); 549 } 550 status[index] = RISING; 551 } 552 } else { 553 if (status[index] == RISING) { 554 if (isFirstGreaterThanLast((Number )derivedGauge[index], 555 highThreshold, 556 type[index])) { 557 if (notifyHigh) { 558 n = new StatisticMonitorNotification( 559 THRESHOLD_HIGH_VALUE_EXCEEDED, 560 this, 561 0, 562 0, 563 "", 564 null, 565 null, 566 null, 567 highThreshold); 568 } 569 status[index] = FALLING; 570 } 571 } else if (status[index] == FALLING) { 572 if (isFirstGreaterThanLast(lowThreshold, 573 (Number )derivedGauge[index], 574 type[index])) { 575 if (notifyLow) { 576 n = new StatisticMonitorNotification( 577 THRESHOLD_LOW_VALUE_EXCEEDED, 578 this, 579 0, 580 0, 581 "", 582 null, 583 null, 584 null, 585 lowThreshold); 586 } 587 status[index] = RISING; 588 } 589 } 590 } 591 } 592 593 return n; 594 } 595 596 604 private synchronized void setDerivedGaugeWithDifference(Number scanGauge, 605 int index) { 606 Number prev = previousScanGauge[index]; 607 Number der; 608 switch (type[index]) { 609 case INTEGER: 610 der = new Integer (((Integer )scanGauge).intValue() - 611 ((Integer )prev).intValue()); 612 break; 613 case BYTE: 614 der = new Byte ((byte)(((Byte )scanGauge).byteValue() - 615 ((Byte )prev).byteValue())); 616 break; 617 case SHORT: 618 der = new Short ((short)(((Short )scanGauge).shortValue() - 619 ((Short )prev).shortValue())); 620 break; 621 case LONG: 622 der = new Long (((Long )scanGauge).longValue() - 623 ((Long )prev).longValue()); 624 break; 625 case FLOAT: 626 der = new Float (((Float )scanGauge).floatValue() - 627 ((Float )prev).floatValue()); 628 break; 629 case DOUBLE: 630 der = new Double (((Double )scanGauge).doubleValue() - 631 ((Double )prev).doubleValue()); 632 break; 633 default: 634 if ( _logger.isLoggable(Level.WARNING) ) 636 _logger.log(Level.WARNING,"The threshold type is invalid"); 637 return; 638 } 639 derivedGauge[index] = der; 640 } 641 642 653 private boolean isFirstGreaterThanLast(Number greater, 654 Number less, 655 NumericalType type) { 656 657 switch(type) { 658 case INTEGER: 659 case BYTE: 660 case SHORT: 661 case LONG: 662 return (greater.longValue() >= less.longValue()); 663 case FLOAT: 664 case DOUBLE: 665 return (greater.doubleValue() >= less.doubleValue()); 666 default: 667 if ( _logger.isLoggable(Level.WARNING) ) 669 _logger.log(Level.WARNING,"The threshold type is invalid"); 670 return false; 671 } 672 } 673 674 684 private boolean isFirstStrictlyGreaterThanLast(Number greater, 685 Number less, 686 String className) { 687 688 if (className.equals("java.lang.Integer") || 689 className.equals("java.lang.Byte") || 690 className.equals("java.lang.Short") || 691 className.equals("java.lang.Long")) { 692 693 return (greater.longValue() > less.longValue()); 694 } 695 else if (className.equals("java.lang.Float") || 696 className.equals("java.lang.Double")) { 697 698 return (greater.doubleValue() > less.doubleValue()); 699 } 700 else { 701 if ( _logger.isLoggable(Level.WARNING) ) 703 _logger.log(Level.WARNING,"The threshold type is invalid"); 704 return false; 705 } 706 } 707 708 713 714 720 boolean isComparableTypeValid(ObjectName object, 721 String attribute, 722 Comparable <?> value) { 723 int index = indexOf(object); 724 725 if (value instanceof Integer ) { 729 type[index] = INTEGER; 730 } else if (value instanceof Byte ) { 731 type[index] = BYTE; 732 } else if (value instanceof Short ) { 733 type[index] = SHORT; 734 } else if (value instanceof Long ) { 735 type[index] = LONG; 736 } else if (value instanceof Float ) { 737 type[index] = FLOAT; 738 } else if (value instanceof Double ) { 739 type[index] = DOUBLE; 740 } else { 741 return false; 742 } 743 return true; 744 } 745 746 Comparable <?> getDerivedGaugeFromComparable(ObjectName object, 747 String attribute, 748 Comparable <?> value) { 749 int index = indexOf(object); 750 751 derivedGaugeValid[index] = updateDerivedGauge(value, index); 759 760 return (Comparable <?>) derivedGauge[index]; 761 } 762 763 void onErrorNotification(StatisticMonitorNotification notification) { 764 int index = indexOf(notification.getObservedObject()); 765 synchronized(this) { 766 status[index] = RISING_OR_FALLING; 769 previousScanGauge[index] = null; 770 } 771 } 772 773 StatisticMonitorNotification buildAlarmNotification(ObjectName object, 774 String attribute, 775 Comparable <?> value) { 776 int index = indexOf(object); 777 778 StatisticMonitorNotification alarm = null; 782 if (derivedGaugeValid[index]) 783 alarm = updateNotifications(index); 784 return alarm; 785 } 786 787 803 synchronized boolean isThresholdTypeValid(ObjectName object, 804 String attribute, 805 Comparable <?> value) { 806 int index = indexOf(object); 807 Class <? extends Number > c = classForType(type[index]); 808 return (isValidForType(highThreshold, c) && 809 isValidForType(lowThreshold, c)); 810 } 811 812 817 synchronized void insertSpecificElementAt(int index) { 818 if (elementCount >= previousScanGauge.length) { 821 previousScanGauge = expandArray(previousScanGauge); 822 status = expandArray(status); 823 type = expandArray(type); 824 derivedGaugeValid = expandArray(derivedGaugeValid); 825 } 826 previousScanGauge[index] = null; 827 status[index] = RISING_OR_FALLING; 828 type[index] = INTEGER; 829 derivedGaugeValid[index] = false; 830 } 831 832 837 synchronized void removeSpecificElementAt(int index) { 838 removeElementAt(previousScanGauge, index); 841 removeElementAt(status, index); 842 removeElementAt(type, index); 843 removeElementAt(derivedGaugeValid, index); 844 } 845 } 846 | Popular Tags |