package org.jboss.monitor.alarm;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Collection;
import javax.management.Notification;
import javax.management.ObjectName;
public class AlarmTable
{
protected MBeanImplAccess mbeanImpl;
private String serverId;
private Map alarmMap;
public AlarmTable(MBeanImplAccess mbeanImpl)
{
this.mbeanImpl = mbeanImpl;
this.alarmMap = new HashMap();
}
public void setServerId(String serverId)
{
this.serverId = serverId;
}
public String getServerId()
{
return this.serverId;
}
public void update(Notification n)
{
if (n instanceof AlarmTableNotification) {
}
else if (n instanceof AlarmNotification) {
AlarmNotification an = (AlarmNotification)n;
if (an.getAlarmState() == Alarm.STATE_NONE)
updateNotificationStateless(n, an.getSeverity());
else
updateNotificationStatefull(an);
}
else
updateNotificationStateless(n, Alarm.SEVERITY_UNKNOWN);
}
public boolean acknowledge(String serverId, String source, String type,
String user, String system)
{
AlarmKey key = AlarmKey.createKey(serverId, source, type);
return this.acknowledge(key, user, system);
}
public boolean acknowledge(Object key, String user, String system)
{
AlarmTableNotification atn;
synchronized (this) {
AlarmTableNotification entry =
(AlarmTableNotification)this.alarmMap.get(key);
if (entry == null || entry.getAckState() == true)
return false;
entry.setAckParams(true, System.currentTimeMillis(), user, system);
atn = new AlarmTableNotification(entry);
atn.setSequenceNumber(this.mbeanImpl.getSequenceNumber());
atn.setTimeStamp(System.currentTimeMillis());
int alarmState = entry.getAlarmState();
if (alarmState == Alarm.STATE_NONE || alarmState == Alarm.STATE_CLEARED)
this.alarmMap.remove(key);
}
this.mbeanImpl.emitNotification(atn);
return true; }
public boolean unacknowledge(String serverId, String source, String type,
String user, String system)
{
AlarmKey key = AlarmKey.createKey(serverId, source, type);
return this.unacknowledge(key, user, system);
}
public boolean unacknowledge(Object key, String user, String system)
{
AlarmTableNotification atn;
synchronized (this) {
AlarmTableNotification entry =
(AlarmTableNotification)this.alarmMap.get(key);
if (entry == null || entry.getAckState() == false)
return false;
entry.setAckParams(false, System.currentTimeMillis(), user, system);
atn = new AlarmTableNotification(entry);
atn.setSequenceNumber(this.mbeanImpl.getSequenceNumber());
atn.setTimeStamp(System.currentTimeMillis());
}
this.mbeanImpl.emitNotification(atn);
return true; }
public AlarmTableNotification[] getAlarmTable()
{
synchronized (this) {
int size = this.alarmMap.size();
AlarmTableNotification[] copy =
new AlarmTableNotification[size];
Collection values = this.alarmMap.values();
Iterator i = values.iterator();
while (i.hasNext()) {
AlarmTableNotification atn =
(AlarmTableNotification)i.next();
copy[--size] = new AlarmTableNotification(atn);
}
return copy;
}
}
private void updateNotificationStatefull(AlarmNotification an)
{
int alarmState = an.getAlarmState();
int severity = an.getSeverity();
AlarmTableNotification atn =
new AlarmTableNotification(
AlarmTableNotification.ALARM_TABLE_UPDATE,
this.mbeanImpl.getMBeanName(),
this.mbeanImpl.getSequenceNumber(),
System.currentTimeMillis(),
null,
alarmState,
severity,
this.serverId
);
atn.setUserData(an);
Object key = atn.createKey();
synchronized (this) {
if (alarmState == Alarm.STATE_CLEARED) {
AlarmTableNotification entry =
(AlarmTableNotification)this.alarmMap.get(key);
if (entry != null && entry.getAckState() == true) {
this.alarmMap.remove(key);
atn.setAckParams(true, entry.getAckTime(),
entry.getAckUser(), entry.getAckSystem());
}
else {
this.alarmMap.put(key, atn);
}
}
else {
this.alarmMap.put(key, atn);
}
}
if (atn.getAckState() == true)
this.mbeanImpl.emitNotification(atn);
else this.mbeanImpl.emitNotification(new AlarmTableNotification(atn));
}
private void updateNotificationStateless(Notification n, int severity)
{
AlarmTableNotification atn =
new AlarmTableNotification(
AlarmTableNotification.ALARM_TABLE_UPDATE,
this.mbeanImpl.getMBeanName(),
this.mbeanImpl.getSequenceNumber(),
System.currentTimeMillis(),
null,
Alarm.STATE_NONE,
severity,
this.serverId
);
atn.setUserData(n);
Object key = atn.createKey();
synchronized (this) {
this.alarmMap.put(key, atn);
}
this.mbeanImpl.emitNotification(new AlarmTableNotification(atn));
}
}