1 22 package org.jboss.monitor.alarm; 23 24 import java.util.Collection ; 25 import java.util.HashMap ; 26 import java.util.LinkedHashMap ; 27 import java.util.Map ; 28 29 import javax.management.Notification ; 30 import javax.management.ObjectName ; 31 32 import org.jboss.system.ServiceMBeanSupport; 33 34 import EDU.oswego.cs.dl.util.concurrent.SynchronizedLong; 35 36 42 public class AlarmTable 43 { 44 46 47 protected MBeanImplAccess mbeanImpl; 48 49 50 private String serverId; 51 52 53 private SynchronizedLong alarmIdCount; 54 55 56 private Map alarmMap; 57 58 59 private Map statefulMap; 60 61 62 private int maxSize = -1; 63 64 66 69 public AlarmTable(MBeanImplAccess mbeanImpl) 70 { 71 this.mbeanImpl = mbeanImpl; 72 this.alarmIdCount = new SynchronizedLong(0); 73 this.alarmMap = new LinkedHashMap (); 74 this.statefulMap = new HashMap (); 75 } 76 77 82 public AlarmTable(final ServiceMBeanSupport service) 83 { 84 this(new MBeanImplAccess() { 85 public ObjectName getMBeanName() { return service.getServiceName(); } 86 public long getSequenceNumber() { return service.nextNotificationSequenceNumber(); } 87 public void emitNotification(Notification n) { service.sendNotification(n); } 88 }); 89 } 90 91 93 96 public void setServerId(String serverId) 97 { 98 this.serverId = serverId; 99 } 100 101 104 public String getServerId() 105 { 106 return serverId; 107 } 108 109 113 public void setMaxSize(int maxSize) 114 { 115 this.maxSize = maxSize; 116 } 117 118 121 public int getMaxSize() 122 { 123 return maxSize; 124 } 125 126 129 public void update(Notification n) 130 { 131 if (n instanceof AlarmTableNotification) 132 { 133 } 136 else if (n instanceof AlarmNotification) 137 { 138 AlarmNotification an = (AlarmNotification)n; 139 140 if (an.getAlarmState() == Alarm.STATE_NONE) 141 { 142 updateNotificationStateless(n, an.getSeverity()); 143 } 144 else 145 { 146 updateNotificationStatefull(an); 147 } 148 } 149 else 150 { 151 updateNotificationStateless(n, Alarm.SEVERITY_UNKNOWN); 152 } 153 } 154 155 161 public boolean acknowledge(String alarmId, String user, String system) 162 { 163 AlarmTableNotification atn; 164 165 synchronized (this) 166 { 167 AlarmTableNotification entry = 168 (AlarmTableNotification)alarmMap.get(alarmId); 169 170 if (entry == null || entry.getAckState() == true) 171 { 172 return false; } 174 entry.setAckParams(true, System.currentTimeMillis(), user, system); 176 177 atn = new AlarmTableNotification(entry); 179 180 atn.setSequenceNumber(mbeanImpl.getSequenceNumber()); 182 atn.setTimeStamp(System.currentTimeMillis()); 183 184 int alarmState = entry.getAlarmState(); 186 if (alarmState == Alarm.STATE_NONE || alarmState == Alarm.STATE_CLEARED) 187 { 188 alarmMap.remove(alarmId); 189 } 190 } 191 mbeanImpl.emitNotification(atn); 193 194 return true; } 196 197 203 public boolean unacknowledge(String alarmId, String user, String system) 204 { 205 AlarmTableNotification atn; 206 207 synchronized (this) 208 { 209 AlarmTableNotification entry = 210 (AlarmTableNotification)alarmMap.get(alarmId); 211 212 if (entry == null || entry.getAckState() == false) 213 { 214 return false; } 216 entry.setAckParams(false, System.currentTimeMillis(), user, system); 218 219 atn = new AlarmTableNotification(entry); 221 222 atn.setSequenceNumber(mbeanImpl.getSequenceNumber()); 224 atn.setTimeStamp(System.currentTimeMillis()); 225 } 226 mbeanImpl.emitNotification(atn); 228 229 return true; } 231 232 235 public AlarmTableNotification[] getAlarmTable() 236 { 237 synchronized (this) 239 { 240 Collection alarms = alarmMap.values(); 241 AlarmTableNotification[] array = new AlarmTableNotification[alarms.size()]; 242 return (AlarmTableNotification[])alarms.toArray(array); 243 } 244 } 245 246 249 public int getAlarmSize() 250 { 251 synchronized(this) 252 { 253 return alarmMap.size(); 254 } 255 } 256 257 259 263 private void updateNotificationStatefull(AlarmNotification an) 264 { 265 int alarmState = an.getAlarmState(); 266 int severity = an.getSeverity(); 267 268 Object alarmKey = AlarmNotification.createKey(an); 270 271 AlarmTableNotification atn; 272 273 synchronized (this) 275 { 276 String alarmId = (String )statefulMap.get(alarmKey); 277 if (alarmId == null) 278 { 279 if (isMaxSizeReached()) 281 { 282 return; 284 } 285 else 286 { 287 alarmId = generateAlarmId(); 289 } 290 } 291 atn = new AlarmTableNotification( 293 alarmId, 294 AlarmTableNotification.ALARM_TABLE_UPDATE, 295 this.mbeanImpl.getMBeanName(), 296 null, 297 severity, 298 alarmState, 299 this.mbeanImpl.getSequenceNumber(), 300 System.currentTimeMillis(), 301 null 302 ); 303 304 atn.setUserData(an); 306 307 if (alarmState == Alarm.STATE_CLEARED) 311 { 312 AlarmTableNotification entry = 313 (AlarmTableNotification)alarmMap.get(alarmId); 314 315 if (entry != null && entry.getAckState() == true) 316 { 317 statefulMap.remove(alarmKey); 318 alarmMap.remove(alarmId); 319 320 atn.setAckParams(true, entry.getAckTime(), 321 entry.getAckUser(), entry.getAckSystem()); 322 } 323 else 324 { 325 statefulMap.put(alarmKey, alarmId); 327 alarmMap.put(alarmId, atn); 328 } 329 } 330 else 331 { 332 statefulMap.put(alarmKey, alarmId); 334 alarmMap.put(alarmId, atn); 335 } 336 } 337 if (atn.getAckState() == true) 340 { 341 mbeanImpl.emitNotification(atn); 342 } 343 else { 345 mbeanImpl.emitNotification(new AlarmTableNotification(atn)); 346 } 347 } 348 349 352 private void updateNotificationStateless(Notification n, int severity) 353 { 354 synchronized (this) 355 { 356 if (isMaxSizeReached()) 357 { 358 return; 360 } 361 } 362 AlarmTableNotification atn = 364 new AlarmTableNotification( 365 generateAlarmId(), 366 AlarmTableNotification.ALARM_TABLE_UPDATE, 367 this.mbeanImpl.getMBeanName(), 368 null, 369 severity, 370 Alarm.STATE_NONE, 371 this.mbeanImpl.getSequenceNumber(), 372 System.currentTimeMillis(), 373 null 374 ); 375 atn.setUserData(n); 377 378 synchronized (this) 380 { 381 alarmMap.put(atn.getAlarmId(), atn); 382 } 383 384 mbeanImpl.emitNotification(new AlarmTableNotification(atn)); 386 } 387 388 391 private String generateAlarmId() 392 { 393 return serverId + '-' + alarmIdCount.increment(); 394 } 395 396 399 private boolean isMaxSizeReached() 400 { 401 if (maxSize != -1) 402 { 403 return (alarmMap.size() >= maxSize) ? true : false; 404 } 405 else 406 { 407 return false; 408 } 409 } 410 } 411 | Popular Tags |