001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.monitor.services;
023:
024: import javax.management.Notification;
025:
026: import org.jboss.logging.DynamicLogger;
027: import org.jboss.monitor.alarm.Alarm;
028: import org.jboss.monitor.alarm.AlarmHelper;
029: import org.jboss.monitor.alarm.AlarmNotification;
030: import org.jboss.monitor.alarm.AlarmTable;
031: import org.jboss.monitor.alarm.AlarmTableNotification;
032: import org.jboss.system.ListenerServiceMBeanSupport;
033: import org.jboss.util.Strings;
034:
035: import EDU.oswego.cs.dl.util.concurrent.SynchronizedLong;
036:
037: /**
038: * ActiveAlarmTable
039: *
040: * @jmx:mbean
041: * extends="org.jboss.system.ListenerServiceMBean"
042: *
043: * @author <a href="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
044: * @version $Revision: 57210 $
045: */
046: public class ActiveAlarmTable extends ListenerServiceMBeanSupport
047: implements ActiveAlarmTableMBean {
048: /** DynamicLogger */
049: protected static final DynamicLogger log = DynamicLogger
050: .getDynamicLogger(ActiveAlarmTable.class);
051:
052: // Private Data --------------------------------------------------
053:
054: /** Number of processed JMX notifications */
055: private SynchronizedLong notificationCount;
056:
057: /** alarm table */
058: AlarmTable almtab = new AlarmTable(this );
059:
060: // Constructors --------------------------------------------------
061:
062: /**
063: * CTOR
064: */
065: public ActiveAlarmTable() {
066: notificationCount = new SynchronizedLong(0);
067: almtab.setServerId(Alarm.DEFAULT_SERVER_ID);
068: almtab.setMaxSize(1000);
069: }
070:
071: // Attributes ----------------------------------------------------
072:
073: /**
074: * @jmx:managed-attribute
075: */
076: public int getActiveAlarmCount() {
077: return almtab.getAlarmSize();
078: }
079:
080: /**
081: * @jmx:managed-attribute
082: */
083: public long getNotificationsReceived() {
084: return notificationCount.get();
085: }
086:
087: /**
088: * @jmx:managed-attribute
089: */
090: public void setServerId(String serverId) {
091: if (serverId != null) {
092: almtab.setServerId(serverId);
093: }
094: }
095:
096: /**
097: * @jmx:managed-attribute
098: */
099: public void setMaxTableSize(int maxSize) {
100: almtab.setMaxSize(maxSize);
101: }
102:
103: /**
104: * @jmx:managed-attribute
105: */
106: public int getMaxTableSize() {
107: return almtab.getMaxSize();
108: }
109:
110: /**
111: * @jmx:managed-attribute
112: */
113: public String getServerId() {
114: return almtab.getServerId();
115: }
116:
117: /**
118: * @jmx:managed-attribute
119: */
120: public void setLogLevel(String logLevel) {
121: log.setLogLevelAsString(logLevel);
122: }
123:
124: /**
125: * @jmx:managed-attribute
126: */
127: public String getLogLevel() {
128: return log.getLogLevelAsString();
129: }
130:
131: // Operations ----------------------------------------------------
132:
133: /**
134: * Acknowledge all
135: *
136: * @jmx:managed-operation
137: *
138: * @return number of acknowledged alarms
139: */
140: public int acknowledgeAll(String user, String system) {
141: AlarmTableNotification[] atns = almtab.getAlarmTable();
142: int acked = 0;
143:
144: for (int i = 0; i < atns.length; ++i) {
145: AlarmTableNotification atn = atns[i];
146: String alarmId = atn.getAlarmId();
147: if (almtab.acknowledge(alarmId, user, system)) {
148: ++acked;
149: }
150: }
151: return acked;
152: }
153:
154: /**
155: * Uncknowledge all
156: *
157: * @jmx:managed-operation
158: *
159: * @return number of unacknowledged alarms
160: */
161: public int unacknowledgeAll(String user, String system) {
162: AlarmTableNotification[] atns = almtab.getAlarmTable();
163: int unacked = 0;
164:
165: for (int i = 0; i < atns.length; ++i) {
166: AlarmTableNotification atn = atns[i];
167: String alarmId = atn.getAlarmId();
168: if (almtab.unacknowledge(alarmId, user, system)) {
169: ++unacked;
170: }
171: }
172: return unacked;
173: }
174:
175: /**
176: * Acknowledge an Alarm
177: *
178: * @jmx:managed-operation
179: *
180: * @return true if ack was succesful, false otherwise
181: * (not in table or acked already)
182: */
183: public boolean acknowledge(String alarmId, String user,
184: String system) {
185: return almtab.acknowledge(alarmId, user, system);
186: }
187:
188: /**
189: * Unacknowledge an Alarm
190: *
191: * @jmx:managed-operation
192: *
193: * @return true if unack was succesful, false otherwise
194: * (not in table or unacked already)
195: */
196: public boolean unacknowledge(String alarmId, String user,
197: String system) {
198: return almtab.unacknowledge(alarmId, user, system);
199: }
200:
201: /**
202: * Gets the ActiveAlarmTable
203: *
204: * @jmx:managed-operation
205: */
206: public AlarmTableNotification[] fetchAlarmTable() {
207: return this .almtab.getAlarmTable();
208: }
209:
210: /**
211: * Gets the ActiveAlarmTable as Html
212: *
213: * @jmx:managed-operation
214: */
215: public String fetchAlarmTableAsHtml() {
216: AlarmTableNotification[] tab = almtab.getAlarmTable();
217:
218: StringBuffer sbuf = new StringBuffer(8192);
219:
220: sbuf.append("<p>Number of Alarms: ").append(tab.length).append(
221: "</p>").append("\n");
222: sbuf.append("<table border=\"1\">").append("\n");
223: sbuf.append("<tr>");
224: sbuf.append("<th width=\"20%\">AlarmInfo</th>");
225: sbuf.append("<th>NotificationInfo</th>");
226: sbuf.append("</tr>").append("\n");
227:
228: for (int i = 0; i < tab.length; i++) {
229: AlarmTableNotification atn = tab[i];
230: Notification n = (Notification) atn.getUserData();
231: Object source = AlarmNotification.getEffectiveSource(n);
232:
233: sbuf.append("<tr>");
234: sbuf.append("<td>").append("alarmId: ").append(
235: atn.getAlarmId()).append("<br><br>").append(
236: "severity: ").append(
237: AlarmHelper.getSeverityAsString(atn.getSeverity()))
238: .append("<br>").append("alarmState: ").append(
239: AlarmHelper.getStateAsString(atn
240: .getAlarmState())).append(
241: "<br><br>").append("ackState: ").append(
242: atn.getAckState()).append("<br><br>")
243: .append("ackTime: ").append(atn.getAckTime())
244: .append("<br>").append("ackUser: ").append(
245: atn.getAckUser()).append("<br>").append(
246: "ackSystem: ").append(atn.getAckSystem())
247: .append("</td>");
248: sbuf.append("<td>").append("source: ").append(source)
249: .append("<br>").append("type: ")
250: .append(n.getType()).append("<br>").append(
251: "timeStamp: ").append(n.getTimeStamp())
252: .append("<br>").append("sequenceNumber: ").append(
253: n.getSequenceNumber()).append("<br><br>")
254: .append("message: ").append(
255: substNewLines(n.getMessage())).append(
256: "<br><br>").append("userData: ").append(
257: substNewLines(n.getUserData())).append(
258: "</td>");
259: sbuf.append("</tr>").append("\n");
260: }
261: sbuf.append("</table>").append("\n");
262:
263: return sbuf.toString();
264: }
265:
266: // Lifecycle control (ServiceMBeanSupport) -----------------------
267:
268: /**
269: * Start
270: */
271: public void startService() throws Exception {
272: // subsbscribe myself for notifications
273: super .subscribe(true);
274: }
275:
276: /**
277: * Stop
278: */
279: public void stopService() throws Exception {
280: // unsubscribe for notifications
281: super .unsubscribe();
282: }
283:
284: // ListenerServiceMBeanSupport -----------------------------------
285:
286: /**
287: * Overriden to add handling!
288: */
289: public void handleNotification2(Notification notification,
290: Object handback) {
291: log.log("Got notification (#"
292: + Long.toString(this .notificationCount.increment())
293: + "): " + notification + ", handback: " + handback);
294:
295: almtab.update(notification);
296: }
297:
298: // Protected -----------------------------------------------------
299:
300: /**
301: * Convert every occurence of "\n" to "<br>"
302: */
303: protected String substNewLines(Object input) {
304: if (input == null) {
305: return "null";
306: } else {
307: return Strings.subst("\n", "<br>", input.toString());
308: }
309: }
310: }
|