1 7 8 package javax.management.monitor; 9 10 11 import java.util.Date ; 14 import java.util.Timer ; 15 import java.util.TimerTask ; 16 17 import javax.management.ObjectName ; 20 import javax.management.MBeanNotificationInfo ; 21 import javax.management.AttributeNotFoundException ; 22 import javax.management.InstanceNotFoundException ; 23 import javax.management.MBeanException ; 24 import javax.management.ReflectionException ; 25 26 53 public class StringMonitor extends Monitor implements StringMonitorMBean { 54 55 56 59 String makeDebugTag() { 60 return "StringMonitor"; 61 } 62 63 68 69 private final static String [] types = { 70 MonitorNotification.RUNTIME_ERROR, 71 MonitorNotification.OBSERVED_OBJECT_ERROR, 72 MonitorNotification.OBSERVED_ATTRIBUTE_ERROR, 73 MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR, 74 MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED, 75 MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED}; 76 private final static MBeanNotificationInfo [] notifsInfo = { 77 new MBeanNotificationInfo (types, 78 "javax.management.monitor.MonitorNotification", 79 "Notifications sent by the StringMonitor MBean")}; 80 81 85 private String stringToCompare = ""; 86 87 92 private boolean notifyMatch = false; 93 94 99 private boolean notifyDiffer = false; 100 101 106 private String derivedGauge[] = new String [capacityIncrement]; 107 108 113 private long derivedGaugeTimestamp[] = new long[capacityIncrement]; 114 115 120 private int status[] = new int[capacityIncrement]; 121 122 private static final int MATCHING = 0; 125 private static final int DIFFERING = 1; 126 private static final int MATCHING_OR_DIFFERING = 2; 127 128 131 private transient Timer timer = null; 132 133 134 139 140 143 public StringMonitor() { 144 dbgTag = makeDebugTag(); 145 } 146 147 152 153 156 public synchronized void start() { 157 158 if (isTraceOn()) { 159 trace("start", "start the string monitor"); 160 } 161 162 if (isActive) { 163 if (isTraceOn()) { 164 trace("start", "the string monitor is already activated"); 165 } 166 167 return; 168 } 169 170 isActive = true; 171 172 for (int i = 0; i < elementCount; i++) { 175 status[i] = MATCHING_OR_DIFFERING; 176 } 177 178 timer = new Timer (); 181 timer.schedule(new StringAlarmClock(this), 182 getGranularityPeriod(), getGranularityPeriod()); 183 } 184 185 188 192 public void stop() { 193 194 if (isTraceOn()) { 195 trace("stop", "stop the string monitor"); 196 } 197 198 synchronized(this) { 199 if (!isActive) { 200 if (isTraceOn()) { 201 trace("stop", "the string monitor is already deactivated"); 202 } 203 204 return; 205 } 206 207 isActive = false; 208 209 if (timer != null) { 212 timer.cancel(); 213 timer = null; 214 } 215 } 216 } 217 218 221 231 public synchronized void setGranularityPeriod(long period) 232 throws IllegalArgumentException { 233 super.setGranularityPeriod(period); 234 235 if (isActive) { 238 timer.cancel(); 239 timer = new Timer (); 240 timer.schedule(new StringAlarmClock(this), 241 getGranularityPeriod(), getGranularityPeriod()); 242 } 243 } 244 245 255 public synchronized String getDerivedGauge(ObjectName object) { 256 int index = indexOf(object); 257 if (index != -1) 258 return derivedGauge[index]; 259 else 260 return null; 261 } 262 263 275 public synchronized long getDerivedGaugeTimeStamp(ObjectName object) { 276 int index = indexOf(object); 277 if (index != -1) 278 return derivedGaugeTimestamp[index]; 279 else 280 return 0; 281 } 282 283 290 public synchronized String getDerivedGauge() { 291 return derivedGauge[0]; 292 } 293 294 302 @Deprecated 303 public synchronized long getDerivedGaugeTimeStamp() { 304 return derivedGaugeTimestamp[0]; 305 } 306 307 315 public synchronized String getStringToCompare() { 316 return stringToCompare; 317 } 318 319 329 public synchronized void setStringToCompare(String value) 330 throws java.lang.IllegalArgumentException { 331 332 if (value == null) { 333 throw new IllegalArgumentException ("Null string to compare"); 334 } 335 336 stringToCompare = value; 337 338 for (int i = 0; i < elementCount; i++) { 341 status[i] = MATCHING_OR_DIFFERING; 342 } 343 } 344 345 354 public synchronized boolean getNotifyMatch() { 355 return notifyMatch; 356 } 357 358 366 public synchronized void setNotifyMatch(boolean value) { 367 notifyMatch = value; 368 } 369 370 379 public synchronized boolean getNotifyDiffer() { 380 return notifyDiffer; 381 } 382 383 391 public synchronized void setNotifyDiffer(boolean value) { 392 notifyDiffer = value; 393 } 394 395 400 public MBeanNotificationInfo [] getNotificationInfo() { 401 return notifsInfo; 402 } 403 404 409 410 417 private synchronized void updateDerivedGauge(Object scanString, 418 int index) { 419 derivedGaugeTimestamp[index] = System.currentTimeMillis(); 420 derivedGauge[index] = (String )scanString; 421 } 422 423 429 private void updateNotifications(int index) { 430 boolean sendNotify = false; 431 String type = null; 432 long timeStamp = 0; 433 String msg = null; 434 Object derGauge = null; 435 Object trigger = null; 436 437 synchronized(this) { 438 if (status[index] == MATCHING_OR_DIFFERING) { 442 if (derivedGauge[index].equals(stringToCompare)) { 443 if (notifyMatch) { 444 sendNotify = true; 445 type = 446 MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED; 447 timeStamp = derivedGaugeTimestamp[index]; 448 msg = ""; 449 derGauge = derivedGauge[index]; 450 trigger = stringToCompare; 451 } 452 453 status[index] = DIFFERING; 454 } else { 455 if (notifyDiffer) { 456 sendNotify = true; 457 type = 458 MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED; 459 timeStamp = derivedGaugeTimestamp[index]; 460 msg = ""; 461 derGauge = derivedGauge[index]; 462 trigger = stringToCompare; 463 } 464 status[index] = MATCHING; 465 } 466 } else { 467 if (status[index] == MATCHING) { 468 if (derivedGauge[index].equals(stringToCompare)) { 469 if (notifyMatch) { 470 sendNotify = true; 471 type = 472 MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED; 473 timeStamp = derivedGaugeTimestamp[index]; 474 msg = ""; 475 derGauge = derivedGauge[index]; 476 trigger = stringToCompare; 477 478 } 479 status[index] = DIFFERING; 480 } 481 } else if (status[index] == DIFFERING) { 482 if (!derivedGauge[index].equals(stringToCompare)) { 483 if (notifyDiffer) { 484 sendNotify = true; 485 type = 486 MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED; 487 timeStamp = derivedGaugeTimestamp[index]; 488 msg = ""; 489 derGauge = derivedGauge[index]; 490 trigger = stringToCompare; 491 } 492 status[index] = MATCHING; 493 } 494 } 495 } 496 } 497 498 if (sendNotify) { 499 sendNotification(type, timeStamp, msg, derGauge, trigger, index); 500 } 501 } 502 503 508 509 514 void notifyAlarmClock(int index) { 515 boolean sendNotify = false; 516 String type = null; 517 long timeStamp = 0; 518 String msg = null; 519 Object derGauge = null; 520 Object trigger = null; 521 522 Object scan_string = null; 523 String notif_type = null; 524 525 synchronized(this) { 526 if (!isActive) 527 return; 528 529 532 if ((getObservedObject(index) == null) || 538 (getObservedAttribute() == null)) 539 return; 540 541 try { 546 scan_string = server.getAttribute(getObservedObject(index), 547 getObservedAttribute()); 548 if (scan_string == null) 549 return; 550 } catch (NullPointerException np_ex) { 551 if (alreadyNotified(index, RUNTIME_ERROR_NOTIFIED)) 552 return; 553 else { 554 notif_type = MonitorNotification.RUNTIME_ERROR; 555 setAlreadyNotified(index, RUNTIME_ERROR_NOTIFIED); 556 msg = 557 "The string monitor must be registered in " + 558 "the MBean server."; 559 } 560 } catch (InstanceNotFoundException inf_ex) { 561 if (alreadyNotified(index, OBSERVED_OBJECT_ERROR_NOTIFIED)) 562 return; 563 else { 564 notif_type = MonitorNotification.OBSERVED_OBJECT_ERROR; 565 setAlreadyNotified(index, OBSERVED_OBJECT_ERROR_NOTIFIED); 566 msg = 567 "The observed object must be registered in " + 568 "the MBean server."; 569 } 570 } catch (AttributeNotFoundException anf_ex) { 571 if (alreadyNotified(index, OBSERVED_ATTRIBUTE_ERROR_NOTIFIED)) 572 return; 573 else { 574 notif_type = MonitorNotification.OBSERVED_ATTRIBUTE_ERROR; 575 setAlreadyNotified(index, 576 OBSERVED_ATTRIBUTE_ERROR_NOTIFIED); 577 msg = 578 "The observed attribute must be accessible in " + 579 "the observed object."; 580 } 581 } catch (MBeanException mb_ex) { 582 if (alreadyNotified(index, RUNTIME_ERROR_NOTIFIED)) 583 return; 584 else { 585 notif_type = MonitorNotification.RUNTIME_ERROR; 586 setAlreadyNotified(index, RUNTIME_ERROR_NOTIFIED); 587 msg = mb_ex.getMessage(); 588 } 589 } catch (ReflectionException ref_ex) { 590 if (alreadyNotified(index, OBSERVED_ATTRIBUTE_ERROR_NOTIFIED)) 591 return; 592 else { 593 notif_type = MonitorNotification.OBSERVED_ATTRIBUTE_ERROR; 594 setAlreadyNotified(index, OBSERVED_ATTRIBUTE_ERROR_NOTIFIED); 595 msg = ref_ex.getMessage(); 596 } 597 } 598 599 if (msg == null) { 600 if (!(scan_string instanceof String )) { 603 if (alreadyNotified(index, 604 OBSERVED_ATTRIBUTE_TYPE_ERROR_NOTIFIED)) 605 return; 606 else { 607 notif_type = 608 MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR; 609 setAlreadyNotified(index, 610 OBSERVED_ATTRIBUTE_TYPE_ERROR_NOTIFIED); 611 msg = 612 "The observed attribute type must be " + 613 "a string type."; 614 } 615 } 616 } 617 618 if (msg == null) { 619 620 resetAllAlreadyNotified(index); 623 624 updateDerivedGauge(scan_string, index); 627 628 updateNotifications(index); 631 632 } else { 633 634 636 timeStamp = derivedGaugeTimestamp[index]; 637 derGauge = derivedGauge[index]; 638 639 status[index] = MATCHING_OR_DIFFERING; 642 } 643 } 644 645 if (msg != null) { 646 sendNotification(type, timeStamp, msg, derGauge, null, index); 647 } 648 } 649 650 655 synchronized void insertSpecificElementAt(int index) { 656 659 if (index != elementCount) 660 throw new Error ("Internal error: index != elementCount"); 661 662 if (elementCount >= derivedGauge.length) { 663 derivedGauge = expandArray(derivedGauge); 664 derivedGaugeTimestamp = expandArray(derivedGaugeTimestamp); 665 status = expandArray(status); 666 } 667 668 derivedGauge[index] = ""; 669 derivedGaugeTimestamp[index] = System.currentTimeMillis(); 670 status[index] = MATCHING_OR_DIFFERING; 671 } 672 673 678 synchronized void removeSpecificElementAt(int index) { 679 if (index < 0 || index >= elementCount) 680 return; 681 682 removeElementAt(derivedGauge, index); 685 removeElementAt(derivedGaugeTimestamp, index); 686 removeElementAt(status, index); 687 } 688 689 695 696 private static class StringAlarmClock extends TimerTask { 697 698 StringMonitor listener = null; 699 700 705 706 public StringAlarmClock(StringMonitor listener) { 707 this.listener = listener; 708 } 709 710 715 716 720 public void run() { 721 if (listener.isActive) { 722 for (int i = 0; i < listener.elementCount; i++) { 723 listener.notifyAlarmClock(i); 724 } 725 } 726 } 727 } 728 } 729 | Popular Tags |