package org.jboss.monitor.alarm;
import java.util.Map;
import java.util.HashMap;
import javax.management.Notification;
import javax.management.AttributeChangeNotification;
public class AlarmManager
{
protected MBeanImplAccess mbeanImpl;
private Map typeMap;
public AlarmManager(MBeanImplAccess mbeanImpl)
{
this.mbeanImpl = mbeanImpl;
this.typeMap = new HashMap();
}
public void setSeverity(String type, int severity)
{
synchronized (this) {
AlarmInfo ai = (AlarmInfo)this.typeMap.get(type);
if (ai == null)
this.typeMap.put(type, new AlarmInfo(severity));
else
ai.setSeverity(severity);
}
}
public int getSeverity(String type)
{
AlarmInfo ai;
synchronized (this) {
ai = (AlarmInfo)this.typeMap.get(type);
if (ai == null) {
ai = new AlarmInfo(Alarm.SEVERITY_NORMAL);
this.typeMap.put(type, ai);
}
}
return ai.getSeverity();
}
public String getSeverityAsString(String type)
{
return Alarm.SEVERITY_STRINGS[getSeverity(type)];
}
public void setAlarm(String type, int severity, String message, Object userData)
{
AlarmInfo ai;
synchronized (this) {
ai = (AlarmInfo)this.typeMap.get(type);
if (ai == null) {
ai = new AlarmInfo(Alarm.SEVERITY_NORMAL);
this.typeMap.put(type, ai);
}
}
int oldSeverity = ai.getSeverity();
if (severity != oldSeverity) {
ai.setSeverity(severity);
if (severity == Alarm.SEVERITY_NORMAL)
sendAlarmNotification(
type, Alarm.STATE_CLEARED, severity, message, userData);
else if (oldSeverity == Alarm.SEVERITY_NORMAL)
sendAlarmNotification(
type, Alarm.STATE_CREATED, severity, message, userData);
else
sendAlarmNotification(
type, Alarm.STATE_CHANGED, severity, message, userData);
}
}
public void setAlarm(
String type, int severity, String message, String key, Object value)
{
HashMap map = new HashMap();
map.put(key, value);
this.setAlarm(type, severity, message, map);
}
public void sendAlarmNotification(
String type, int alarmState, int severity,
String message,
String key, Object value)
{
HashMap map = new HashMap();
map.put(key, value);
this.sendAlarmNotification(type, alarmState, severity, message, map);
}
public void sendAlarmNotification(
String type, int alarmState, int severity,
String message, Object userData)
{
Notification n = new AlarmNotification(
type,
this.mbeanImpl.getMBeanName(), this.mbeanImpl.getSequenceNumber(),
System.currentTimeMillis(),
message,
alarmState,
severity
);
n.setUserData(userData);
this.mbeanImpl.emitNotification(n);
}
public void sendAttributeChangeNotification(
String type, String message, Object userData,
String attributeName, String attributeType,
Object oldValue, Object newValue)
{
Notification n = new AttributeChangeNotification(
this.mbeanImpl.getMBeanName(), this.mbeanImpl.getSequenceNumber(),
System.currentTimeMillis(),
message,
attributeName,
attributeType,
oldValue,
newValue
);
n.setUserData(userData);
this.mbeanImpl.emitNotification(n);
}
public void sendNotification(String type, String message, Object userData)
{
Notification n = new Notification(
type,
this.mbeanImpl.getMBeanName(), this.mbeanImpl.getSequenceNumber(),
System.currentTimeMillis(),
message
);
n.setUserData(userData);
this.mbeanImpl.emitNotification(n);
}
private static class AlarmInfo
{
private int severity;
public AlarmInfo(int severity)
{
this.severity = severity;
}
public void setSeverity(int severity)
{
this.severity = severity;
}
public int getSeverity()
{
return this.severity;
}
}
}