001: /* JFox, the OpenSource J2EE Application Server
002: *
003: * Copyright (C) 2002 huihoo.org
004: * Distributable under GNU LGPL license
005: * See the GNU Lesser General Public License for more details.
006: */
007:
008: package javax.management.monitor;
009:
010: import javax.management.MBeanNotificationInfo;
011: import javax.management.ObjectName;
012:
013: /**
014: * Defines a monitor MBean designed to observe the values of a string
015: * attribute.
016: * <P>
017: * A string monitor sends notifications as follows:
018: * <UL>
019: * <LI> if the attribute value matches the string to compare value,
020: * a {@link MonitorNotification#STRING_TO_COMPARE_VALUE_MATCHED match notification} is sent.
021: * The notify match flag must be set to <CODE>true</CODE>.
022: * <BR>Subsequent matchings of the string to compare values do not
023: * cause further notifications unless
024: * the attribute value differs from the string to compare value.
025: * <LI> if the attribute value differs from the string to compare value,
026: * a {@link MonitorNotification#STRING_TO_COMPARE_VALUE_DIFFERED differ notification} is sent.
027: * The notify differ flag must be set to <CODE>true</CODE>.
028: * <BR>Subsequent differences from the string to compare value do
029: * not cause further notifications unless
030: * the attribute value matches the string to compare value.
031: * </UL>
032: *
033: * @author <a href="mailto:young_yy@hotmail.org">Young Yang</a>
034: */
035:
036: public class StringMonitor extends Monitor implements
037: StringMonitorMBean {
038:
039: private String stringToCompare = "";
040: private boolean notifyMatch = false;
041: private boolean notifyDiffer = false;
042: // private String derivedGauge = "";
043: // private long derivedGaugeTimestamp = System.currentTimeMillis();
044:
045: private long times = 0L; // except the first time
046:
047: public StringMonitor() {
048:
049: }
050:
051: public void start() {
052: super ._start();
053: }
054:
055: public synchronized void stop() {
056: super ._stop();
057: }
058:
059: public String getDerivedGauge(ObjectName observedObject) {
060: return ((StringMonitorData) getMonitorData(observedObject)).derivedGauge;
061: }
062:
063: public long getDerivedGaugeTimeStamp(ObjectName observedObject) {
064: return ((StringMonitorData) getMonitorData(observedObject)).derivedGaugeTimestamp;
065: }
066:
067: public String getStringToCompare() {
068: return stringToCompare;
069: }
070:
071: public void setStringToCompare(String value)
072: throws IllegalArgumentException {
073: if (value == null)
074: throw new IllegalArgumentException(
075: "The string to compare cannot be null.");
076: stringToCompare = value;
077: }
078:
079: public boolean getNotifyMatch() {
080: return notifyMatch;
081: }
082:
083: public void setNotifyMatch(boolean value) {
084: notifyMatch = value;
085: }
086:
087: public boolean getNotifyDiffer() {
088: return notifyDiffer;
089: }
090:
091: public void setNotifyDiffer(boolean value) {
092: notifyDiffer = value;
093: }
094:
095: public MBeanNotificationInfo[] getNotificationInfo() {
096: String[] notifTypes = { MonitorNotification.RUNTIME_ERROR,
097: MonitorNotification.OBSERVED_OBJECT_ERROR,
098: MonitorNotification.OBSERVED_ATTRIBUTE_ERROR,
099: MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR,
100: MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED,
101: MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED };
102: MBeanNotificationInfo[] notifInfos = { new MBeanNotificationInfo(
103: notifTypes, MonitorNotification.class.getName(),
104: "Notifications sent by the StringMonitor MBean") };
105: return notifInfos;
106: }
107:
108: // implemented by every entity monitor
109: protected void doMonitor(ObjectName observedObject, Object value) {
110: // System.out.println("StringMonitor.doMonitor()");
111: StringMonitorData data = (StringMonitorData) getMonitorData(observedObject);
112: try {
113: long derivedGaugeTimestamp = System.currentTimeMillis(); // update timestamp
114: data.derivedGaugeTimestamp = derivedGaugeTimestamp;
115: if (!(value instanceof String)) {
116: sendNotification(
117: MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR,
118: derivedGaugeTimestamp,
119: "The observed attribute type must be a string type.",
120: data.derivedGauge, stringToCompare,
121: observedObject);
122: return;
123: }
124: if (stringToCompare == null) {
125: sendNotification(
126: MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR,
127: derivedGaugeTimestamp,
128: "The observed attribute target value must not be null",
129: data.derivedGauge, stringToCompare,
130: observedObject);
131: return;
132: }
133: // attribute value not change
134: if (data.derivedGauge.equals(value))
135: return;
136:
137: boolean matches = stringToCompare.equals(value);
138: // System.out.println("Matches: " + matches);
139: boolean lastMatches = stringToCompare
140: .equals(data.derivedGauge);
141: // System.out.println("lastMatches: " + lastMatches);
142:
143: // notify match when this time is matches and last time is not matched
144: if (notifyMatch && matches) {
145: if (times == 0 || !lastMatches)
146: sendNotification(
147: MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED,
148: derivedGaugeTimestamp, "",
149: data.derivedGauge, stringToCompare,
150: observedObject);
151: }
152:
153: // notify differ when this time is not match and last time is matched
154: if (notifyDiffer && !matches) {
155: if (times == 0 || lastMatches)
156: sendNotification(
157: MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED,
158: derivedGaugeTimestamp, "",
159: data.derivedGauge, stringToCompare,
160: observedObject);
161: }
162:
163: data.derivedGauge = (String) value; // update derived gauge
164: times++;
165: } catch (Exception e) {
166: sendNotification(MonitorNotification.RUNTIME_ERROR,
167: data.derivedGaugeTimestamp, e.getMessage(), null,
168: null, observedObject);
169: }
170: }
171:
172: public void addObservedObject(ObjectName objectName)
173: throws IllegalArgumentException {
174: if (objectName == null)
175: throw new IllegalArgumentException(
176: "The object to observe cannot be null.");
177: observedObjects.put(objectName, new StringMonitorData(
178: objectName));
179:
180: }
181:
182: class StringMonitorData extends MonitorData {
183: String derivedGauge = "";
184: long derivedGaugeTimestamp = System.currentTimeMillis();
185:
186: public StringMonitorData(ObjectName objectName) {
187: super (objectName);
188: }
189:
190: public ObjectName getObjectName() {
191: return super.getObjectName();
192: }
193: }
194: }
|