KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.management.Notification JavaDoc;
25 import javax.management.ObjectName JavaDoc;
26
27 /**
28  * AlarmNotification
29  *
30  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
31  * @version $Revision: 42955 $
32  */

33 public class AlarmNotification extends Notification JavaDoc
34 {
35    // Private Data --------------------------------------------------
36

37    /** @since 4.0.4 */
38    private static final long serialVersionUID = -7041616127511632675L;
39    
40    /** Set when the alarm refers to some other MBean */
41    private ObjectName JavaDoc target;
42    
43    /** The alarm severity */
44    private int severity;
45    
46    /** The alarm state */
47    private int alarmState;
48    
49    // CTORS ---------------------------------------------------------
50

51    /**
52     * Complete CTOR, creates an AlarmNotification object
53     *
54     * Note:
55     * STATE_CLEARED forces severity to SEVERITY_NORMAL
56     * STATE_CREATED/CHANGED have valid severities WARNING to UNKNOWN
57     * STATE_NONE has valid severities NORMAL to UNKNOWN
58     * Also:
59     * Out-of-range states are mapped to STATE_NONE
60     * Out-of-range severities are mapped to SEVERITY_UNKNOWN
61     */

62    public AlarmNotification(
63       String JavaDoc type, Object JavaDoc source,
64       ObjectName JavaDoc target, int severity, int alarmState,
65       long sequenceNumber, long timeStamp, String JavaDoc message
66    )
67    {
68       super(type, source, sequenceNumber, timeStamp, message);
69       
70       this.target = target;
71       
72       switch (alarmState)
73       {
74          case Alarm.STATE_CLEARED:
75             this.alarmState = Alarm.STATE_CLEARED;
76             // forces severity=SEVERITY_NORMAL
77
this.severity = Alarm.SEVERITY_NORMAL;
78             break;
79             
80          case Alarm.STATE_CREATED:
81          case Alarm.STATE_CHANGED:
82             this.alarmState = alarmState;
83             // can't have SEVERITY_NORMAL!
84
if (severity > Alarm.SEVERITY_NORMAL && severity <= Alarm.SEVERITY_UNKNOWN)
85             {
86                this.severity = severity;
87             }
88             else // handle out of range severity as SEVERITY_UNKNOWN
89
{
90                this.severity = Alarm.SEVERITY_UNKNOWN;
91             }
92             break;
93
94          case Alarm.STATE_NONE:
95          default: // handle out of range alarmState as STATE_NONE
96
this.alarmState = Alarm.STATE_NONE;
97             if (severity >= Alarm.SEVERITY_NORMAL && severity <= Alarm.SEVERITY_UNKNOWN)
98             {
99                this.severity = severity;
100             }
101             else // handle out of range severity as SEVERITY_UNKNOWN
102
{
103                this.severity = Alarm.SEVERITY_UNKNOWN;
104             }
105             break;
106       }
107    }
108    
109    // Static --------------------------------------------------------
110

111    /**
112     * Returns a key that can be used in AlarmTables (maps)
113     */

114    public static Object JavaDoc createKey(Notification JavaDoc n)
115    {
116       Object JavaDoc source = getEffectiveSource(n);
117       return AlarmKey.createKey(source, n.getType());
118    }
119    
120    /**
121     * Returns the effective source for the notification.
122     * In case of a AlarmNotification with a non-null target
123     * the target becomes the source.
124     */

125    public static Object JavaDoc getEffectiveSource(Notification JavaDoc n)
126    {
127       Object JavaDoc source = n.getSource();
128       if (n instanceof AlarmNotification)
129       {
130          ObjectName JavaDoc target = ((AlarmNotification)n).getTarget();
131          if (target != null)
132          {
133             source = target;
134          }
135       }
136       return source;
137    }
138    
139    // Accessors -----------------------------------------------------
140

141    /**
142     * Gets the target MBean name, when the alarm is produced
143     * by 'source' on behalf of the 'target', or null.
144     */

145    public ObjectName JavaDoc getTarget()
146    {
147       return target;
148    }
149    
150    /**
151     * Gets alarm severity
152     */

153    public int getSeverity()
154    {
155       return this.severity;
156    }
157    
158    /**
159     * Gets alarm state
160     */

161    public int getAlarmState()
162    {
163       return this.alarmState;
164    }
165    
166    // Object stuff --------------------------------------------------
167

168    /**
169     * toString()
170     */

171    public String JavaDoc toString()
172    {
173       StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc(256);
174       
175       sbuf.append(AlarmNotification.class.getName());
176       sbuf.append(" [ type=").append(getType());
177       sbuf.append(", source=").append(getSource());
178       sbuf.append(", target=").append(target);
179       sbuf.append(", severity=").append(Alarm.SEVERITY_STRINGS[severity]);
180       sbuf.append(", alarmState=").append(Alarm.STATE_STRINGS[alarmState]);
181       sbuf.append(", sequenceNumber=").append(getSequenceNumber());
182       sbuf.append(", timeStamp=").append(getTimeStamp());
183       sbuf.append(", message=").append(getMessage());
184       sbuf.append(", userData={").append(getUserData());
185       sbuf.append("} ]");
186       
187       return sbuf.toString();
188    }
189 }
190
Popular Tags