001: /*
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */
008:
009: package mx4j.monitor;
010:
011: import javax.management.MBeanNotificationInfo;
012: import javax.management.NotCompliantMBeanException;
013: import javax.management.ObjectName;
014: import javax.management.monitor.MonitorNotification;
015:
016: import mx4j.log.Logger;
017:
018: /**
019: * @version $Revision: 1.3 $
020: */
021: public class MX4JStringMonitor extends MX4JMonitor implements
022: MX4JStringMonitorMBean {
023: private static final String EMPTY = "";
024:
025: private String stringToCompare = EMPTY;
026: private boolean notifyMatch;
027: private boolean notifyDiffer;
028:
029: public MX4JStringMonitor() throws NotCompliantMBeanException {
030: super (MX4JStringMonitorMBean.class);
031: }
032:
033: public MX4JStringMonitor(Class management)
034: throws NotCompliantMBeanException {
035: super (management);
036: }
037:
038: public MBeanNotificationInfo[] getNotificationInfo() {
039: // TODO
040: return new MBeanNotificationInfo[0];
041: }
042:
043: public synchronized String getStringToCompare() {
044: return stringToCompare;
045: }
046:
047: public synchronized void setStringToCompare(String value)
048: throws IllegalArgumentException {
049: if (value == null)
050: throw new IllegalArgumentException(
051: "String to compare cannot be null");
052: this .stringToCompare = value;
053: }
054:
055: public synchronized boolean getNotifyMatch() {
056: return notifyMatch;
057: }
058:
059: public synchronized void setNotifyMatch(boolean notifyMatch) {
060: this .notifyMatch = notifyMatch;
061: }
062:
063: public synchronized boolean getNotifyDiffer() {
064: return notifyDiffer;
065: }
066:
067: public synchronized void setNotifyDiffer(boolean notifyDiffer) {
068: this .notifyDiffer = notifyDiffer;
069: }
070:
071: public String getDerivedGauge(ObjectName objectName) {
072: StringMonitorInfo info = (StringMonitorInfo) getMonitorInfo(objectName);
073: return info.getGauge();
074: }
075:
076: public long getDerivedGaugeTimeStamp(ObjectName objectName) {
077: StringMonitorInfo info = (StringMonitorInfo) getMonitorInfo(objectName);
078: return info.getTimestamp();
079: }
080:
081: protected MonitorInfo createMonitorInfo() {
082: return new StringMonitorInfo();
083: }
084:
085: protected int compare(String left, String right) {
086: return left == null ? right == null ? 0 : -1
087: : right == null ? 1 : left.compareTo(right);
088: }
089:
090: protected void monitor(ObjectName name, String attribute,
091: Object value, MonitorInfo monitorInfo) {
092: if (!(value instanceof String)) {
093: sendErrorNotification(monitorInfo,
094: MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR,
095: "Attribute type must be a String, not "
096: + value.getClass(), name, attribute);
097: return;
098: }
099:
100: String gauge = (String) value;
101:
102: String reference = null;
103: synchronized (this ) {
104: reference = getStringToCompare();
105: }
106:
107: Logger logger = getLogger();
108:
109: StringMonitorInfo info = (StringMonitorInfo) monitorInfo;
110: if (logger.isEnabledFor(Logger.DEBUG)) {
111: logger.debug("Computing gauge, previous values are: "
112: + info);
113: logger.debug("Current values are: gauge=" + gauge
114: + ", stringToCompare=" + reference);
115: }
116:
117: compareAndSendNotification(gauge, reference, info, name,
118: attribute);
119:
120: info.setGauge(gauge);
121: info.setTimestamp(System.currentTimeMillis());
122: }
123:
124: private void compareAndSendNotification(String gauge,
125: String reference, StringMonitorInfo info, ObjectName name,
126: String attribute) {
127: Logger logger = getLogger();
128:
129: boolean equals = compare(gauge, reference) == 0;
130:
131: if (info.isDifferNotified() && !equals) {
132: if (logger.isEnabledFor(Logger.DEBUG))
133: logger.debug("Difference already notified, gauge="
134: + gauge + ", string-to-compare=" + reference);
135: return;
136: }
137: if (info.isMatchNotified() && equals) {
138: if (logger.isEnabledFor(Logger.DEBUG))
139: logger.debug("Match already notified, gauge=" + gauge
140: + ", string-to-compare=" + reference);
141: return;
142: }
143:
144: if (equals) {
145: if (logger.isEnabledFor(Logger.DEBUG))
146: logger.debug("Gauge matches, gauge=" + gauge
147: + ", string-to-compare=" + reference);
148: info.setDifferNotified(false);
149: if (getNotifyMatch()) {
150: if (logger.isEnabledFor(Logger.DEBUG))
151: logger.debug("Sending string match notification");
152: info.setMatchNotified(true);
153: sendNotification(
154: MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED,
155: "Gauge " + gauge + " matched " + reference,
156: name, attribute, gauge, reference);
157: } else {
158: info.setMatchNotified(false);
159: if (logger.isEnabledFor(Logger.DEBUG))
160: logger
161: .debug("StringMonitor is configured in non-match-notification mode");
162: }
163: } else {
164: if (logger.isEnabledFor(Logger.DEBUG))
165: logger.debug("Gauge differs, gauge=" + gauge
166: + ", string-to-compare=" + reference);
167: info.setMatchNotified(false);
168: if (getNotifyDiffer()) {
169: if (logger.isEnabledFor(Logger.DEBUG))
170: logger.debug("Sending string differ notification");
171: info.setDifferNotified(true);
172: sendNotification(
173: MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED,
174: "Gauge " + gauge + " differs from " + reference,
175: name, attribute, gauge, reference);
176: } else {
177: info.setDifferNotified(false);
178: if (logger.isEnabledFor(Logger.DEBUG))
179: logger
180: .debug("StringMonitor is configured in non-differ-notification mode");
181: }
182: }
183: }
184:
185: protected class StringMonitorInfo extends MonitorInfo {
186: private String gauge;
187: private long timestamp;
188: private boolean matchNotified;
189: private boolean differNotified;
190:
191: public String getGauge() {
192: return gauge;
193: }
194:
195: public void setGauge(String gauge) {
196: this .gauge = gauge;
197: }
198:
199: public long getTimestamp() {
200: return timestamp;
201: }
202:
203: public void setTimestamp(long timestamp) {
204: this .timestamp = timestamp;
205: }
206:
207: public boolean isMatchNotified() {
208: return matchNotified;
209: }
210:
211: public void setMatchNotified(boolean matchNotified) {
212: this .matchNotified = matchNotified;
213: }
214:
215: public boolean isDifferNotified() {
216: return differNotified;
217: }
218:
219: public void setDifferNotified(boolean differNotified) {
220: this .differNotified = differNotified;
221: }
222:
223: public void clearNotificationStatus() {
224: super .clearNotificationStatus();
225: differNotified = false;
226: matchNotified = false;
227: }
228:
229: public String toString() {
230: StringBuffer buffer = new StringBuffer(super .toString());
231: buffer.append(", gauge=").append(getGauge());
232: buffer.append(", matchNotified=").append(isMatchNotified());
233: buffer.append(", differNotified=").append(
234: isDifferNotified());
235: return buffer.toString();
236: }
237: }
238: }
|