KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > monitor > services > ActiveAlarmTable


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.monitor.services;
23
24 import javax.management.Notification JavaDoc;
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 /**
38  * ActiveAlarmTable
39  *
40  * @jmx:mbean
41  * extends="org.jboss.system.ListenerServiceMBean"
42  *
43  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
44  * @version $Revision: 37459 $
45  */

46 public class ActiveAlarmTable extends ListenerServiceMBeanSupport
47    implements ActiveAlarmTableMBean
48 {
49    /** DynamicLogger */
50    protected static final DynamicLogger log = DynamicLogger.getDynamicLogger(ActiveAlarmTable.class);
51    
52    // Private Data --------------------------------------------------
53

54    /** Number of processed JMX notifications */
55    private SynchronizedLong notificationCount;
56    
57    /** alarm table */
58    AlarmTable almtab = new AlarmTable(this);
59       
60    // Constructors --------------------------------------------------
61

62    /**
63     * CTOR
64     */

65    public ActiveAlarmTable()
66    {
67       notificationCount = new SynchronizedLong(0);
68       almtab.setServerId(Alarm.DEFAULT_SERVER_ID);
69       almtab.setMaxSize(1000);
70    }
71    
72    // Attributes ----------------------------------------------------
73

74    
75    /**
76     * @jmx:managed-attribute
77     */

78    public int getActiveAlarmCount()
79    {
80       return almtab.getAlarmSize();
81    }
82    
83    /**
84     * @jmx:managed-attribute
85     */

86    public long getNotificationsReceived()
87    {
88       return notificationCount.get();
89    }
90
91    /**
92     * @jmx:managed-attribute
93     */

94    public void setServerId(String JavaDoc serverId)
95    {
96       if (serverId != null)
97       {
98          almtab.setServerId(serverId);
99       }
100    }
101    
102    /**
103     * @jmx:managed-attribute
104     */

105    public void setMaxTableSize(int maxSize)
106    {
107       almtab.setMaxSize(maxSize);
108    }
109    
110    /**
111     * @jmx:managed-attribute
112     */

113    public int getMaxTableSize()
114    {
115       return almtab.getMaxSize();
116    }
117    
118    /**
119     * @jmx:managed-attribute
120     */

121    public String JavaDoc getServerId()
122    {
123       return almtab.getServerId();
124    }
125    
126    /**
127     * @jmx:managed-attribute
128     */

129    public void setLogLevel(String JavaDoc logLevel)
130    {
131       log.setLogLevelAsString(logLevel);
132    }
133    
134    /**
135     * @jmx:managed-attribute
136     */

137    public String JavaDoc getLogLevel()
138    {
139       return log.getLogLevelAsString();
140    }
141    
142    // Operations ----------------------------------------------------
143

144    /**
145     * Acknowledge all
146     *
147     * @jmx:managed-operation
148     *
149     * @return number of acknowledged alarms
150     */

151    public int acknowledgeAll(String JavaDoc user, String JavaDoc 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 JavaDoc alarmId = atn.getAlarmId();
160          if (almtab.acknowledge(alarmId, user, system))
161          {
162             ++acked;
163          }
164       }
165       return acked;
166    }
167
168    /**
169     * Uncknowledge all
170     *
171     * @jmx:managed-operation
172     *
173     * @return number of unacknowledged alarms
174     */

175    public int unacknowledgeAll(String JavaDoc user, String JavaDoc 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 JavaDoc alarmId = atn.getAlarmId();
184          if (almtab.unacknowledge(alarmId, user, system))
185          {
186             ++unacked;
187          }
188       }
189       return unacked;
190    }
191    
192    /**
193     * Acknowledge an Alarm
194     *
195     * @jmx:managed-operation
196     *
197     * @return true if ack was succesful, false otherwise
198     * (not in table or acked already)
199     */

200    public boolean acknowledge(String JavaDoc alarmId, String JavaDoc user, String JavaDoc system)
201    {
202       return almtab.acknowledge(alarmId, user, system);
203    }
204
205    /**
206     * Unacknowledge an Alarm
207     *
208     * @jmx:managed-operation
209     *
210     * @return true if unack was succesful, false otherwise
211     * (not in table or unacked already)
212     */

213    public boolean unacknowledge(String JavaDoc alarmId, String JavaDoc user, String JavaDoc system)
214    {
215       return almtab.unacknowledge(alarmId, user, system);
216    }
217    
218    /**
219     * Gets the ActiveAlarmTable
220     *
221     * @jmx:managed-operation
222     */

223    public AlarmTableNotification[] fetchAlarmTable()
224    {
225       return this.almtab.getAlarmTable();
226    }
227    
228    /**
229     * Gets the ActiveAlarmTable as Html
230     *
231     * @jmx:managed-operation
232     */

233    public String JavaDoc fetchAlarmTableAsHtml()
234    {
235       AlarmTableNotification[] tab = almtab.getAlarmTable();
236       
237       StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc(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 JavaDoc n = (Notification JavaDoc)atn.getUserData();
250          Object JavaDoc 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    // Lifecycle control (ServiceMBeanSupport) -----------------------
276

277    /**
278     * Start
279     */

280    public void startService() throws Exception JavaDoc
281    {
282       // subsbscribe myself for notifications
283
super.subscribe(true);
284    }
285    
286    /**
287     * Stop
288     */

289    public void stopService() throws Exception JavaDoc
290    {
291       // unsubscribe for notifications
292
super.unsubscribe();
293    }
294    
295    // ListenerServiceMBeanSupport -----------------------------------
296

297    /**
298     * Overriden to add handling!
299     */

300    public void handleNotification2(Notification JavaDoc notification, Object JavaDoc handback)
301    {
302       log.log("Got notification (#" + Long.toString(this.notificationCount.increment())
303              + "): " + notification + ", handback: " + handback);
304       
305       almtab.update(notification);
306    }
307    
308    // Protected -----------------------------------------------------
309

310    /**
311     * Convert every occurence of "\n" to "<br>"
312     */

313    protected String JavaDoc substNewLines(Object JavaDoc 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