package org.jboss.mx.util;
import java.util.Iterator;
import java.util.Map;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.monitor.Monitor;
import javax.management.monitor.MonitorNotification;
import org.jboss.logging.Logger;
public class MonitorRunnable
extends SchedulableRunnable
{
private static final Logger log = Logger.getLogger(MonitorRunnable.class);
static RunnableScheduler scheduler;
static
{
scheduler = new RunnableScheduler();
scheduler.start();
}
private Monitor monitor;
private ObjectName monitorName;
private MonitorCallback callback;
private Map observedObjects;
private MBeanServer server;
public MonitorRunnable(Monitor monitor, ObjectName monitorName,
MonitorCallback callback, Map observedObjects, MBeanServer server)
{
this.monitor = monitor;
this.monitorName = monitorName;
this.callback = callback;
this.observedObjects = observedObjects;
this.server = server;
setScheduler(scheduler);
}
void runMonitor(ObservedObject object)
{
try
{
MBeanInfo mbeanInfo = null;
try
{
mbeanInfo = server.getMBeanInfo(object.getObjectName());
}
catch (InstanceNotFoundException e)
{
sendObjectErrorNotification(object, "The observed object is not registered.");
return;
}
MBeanAttributeInfo[] mbeanAttributeInfo = mbeanInfo.getAttributes();
MBeanAttributeInfo attributeInfo = null;
for (int i = 0; i < mbeanAttributeInfo.length; i++)
{
if (mbeanAttributeInfo[i].getName().equals(monitor.getObservedAttribute()))
{
attributeInfo = mbeanAttributeInfo[i];
break;
}
}
if (attributeInfo == null)
{
sendAttributeErrorNotification(object,
"The observed attribute does not exist");
return;
}
if (!attributeInfo.isReadable())
{
sendAttributeErrorNotification(object, "Attribute not readable.");
return;
}
Object value = null;
try
{
value = server.getAttribute(object.getObjectName(), monitor.getObservedAttribute());
}
catch (InstanceNotFoundException e)
{
sendObjectErrorNotification(object, "The observed object is not registered.");
return;
}
if (value == null)
{
sendAttributeTypeErrorNotification(object, "Attribute is null");
return;
}
callback.monitorCallback(object, attributeInfo, value);
}
catch (Throwable e)
{
log.debug("Error in monitor ", e);
sendRuntimeErrorNotification(object, "General error: " + e.toString());
}
}
public void doRun()
{
runMonitor();
setNextRun(System.currentTimeMillis() + monitor.getGranularityPeriod());
}
void runMonitor()
{
boolean isActive = monitor.isActive();
for (Iterator i = observedObjects.values().iterator(); i.hasNext() && isActive;)
runMonitor((ObservedObject) i.next());
}
void sendNotification(ObservedObject object, String type, long timestamp, String message,
String attribute, Object gauge, Object trigger)
{
MonitorNotification n = callback.createNotification(type,
monitorName, timestamp, message, gauge, attribute,
object.getObjectName(), trigger);
monitor.sendNotification(n);
}
void sendRuntimeErrorNotification(ObservedObject object, String message)
{
if (object.notAlreadyNotified(ObservedObject.RUNTIME_ERROR_NOTIFIED))
sendNotification(object, MonitorNotification.RUNTIME_ERROR, 0,
message, monitor.getObservedAttribute(), null, null);
}
void sendObjectErrorNotification(ObservedObject object, String message)
{
if (object.notAlreadyNotified(ObservedObject.OBSERVED_OBJECT_ERROR_NOTIFIED))
sendNotification(object, MonitorNotification.OBSERVED_OBJECT_ERROR, 0,
message, monitor.getObservedAttribute(), null, null);
}
void sendAttributeErrorNotification(ObservedObject object, String message)
{
if (object.notAlreadyNotified(ObservedObject.OBSERVED_ATTRIBUTE_ERROR_NOTIFIED))
sendNotification(object, MonitorNotification.OBSERVED_ATTRIBUTE_ERROR, 0,
message, monitor.getObservedAttribute(), null, null);
}
void sendAttributeTypeErrorNotification(ObservedObject object, String message)
{
if (object.notAlreadyNotified(ObservedObject.OBSERVED_ATTRIBUTE_TYPE_ERROR_NOTIFIED))
sendNotification(object, MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR, 0,
message, monitor.getObservedAttribute(), null, null);
}
}