mx4j/src/core/javax/management/monitor CounterMonitor.java,1.5,1.6 CounterMonitorMBean.java,1.4,1.5 GaugeMonitor.java,1.9,1.10 GaugeMonitorMBean.java,1.4,1.5 Monitor.java,1.11,1.12 MonitorMBean.java,1.4,1.5 MonitorNotification.java,1.5,1.6 MonitorSettingException.java,1.2,1.3 StringMonitor.java,1.5,1.6 StringMonitorMBean.java,1.4,1.5
Simone Bordet <[email protected]> Sat, 04 Sep 2004 13:57:36 +0000
| Newsgroups | gmane.comp.java.mx4j.cvs |
|---|---|
| Message-ID | <[email protected]> |
Update of /cvsroot/mx4j/mx4j/src/core/javax/management/monitor In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8784/src/core/javax/management/monitor Modified Files: CounterMonitor.java CounterMonitorMBean.java GaugeMonitor.java GaugeMonitorMBean.java Monitor.java MonitorMBean.java MonitorNotification.java MonitorSettingException.java StringMonitor.java StringMonitorMBean.java Log Message: Rewritten the monitor package and its tests Index: CounterMonitor.java =================================================================== RCS file: /cvsroot/mx4j/mx4j/src/core/javax/management/monitor/CounterMonitor.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** CounterMonitor.java 13 Dec 2003 23:52:18 -0000 1.5 --- CounterMonitor.java 4 Sep 2004 13:57:33 -0000 1.6 *************** *** 1,4 **** /* ! * Copyright (C) MX4J. * All rights reserved. * --- 1,4 ---- /* ! * Copyright (C) The MX4J contributors. * All rights reserved. * *************** *** 9,407 **** package javax.management.monitor; - import java.util.HashMap; - import javax.management.MBeanNotificationInfo; import javax.management.ObjectName; /** ! * ! * ! * @author <a href="mailto:[email protected]">Carlos Quiroz</a> * @version $Revision$ */ ! public class CounterMonitor extends Monitor implements MonitorMBean, CounterMonitorMBean { ! /** Indicates whether the monitor notifies when exceeding the threshold */ ! private boolean notify = false; ! ! // global ! private boolean differenceMode = false; ! ! // global ! private Number modulus = new Integer(0); ! ! // global ! private Number offset = new Integer(0); ! ! // global ! private Number initThreshold = new Integer(0); ! ! // local ! private HashMap counterInfos = new HashMap(); ! ! private transient Class type = NONE; ! ! private static final Class NONE = null; ! private static final Class INT = Integer.class; ! private static final Class LONG = Long.class; ! private static final Class BYTE = Byte.class; ! private static final Class SHORT = Short.class; ! ! private static final MBeanNotificationInfo[] notificationInfos = { ! new MBeanNotificationInfo(new String[] { ! MonitorNotification.RUNTIME_ERROR, ! MonitorNotification.OBSERVED_OBJECT_ERROR, ! MonitorNotification.OBSERVED_ATTRIBUTE_ERROR, ! MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR, ! MonitorNotification.THRESHOLD_ERROR, ! MonitorNotification.THRESHOLD_VALUE_EXCEEDED ! } ! , "javax.management.monitor.MonitorNotification", "Notifications sent by the CounterMonitor MBean") ! }; ! ! public synchronized void start() { ! doStart(); ! } ! ! public synchronized void stop() { ! doStop(); ! } ! ! long time = System.currentTimeMillis(); ! void executeMonitor(ObjectName objectName,Object attributeValue){ ! CounterInfo ct = (CounterInfo)counterInfos.get(objectName); ! Number threshold = ct.getThreshold(); ! if (threshold == null || threshold.longValue() == 0) { ! if (!errorNotified) { ! getLogger().info(new StringBuffer("Monitor ").append(this).append(" threshold value is null or zero")); ! notifyListeners(MonitorNotification.THRESHOLD_ERROR, objectName, attribute); ! errorNotified = true; ! return; ! } ! } ! // need to be refined ! if (!(attributeValue instanceof Number)) { ! if (!errorNotified) { ! getLogger().info(new StringBuffer("Monitor ").append(this).append(" attribute is not a Number")); ! notifyListeners(MonitorNotification.THRESHOLD_ERROR, objectName, attribute); ! errorNotified = true; ! return; ! } ! } ! determineType(attributeValue,ct); ! if (type == NONE) { ! if (!errorNotified) { ! getLogger().info(new StringBuffer("Monitor ").append(this).append(" attribute, threshold, offset and modules types don't match")); ! notifyListeners(MonitorNotification.THRESHOLD_ERROR, objectName, attribute); ! errorNotified = true; ! return; ! } ! } ! calculateDerivedGauge((Number)attributeValue,objectName); ! if (!ct.getWasNotified()) { ! if (((Number)attributeValue).longValue() >= threshold.longValue()) { ! // roll-over ! if (modulus != null && modulus.longValue() > 0 &&((Number)attributeValue).longValue() ! >= modulus.longValue()) { ! ct.setThreshold(initThreshold); ! } ! else if (offset != null && offset.longValue() > 0) { ! while (ct.getThreshold().longValue() <= ((Number)attributeValue).longValue()) { ! ct.setThreshold(createNumber(ct.getThreshold().longValue() + offset.longValue())); ! } ! } ! if (notify) { ! getLogger().info(new StringBuffer("Monitor ").append(this).append(" counter over the threshold")); ! notifyListeners(MonitorNotification.THRESHOLD_VALUE_EXCEEDED, objectName, attribute); ! } } - ct.setWasNotified(true); - } - } ! void determineType(Object attributeValue,CounterInfo counter) { ! Class targetClass = attributeValue.getClass(); ! if (counter.getThreshold() != null) { ! if (targetClass.equals(counter.getThreshold().getClass())) { ! boolean match = true; ! if (offset != null && !targetClass.equals(offset.getClass())) { ! match = false; ! } ! if (modulus != null && !targetClass.equals(modulus.getClass())) { ! match = false; ! } ! if (targetClass != INT && targetClass != LONG && targetClass != BYTE ! && targetClass != SHORT) { ! match = false; ! } ! if (match) { ! type = targetClass; ! } ! } ! else { ! type = NONE; } - } - else { - type = NONE; - } - } ! void calculateDerivedGauge(Number attributeValue,ObjectName objectName) { ! CounterInfo ct = (CounterInfo)counterInfos.get(objectName); ! ct.setLastDerivatedGaugeTimestamp(System.currentTimeMillis()); ! if (differenceMode) { ! if (ct.getLastValue() != null) { ! long difference = attributeValue.longValue() - ct.getLastValue().longValue(); ! if (modulus != null && modulus.longValue()>0 && attributeValue.longValue()>modulus.longValue()) { ! difference = attributeValue.longValue() - modulus.longValue(); ! } ! ct.setLastDerivatedGauge(createNumber(difference)); } ! } ! if (ct.getLastValue() != null && !ct.getLastValue().equals(attributeValue)) { ! ct.setWasNotified(false); ! } ! ct.setLastValue(attributeValue); ! } ! ! Number createNumber(long value) { ! Number result = null; ! if (type == INT) { ! result = new Integer((int)value); ! } ! else if (type == LONG) { ! result = new Long(value); ! } ! else if (type == SHORT) { ! result = new Short((short)value); ! } ! else if (type == BYTE) { ! result = new Byte((byte)value); ! } ! return result; ! } ! ! /** ! * @deprecated ! */ ! ! public Number getDerivedGauge() { ! return getDerivedGauge(getObservedObject()); ! } ! ! public Number getDerivedGauge(ObjectName objectName) { ! CounterInfo ct = (CounterInfo)counterInfos.get(objectName); ! return ct != null ? ct.getLastDerivatedGauge() : null; ! } ! ! ! public long getDerivedGaugeTimeStamp() { ! return getDerivedGaugeTimeStamp(getObservedObject()); ! } ! ! public long getDerivedGaugeTimeStamp(ObjectName objectName) { ! CounterInfo ct = (CounterInfo)counterInfos.get(objectName); ! return ct != null ? ct.getLastDerivatedGaugeTimestamp() : 0; ! } ! ! public Number getThreshold() { ! return getThreshold(getObservedObject()); ! } ! ! public Number getThreshold(ObjectName objectName) { ! CounterInfo ct = (CounterInfo)counterInfos.get(objectName); ! return ct != null ? ct.getThreshold() : null; ! } ! ! public Number getInitThreshold(){ ! return initThreshold; ! } ! ! public synchronized void setInitThreshold(Number value) throws java.lang.IllegalArgumentException { ! if (value == null || value.longValue() < 0L) { ! throw new IllegalArgumentException("The threshold value has to be a valid number higher than 0"); ! } ! initThreshold = value; ! for(int i = 0; i < objectNames.size(); i++){ ! CounterInfo ct = (CounterInfo)counterInfos.get(objectNames.get(i)); ! ct.setThreshold(value); ! ct.setLastValue(null); ! ct.setWasNotified(false); ! } ! } ! ! public void setThreshold(Number value) throws java.lang.IllegalArgumentException { ! setInitThreshold(value); ! } ! ! public Number getOffset() { ! return offset; ! } ! ! public synchronized void setOffset(Number value) throws java.lang.IllegalArgumentException { ! if (value == null || value.longValue() < 0L) { ! throw new IllegalArgumentException("The threshold value has to be a valid number higher than 0"); ! } ! this.offset = value; ! for(int i = 0; i < objectNames.size(); i++){ ! CounterInfo ct = (CounterInfo)counterInfos.get(objectNames.get(i)); ! ct.setWasNotified(false); ! } ! } ! ! public Number getModulus() { ! return modulus; ! } ! ! public void setModulus(Number value) throws java.lang.IllegalArgumentException { ! if (value == null || value.longValue() < 0L) { ! throw new IllegalArgumentException("The threshold value has to be a valid number higher than 0"); ! } ! this.modulus = value; ! } ! ! public boolean getNotify() { ! return notify; ! } ! ! public void setNotify(boolean value) { ! this.notify = value; ! } ! ! public boolean getDifferenceMode() { ! return differenceMode; ! } ! ! public void setDifferenceMode(boolean value) { ! this.differenceMode = value; ! } ! ! public String toString() { ! return new StringBuffer("CounterMonitor on ").append(super.toString()).toString(); ! } ! ! public MBeanNotificationInfo[] getNotificationInfo() { ! return notificationInfos; ! } ! ! public synchronized void addObservedObject(ObjectName objectName) throws java.lang.IllegalArgumentException { ! super.addObservedObject(objectName); ! counterInfos.put(objectName,new CounterInfo()); ! } ! public void removeObservedObject(ObjectName objectName){ ! super.removeObservedObject(objectName); ! counterInfos.remove(objectName); ! } ! class CounterInfo{ ! // local ! private Number lastDerivatedGauge = new Integer(0); ! // local ! private long lastDerivatedGaugeTimestamp = 0; ! // local? ! private transient Number lastValue = null, threshold = null; ! // local? ! private transient boolean wasNotified = false, errorNotified = false; ! /** Getter for property errorNotified. ! * @return Value of property errorNotified. ! * ! */ ! public boolean getErrorNotified() { ! return errorNotified; ! } ! /** Setter for property errorNotified. ! * @param errorNotified New value of property errorNotified. ! * ! */ ! public void setErrorNotified(boolean errorNotified) { ! this.errorNotified = errorNotified; ! } ! /** Getter for property lastDerivatedGauge. ! * @return Value of property lastDerivatedGauge. ! * ! */ ! public java.lang.Number getLastDerivatedGauge() { ! return lastDerivatedGauge; ! } ! /** Setter for property lastDerivatedGauge. ! * @param lastDerivatedGauge New value of property lastDerivatedGauge. ! * ! */ ! public void setLastDerivatedGauge(java.lang.Number lastDerivatedGauge) { ! this.lastDerivatedGauge = lastDerivatedGauge; ! } ! /** Getter for property lastDerivatedGaugeTimestamp. ! * @return Value of property lastDerivatedGaugeTimestamp. ! * ! */ ! public long getLastDerivatedGaugeTimestamp() { ! return lastDerivatedGaugeTimestamp; ! } ! /** Setter for property lastDerivatedGaugeTimestamp. ! * @param lastDerivatedGaugeTimestamp New value of property lastDerivatedGaugeTimestamp. ! * ! */ ! public void setLastDerivatedGaugeTimestamp(long lastDerivatedGaugeTimestamp) { ! this.lastDerivatedGaugeTimestamp = lastDerivatedGaugeTimestamp; ! } ! /** Getter for property lastValue. ! * @return Value of property lastValue. ! * ! */ ! public java.lang.Number getLastValue() { ! return lastValue; ! } ! /** Setter for property lastValue. ! * @param lastValue New value of property lastValue. ! * ! */ ! public void setLastValue(java.lang.Number lastValue) { ! this.lastValue = lastValue; ! } ! /** Getter for property threshold. ! * @return Value of property threshold. ! * ! */ ! public java.lang.Number getThreshold() { ! return threshold; ! } ! /** Setter for property threshold. ! * @param threshold New value of property threshold. ! * ! */ ! public void setThreshold(java.lang.Number threshold) { ! this.threshold = threshold; ! } ! /** Getter for property wasNotified. ! * @return Value of property wasNotified. ! * ! */ ! public boolean getWasNotified() { ! return wasNotified; ! } ! /** Setter for property wasNotified. ! * @param wasNotified New value of property wasNotified. ! * ! */ ! public void setWasNotified(boolean wasNotified) { ! this.wasNotified = wasNotified; ! } ! } } --- 9,185 ---- package javax.management.monitor; import javax.management.MBeanNotificationInfo; + import javax.management.NotCompliantMBeanException; + import javax.management.Notification; + import javax.management.NotificationBroadcasterSupport; import javax.management.ObjectName; + + import mx4j.monitor.MX4JCounterMonitor; + import mx4j.monitor.MX4JMonitor; + /** ! * @author <a href="mailto:[email protected]">Simone Bordet</a> * @version $Revision$ */ ! public class CounterMonitor extends Monitor implements CounterMonitorMBean ! { ! private static final MBeanNotificationInfo[] notificationInfos = ! { ! new MBeanNotificationInfo(new String[] ! { ! MonitorNotification.RUNTIME_ERROR, ! MonitorNotification.OBSERVED_OBJECT_ERROR, ! MonitorNotification.OBSERVED_ATTRIBUTE_ERROR, ! MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR, ! MonitorNotification.THRESHOLD_ERROR, ! MonitorNotification.THRESHOLD_VALUE_EXCEEDED ! }, ! MonitorNotification.class.getName(), ! "Notifications sent by the CounterMonitor MBean") ! }; ! MX4JMonitor createMX4JMonitor() ! { ! try ! { ! return new MX4JCounterMonitor() ! { ! protected NotificationBroadcasterSupport createNotificationEmitter() ! { ! return CounterMonitor.this; } ! public MBeanNotificationInfo[] getNotificationInfo() ! { ! return notificationInfos; } ! protected Notification createMonitorNotification(String type, long sequence, String message, ObjectName observed, String attribute, Object gauge, Object trigger) ! { ! return new MonitorNotification(type, this, sequence, System.currentTimeMillis(), message, observed, attribute, gauge, trigger); } ! }; ! } ! catch (NotCompliantMBeanException x) ! { ! return null; ! } ! } ! /** ! * @deprecated ! */ ! public Number getDerivedGauge() ! { ! return getDerivedGauge(getObservedObject()); ! } ! /** ! * @deprecated ! */ ! public long getDerivedGaugeTimeStamp() ! { ! return getDerivedGaugeTimeStamp(getObservedObject()); ! } ! /** ! * @deprecated ! */ ! public Number getThreshold() ! { ! MX4JCounterMonitor monitor = (MX4JCounterMonitor)getMX4JMonitor(); ! return monitor.getThreshold(getObservedObject()); ! } ! /** ! * @deprecated ! */ ! public void setThreshold(Number value) throws java.lang.IllegalArgumentException ! { ! setInitThreshold(value); ! } ! public Number getDerivedGauge(ObjectName objectName) ! { ! MX4JCounterMonitor monitor = (MX4JCounterMonitor)getMX4JMonitor(); ! return monitor.getDerivedGauge(objectName); ! } ! public long getDerivedGaugeTimeStamp(ObjectName objectName) ! { ! MX4JCounterMonitor monitor = (MX4JCounterMonitor)getMX4JMonitor(); ! return monitor.getDerivedGaugeTimeStamp(objectName); ! } ! public Number getThreshold(ObjectName objectName) ! { ! MX4JCounterMonitor monitor = (MX4JCounterMonitor)getMX4JMonitor(); ! return monitor.getThreshold(objectName); ! } ! public Number getInitThreshold() ! { ! MX4JCounterMonitor monitor = (MX4JCounterMonitor)getMX4JMonitor(); ! return monitor.getInitThreshold(); ! } ! public void setInitThreshold(Number value) throws java.lang.IllegalArgumentException ! { ! MX4JCounterMonitor monitor = (MX4JCounterMonitor)getMX4JMonitor(); ! monitor.setInitThreshold(value); ! } ! public Number getOffset() ! { ! MX4JCounterMonitor monitor = (MX4JCounterMonitor)getMX4JMonitor(); ! return monitor.getOffset(); ! } ! public synchronized void setOffset(Number value) throws java.lang.IllegalArgumentException ! { ! MX4JCounterMonitor monitor = (MX4JCounterMonitor)getMX4JMonitor(); ! monitor.setOffset(value); ! } ! public Number getModulus() ! { ! MX4JCounterMonitor monitor = (MX4JCounterMonitor)getMX4JMonitor(); ! return monitor.getModulus(); ! } ! public void setModulus(Number value) throws java.lang.IllegalArgumentException ! { ! MX4JCounterMonitor monitor = (MX4JCounterMonitor)getMX4JMonitor(); ! monitor.setModulus(value); ! } ! public boolean getNotify() ! { ! MX4JCounterMonitor monitor = (MX4JCounterMonitor)getMX4JMonitor(); ! return monitor.getNotify(); ! } ! public void setNotify(boolean value) ! { ! MX4JCounterMonitor monitor = (MX4JCounterMonitor)getMX4JMonitor(); ! monitor.setNotify(value); ! } ! public boolean getDifferenceMode() ! { ! MX4JCounterMonitor monitor = (MX4JCounterMonitor)getMX4JMonitor(); ! return monitor.getDifferenceMode(); ! } ! public void setDifferenceMode(boolean value) ! { ! MX4JCounterMonitor monitor = (MX4JCounterMonitor)getMX4JMonitor(); ! monitor.setDifferenceMode(value); ! } ! public MBeanNotificationInfo[] getNotificationInfo() ! { ! MX4JCounterMonitor monitor = (MX4JCounterMonitor)getMX4JMonitor(); ! return monitor.getNotificationInfo(); ! } } Index: MonitorNotification.java =================================================================== RCS file: /cvsroot/mx4j/mx4j/src/core/javax/management/monitor/MonitorNotification.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** MonitorNotification.java 28 Aug 2004 16:36:46 -0000 1.5 --- MonitorNotification.java 4 Sep 2004 13:57:33 -0000 1.6 *************** *** 1,4 **** /* ! * Copyright (C) MX4J. * All rights reserved. * --- 1,4 ---- /* ! * Copyright (C) The MX4J contributors. * All rights reserved. * Index: Monitor.java =================================================================== RCS file: /cvsroot/mx4j/mx4j/src/core/javax/management/monitor/Monitor.java,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** Monitor.java 13 Dec 2003 23:52:18 -0000 1.11 --- Monitor.java 4 Sep 2004 13:57:33 -0000 1.12 *************** *** 1,4 **** ! /* ! * Copyright (C) MX4J. * All rights reserved. * --- 1,4 ---- ! /* ! * Copyright (C) The MX4J contributors. * All rights reserved. * *************** *** 9,306 **** package javax.management.monitor; - import javax.management.ObjectName; - import javax.management.NotificationBroadcasterSupport; import javax.management.MBeanRegistration; import javax.management.MBeanServer; ! import javax.management.MBeanInfo; ! import javax.management.MBeanAttributeInfo; ! import java.util.Timer; ! import java.util.TimerTask; ! import java.util.ArrayList; ! import mx4j.log.Logger; ! import mx4j.log.Log; /** ! * Class monitor. Parent class of all Monitoring classes ! * ! * In JMX 1.2 monitors can observe multiple objects. This makes the ! * monitor execution a bit more complex, and synchronization is of ! * particular concern. I haven't paid much attention to synchronization so far, ! * but plan to address it in the test cases applied to this class. I believe ! * the only piece that needs synchronization is the objectNames list of monitored ! * objects -markmcbride ! * @see MonitorMBean ! * @author <a href="mailto:[email protected]">Carlos Quiroz</a> ! * @author <a href="mailto:[email protected]">Mark McBride</a> * @version $Revision$ */ ! public abstract class Monitor extends NotificationBroadcasterSupport ! implements MonitorMBean, MBeanRegistration { ! /** @deprecated */ ! protected int alreadyNotified; ! protected int alreadyNotifieds[]; ! protected static final int capacityIncrement = 16; ! /** @deprecated */ ! protected String dbgTag; ! protected int elementCount; ! protected static final int OBSERVED_ATTRIBUTE_ERROR_NOTIFIED = 2; ! protected static final int OBSERVED_ATTRIBUTE_TYPE_ERROR_NOTIFIED = 4; ! protected static final int OBSERVED_OBJECT_ERROR_NOTIFIED = 1; ! protected static final int RESET_FLAGS_ALREADY_NOTIFIED = 0; ! protected static final int RUNTIME_ERROR_NOTIFIED = 8; ! protected MBeanServer server; ! ! /** Object names of the monitored MBeans */ ! ArrayList objectNames = new ArrayList(); ! ! /** Monitored attribute */ ! String attribute; ! ! /** Granularity of the monitoring in milliseconds */ ! long granularity = 10000; ! ! /** Indicates whether the Monitor is active */ ! boolean isActive = false; ! ! transient boolean errorNotified = false, mBeanFound = true; ! ! static Timer notificationTimer = new Timer(); ! ! static long notificationID = 0; ! ! MonitorTask monitorTask = null; ! ! static synchronized long createNotificationID() { ! return ++notificationID; ! } ! ! abstract void executeMonitor(ObjectName objectName,Object attribute); ! ! Logger getLogger() { ! return Log.getLogger(getClass().getName()); ! } ! ! private class MonitorTask extends TimerTask { ! public void run() { ! if (isActive()) { ! // if no objects are set to be monitored then send a jmx.monitor.error.mbean ! // notification to the listeners ! if(objectNames.size() == 0){ ! getLogger().warn(new StringBuffer("Monitor ").append(Monitor.this.toString()).append(" no objects specified for monitoring")); ! if(!errorNotified){ ! notifyListeners(MonitorNotification.OBSERVED_OBJECT_ERROR,null); ! } ! errorNotified = true; ! } ! // loop over all monitored objects in JMX 1.2 ! // note that on an error we continue to the next object ! // rather than just returning as we did in JMX 1.1 ! for(int i = 0; i < objectNames.size(); i++){ ! ObjectName objectName = (ObjectName)objectNames.get(i); ! try { ! if (objectName == null) { ! getLogger().warn(new StringBuffer("Monitor ").append(Monitor.this.toString()).append(" object name ").append(objectName).append(" not found").toString()); ! if (!errorNotified) { ! notifyListeners(MonitorNotification.OBSERVED_OBJECT_ERROR, objectName); ! } ! errorNotified = true; ! continue; ! } ! getLogger().info("Execute monitor " + Monitor.this.toString()); ! if (!server.isRegistered(objectName)) { ! getLogger().warn(new StringBuffer("Monitor ").append(Monitor.this.toString()).append(" object name ").append(objectName).append(" not found").toString()); ! if (mBeanFound) { ! mBeanFound = false; ! notifyListeners(MonitorNotification.OBSERVED_OBJECT_ERROR, objectName); ! } ! continue; ! } ! mBeanFound = true; ! if (errorNotified) { ! continue; ! } ! MBeanInfo info = server.getMBeanInfo(objectName); ! MBeanAttributeInfo[] attributes =info.getAttributes(); ! boolean found = false; ! if (attributes != null) { ! for (int j=0;j<attributes.length;j++) { ! if (attributes[j].getName().equals(attribute)) { ! found = true; ! } ! } ! } ! if (!found) { ! getLogger().warn(new StringBuffer("Monitor ").append(Monitor.this.toString()).append(" attribute ").append(attribute).append(" not found").toString()); ! errorNotified = true; ! notifyListeners(MonitorNotification.OBSERVED_ATTRIBUTE_ERROR, objectName, attribute); ! continue; ! } ! Object attributeValue = server.getAttribute(objectName, attribute); ! if (attributeValue == null) { ! getLogger().warn(new StringBuffer("Monitor ").append(Monitor.this.toString()).append(" attribute ").append(attribute).append(" is null").toString()); ! errorNotified = true; ! notifyListeners(MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR, objectName, attribute); ! continue; ! } ! executeMonitor(objectName,attributeValue); ! } catch (Exception e) { ! getLogger().warn(new StringBuffer("Monitor ").append(Monitor.this.toString()).append(" object name ").append(objectName).append(" not found").toString()); ! errorNotified = true; ! notifyListeners(MonitorNotification.RUNTIME_ERROR, objectName, attribute, e); ! } ! } ! } ! } ! } ! ! public abstract void start(); ! ! synchronized void doStart() { ! if (!isActive) { ! getLogger().info(new StringBuffer("Starting monitor ").append(this).append(" every ").append(granularity).append(" milliseconds").toString()); ! this.isActive = true; ! errorNotified = false; ! // updated to create a new MonitorTask every time ! monitorTask = new MonitorTask(); ! notificationTimer.scheduleAtFixedRate(monitorTask, 0, granularity); ! } ! } ! ! public abstract void stop(); ! ! synchronized void doStop() { ! if (isActive) { ! getLogger().info("Stopping monitor " + toString()); ! this.isActive = false; ! monitorTask.cancel(); ! } ! } ! ! /** ! * @deprecated ! */ ! public synchronized ObjectName getObservedObject() { ! if(objectNames.size() == 0) return null; ! return (ObjectName)objectNames.get(0); ! } ! public synchronized ObjectName[] getObservedObjects() { ! if(objectNames.size() == 0) return null; ! ObjectName [] names = new ObjectName[objectNames.size()]; ! for(int i = 0; i < names.length; i++){ ! names[i] = (ObjectName)objectNames.get(i); ! } ! return names; ! } ! public synchronized void addObservedObject(ObjectName objectName) throws java.lang.IllegalArgumentException { ! if(objectName == null){ ! throw new IllegalArgumentException("The observed object name cannot be null"); ! } ! if(objectNames.contains(objectName)){ ! // changed behaviour as JMX 1.2 No exception is thrown ! //throw new IllegalArgumentException(objectName.getCanonicalName() + " is already being monitored"); ! return; ! } ! errorNotified = false; ! objectNames.add(objectNames.size(),objectName); ! //objectNames.add(objectName); ! } ! /** ! * @deprecated ! */ ! public synchronized void setObservedObject(ObjectName objectName) throws java.lang.IllegalArgumentException { ! addObservedObject(objectName); ! } ! public String getObservedAttribute() { ! return attribute; ! } ! public synchronized void setObservedAttribute(String attribute) throws java.lang.IllegalArgumentException { ! if (attribute == null) { ! throw new IllegalArgumentException("The observed attribute cannot be null"); ! } ! errorNotified = true; ! this.attribute = attribute; ! } ! public long getGranularityPeriod() { ! return granularity; ! } ! public void setGranularityPeriod(long period) throws java.lang.IllegalArgumentException { ! if (period <=0) { ! throw new IllegalArgumentException("The monitoring period can't be negative or zero"); ! } ! this.granularity = period; ! } ! public synchronized boolean isActive() { ! return isActive; ! } ! public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception { ! getLogger().info("Pre register of monitor " + toString()); ! this.server = server; ! errorNotified = false; ! return name; ! } ! public void postRegister(Boolean registrationDone) {} ! public void preDeregister() throws Exception { ! getLogger().info("Pre deregister of monitor " + toString()); ! this.stop(); ! } ! public void postDeregister() {} ! void notifyListeners(String type, ObjectName objectName) { ! MonitorNotification not = new MonitorNotification(type, this, createNotificationID(), System.currentTimeMillis(), "", objectName, null, null, null); ! sendNotification(not); ! } ! void notifyListeners(String type, ObjectName objectName, String attribute) { ! MonitorNotification not = new MonitorNotification(type, this, createNotificationID() ! , System.currentTimeMillis(), "", objectName, attribute, null, null); ! sendNotification(not); ! } ! void notifyListeners(String type, ObjectName objectName, String attribute, Throwable e) { ! MonitorNotification not = new MonitorNotification(type, this, createNotificationID() ! , System.currentTimeMillis(), e.toString(), objectName, attribute, null, null); ! sendNotification(not); ! } ! public synchronized String toString() { ! StringBuffer sb = new StringBuffer(); ! if(objectNames == null || objectNames.size() == 0){ ! sb.append("?"); ! }else{ ! for(int i = 0; i < objectNames.size(); i++){ ! sb.append(objectNames.get(i).toString()).append(","); ! } ! sb.setLength(sb.length() - 1); ! } ! sb.append(" attribute ").append(attribute); ! return sb.toString(); ! } ! public synchronized boolean containsObservedObject(ObjectName objectName){ ! return objectNames.contains(objectName); ! } ! public synchronized void removeObservedObject(ObjectName objectName){ ! // The behaviour changed in JMX 1.2. No exception is thrown if the object is not found ! if(objectNames.contains(objectName)){ ! objectNames.remove(objectName); ! } ! } } --- 9,173 ---- package javax.management.monitor; import javax.management.MBeanRegistration; import javax.management.MBeanServer; ! import javax.management.NotificationBroadcasterSupport; ! import javax.management.ObjectName; ! import mx4j.monitor.MX4JMonitor; /** ! * @author <a href="biorn_steedom:[email protected]">Simone Bordet</a> * @version $Revision$ */ ! public abstract class Monitor extends NotificationBroadcasterSupport implements MonitorMBean, MBeanRegistration ! { ! /** ! * @deprecated ! */ ! protected int alreadyNotified; ! protected int alreadyNotifieds[]; ! protected static final int capacityIncrement = 16; ! /** ! * @deprecated ! */ ! protected String dbgTag; ! protected int elementCount; ! protected static final int OBSERVED_ATTRIBUTE_ERROR_NOTIFIED = 2; ! protected static final int OBSERVED_ATTRIBUTE_TYPE_ERROR_NOTIFIED = 4; ! protected static final int OBSERVED_OBJECT_ERROR_NOTIFIED = 1; ! protected static final int RESET_FLAGS_ALREADY_NOTIFIED = 0; ! protected static final int RUNTIME_ERROR_NOTIFIED = 8; ! // Fields above are a mistake in the spec: JMX 1.0 was poorly written and these fields ! // made their way into the specification. MX4J's implementation is different from RI's ! // and we don't value the fields above. ! protected MBeanServer server; + private MX4JMonitor monitor; + abstract MX4JMonitor createMX4JMonitor(); ! synchronized MX4JMonitor getMX4JMonitor() ! { ! if (monitor == null) ! { ! monitor = createMX4JMonitor(); ! } ! return monitor; ! } ! /** ! * @deprecated ! */ ! public ObjectName getObservedObject() ! { ! ObjectName[] observed = getObservedObjects(); ! if (observed == null || observed.length < 1) return null; ! return observed[0]; ! } ! /** ! * @deprecated ! */ ! public void setObservedObject(ObjectName objectName) throws java.lang.IllegalArgumentException ! { ! MX4JMonitor monitor = getMX4JMonitor(); ! synchronized (monitor) ! { ! monitor.clearObservedObjects(); ! monitor.addObservedObject(objectName); ! } ! } ! public String getObservedAttribute() ! { ! MX4JMonitor monitor = getMX4JMonitor(); ! return monitor.getObservedAttribute(); ! } ! public void setObservedAttribute(String attribute) throws java.lang.IllegalArgumentException ! { ! MX4JMonitor monitor = getMX4JMonitor(); ! monitor.setObservedAttribute(attribute); ! } ! public long getGranularityPeriod() ! { ! MX4JMonitor monitor = getMX4JMonitor(); ! return monitor.getGranularityPeriod(); ! } ! public void setGranularityPeriod(long period) throws java.lang.IllegalArgumentException ! { ! MX4JMonitor monitor = getMX4JMonitor(); ! monitor.setGranularityPeriod(period); ! } ! public void start() ! { ! MX4JMonitor monitor = getMX4JMonitor(); ! monitor.start(); ! } ! public void stop() ! { ! MX4JMonitor monitor = getMX4JMonitor(); ! monitor.stop(); ! } ! public boolean isActive() ! { ! MX4JMonitor monitor = getMX4JMonitor(); ! return monitor.isActive(); ! } ! public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception ! { ! this.server = server; ! MX4JMonitor monitor = getMX4JMonitor(); ! return monitor.preRegister(server, name); ! } ! public void postRegister(Boolean registrationDone) ! { ! MX4JMonitor monitor = getMX4JMonitor(); ! monitor.postRegister(registrationDone); ! } ! public void preDeregister() throws Exception ! { ! MX4JMonitor monitor = getMX4JMonitor(); ! monitor.preDeregister(); ! } ! public void postDeregister() ! { ! MX4JMonitor monitor = getMX4JMonitor(); ! monitor.postDeregister(); ! } ! public void addObservedObject(ObjectName objectName) throws IllegalArgumentException ! { ! MX4JMonitor monitor = getMX4JMonitor(); ! monitor.addObservedObject(objectName); ! } ! public ObjectName[] getObservedObjects() ! { ! MX4JMonitor monitor = getMX4JMonitor(); ! return monitor.getObservedObjects(); ! } ! public boolean containsObservedObject(ObjectName objectName) ! { ! MX4JMonitor monitor = getMX4JMonitor(); ! return monitor.containsObservedObject(objectName); ! } ! public void removeObservedObject(ObjectName objectName) ! { ! MX4JMonitor monitor = getMX4JMonitor(); ! monitor.removeObservedObject(objectName); ! } } Index: CounterMonitorMBean.java =================================================================== RCS file: /cvsroot/mx4j/mx4j/src/core/javax/management/monitor/CounterMonitorMBean.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** CounterMonitorMBean.java 28 Aug 2004 16:40:25 -0000 1.4 --- CounterMonitorMBean.java 4 Sep 2004 13:57:33 -0000 1.5 *************** *** 1,4 **** /* ! * Copyright (C) MX4J. * All rights reserved. * --- 1,4 ---- /* ! * Copyright (C) The MX4J contributors. * All rights reserved. * Index: MonitorSettingException.java =================================================================== RCS file: /cvsroot/mx4j/mx4j/src/core/javax/management/monitor/MonitorSettingException.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** MonitorSettingException.java 28 Aug 2004 16:38:41 -0000 1.2 --- MonitorSettingException.java 4 Sep 2004 13:57:33 -0000 1.3 *************** *** 1,4 **** /* ! * Copyright (C) MX4J. * All rights reserved. * --- 1,4 ---- /* ! * Copyright (C) The MX4J contributors. * All rights reserved. * Index: StringMonitor.java =================================================================== RCS file: /cvsroot/mx4j/mx4j/src/core/javax/management/monitor/StringMonitor.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** StringMonitor.java 13 Dec 2003 23:52:18 -0000 1.5 --- StringMonitor.java 4 Sep 2004 13:57:33 -0000 1.6 *************** *** 1,4 **** /* ! * Copyright (C) MX4J. * All rights reserved. * --- 1,4 ---- /* ! * Copyright (C) The MX4J Contributors. * All rights reserved. * *************** *** 9,247 **** package javax.management.monitor; - import java.util.HashMap; import javax.management.MBeanNotificationInfo; import javax.management.ObjectName; /** ! * ! * ! * @see Monitor ! * @author <a href="mailto:[email protected]">Carlos Quiroz</a> * @version $Revision$ */ public class StringMonitor extends Monitor implements StringMonitorMBean { ! private String stringToCompare; ! ! private boolean notifyMatch, notifyDiffers; ! ! private static final MBeanNotificationInfo[] notificationInfos = ! { ! new MBeanNotificationInfo(new String[] ! { ! MonitorNotification.RUNTIME_ERROR, ! MonitorNotification.OBSERVED_OBJECT_ERROR, ! MonitorNotification.OBSERVED_ATTRIBUTE_ERROR, ! MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR, ! MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED, ! MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED ! } ! , "javax.management.monitor.MonitorNotification", "Notifications sent by the StringMonitor MBean") ! }; ! ! private HashMap infos = new HashMap(); ! ! public synchronized void start() ! { ! doStart(); ! } ! ! public synchronized void stop() { ! doStop(); ! } ! ! synchronized void executeMonitor(ObjectName objectName, Object attributeValue) ! { ! StringMonitorInfo smi = (StringMonitorInfo)infos.get(objectName); ! if (smi.getLastValue() != null && smi.getLastValue().equals(attributeValue)) ! { ! return; ! } ! if (stringToCompare == null) ! { ! getLogger().info(new StringBuffer("Monitor ").append(this).append(" target value is null")); ! notifyListeners("jmx.notification.error.type", objectName, attribute); ! } ! if (!(attributeValue instanceof String)) ! { ! getLogger().info(new StringBuffer("Monitor ").append(this).append(" attribute is not String")); ! notifyListeners("jmx.notification.error.type", objectName, attribute); ! return; ! } ! calculateDerivedGauge(smi,(String)attributeValue); ! boolean matches = attributeValue.equals(stringToCompare); ! smi.setLastValue((String) attributeValue); ! if (notifyMatch && matches) ! { ! getLogger().info(new StringBuffer("Monitor ").append(this).append(" found a match")); ! notifyListeners(MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED, objectName, attribute); ! } ! if (notifyDiffers && !matches) ! { ! getLogger().info(new StringBuffer("Monitor ").append(this).append(" found a difference")); ! notifyListeners(MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED, objectName, attribute); ! } ! } ! ! void calculateDerivedGauge(StringMonitorInfo smi, String value) ! { ! smi.setDerivedGauge(value); ! smi.setDerivedGaugeTimestamp(System.currentTimeMillis()); ! } ! ! /** ! * @deprecated ! */ ! public synchronized String getDerivedGauge() ! { ! if(objectNames.size() == 0) return null; ! return getDerivedGauge((ObjectName)objectNames.get(0)); ! } ! public synchronized String getDerivedGauge(ObjectName objectName) { ! if(objectNames.size() == 0) return null; ! return ((StringMonitorInfo)infos.get(objectName)).getDerivedGauge(); ! } ! ! ! /** ! * @deprecated ! */ ! public synchronized long getDerivedGaugeTimeStamp() ! { ! if(objectNames.size() == 0) return 0; ! return getDerivedGaugeTimeStamp((ObjectName)objectNames.get(0)); ! } ! ! public synchronized long getDerivedGaugeTimeStamp(ObjectName objectName) { ! if(objectNames.size() == 0) return 0; ! return ((StringMonitorInfo)infos.get(objectName)).getDerivedGaugeTimestamp(); ! } ! ! public String getStringToCompare() ! { ! return stringToCompare; ! } ! ! public void setStringToCompare(String value) throws java.lang.IllegalArgumentException ! { ! if (value == null) ! { ! throw new IllegalArgumentException("Cannot compare to null"); ! } ! synchronized(this){ ! resetLastValue(); ! } ! this.stringToCompare = value; ! } ! ! public boolean getNotifyMatch() ! { ! return notifyMatch; ! } ! ! public synchronized void setNotifyMatch(boolean value) ! { ! resetLastValue(); ! this.notifyMatch = value; ! } ! ! public boolean getNotifyDiffer() ! { ! return notifyDiffers; ! } ! ! public synchronized void setNotifyDiffer(boolean value) ! { ! resetLastValue(); ! this.notifyDiffers = value; ! } ! ! public MBeanNotificationInfo[] getNotificationInfo() ! { ! return notificationInfos; ! } ! ! public String toString() ! { ! return new StringBuffer("StringMonitor on ").append(super.toString()).toString(); ! } ! public synchronized void addObservedObject(ObjectName objectName) throws java.lang.IllegalArgumentException { ! super.addObservedObject(objectName); ! infos.put(objectName,new StringMonitorInfo(null, System.currentTimeMillis())); ! } ! public synchronized void removeObservedObject(ObjectName objectName){ ! super.removeObservedObject(objectName); ! infos.remove(objectName); ! } ! private void resetLastValue(){ ! for(int i = 0; i < objectNames.size(); i++){ ! StringMonitorInfo smi = (StringMonitorInfo)infos.get(objectNames.get(i)); ! smi.setLastValue(null); } ! } ! class StringMonitorInfo{ ! String derivedGauge; ! String lastValue; ! long derivedGaugeTimestamp; ! public StringMonitorInfo(String derivedGauge, long derivedGaugeTimestamp){ ! this.derivedGauge = derivedGauge; ! this.derivedGaugeTimestamp = derivedGaugeTimestamp; ! this.lastValue = null; ! } ! /** Getter for property derivedGauge. ! * @return Value of property derivedGauge. ! * ! */ ! public java.lang.String getDerivedGauge() { ! return derivedGauge; ! } ! /** Setter for property derivedGauge. ! * @param derivedGauge New value of property derivedGauge. ! * ! */ ! public void setDerivedGauge(java.lang.String derivedGauge) { ! this.derivedGauge = derivedGauge; ! } ! /** Getter for property derivedGaugeTimestamp. ! * @return Value of property derivedGaugeTimestamp. ! * ! */ ! public long getDerivedGaugeTimestamp() { ! return derivedGaugeTimestamp; ! } ! /** Setter for property derivedGaugeTimestamp. ! * @param derivedGaugeTimestamp New value of property derivedGaugeTimestamp. ! * ! */ ! public void setDerivedGaugeTimestamp(long derivedGaugeTimestamp) { ! this.derivedGaugeTimestamp = derivedGaugeTimestamp; ! } ! /** Getter for property lastValue. ! * @return Value of property lastValue. ! * ! */ ! public java.lang.String getLastValue() { ! return lastValue; ! } ! /** Setter for property lastValue. ! * @param lastValue New value of property lastValue. ! * ! */ ! public void setLastValue(java.lang.String lastValue) { ! this.lastValue = lastValue; ! } ! } } --- 9,138 ---- package javax.management.monitor; import javax.management.MBeanNotificationInfo; + import javax.management.NotCompliantMBeanException; + import javax.management.Notification; + import javax.management.NotificationBroadcasterSupport; import javax.management.ObjectName; + import mx4j.monitor.MX4JMonitor; + import mx4j.monitor.MX4JStringMonitor; + /** ! * @author <a href="mailto:[email protected]">Simone Bordet</a> * @version $Revision$ */ public class StringMonitor extends Monitor implements StringMonitorMBean { ! private static final MBeanNotificationInfo[] notificationInfos = ! { ! new MBeanNotificationInfo(new String[] ! { ! javax.management.monitor.MonitorNotification.RUNTIME_ERROR, ! MonitorNotification.OBSERVED_OBJECT_ERROR, ! MonitorNotification.OBSERVED_ATTRIBUTE_ERROR, ! MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR, ! MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED, ! MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED ! } ! , MonitorNotification.class.getName(), ! "Notifications sent by the StringMonitor MBean") ! }; ! MX4JMonitor createMX4JMonitor() ! { ! try ! { ! return new MX4JStringMonitor() ! { ! protected NotificationBroadcasterSupport createNotificationEmitter() ! { ! return StringMonitor.this; ! } ! public MBeanNotificationInfo[] getNotificationInfo() ! { ! return notificationInfos; ! } ! protected Notification createMonitorNotification(String type, long sequence, String message, ObjectName observed, String attribute, Object gauge, Object trigger) ! { ! return new MonitorNotification(type, this, sequence, System.currentTimeMillis(), message, observed, attribute, gauge, trigger); } ! }; ! } ! catch (NotCompliantMBeanException x) ! { ! return null; ! } ! } ! /** ! * @deprecated ! */ ! public String getDerivedGauge() ! { ! return getDerivedGauge(getObservedObject()); ! } ! /** ! * @deprecated ! */ ! public long getDerivedGaugeTimeStamp() ! { ! return getDerivedGaugeTimeStamp(getObservedObject()); ! } ! public String getDerivedGauge(ObjectName objectName) ! { ! MX4JStringMonitor monitor = (MX4JStringMonitor)getMX4JMonitor(); ! return monitor.getDerivedGauge(objectName); ! } ! public long getDerivedGaugeTimeStamp(ObjectName objectName) ! { ! MX4JStringMonitor monitor = (MX4JStringMonitor)getMX4JMonitor(); ! return monitor.getDerivedGaugeTimeStamp(objectName); ! } ! public String getStringToCompare() ! { ! MX4JStringMonitor monitor = (MX4JStringMonitor)getMX4JMonitor(); ! return monitor.getStringToCompare(); ! } ! public void setStringToCompare(String value) throws IllegalArgumentException ! { ! MX4JStringMonitor monitor = (MX4JStringMonitor)getMX4JMonitor(); ! monitor.setStringToCompare(value); ! } ! public boolean getNotifyMatch() ! { ! MX4JStringMonitor monitor = (MX4JStringMonitor)getMX4JMonitor(); ! return monitor.getNotifyMatch(); ! } ! public void setNotifyMatch(boolean value) ! { ! MX4JStringMonitor monitor = (MX4JStringMonitor)getMX4JMonitor(); ! monitor.setNotifyMatch(value); ! } ! public boolean getNotifyDiffer() ! { ! MX4JStringMonitor monitor = (MX4JStringMonitor)getMX4JMonitor(); ! return monitor.getNotifyDiffer(); ! } ! ! public void setNotifyDiffer(boolean value) ! { ! MX4JStringMonitor monitor = (MX4JStringMonitor)getMX4JMonitor(); ! monitor.setNotifyDiffer(value); ! } + public MBeanNotificationInfo[] getNotificationInfo() + { + MX4JStringMonitor monitor = (MX4JStringMonitor)getMX4JMonitor(); + return monitor.getNotificationInfo(); + } } Index: GaugeMonitor.java =================================================================== RCS file: /cvsroot/mx4j/mx4j/src/core/javax/management/monitor/GaugeMonitor.java,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** GaugeMonitor.java 13 Dec 2003 23:52:18 -0000 1.9 --- GaugeMonitor.java 4 Sep 2004 13:57:33 -0000 1.10 *************** *** 1,4 **** /* ! * Copyright (C) MX4J. * All rights reserved. * --- 1,4 ---- /* ! * Copyright (C) The MX4J contributors. * All rights reserved. * *************** *** 10,499 **** import javax.management.MBeanNotificationInfo; ! ! import java.util.HashMap; ! import javax.management.ObjectName; /** - * - * * @author <a href="mailto:[email protected]">Carlos Quiroz</a> * @version $Revision$ */ ! public class GaugeMonitor extends Monitor implements MonitorMBean, GaugeMonitorMBean { ! private static final Integer NULLINTEGER = new Integer(0); ! private static final Class NONE = null; ! private static final Class INT = Integer.class; ! private static final Class LONG = Long.class; ! private static final Class BYTE = Byte.class; ! private static final Class SHORT = Short.class; ! private static final Class FLOAT = Float.class; ! private static final Class DOUBLE = Double.class; ! ! private Number highThreshold = NULLINTEGER; ! private Number lowThreshold = NULLINTEGER; ! private boolean notifyHigh = false, notifyLow = false; ! private boolean differenceMode = false; ! private transient boolean errorNotified = false; ! ! private transient Class type = NONE; ! private transient boolean isLong = false; ! ! // hold info on last values/timestamps for all observed objects ! private HashMap infos = new HashMap(); ! ! private static final MBeanNotificationInfo[] notificationInfos = { ! new MBeanNotificationInfo(new String[]{ ! MonitorNotification.RUNTIME_ERROR, ! MonitorNotification.OBSERVED_OBJECT_ERROR, ! MonitorNotification.OBSERVED_ATTRIBUTE_ERROR, ! MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR, ! MonitorNotification.THRESHOLD_ERROR, ! MonitorNotification.THRESHOLD_HIGH_VALUE_EXCEEDED, ! MonitorNotification.THRESHOLD_LOW_VALUE_EXCEEDED ! } ! , "javax.management.monitor.MonitorNotification", "Notifications sent by the GaugeMonitor MBean") ! }; ! ! public synchronized void start() ! { ! doStart(); ! } ! ! public synchronized void stop() ! { ! doStop(); ! } ! void executeMonitor(ObjectName objectName, Object attributeValue) { ! GaugeInfo gi = (GaugeInfo)infos.get(objectName); ! if (highThreshold == null || highThreshold == NULLINTEGER) ! { ! if (!errorNotified) ! { ! getLogger().info(new StringBuffer("Monitor ").append(this).append(" threshold value is null or zero")); ! notifyListeners(MonitorNotification.THRESHOLD_ERROR, objectName, attribute); ! errorNotified = true; ! return; ! } ! } ! if (lowThreshold == null || lowThreshold == NULLINTEGER) ! { ! if (!errorNotified) ! { ! getLogger().info(new StringBuffer("Monitor ").append(this).append(" threshold value is null or zero")); ! notifyListeners(MonitorNotification.THRESHOLD_ERROR, objectName, attribute); ! errorNotified = true; ! return; ! } ! } ! // need to be refined ! if (!(attributeValue instanceof Number)) ! { ! if (!errorNotified) ! { ! getLogger().info(new StringBuffer("Monitor ").append(this).append(" attribute is not a Number")); ! notifyListeners(MonitorNotification.THRESHOLD_ERROR, objectName, attribute); ! errorNotified = true; ! return; ! } ! } ! determineType(attributeValue); ! if (type == NONE) ! { ! if (!errorNotified) ! { ! getLogger().info(new StringBuffer("Monitor ").append(this).append(" attribute, threshold, offset and modules types don't match")); ! notifyListeners(MonitorNotification.THRESHOLD_ERROR, objectName, attribute); ! errorNotified = true; ! return; ! } ! } ! calculateDerivedGauge(gi, (Number)attributeValue); ! if (isLong) ! { ! if (((Number)attributeValue).longValue() >= highThreshold.longValue()) ! { ! if (!gi.isHighNotified() && notifyHigh) ! { ! getLogger().info(new StringBuffer("Monitor ").append(this).append(" counter over the threshold")); ! notifyListeners(MonitorNotification.THRESHOLD_HIGH_VALUE_EXCEEDED, objectName, attribute); ! gi.setHighNotified(true); ! // hystersis ! gi.setLowNotified(false); ! } ! if (!notifyHigh) ! { ! gi.setLowNotified(false); ! } ! } ! if (((Number)attributeValue).longValue() <= lowThreshold.longValue()) ! { ! if (!gi.isLowNotified() && notifyLow) ! { ! getLogger().info(new StringBuffer("Monitor ").append(this).append(" counter over the threshold")); ! notifyListeners(MonitorNotification.THRESHOLD_LOW_VALUE_EXCEEDED, objectName, attribute); ! gi.setLowNotified(true); ! // hystersis ! gi.setHighNotified(false); ! } ! if (!notifyLow) ! { ! gi.setHighNotified(false); ! } ! } ! } ! else { ! if (((Number)attributeValue).doubleValue() >= highThreshold.doubleValue()) ! { ! if (!gi.isHighNotified() && notifyHigh) ! { ! getLogger().info(new StringBuffer("Monitor ").append(this).append(" counter over the threshold")); ! notifyListeners(MonitorNotification.THRESHOLD_HIGH_VALUE_EXCEEDED, objectName, attribute); ! gi.setHighNotified(true); ! // hystersis ! gi.setLowNotified(false); ! } ! if (!notifyHigh) ! { ! gi.setLowNotified(false); ! } ! } ! if (((Number)attributeValue).doubleValue() <= lowThreshold.doubleValue()) { ! if (!gi.isLowNotified() && notifyLow) ! { ! getLogger().info(new StringBuffer("Monitor ").append(this).append(" counter over the threshold")); ! notifyListeners(MonitorNotification.THRESHOLD_LOW_VALUE_EXCEEDED, objectName, attribute); ! gi.setLowNotified(true); ! // hystersis ! gi.setHighNotified(false); ! } ! if (!notifyLow) { ! gi.setHighNotified(false); } - } - } - } ! void determineType(Object attributeValue) ! { ! Class targetClass = attributeValue.getClass(); ! if (highThreshold != null && lowThreshold != null) ! { ! if (highThreshold.getClass().equals(lowThreshold.getClass()) && highThreshold.getClass().equals(targetClass)) ! { ! boolean match = true; ! if (targetClass != INT && targetClass != LONG && targetClass != BYTE ! && targetClass != SHORT && targetClass != FLOAT && targetClass != DOUBLE) ! { ! match = false; ! } ! if (match) { ! type = targetClass; ! if (targetClass.equals(FLOAT) || targetClass.equals(DOUBLE)) ! { ! isLong = false; ! } ! else ! { ! isLong = true; ! } } - } - else - { - type = NONE; - } - } - else - { - type = NONE; - } - } ! void calculateDerivedGauge(GaugeInfo gi, Number attributeValue) ! { ! gi.setLastDerivatedGaugeTimestamp(System.currentTimeMillis()); ! if (differenceMode) ! { ! if (gi.getLastValue() != null) ! { ! if (isLong) ! { ! long difference = attributeValue.longValue() - gi.getLastValue().longValue(); ! gi.setLastDerivatedGauge(createNumber(difference)); ! } ! else { ! double difference = attributeValue.doubleValue() - gi.getLastValue().doubleValue(); ! gi.setLastDerivatedGauge(createNumber(difference)); } ! } ! } ! else ! { ! gi.setLastDerivatedGauge(attributeValue); ! } ! gi.setLastValue(attributeValue); ! } ! ! Number createNumber(long value) ! { ! Number result = null; ! if (type == INT) ! { ! result = new Integer((int)value); ! } ! else if (type == LONG) ! { ! result = new Long(value); ! } ! else if (type == SHORT) ! { ! result = new Short((short)value); ! } ! else if (type == BYTE) ! { ! result = new Byte((byte)value); } ! else { ! getLogger().error("Invalid type " + type + " in createNumber(long)"); } - return result; } ! Number createNumber(double value) { ! Number result = null; ! if (type == FLOAT) ! { ! result = new Float((float)value); ! } ! else if (type == DOUBLE) ! { ! result = new Double(value); ! } ! else ! { ! getLogger().error("Invalid type " + type + " in createNumber(double)"); ! } ! return result; } ! public synchronized Number getDerivedGauge() { ! if (objectNames.size() == 0) return null; ! return getDerivedGauge((ObjectName)objectNames.get(0)); } public Number getDerivedGauge(ObjectName objectName) { ! return ((GaugeInfo)infos.get(objectName)).getLastDerivatedGauge(); ! } ! ! ! public synchronized long getDerivedGaugeTimeStamp() ! { ! if (objectNames.size() == 0) return 0; ! return getDerivedGaugeTimeStamp((ObjectName)objectNames.get(0)); } public long getDerivedGaugeTimeStamp(ObjectName objectName) { ! return ((GaugeInfo)infos.get(objectName)).getLastDerivatedGaugeTimestamp(); } public Number getHighThreshold() { ! return highThreshold; } public Number getLowThreshold() { ! return lowThreshold; } ! public void setThresholds(Number highValue, Number lowValue) throws java.lang.IllegalArgumentException { ! if (highValue == null || lowValue == null) ! { ! throw new IllegalArgumentException("Threshold values cannot be null"); ! } ! if (!highValue.getClass().equals(lowValue.getClass())) ! { ! throw new IllegalArgumentException("Threshold need to be of the same class"); ! } ! if (highValue.doubleValue() < lowValue.doubleValue()) ! { ! throw new IllegalArgumentException("High threshold value must be greater than low threshold value"); ! } ! highThreshold = highValue; ! lowThreshold = lowValue; } public boolean getNotifyHigh() { ! return notifyHigh; } public void setNotifyHigh(boolean value) { ! this.notifyHigh = value; } public boolean getNotifyLow() { ! return notifyLow; } public void setNotifyLow(boolean value) { ! this.notifyLow = value; } public boolean getDifferenceMode() { ! return differenceMode; } public void setDifferenceMode(boolean value) { ! this.differenceMode = value; } public MBeanNotificationInfo[] getNotificationInfo() { ! return notificationInfos; ! } ! ! public String toString() ! { ! return new StringBuffer("GaugeMonitor on ").append(super.toString()).toString(); ! } ! ! public synchronized void addObservedObject(ObjectName objectName) throws java.lang.IllegalArgumentException ! { ! super.addObservedObject(objectName); ! infos.put(objectName, new GaugeInfo()); ! } ! ! public void removeObservedObject(ObjectName objectName) ! { ! super.removeObservedObject(objectName); ! infos.remove(objectName); ! } ! ! class GaugeInfo ! { ! Number lastDerivatedGauge = new Integer(0); ! long lastDerivatedGaugeTimestamp = 0; ! ! boolean highNotified = false; ! boolean lowNotified = false; ! Number lastValue = null; ! ! public GaugeInfo() ! { ! } ! ! /** Getter for property lastDerivatedGauge. ! * @return Value of property lastDerivatedGauge. ! * ! */ ! public java.lang.Number getLastDerivatedGauge() ! { ! return lastDerivatedGauge; ! } ! ! /** Setter for property lastDerivatedGauge. ! * @param lastDerivatedGauge New value of property lastDerivatedGauge. ! * ! */ ! public void setLastDerivatedGauge(java.lang.Number lastDerivatedGauge) ! { ! this.lastDerivatedGauge = lastDerivatedGauge; ! } ! ! /** Getter for property lastDerivatedGaugeTimestamp. ! * @return Value of property lastDerivatedGaugeTimestamp. ! * ! */ ! public long getLastDerivatedGaugeTimestamp() ! { ! return lastDerivatedGaugeTimestamp; ! } ! ! /** Setter for property lastDerivatedGaugeTimestamp. ! * @param lastDerivatedGaugeTimestamp New value of property lastDerivatedGaugeTimestamp. ! * ! */ ! public void setLastDerivatedGaugeTimestamp(long lastDerivatedGaugeTimestamp) ! { ! this.lastDerivatedGaugeTimestamp = lastDerivatedGaugeTimestamp; ! } ! ! /** Getter for property lastValue. ! * @return Value of property lastValue. ! * ! */ ! public java.lang.Number getLastValue() ! { ! return lastValue; ! } ! ! /** Setter for property lastValue. ! * @param lastValue New value of property lastValue. ! * ! */ ! public void setLastValue(java.lang.Number lastValue) ! { ! this.lastValue = lastValue; ! } ! ! /** Getter for property highNotified. ! * @return Value of property highNotified. ! * ! */ ! public boolean isHighNotified() ! { ! return highNotified; ! } ! ! /** Setter for property highNotified. ! * @param highNotified New value of property highNotified. ! * ! */ ! public void setHighNotified(boolean highNotified) ! { ! this.highNotified = highNotified; ! } ! ! /** Getter for property lowNotified. ! * @return Value of property lowNotified. ! * ! */ ! public boolean isLowNotified() ! { ! return lowNotified; ! } ! ! /** Setter for property lowNotified. ! * @param lowNotified New value of property lowNotified. ! * ! */ ! public void setLowNotified(boolean lowNotified) ! { ! this.lowNotified = lowNotified; ! } ! } - - } --- 10,157 ---- import javax.management.MBeanNotificationInfo; ! import javax.management.NotCompliantMBeanException; ! import javax.management.Notification; ! import javax.management.NotificationBroadcasterSupport; import javax.management.ObjectName; + import mx4j.monitor.MX4JGaugeMonitor; + import mx4j.monitor.MX4JMonitor; + /** * @author <a href="mailto:[email protected]">Carlos Quiroz</a> * @version $Revision$ */ ! public class GaugeMonitor extends Monitor implements GaugeMonitorMBean { ! private static final MBeanNotificationInfo[] notificationInfos = ! { ! new MBeanNotificationInfo(new String[] ! { ! MonitorNotification.RUNTIME_ERROR, ! MonitorNotification.OBSERVED_OBJECT_ERROR, ! MonitorNotification.OBSERVED_ATTRIBUTE_ERROR, ! MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR, ! MonitorNotification.THRESHOLD_ERROR, ! MonitorNotification.THRESHOLD_HIGH_VALUE_EXCEEDED, ! MonitorNotification.THRESHOLD_LOW_VALUE_EXCEEDED ! }, ! MonitorNotification.class.getName(), ! "Notifications sent by the GaugeMonitor MBean") ! }; ! MX4JMonitor createMX4JMonitor() { ! try { ! return new MX4JGaugeMonitor() { ! protected NotificationBroadcasterSupport createNotificationEmitter() { ! return GaugeMonitor.this; } ! public MBeanNotificationInfo[] getNotificationInfo() { ! return notificationInfos; } ! protected Notification createMonitorNotification(String type, long sequence, String message, ObjectName observed, String attribute, Object gauge, Object trigger) { ! return new MonitorNotification(type, this, sequence, System.currentTimeMillis(), message, observed, attribute, gauge, trigger); } ! }; } ! catch (NotCompliantMBeanException x) { ! return null; } } ! /** ! * @deprecated ! */ ! public Number getDerivedGauge() { ! return getDerivedGauge(getObservedObject()); } ! /** ! * @deprecated ! */ ! public long getDerivedGaugeTimeStamp() { ! return getDerivedGaugeTimeStamp(getObservedObject()); } public Number getDerivedGauge(ObjectName objectName) { ! MX4JGaugeMonitor monitor = (MX4JGaugeMonitor)getMX4JMonitor(); ! return monitor.getDerivedGauge(objectName); } public long getDerivedGaugeTimeStamp(ObjectName objectName) { ! MX4JGaugeMonitor monitor = (MX4JGaugeMonitor)getMX4JMonitor(); ! return monitor.getDerivedGaugeTimeStamp(objectName); } public Number getHighThreshold() { ! MX4JGaugeMonitor monitor = (MX4JGaugeMonitor)getMX4JMonitor(); ! return monitor.getHighThreshold(); } public Number getLowThreshold() { ! MX4JGaugeMonitor monitor = (MX4JGaugeMonitor)getMX4JMonitor(); ! return monitor.getLowThreshold(); } ! public void setThresholds(Number highValue, Number lowValue) throws IllegalArgumentException { ! MX4JGaugeMonitor monitor = (MX4JGaugeMonitor)getMX4JMonitor(); ! monitor.setThresholds(highValue, lowValue); } public boolean getNotifyHigh() { ! MX4JGaugeMonitor monitor = (MX4JGaugeMonitor)getMX4JMonitor(); ! return monitor.getNotifyHigh(); } public void setNotifyHigh(boolean value) { ! MX4JGaugeMonitor monitor = (MX4JGaugeMonitor)getMX4JMonitor(); ! monitor.setNotifyHigh(value); } public boolean getNotifyLow() { ! MX4JGaugeMonitor monitor = (MX4JGaugeMonitor)getMX4JMonitor(); ! return monitor.getNotifyLow(); } public void setNotifyLow(boolean value) { ! MX4JGaugeMonitor monitor = (MX4JGaugeMonitor)getMX4JMonitor(); ! monitor.setNotifyLow(value); } public boolean getDifferenceMode() { ! MX4JGaugeMonitor monitor = (MX4JGaugeMonitor)getMX4JMonitor(); ! return monitor.getDifferenceMode(); } public void setDifferenceMode(boolean value) { ! MX4JGaugeMonitor monitor = (MX4JGaugeMonitor)getMX4JMonitor(); ! monitor.setDifferenceMode(value); } public MBeanNotificationInfo[] getNotificationInfo() { ! MX4JGaugeMonitor monitor = (MX4JGaugeMonitor)getMX4JMonitor(); ! return monitor.getNotificationInfo(); } } Index: MonitorMBean.java =================================================================== RCS file: /cvsroot/mx4j/mx4j/src/core/javax/management/monitor/MonitorMBean.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** MonitorMBean.java 28 Aug 2004 16:34:18 -0000 1.4 --- MonitorMBean.java 4 Sep 2004 13:57:33 -0000 1.5 *************** *** 1,4 **** /* ! * Copyright (C) MX4J. * All rights reserved. * --- 1,4 ---- /* ! * Copyright (C) The MX4J contributors. * All rights reserved. * Index: GaugeMonitorMBean.java =================================================================== RCS file: /cvsroot/mx4j/mx4j/src/core/javax/management/monitor/GaugeMonitorMBean.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** GaugeMonitorMBean.java 28 Aug 2004 16:40:25 -0000 1.4 --- GaugeMonitorMBean.java 4 Sep 2004 13:57:33 -0000 1.5 *************** *** 1,4 **** /* ! * Copyright (C) MX4J. * All rights reserved. * --- 1,4 ---- /* ! * Copyright (C) The MX4J contributors. * All rights reserved. * Index: StringMonitorMBean.java =================================================================== RCS file: /cvsroot/mx4j/mx4j/src/core/javax/management/monitor/StringMonitorMBean.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** StringMonitorMBean.java 28 Aug 2004 16:40:25 -0000 1.4 --- StringMonitorMBean.java 4 Sep 2004 13:57:33 -0000 1.5 *************** *** 1,4 **** /* ! * Copyright (C) MX4J. * All rights reserved. * --- 1,4 ---- /* ! * Copyright (C) The MX4J contributors. * All rights reserved. * *************** *** 15,19 **** * @version $Revision$ */ ! public interface StringMonitorMBean extends MonitorMBean { /** --- 15,19 ---- * @version $Revision$ */ ! public interface StringMonitorMBean extends javax.management.monitor.MonitorMBean { /** ------------------------------------------------------- This SF.Net email is sponsored by BEA Weblogic Workshop FREE Java Enterprise J2EE developer tools! Get your free copy of BEA WebLogic Workshop 8.1 today. http://ads.osdn.com/?ad_id=5047&alloc_id=10808&op=click