1 22 package org.jboss.monitor.alarm; 23 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.Map ; 27 28 import javax.management.AttributeChangeNotification ; 29 import javax.management.Notification ; 30 import javax.management.ObjectName ; 31 32 import org.jboss.system.ServiceMBeanSupport; 33 34 40 public class AlarmManager 41 { 42 44 45 protected MBeanImplAccess mbeanImpl; 46 47 48 private Map nameMap; 49 50 52 57 public AlarmManager(MBeanImplAccess mbeanImpl) 58 { 59 this.mbeanImpl = mbeanImpl; 60 this.nameMap = new HashMap (); 61 } 62 63 68 public AlarmManager(final ServiceMBeanSupport service) 69 { 70 this(new MBeanImplAccess() { 71 public ObjectName getMBeanName() { return service.getServiceName(); } 72 public long getSequenceNumber() { return service.nextNotificationSequenceNumber(); } 73 public void emitNotification(Notification n) { service.sendNotification(n); } 74 }); 75 } 76 77 81 85 public void setSeverity(String type, int severity) 86 { 87 setSeverity(mbeanImpl.getMBeanName(), type, severity); 88 } 89 90 94 public void setSeverity(ObjectName name, String type, int severity) 95 { 96 synchronized (this) 97 { 98 Map typeMap = getTypeMap(name); 100 101 Severity s = (Severity)typeMap.get(type); 102 if (s == null) 103 { 104 typeMap.put(type, new Severity(severity)); 105 } 106 else 107 { 108 s.severity = severity; 109 } 110 } 111 } 112 113 116 public int getSeverity(String type) 117 { 118 return getSeverity(mbeanImpl.getMBeanName(), type); 119 } 120 121 124 public int getSeverity(ObjectName name, String type) 125 { 126 synchronized (this) 127 { 128 Map typeMap = (Map )nameMap.get(name); 129 if (typeMap == null) 130 { 131 return Alarm.SEVERITY_NORMAL; 132 } 133 else 134 { 135 Severity s = (Severity)typeMap.get(type); 136 if (s == null) 137 { 138 return Alarm.SEVERITY_NORMAL; 139 } 140 else 141 { 142 return s.severity; 143 } 144 } 145 } 146 } 147 148 152 public String getSeverityAsString(String type) 153 { 154 return getSeverityAsString(mbeanImpl.getMBeanName(), type); 155 } 156 157 161 public String getSeverityAsString(ObjectName name, String type) 162 { 163 return Alarm.SEVERITY_STRINGS[getSeverity(name, type)]; 164 } 165 166 172 public void setAlarm(String type, int severity, String message, Object userData) 173 { 174 setAlarm(mbeanImpl.getMBeanName(), type, severity, message, userData); 175 } 176 177 183 public void setAlarm(ObjectName target, String type, int severity, String message, Object userData) 184 { 185 Severity s; 186 synchronized (this) 187 { 188 Map typeMap = getTypeMap(target); 189 s = (Severity)typeMap.get(type); 190 191 if (s == null) 193 { 194 s = new Severity(Alarm.SEVERITY_NORMAL); 195 typeMap.put(type, s); 196 } 197 } 198 199 int oldSeverity = s.severity; 203 204 if (severity != oldSeverity) 206 { 207 s.severity = severity; 209 210 if (severity == Alarm.SEVERITY_NORMAL) 211 { 212 sendAlarmNotification( 213 target, type, severity, Alarm.STATE_CLEARED, message, userData); 214 } 215 else if (oldSeverity == Alarm.SEVERITY_NORMAL) 216 { 217 sendAlarmNotification( 218 target, type, severity, Alarm.STATE_CREATED, message, userData); 219 } 220 else 221 { 222 sendAlarmNotification( 223 target, type, severity, Alarm.STATE_CHANGED, message, userData); 224 } 225 } 226 } 227 228 237 public void setAlarm(String type, int severity, String message, String key, Object value) 238 { 239 setAlarm(mbeanImpl.getMBeanName(), type, severity, message, key, value); 240 } 241 242 251 public void setAlarm(ObjectName target, String type, int severity, String message, String key, Object value) 252 { 253 HashMap map = new HashMap (); 254 map.put(key, value); 255 setAlarm(target, type, severity, message, map); 256 } 257 258 262 268 public void sendAlarm(String type, int severity, String message, String key, Object value) 269 { 270 sendAlarm(null, type, severity, message, key, value); 271 } 272 273 279 public void sendAlarm(ObjectName target, String type, int severity, String message, String key, Object value) 280 { 281 HashMap map = new HashMap (); 282 map.put(key, value); 283 sendAlarm(target, type, severity, message, map); 284 } 285 286 292 public void sendAlarm(String type, int severity, String message, Object userData) 293 { 294 sendAlarm(null, type, severity, message, userData); 295 } 296 297 303 public void sendAlarm( 304 ObjectName target, String type, int severity, String message, Object userData) 305 { 306 sendAlarmNotification(target, type, severity, Alarm.STATE_NONE, message, userData); 307 } 308 309 317 protected void sendAlarmNotification( 318 ObjectName target, String type, int severity, int alarmState, String message, Object userData) 319 { 320 Notification n = new AlarmNotification( 321 type, 322 mbeanImpl.getMBeanName(), target, 324 severity, 325 alarmState, 326 this.mbeanImpl.getSequenceNumber(), 327 System.currentTimeMillis(), 328 message 329 ); 330 n.setUserData(userData); 331 332 mbeanImpl.emitNotification(n); 334 } 335 336 342 public void sendAttributeChangeNotification( 343 String type, String message, Object userData, 344 String attributeName, String attributeType, 345 Object oldValue, Object newValue) 346 { 347 Notification n = new AttributeChangeNotification ( 348 mbeanImpl.getMBeanName(), mbeanImpl.getSequenceNumber(), 350 System.currentTimeMillis(), 351 message, 352 attributeName, 353 attributeType, 354 oldValue, 355 newValue 356 ); 357 n.setUserData(userData); 358 359 mbeanImpl.emitNotification(n); 361 } 362 363 369 public void sendNotification(String type, String message, Object userData) 370 { 371 Notification n = new Notification ( 372 type, 373 mbeanImpl.getMBeanName(), mbeanImpl.getSequenceNumber(), 375 System.currentTimeMillis(), 376 message 377 ); 378 n.setUserData(userData); 379 380 mbeanImpl.emitNotification(n); 382 } 383 384 388 public void clear() 389 { 390 synchronized (this) 391 { 392 for (Iterator i = nameMap.entrySet().iterator(); i.hasNext(); ) 393 { 394 Map.Entry entry = (Map.Entry )i.next(); 395 Map typeMap = (Map )entry.getValue(); 396 typeMap.clear(); 397 } 398 nameMap.clear(); 399 } 400 } 401 402 404 407 private Map getTypeMap(ObjectName name) 408 { 409 Map typeMap = (Map )nameMap.get(name); 410 if (typeMap == null) 411 { 412 typeMap = new HashMap (); 413 nameMap.put(name, typeMap); 414 } 415 return typeMap; 416 } 417 418 420 423 private static class Severity 424 { 425 public int severity; 426 427 public Severity(int severity) 428 { 429 this.severity = severity; 430 } 431 } 432 433 } 434 | Popular Tags |