package javax.management.monitor;
import java.util.Iterator;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanNotificationInfo;
import javax.management.ObjectName;
import org.jboss.mx.util.MonitorRunnable;
import org.jboss.mx.util.ObservedObject;
import org.jboss.mx.util.MonitorCallback;
public class StringMonitor
extends Monitor
implements StringMonitorMBean
{
int STRING_MATCHES_NOTIFIED = 16;
int STRING_DIFFERS_NOTIFIED = 32;
String stringToCompare = new String();
boolean notifyMatch = false;
boolean notifyDiffer = false;
private MonitorRunnable monitorRunnable;
public StringMonitor()
{
}
public MBeanNotificationInfo[] getNotificationInfo()
{
MBeanNotificationInfo[] result = new MBeanNotificationInfo[1];
String[] types = 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
};
result[0] = new MBeanNotificationInfo(types,
"javax.management.monitor.MonitorNotification",
"Notifications sent by the String Monitor Service MBean");
return result;
}
public String getDerivedGauge()
{
ObservedObject object = getFirstObservedObject();
if (object != null)
return (String) object.getDerivedGauge();
else
return null;
}
public String getDerivedGauge(ObjectName name)
{
ObservedObject object = retrieveObservedObject(name);
if (object != null)
return (String) object.getDerivedGauge();
else
return null;
}
public long getDerivedGaugeTimeStamp()
{
ObservedObject object = getFirstObservedObject();
if (object != null)
return object.getDerivedGaugeTimeStamp();
else
return 0L;
}
public long getDerivedGaugeTimeStamp(ObjectName name)
{
ObservedObject object = retrieveObservedObject(name);
if (object != null)
return object.getDerivedGaugeTimeStamp();
else
return 0L;
}
public String getStringToCompare()
{
return stringToCompare;
}
public void setStringToCompare(String value)
throws IllegalArgumentException
{
if (value == null)
throw new IllegalArgumentException("Null string to compare.");
this.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 synchronized void start()
{
if (active)
return;
active = true;
for (Iterator i = retrieveObservedObjects().values().iterator(); i.hasNext();)
{
ObservedObject object = (ObservedObject) i.next();
initObservedObject(object);
}
MonitorCallback callback = new MonitorCallback()
{
public void monitorCallback(ObservedObject object,
MBeanAttributeInfo attributeInfo, Object value) throws Exception
{
monitor(object, attributeInfo, value);
}
public MonitorNotification createNotification(String type, Object source,
long timeStamp, String message, Object derivedGauge,
String observedAttribute, ObjectName observedObject,
Object trigger)
{
return new MonitorNotification(type, source, nextSeqNo(), timeStamp,
message, derivedGauge, observedAttribute, observedObject,
trigger);
}
};
monitorRunnable = new MonitorRunnable(this, objectName, callback,
observedObjects, server);
}
public synchronized void stop()
{
if (!active)
return;
active = false;
monitorRunnable.setScheduler(null);
monitorRunnable = null;
}
void initObservedObject(ObservedObject object)
{
super.initObservedObject(object);
object.setDerivedGauge(new String());
}
void monitor(ObservedObject object, MBeanAttributeInfo attributeInfo, Object value)
throws Exception
{
if (!(value instanceof String))
{
sendAttributeTypeErrorNotification(object, "Not a string attribute");
return;
}
object.setDerivedGauge(value);
object.setDerivedGaugeTimeStamp(System.currentTimeMillis());
if (value.equals(stringToCompare))
sendStringMatchesNotification(object, (String) value);
else
sendStringDiffersNotification(object, (String) value);
}
void sendStringMatchesNotification(ObservedObject object, String value)
{
if (notifyMatch && object.notAlreadyNotified(STRING_MATCHES_NOTIFIED))
sendNotification(object, MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED,
object.getDerivedGaugeTimeStamp(), "matches", observedAttribute, value,
stringToCompare);
object.setNotAlreadyNotified(STRING_DIFFERS_NOTIFIED);
}
void sendStringDiffersNotification(ObservedObject object, String value)
{
if (notifyDiffer && object.notAlreadyNotified(STRING_DIFFERS_NOTIFIED))
sendNotification(object, MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED,
object.getDerivedGaugeTimeStamp(), "differs", observedAttribute, value,
stringToCompare);
object.setNotAlreadyNotified(STRING_MATCHES_NOTIFIED);
}
}