1 7 8 package javax.management.monitor; 9 10 import java.util.ArrayList ; 13 import java.util.Arrays ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import javax.management.MBeanServer ; 20 import javax.management.MBeanRegistration ; 21 import javax.management.ObjectName ; 22 23 import com.sun.jmx.trace.Trace; 24 25 37 public abstract class Monitor 38 extends javax.management.NotificationBroadcasterSupport 39 implements MonitorMBean , javax.management.MBeanRegistration 40 { 41 46 47 51 private List observedObjects = new ArrayList (); 52 53 57 private String observedAttribute = null; 58 59 63 private long granularityPeriod = 10000; 64 65 66 71 72 77 protected final static int capacityIncrement = 16; 78 79 84 protected int elementCount = 0; 85 86 90 @Deprecated 91 protected int alreadyNotified = 0; 92 93 104 protected int alreadyNotifieds[] = new int[capacityIncrement]; 105 106 113 protected MBeanServer server = null; 114 115 118 122 protected static final int RESET_FLAGS_ALREADY_NOTIFIED = 0; 123 124 130 protected static final int OBSERVED_OBJECT_ERROR_NOTIFIED = 1; 131 132 138 protected static final int OBSERVED_ATTRIBUTE_ERROR_NOTIFIED = 2; 139 140 147 protected static final int OBSERVED_ATTRIBUTE_TYPE_ERROR_NOTIFIED = 4; 148 149 156 protected static final int RUNTIME_ERROR_NOTIFIED = 8; 157 158 163 @Deprecated 164 protected String dbgTag = "Monitor"; 165 166 167 172 173 177 boolean isActive = false; 178 179 183 long sequenceNumber = 0; 184 185 188 static boolean isTraceOn() { 189 return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_MONITOR); 190 } 191 192 static void trace(String clz, String func, String info) { 193 Trace.send(Trace.LEVEL_TRACE, Trace.INFO_MONITOR, clz, func, info); 194 } 195 196 void trace(String func, String info) { 197 trace(dbgTag, func, info); 198 } 199 200 static boolean isDebugOn() { 201 return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_MONITOR); 202 } 203 204 static void debug(String clz, String func, String info) { 205 Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_MONITOR, clz, func, info); 206 } 207 208 void debug(String func, String info) { 209 debug(dbgTag, func, info); 210 } 211 212 217 218 232 public ObjectName preRegister(MBeanServer server, ObjectName name) 233 throws java.lang.Exception { 234 235 if (isTraceOn()) { 236 trace("preRegister", 237 "initialize the reference on the MBean server"); 238 } 239 240 this.server = server; 241 return name; 242 } 243 244 251 public void postRegister (Boolean registrationDone) { 252 } 253 254 262 public void preDeregister() throws java.lang.Exception { 263 264 if (isTraceOn()) { 265 trace("preDeregister", "stop the monitor"); 266 } 267 268 stop(); 271 } 272 273 279 public void postDeregister() { 280 } 281 282 285 public abstract void start(); 286 287 290 public abstract void stop(); 291 292 295 305 @Deprecated 306 public ObjectName getObservedObject() { 307 synchronized(this) { 308 if (observedObjects.isEmpty()) { 309 return null; 310 } else { 311 return (ObjectName )observedObjects.get(0); 312 } 313 } 314 } 315 316 328 @Deprecated 329 public synchronized void setObservedObject(ObjectName object) 330 throws IllegalArgumentException { 331 while (!observedObjects.isEmpty()) { 332 removeObservedObject((ObjectName )observedObjects.get(0)); 333 } 334 335 addObservedObject(object); 336 } 337 338 347 public synchronized void addObservedObject(ObjectName object) 348 throws IllegalArgumentException { 349 350 if (object == null) { 351 throw new IllegalArgumentException ("Null observed object"); 352 } 353 354 if (observedObjects.contains(object)) { 357 return; 358 } 359 360 observedObjects.add(object); 363 364 int value = RESET_FLAGS_ALREADY_NOTIFIED; 367 value &= ~(OBSERVED_OBJECT_ERROR_NOTIFIED | 368 OBSERVED_ATTRIBUTE_ERROR_NOTIFIED | 369 OBSERVED_ATTRIBUTE_TYPE_ERROR_NOTIFIED); 370 if (alreadyNotifieds.length >= elementCount) 371 alreadyNotifieds = expandArray(alreadyNotifieds); 372 alreadyNotifieds[elementCount] = value; 373 374 updateDeprecatedAlreadyNotified(); 375 376 insertSpecificElementAt(elementCount); 379 380 elementCount++; 383 } 384 385 392 public void removeObservedObject(ObjectName object) { 393 synchronized(this) { 394 int index = observedObjects.indexOf(object); 395 if (index >= 0) { 396 observedObjects.remove(index); 397 398 removeElementAt(alreadyNotifieds, index); 401 updateDeprecatedAlreadyNotified(); 402 403 removeSpecificElementAt(index); 406 407 elementCount--; 410 } 411 } 412 } 413 414 423 public boolean containsObservedObject(ObjectName object) { 424 synchronized(this) { 425 return observedObjects.contains(object); 426 } 427 } 428 429 436 public ObjectName [] getObservedObjects() { 437 ObjectName [] objects; 438 synchronized(this) { 439 objects = new ObjectName [elementCount]; 440 for (int i=0; i<elementCount; i++) { 441 objects[i] = (ObjectName )observedObjects.get(i); 442 } 443 } 444 return objects; 445 } 446 447 455 public String getObservedAttribute() { 456 return observedAttribute; 457 } 458 459 469 public void setObservedAttribute(String attribute) 470 throws IllegalArgumentException { 471 472 if (attribute == null) { 473 throw new IllegalArgumentException ("Null observed attribute"); 474 } 475 476 synchronized(this) { 479 observedAttribute = attribute; 480 481 for (int i = 0; i < elementCount; i++) { 482 resetAlreadyNotified(i, 483 OBSERVED_ATTRIBUTE_ERROR_NOTIFIED | 484 OBSERVED_ATTRIBUTE_TYPE_ERROR_NOTIFIED); 485 } 486 } 487 } 488 489 497 public synchronized long getGranularityPeriod() { 498 return granularityPeriod; 499 } 500 501 511 public synchronized void setGranularityPeriod(long period) 512 throws IllegalArgumentException { 513 514 if (period <= 0) { 515 throw new IllegalArgumentException ("Nonpositive granularity " + 516 "period"); 517 } 518 granularityPeriod = period; 519 } 520 521 530 533 public synchronized boolean isActive() { 534 return isActive; 535 } 536 537 542 543 549 synchronized ObjectName getObservedObject(int index) 550 throws ArrayIndexOutOfBoundsException { 551 return (ObjectName )observedObjects.get(index); 552 } 553 554 557 synchronized void updateDeprecatedAlreadyNotified() { 558 if (elementCount > 0) 559 alreadyNotified = alreadyNotifieds[0]; 560 else 561 alreadyNotified = 0; 562 } 563 564 569 synchronized void setAlreadyNotified(int index, int mask) { 570 alreadyNotifieds[index] |= mask; 571 if (index == 0) 572 updateDeprecatedAlreadyNotified(); 573 } 574 575 580 synchronized void resetAlreadyNotified(int index, int mask) { 581 alreadyNotifieds[index] &= ~mask; 582 if (index == 0) 583 updateDeprecatedAlreadyNotified(); 584 } 585 586 synchronized boolean alreadyNotified(int index, int mask) { 587 return ((alreadyNotifieds[index] & mask) != 0); 588 } 589 590 595 synchronized void resetAllAlreadyNotified(int index) { 596 alreadyNotifieds[index] = 0; 597 if (index == 0) 598 updateDeprecatedAlreadyNotified(); 599 } 600 601 int[] expandArray(int[] array) { 602 int[] newArray = new int[array.length + capacityIncrement]; 603 System.arraycopy(array, 0, newArray, 0, array.length); 604 return newArray; 605 } 606 607 long[] expandArray(long[] array) { 608 long[] newArray = new long[array.length + capacityIncrement]; 609 System.arraycopy(array, 0, newArray, 0, array.length); 610 return newArray; 611 } 612 613 Number [] expandArray(Number [] array) { 614 Number [] newArray = new Number [array.length + capacityIncrement]; 615 System.arraycopy(array, 0, newArray, 0, array.length); 616 return newArray; 617 } 618 619 boolean[] expandArray(boolean[] array) { 620 boolean[] newArray = new boolean[array.length + capacityIncrement]; 621 System.arraycopy(array, 0, newArray, 0, array.length); 622 return newArray; 623 } 624 625 String [] expandArray(String [] array) { 626 String [] newArray = new String [array.length + capacityIncrement]; 627 System.arraycopy(array, 0, newArray, 0, array.length); 628 return newArray; 629 } 630 631 635 synchronized void removeElementAt(int[] array, int index) { 636 if (index < 0 || index >= elementCount) 637 return; 638 int j = elementCount - index - 1; 639 if (j > 0) { 640 System.arraycopy(array, index + 1, array, index, j); 641 } 642 } 643 644 648 synchronized void removeElementAt(long[] array, int index) { 649 if (index < 0 || index >= elementCount) 650 return; 651 int j = elementCount - index - 1; 652 if (j > 0) { 653 System.arraycopy(array, index + 1, array, index, j); 654 } 655 } 656 657 661 synchronized void removeElementAt(boolean[] array, int index) { 662 if (index < 0 || index >= elementCount) 663 return; 664 int j = elementCount - index - 1; 665 if (j > 0) { 666 System.arraycopy(array, index + 1, array, index, j); 667 } 668 } 669 670 674 synchronized void removeElementAt(Object [] array, int index) { 675 if (index < 0 || index >= elementCount) 676 return; 677 int j = elementCount - index - 1; 678 if (j > 0) { 679 System.arraycopy(array, index + 1, array, index, j); 680 } 681 array[elementCount - 1] = null; 682 } 683 684 688 synchronized int indexOf(ObjectName object) { 689 return observedObjects.indexOf(object); 690 } 691 692 700 void insertSpecificElementAt(int index) {} 701 702 710 void removeSpecificElementAt(int index) {} 711 712 726 void sendNotification(String type, long timeStamp, String msg, 727 Object derGauge, Object trigger, int index) { 728 729 if (isTraceOn()) { 730 trace("sendNotification", "send notification:" + 731 "\n\tNotification observed object = " + 732 getObservedObject(index) + 733 "\n\tNotification observed attribute = " + 734 observedAttribute + 735 "\n\tNotification derived gauge = " + 736 derGauge); 737 } 738 739 long seqno; 740 synchronized (this) { 741 seqno = sequenceNumber++; 742 } 743 744 sendNotification(new MonitorNotification (type, 745 this, 746 seqno, 747 timeStamp, 748 msg, 749 getObservedObject(index), 750 observedAttribute, 751 derGauge, 752 trigger)); 753 } 754 } 755 | Popular Tags |