1 22 package org.jboss.monitor.services; 23 24 import javax.management.Notification ; 25 26 import org.jboss.logging.DynamicLogger; 27 import org.jboss.monitor.alarm.Alarm; 28 import org.jboss.monitor.alarm.AlarmHelper; 29 import org.jboss.monitor.alarm.AlarmNotification; 30 import org.jboss.monitor.alarm.AlarmTable; 31 import org.jboss.monitor.alarm.AlarmTableNotification; 32 import org.jboss.system.ListenerServiceMBeanSupport; 33 import org.jboss.util.Strings; 34 35 import EDU.oswego.cs.dl.util.concurrent.SynchronizedLong; 36 37 46 public class ActiveAlarmTable extends ListenerServiceMBeanSupport 47 implements ActiveAlarmTableMBean 48 { 49 50 protected static final DynamicLogger log = DynamicLogger.getDynamicLogger(ActiveAlarmTable.class); 51 52 54 55 private SynchronizedLong notificationCount; 56 57 58 AlarmTable almtab = new AlarmTable(this); 59 60 62 65 public ActiveAlarmTable() 66 { 67 notificationCount = new SynchronizedLong(0); 68 almtab.setServerId(Alarm.DEFAULT_SERVER_ID); 69 almtab.setMaxSize(1000); 70 } 71 72 74 75 78 public int getActiveAlarmCount() 79 { 80 return almtab.getAlarmSize(); 81 } 82 83 86 public long getNotificationsReceived() 87 { 88 return notificationCount.get(); 89 } 90 91 94 public void setServerId(String serverId) 95 { 96 if (serverId != null) 97 { 98 almtab.setServerId(serverId); 99 } 100 } 101 102 105 public void setMaxTableSize(int maxSize) 106 { 107 almtab.setMaxSize(maxSize); 108 } 109 110 113 public int getMaxTableSize() 114 { 115 return almtab.getMaxSize(); 116 } 117 118 121 public String getServerId() 122 { 123 return almtab.getServerId(); 124 } 125 126 129 public void setLogLevel(String logLevel) 130 { 131 log.setLogLevelAsString(logLevel); 132 } 133 134 137 public String getLogLevel() 138 { 139 return log.getLogLevelAsString(); 140 } 141 142 144 151 public int acknowledgeAll(String user, String system) 152 { 153 AlarmTableNotification[] atns = almtab.getAlarmTable(); 154 int acked = 0; 155 156 for (int i = 0; i < atns.length; ++i) 157 { 158 AlarmTableNotification atn = atns[i]; 159 String alarmId = atn.getAlarmId(); 160 if (almtab.acknowledge(alarmId, user, system)) 161 { 162 ++acked; 163 } 164 } 165 return acked; 166 } 167 168 175 public int unacknowledgeAll(String user, String system) 176 { 177 AlarmTableNotification[] atns = almtab.getAlarmTable(); 178 int unacked = 0; 179 180 for (int i = 0; i < atns.length; ++i) 181 { 182 AlarmTableNotification atn = atns[i]; 183 String alarmId = atn.getAlarmId(); 184 if (almtab.unacknowledge(alarmId, user, system)) 185 { 186 ++unacked; 187 } 188 } 189 return unacked; 190 } 191 192 200 public boolean acknowledge(String alarmId, String user, String system) 201 { 202 return almtab.acknowledge(alarmId, user, system); 203 } 204 205 213 public boolean unacknowledge(String alarmId, String user, String system) 214 { 215 return almtab.unacknowledge(alarmId, user, system); 216 } 217 218 223 public AlarmTableNotification[] fetchAlarmTable() 224 { 225 return this.almtab.getAlarmTable(); 226 } 227 228 233 public String fetchAlarmTableAsHtml() 234 { 235 AlarmTableNotification[] tab = almtab.getAlarmTable(); 236 237 StringBuffer sbuf = new StringBuffer (8192); 238 239 sbuf.append("<p>Number of Alarms: ").append(tab.length).append("</p>").append("\n"); 240 sbuf.append("<table border=\"1\">").append("\n"); 241 sbuf.append("<tr>"); 242 sbuf.append("<th width=\"20%\">AlarmInfo</th>"); 243 sbuf.append("<th>NotificationInfo</th>"); 244 sbuf.append("</tr>").append("\n"); 245 246 for (int i = 0; i < tab.length; i++) 247 { 248 AlarmTableNotification atn = tab[i]; 249 Notification n = (Notification )atn.getUserData(); 250 Object source = AlarmNotification.getEffectiveSource(n); 251 252 sbuf.append("<tr>"); 253 sbuf.append("<td>") 254 .append("alarmId: ").append(atn.getAlarmId()).append("<br><br>") 255 .append("severity: ").append(AlarmHelper.getSeverityAsString(atn.getSeverity())).append("<br>") 256 .append("alarmState: ").append(AlarmHelper.getStateAsString(atn.getAlarmState())).append("<br><br>") 257 .append("ackState: ").append(atn.getAckState()).append("<br><br>") 258 .append("ackTime: ").append(atn.getAckTime()).append("<br>") 259 .append("ackUser: ").append(atn.getAckUser()).append("<br>") 260 .append("ackSystem: ").append(atn.getAckSystem()).append("</td>"); 261 sbuf.append("<td>") 262 .append("source: ").append(source).append("<br>") 263 .append("type: ").append(n.getType()).append("<br>") 264 .append("timeStamp: ").append(n.getTimeStamp()).append("<br>") 265 .append("sequenceNumber: ").append(n.getSequenceNumber()).append("<br><br>") 266 .append("message: ").append(substNewLines(n.getMessage())).append("<br><br>") 267 .append("userData: ").append(substNewLines(n.getUserData())).append("</td>"); 268 sbuf.append("</tr>").append("\n"); 269 } 270 sbuf.append("</table>").append("\n"); 271 272 return sbuf.toString(); 273 } 274 275 277 280 public void startService() throws Exception 281 { 282 super.subscribe(true); 284 } 285 286 289 public void stopService() throws Exception 290 { 291 super.unsubscribe(); 293 } 294 295 297 300 public void handleNotification2(Notification notification, Object handback) 301 { 302 log.log("Got notification (#" + Long.toString(this.notificationCount.increment()) 303 + "): " + notification + ", handback: " + handback); 304 305 almtab.update(notification); 306 } 307 308 310 313 protected String substNewLines(Object input) 314 { 315 if (input == null) 316 { 317 return "null"; 318 } 319 else 320 { 321 return Strings.subst("\n", "<br>", input.toString()); 322 } 323 } 324 } 325 | Popular Tags |