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.alarm;
023:
024: import javax.management.Notification;
025: import javax.management.ObjectName;
026:
027: /**
028: * AlarmNotification
029: *
030: * @author <a href="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
031: * @version $Revision: 57210 $
032: */
033: public class AlarmNotification extends Notification {
034: // Private Data --------------------------------------------------
035:
036: /** @since 4.0.4 */
037: private static final long serialVersionUID = -7041616127511632675L;
038:
039: /** Set when the alarm refers to some other MBean */
040: private ObjectName target;
041:
042: /** The alarm severity */
043: private int severity;
044:
045: /** The alarm state */
046: private int alarmState;
047:
048: // CTORS ---------------------------------------------------------
049:
050: /**
051: * Complete CTOR, creates an AlarmNotification object
052: *
053: * Note:
054: * STATE_CLEARED forces severity to SEVERITY_NORMAL
055: * STATE_CREATED/CHANGED have valid severities WARNING to UNKNOWN
056: * STATE_NONE has valid severities NORMAL to UNKNOWN
057: * Also:
058: * Out-of-range states are mapped to STATE_NONE
059: * Out-of-range severities are mapped to SEVERITY_UNKNOWN
060: */
061: public AlarmNotification(String type, Object source,
062: ObjectName target, int severity, int alarmState,
063: long sequenceNumber, long timeStamp, String message) {
064: super (type, source, sequenceNumber, timeStamp, message);
065:
066: this .target = target;
067:
068: switch (alarmState) {
069: case Alarm.STATE_CLEARED:
070: this .alarmState = Alarm.STATE_CLEARED;
071: // forces severity=SEVERITY_NORMAL
072: this .severity = Alarm.SEVERITY_NORMAL;
073: break;
074:
075: case Alarm.STATE_CREATED:
076: case Alarm.STATE_CHANGED:
077: this .alarmState = alarmState;
078: // can't have SEVERITY_NORMAL!
079: if (severity > Alarm.SEVERITY_NORMAL
080: && severity <= Alarm.SEVERITY_UNKNOWN) {
081: this .severity = severity;
082: } else // handle out of range severity as SEVERITY_UNKNOWN
083: {
084: this .severity = Alarm.SEVERITY_UNKNOWN;
085: }
086: break;
087:
088: case Alarm.STATE_NONE:
089: default: // handle out of range alarmState as STATE_NONE
090: this .alarmState = Alarm.STATE_NONE;
091: if (severity >= Alarm.SEVERITY_NORMAL
092: && severity <= Alarm.SEVERITY_UNKNOWN) {
093: this .severity = severity;
094: } else // handle out of range severity as SEVERITY_UNKNOWN
095: {
096: this .severity = Alarm.SEVERITY_UNKNOWN;
097: }
098: break;
099: }
100: }
101:
102: // Static --------------------------------------------------------
103:
104: /**
105: * Returns a key that can be used in AlarmTables (maps)
106: */
107: public static Object createKey(Notification n) {
108: Object source = getEffectiveSource(n);
109: return AlarmKey.createKey(source, n.getType());
110: }
111:
112: /**
113: * Returns the effective source for the notification.
114: * In case of a AlarmNotification with a non-null target
115: * the target becomes the source.
116: */
117: public static Object getEffectiveSource(Notification n) {
118: Object source = n.getSource();
119: if (n instanceof AlarmNotification) {
120: ObjectName target = ((AlarmNotification) n).getTarget();
121: if (target != null) {
122: source = target;
123: }
124: }
125: return source;
126: }
127:
128: // Accessors -----------------------------------------------------
129:
130: /**
131: * Gets the target MBean name, when the alarm is produced
132: * by 'source' on behalf of the 'target', or null.
133: */
134: public ObjectName getTarget() {
135: return target;
136: }
137:
138: /**
139: * Gets alarm severity
140: */
141: public int getSeverity() {
142: return this .severity;
143: }
144:
145: /**
146: * Gets alarm state
147: */
148: public int getAlarmState() {
149: return this .alarmState;
150: }
151:
152: // Object stuff --------------------------------------------------
153:
154: /**
155: * toString()
156: */
157: public String toString() {
158: StringBuffer sbuf = new StringBuffer(256);
159:
160: sbuf.append(AlarmNotification.class.getName());
161: sbuf.append(" [ type=").append(getType());
162: sbuf.append(", source=").append(getSource());
163: sbuf.append(", target=").append(target);
164: sbuf.append(", severity=").append(
165: Alarm.SEVERITY_STRINGS[severity]);
166: sbuf.append(", alarmState=").append(
167: Alarm.STATE_STRINGS[alarmState]);
168: sbuf.append(", sequenceNumber=").append(getSequenceNumber());
169: sbuf.append(", timeStamp=").append(getTimeStamp());
170: sbuf.append(", message=").append(getMessage());
171: sbuf.append(", userData={").append(getUserData());
172: sbuf.append("} ]");
173:
174: return sbuf.toString();
175: }
176: }
|