KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > monitor > alarm > AlarmManager


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.alarm;
23
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import javax.management.AttributeChangeNotification JavaDoc;
29 import javax.management.Notification JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31
32 import org.jboss.system.ServiceMBeanSupport;
33
34 /**
35  * AlarmManager
36  *
37  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
38  * @version $Revision: 37459 $
39  */

40 public class AlarmManager
41 {
42    // Private/Protected Data ----------------------------------------
43

44    /** Mediates the related MBean */
45    protected MBeanImplAccess mbeanImpl;
46    
47    /** Holds map of maps, each one containing type --> severity mappings */
48    private Map JavaDoc nameMap;
49    
50    // Constructors --------------------------------------------------
51

52    /**
53     * CTOR
54     *
55     * @param mbeanImpl providing access to notification broadcasting
56     */

57    public AlarmManager(MBeanImplAccess mbeanImpl)
58    {
59       this.mbeanImpl = mbeanImpl;
60       this.nameMap = new HashMap JavaDoc();
61    }
62    
63    /**
64     * CTOR
65     *
66     * @param service hosting the AlarmManager
67     */

68    public AlarmManager(final ServiceMBeanSupport service)
69    {
70       this(new MBeanImplAccess() {
71          public ObjectName JavaDoc getMBeanName() { return service.getServiceName(); }
72          public long getSequenceNumber() { return service.nextNotificationSequenceNumber(); }
73          public void emitNotification(Notification JavaDoc n) { service.sendNotification(n); }
74       });
75    }
76
77    // High-level part of the interface used to support Statefull Alarms.
78
// The sending of the actual AlarmNotifications is done through the
79
// sendAlarmNotification() method.
80

81    /**
82     * Sets the severity of an Alarm, keyed by its type, without producing
83     * an AlarmNotification, for the current mbean.
84     */

85    public void setSeverity(String JavaDoc type, int severity)
86    {
87       setSeverity(mbeanImpl.getMBeanName(), type, severity);
88    }
89
90    /**
91     * Sets the severity of an Alarm, keyed by its type, without producing
92     * an AlarmNotification, for the specified mbean.
93     */

94    public void setSeverity(ObjectName JavaDoc name, String JavaDoc type, int severity)
95    {
96       synchronized (this)
97       {
98          // find or add TypeMap
99
Map JavaDoc 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    /**
114     * Gets the severity of an alarm, keyed by its type, for the current mbean.
115     */

116    public int getSeverity(String JavaDoc type)
117    {
118       return getSeverity(mbeanImpl.getMBeanName(), type);
119    }
120    
121    /**
122     * Gets the severity of an alarm, keyed by its type, for the specified mbean.
123     */

124    public int getSeverity(ObjectName JavaDoc name, String JavaDoc type)
125    {
126       synchronized (this)
127       {
128          Map JavaDoc typeMap = (Map JavaDoc)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    /**
149     * Gets the severity of an alarm as a String,
150     * keyed by its type for the current mbean
151     */

152    public String JavaDoc getSeverityAsString(String JavaDoc type)
153    {
154       return getSeverityAsString(mbeanImpl.getMBeanName(), type);
155    }
156    
157    /**
158     * Gets the severity of an alarm as a String,
159     * keyed by its type for the specified mbean
160     */

161    public String JavaDoc getSeverityAsString(ObjectName JavaDoc name, String JavaDoc type)
162    {
163       return Alarm.SEVERITY_STRINGS[getSeverity(name, type)];
164    }
165
166    /**
167     * Sets the alarm for the current mbean, keyed by its type.
168     * If severity has changed an AlarmNotification will be thrown.
169     * The alarmState of the AlarmNotification will be either
170     * Alarm.STATE_CREATED, Alarm.STATE_CHANGED or Alarm.STATE_CLEARED.
171     */

172    public void setAlarm(String JavaDoc type, int severity, String JavaDoc message, Object JavaDoc userData)
173    {
174       setAlarm(mbeanImpl.getMBeanName(), type, severity, message, userData);
175    }
176    
177    /**
178     * Sets the alarm for the specified target mbean, keyed by its type.
179     * If severity has changed an AlarmNotification will be thrown.
180     * The alarmState of the AlarmNotification will be either
181     * Alarm.STATE_CREATED, Alarm.STATE_CHANGED or Alarm.STATE_CLEARED.
182     */

183    public void setAlarm(ObjectName JavaDoc target, String JavaDoc type, int severity, String JavaDoc message, Object JavaDoc userData)
184    {
185       Severity s;
186       synchronized (this)
187       {
188          Map JavaDoc typeMap = getTypeMap(target);
189          s = (Severity)typeMap.get(type);
190          
191          // if alarm does not exist, add it with a default severity
192
if (s == null)
193          {
194             s = new Severity(Alarm.SEVERITY_NORMAL);
195             typeMap.put(type, s);
196          }
197       }
198       
199       // There must be a small race condition here if 2 threads
200
// set the same severity, thus producing duplicate notifications
201
// Not a big deal...
202
int oldSeverity = s.severity;
203       
204       // if the severity has changed, send an AlarmNotification
205
if (severity != oldSeverity)
206       {
207          // store the new severity
208
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    /**
229     * See set Alarm above
230     *
231     * Essentially a helper method that will populate the userData field
232     * of the Notification with a HashMap, containing a single key/value pair.
233     *
234     * Note, that an AlarmNotification will not be emitted if there is no
235     * severity change.
236     */

237    public void setAlarm(String JavaDoc type, int severity, String JavaDoc message, String JavaDoc key, Object JavaDoc value)
238    {
239       setAlarm(mbeanImpl.getMBeanName(), type, severity, message, key, value);
240    }
241    
242    /**
243     * See set Alarm above
244     *
245     * Essentially a helper method that will populate the userData field
246     * of the Notification with a HashMap, containing a single key/value pair.
247     *
248     * Note, that an AlarmNotification will not be thrown if there is no
249     * severity change.
250     */

251    public void setAlarm(ObjectName JavaDoc target, String JavaDoc type, int severity, String JavaDoc message, String JavaDoc key, Object JavaDoc value)
252    {
253       HashMap JavaDoc map = new HashMap JavaDoc();
254       map.put(key, value);
255       setAlarm(target, type, severity, message, map);
256    }
257    
258    // Low-level part of the interface used to generate and send
259
// various types of notifications, including AlarmNotifications
260
// corresponding to Stateless Alarms.
261

262    /**
263     * Generates and sends an AlarmNotification.
264     *
265     * source, sequenceNumber, timeStamp
266     * will be automatically filled.
267     */

268    public void sendAlarm(String JavaDoc type, int severity, String JavaDoc message, String JavaDoc key, Object JavaDoc value)
269    {
270       sendAlarm(null, type, severity, message, key, value);
271    }
272    
273    /**
274     * Generates and sends an AlarmNotification.
275     *
276     * source, sequenceNumber, timeStamp
277     * will be automatically filled.
278     */

279    public void sendAlarm(ObjectName JavaDoc target, String JavaDoc type, int severity, String JavaDoc message, String JavaDoc key, Object JavaDoc value)
280    {
281       HashMap JavaDoc map = new HashMap JavaDoc();
282       map.put(key, value);
283       sendAlarm(target, type, severity, message, map);
284    }
285    
286    /**
287     * Generates and sends an AlarmNotification.
288     *
289     * source, sequenceNumber, timeStamp
290     * will be automatically filled.
291     */

292    public void sendAlarm(String JavaDoc type, int severity, String JavaDoc message, Object JavaDoc userData)
293    {
294       sendAlarm(null, type, severity, message, userData);
295    }
296    
297    /**
298     * Generates and sends an AlarmNotification.
299     *
300     * source, sequenceNumber, timeStamp
301     * will be automatically filled.
302     */

303    public void sendAlarm(
304       ObjectName JavaDoc target, String JavaDoc type, int severity, String JavaDoc message, Object JavaDoc userData)
305    {
306       sendAlarmNotification(target, type, severity, Alarm.STATE_NONE, message, userData);
307    }
308    
309    /**
310     * Generates and sends an AlarmNotification.
311     *
312     * An alarmState of Alarm.STATE_CLEARED forces severity to SEVERITY_NORMAL
313     *
314     * source, sequenceNumber, timeStamp
315     * will be automatically filled.
316     */

317    protected void sendAlarmNotification(
318       ObjectName JavaDoc target, String JavaDoc type, int severity, int alarmState, String JavaDoc message, Object JavaDoc userData)
319    {
320       Notification JavaDoc n = new AlarmNotification(
321          type,
322          mbeanImpl.getMBeanName(), // source
323
target,
324          severity,
325          alarmState,
326          this.mbeanImpl.getSequenceNumber(),
327          System.currentTimeMillis(),
328          message
329       );
330       n.setUserData(userData);
331       
332       // send it away
333
mbeanImpl.emitNotification(n);
334    }
335    
336    /**
337     * Generates and sends an AttributeChangeNotification.
338     *
339     * source, sequenceNumber, timeStamp
340     * will be automatically filled in.
341     */

342    public void sendAttributeChangeNotification(
343       String JavaDoc type, String JavaDoc message, Object JavaDoc userData,
344       String JavaDoc attributeName, String JavaDoc attributeType,
345       Object JavaDoc oldValue, Object JavaDoc newValue)
346    {
347       Notification JavaDoc n = new AttributeChangeNotification JavaDoc(
348          mbeanImpl.getMBeanName(), // source
349
mbeanImpl.getSequenceNumber(),
350          System.currentTimeMillis(),
351          message,
352          attributeName,
353          attributeType,
354          oldValue,
355          newValue
356       );
357       n.setUserData(userData);
358       
359       // send it away
360
mbeanImpl.emitNotification(n);
361    }
362    
363    /**
364     * Generates and sends a simple Notification.
365     *
366     * source, sequenceNumber, timeStamp
367     * will be automatically filled in.
368     */

369    public void sendNotification(String JavaDoc type, String JavaDoc message, Object JavaDoc userData)
370    {
371       Notification JavaDoc n = new Notification JavaDoc(
372          type,
373          mbeanImpl.getMBeanName(), // source
374
mbeanImpl.getSequenceNumber(),
375          System.currentTimeMillis(),
376          message
377       );
378       n.setUserData(userData);
379       
380       // send it away
381
mbeanImpl.emitNotification(n);
382    }
383    
384    /**
385     * Clear all the stored severities
386     *
387     */

388    public void clear()
389    {
390       synchronized (this)
391       {
392          for (Iterator JavaDoc i = nameMap.entrySet().iterator(); i.hasNext(); )
393          {
394             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
395             Map JavaDoc typeMap = (Map JavaDoc)entry.getValue();
396             typeMap.clear();
397          }
398          nameMap.clear();
399       }
400    }
401    
402    // Private -------------------------------------------------------
403

404    /**
405     * Return the typeMap for an ObjectName, create it if needed
406     */

407    private Map JavaDoc getTypeMap(ObjectName JavaDoc name)
408    {
409       Map JavaDoc typeMap = (Map JavaDoc)nameMap.get(name);
410       if (typeMap == null)
411       {
412          typeMap = new HashMap JavaDoc();
413          nameMap.put(name, typeMap);
414       }
415       return typeMap;
416    }
417    
418    // Inner Class ---------------------------------------------------
419

420    /**
421     * Simple Data Holder
422     */

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