package javax.management.monitor;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.management.MBeanRegistration;
import javax.management.MBeanServer;
import javax.management.NotificationBroadcasterSupport;
import javax.management.ObjectName;
import org.jboss.mx.util.ObservedObject;
import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap;
public abstract class Monitor
extends NotificationBroadcasterSupport
implements MonitorMBean, MBeanRegistration
{
protected static final int capacityIncrement = 16;
protected static final int RESET_FLAGS_ALREADY_NOTIFIED = 0;
protected static final int RUNTIME_ERROR_NOTIFIED = 8;
protected static final int OBSERVED_OBJECT_ERROR_NOTIFIED = 1;
protected static final int OBSERVED_ATTRIBUTE_ERROR_NOTIFIED = 2;
protected static final int OBSERVED_ATTRIBUTE_TYPE_ERROR_NOTIFIED = 4;
protected int elementCount = 0;
long granularityPeriod = 10000;
String observedAttribute = null;
ConcurrentHashMap observedObjects = new ConcurrentHashMap();
boolean active = false;
protected MBeanServer server;
ObjectName objectName;
protected int alreadyNotified = 0;
protected int[] alreadyNotifieds = new int[0];
protected String dbgTag = null;
private long sequenceNumber;
public long getGranularityPeriod()
{
return granularityPeriod;
}
public String getObservedAttribute()
{
return observedAttribute;
}
public ObjectName getObservedObject()
{
ObservedObject object = getFirstObservedObject();
if (object != null)
return object.getObjectName();
else
return null;
}
public ObjectName[] getObservedObjects()
{
Set set = new HashSet(observedObjects.values());
elementCount = set.size();
ObjectName[] result = new ObjectName[set.size()];
alreadyNotifieds = new int[set.size()];
int count = 0;
for (Iterator i = set.iterator(); i.hasNext();)
{
ObservedObject object = (ObservedObject) i.next();
result[count] = object.getObjectName();
alreadyNotifieds[count++] = object.getAlreadyNotified();
}
return result;
}
public synchronized boolean isActive()
{
return active;
}
public void setGranularityPeriod(long period)
throws IllegalArgumentException
{
if (period <= 0)
throw new IllegalArgumentException("Period must be positive.");
granularityPeriod = period;
}
public void setObservedAttribute(String attribute)
throws IllegalArgumentException
{
observedAttribute = attribute;
}
public void setObservedObject(ObjectName object)
throws IllegalArgumentException
{
observedObjects.clear();
addObservedObject(object);
}
public void addObservedObject(ObjectName object)
throws IllegalArgumentException
{
if (object == null)
throw new IllegalArgumentException("null object name");
ObservedObject o = new ObservedObject(object);
initObservedObject(o);
observedObjects.put(object, o);
}
public void removeObservedObject(ObjectName object)
{
if (object == null)
throw new IllegalArgumentException("null object name");
observedObjects.remove(object);
}
public boolean containsObservedObject(ObjectName object)
{
if (object == null)
throw new IllegalArgumentException("null object name");
return observedObjects.containsKey(object);
}
public abstract void start();
public abstract void stop();
public String toString()
{
StringBuffer buffer = new StringBuffer(100);
buffer.append(getClass()).append(System.identityHashCode(this)).append(": {");
buffer.append(" objectName=").append(objectName);
return buffer.append("}").toString();
}
public ObjectName preRegister(MBeanServer server, ObjectName objectName)
throws Exception
{
this.server = server;
this.objectName = objectName;
return objectName;
}
public void postRegister(Boolean registrationDone)
{
}
public void preDeregister()
throws Exception
{
stop();
}
public void postDeregister()
{
}
ObservedObject retrieveObservedObject(ObjectName name)
{
return (ObservedObject) observedObjects.get(name);
}
Map retrieveObservedObjects()
{
return observedObjects;
}
ObservedObject getFirstObservedObject()
{
Iterator i = observedObjects.values().iterator();
if (i.hasNext())
return (ObservedObject) i.next();
else
return null;
}
void initObservedObject(ObservedObject object)
{
object.resetAlreadyNotified();
object.setDerivedGaugeTimeStamp(System.currentTimeMillis());
}
void sendNotification(ObservedObject object, String type, long timestamp, String message,
String attribute, Object gauge, Object trigger)
{
long seq = nextSeqNo();
if (timestamp == 0)
timestamp = System.currentTimeMillis();
sendNotification(new MonitorNotification(type, objectName, seq,
timestamp, message, gauge,
attribute, object.getObjectName(), trigger));
}
void sendRuntimeErrorNotification(ObservedObject object, String message)
{
if (object.notAlreadyNotified(RUNTIME_ERROR_NOTIFIED))
sendNotification(object, MonitorNotification.RUNTIME_ERROR, 0,
message, observedAttribute, null, null);
}
void sendObjectErrorNotification(ObservedObject object, String message)
{
if (object.notAlreadyNotified(OBSERVED_OBJECT_ERROR_NOTIFIED))
sendNotification(object, MonitorNotification.OBSERVED_OBJECT_ERROR, 0,
message, observedAttribute, null, null);
}
void sendAttributeErrorNotification(ObservedObject object, String message)
{
if (object.notAlreadyNotified(OBSERVED_ATTRIBUTE_ERROR_NOTIFIED))
sendNotification(object, MonitorNotification.OBSERVED_ATTRIBUTE_ERROR, 0,
message, observedAttribute, null, null);
}
void sendAttributeTypeErrorNotification(ObservedObject object, String message)
{
if (object.notAlreadyNotified(OBSERVED_ATTRIBUTE_TYPE_ERROR_NOTIFIED))
sendNotification(object, MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR, 0,
message, observedAttribute, null, null);
}
void resetAlreadyNotified()
{
for (Iterator i = observedObjects.values().iterator(); i.hasNext();)
((ObservedObject) i.next()).resetAlreadyNotified();
}
synchronized long nextSeqNo()
{
long nextSeqNo = sequenceNumber ++;
return nextSeqNo;
}
}