/* JFox, the OpenSource J2EE Application Server
*
* Copyright (C) 2002 huihoo.org
* Distributable under GNU LGPL license
* See the GNU Lesser General Public License for more details.
*/
package javax.management.monitor;
import javax.management.MBeanNotificationInfo;
import javax.management.ObjectName;
/**
* Defines a monitor MBean designed to observe the values of a string
* attribute.
* <P>
* A string monitor sends notifications as follows:
* <UL>
* <LI> if the attribute value matches the string to compare value,
* a {@link MonitorNotification#STRING_TO_COMPARE_VALUE_MATCHED match notification} is sent.
* The notify match flag must be set to <CODE>true</CODE>.
* <BR>Subsequent matchings of the string to compare values do not
* cause further notifications unless
* the attribute value differs from the string to compare value.
* <LI> if the attribute value differs from the string to compare value,
* a {@link MonitorNotification#STRING_TO_COMPARE_VALUE_DIFFERED differ notification} is sent.
* The notify differ flag must be set to <CODE>true</CODE>.
* <BR>Subsequent differences from the string to compare value do
* not cause further notifications unless
* the attribute value matches the string to compare value.
* </UL>
*
* @author <a href="mailto:young_yy@hotmail.org">Young Yang</a>
*/
public class StringMonitor extends Monitor implements StringMonitorMBean {
private String stringToCompare = "";
private boolean notifyMatch = false;
private boolean notifyDiffer = false;
// private String derivedGauge = "";
// private long derivedGaugeTimestamp = System.currentTimeMillis();
private long times = 0L; // except the first time
public StringMonitor() {
}
public void start() {
super._start();
}
public synchronized void stop() {
super._stop();
}
public String getDerivedGauge(ObjectName observedObject) {
return ((StringMonitorData)getMonitorData(observedObject)).derivedGauge;
}
public long getDerivedGaugeTimeStamp(ObjectName observedObject) {
return ((StringMonitorData)getMonitorData(observedObject)).derivedGaugeTimestamp;
}
public String getStringToCompare() {
return stringToCompare;
}
public void setStringToCompare(String value) throws IllegalArgumentException {
if(value == null) throw new IllegalArgumentException("The string to compare cannot be null.");
stringToCompare = value;
}
public boolean getNotifyMatch() {
return notifyMatch;
}
public void setNotifyMatch(boolean value) {
notifyMatch = value;
}
public boolean getNotifyDiffer() {
return notifyDiffer;
}
public void setNotifyDiffer(boolean value) {
notifyDiffer = value;
}
public MBeanNotificationInfo[] getNotificationInfo() {
String[] notifTypes = {
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
};
MBeanNotificationInfo[] notifInfos = {
new MBeanNotificationInfo(notifTypes, MonitorNotification.class.getName(), "Notifications sent by the StringMonitor MBean")
};
return notifInfos;
}
// implemented by every entity monitor
protected void doMonitor(ObjectName observedObject, Object value) {
// System.out.println("StringMonitor.doMonitor()");
StringMonitorData data = (StringMonitorData)getMonitorData(observedObject);
try {
long derivedGaugeTimestamp = System.currentTimeMillis(); // update timestamp
data.derivedGaugeTimestamp = derivedGaugeTimestamp;
if (!(value instanceof String)) {
sendNotification(MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR,derivedGaugeTimestamp, "The observed attribute type must be a string type.",data.derivedGauge,stringToCompare,observedObject);
return;
}
if(stringToCompare == null) {
sendNotification(MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR,derivedGaugeTimestamp, "The observed attribute target value must not be null",data.derivedGauge,stringToCompare,observedObject);
return;
}
// attribute value not change
if(data.derivedGauge.equals(value)) return;
boolean matches = stringToCompare.equals(value);
// System.out.println("Matches: " + matches);
boolean lastMatches = stringToCompare.equals(data.derivedGauge);
// System.out.println("lastMatches: " + lastMatches);
// notify match when this time is matches and last time is not matched
if(notifyMatch && matches){
if(times == 0 || !lastMatches)
sendNotification(MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED, derivedGaugeTimestamp, "", data.derivedGauge, stringToCompare,observedObject);
}
// notify differ when this time is not match and last time is matched
if(notifyDiffer && !matches){
if(times == 0 || lastMatches)
sendNotification(MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED, derivedGaugeTimestamp, "", data.derivedGauge, stringToCompare,observedObject);
}
data.derivedGauge = (String)value; // update derived gauge
times++;
}
catch(Exception e){
sendNotification(MonitorNotification.RUNTIME_ERROR,data.derivedGaugeTimestamp,e.getMessage(),null,null,observedObject);
}
}
public void addObservedObject(ObjectName objectName) throws IllegalArgumentException {
if(objectName == null) throw new IllegalArgumentException("The object to observe cannot be null.");
observedObjects.put(objectName,new StringMonitorData(objectName));
}
class StringMonitorData extends MonitorData {
String derivedGauge = "";
long derivedGaugeTimestamp = System.currentTimeMillis();
public StringMonitorData(ObjectName objectName) {
super(objectName);
}
public ObjectName getObjectName() {
return super.getObjectName();
}
}
}
|