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 javax.management.monitor;
023:
024: import java.util.Iterator;
025:
026: import javax.management.MBeanAttributeInfo;
027: import javax.management.MBeanNotificationInfo;
028: import javax.management.ObjectName;
029:
030: import org.jboss.mx.util.MonitorRunnable;
031: import org.jboss.mx.util.ObservedObject;
032: import org.jboss.mx.util.MonitorCallback;
033:
034: /**
035: * The string monitor service.
036: *
037: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>
038: * @version $Revision: 57200 $
039: *
040: */
041: public class StringMonitor extends Monitor implements
042: StringMonitorMBean {
043: // Constants -----------------------------------------------------
044:
045: /**
046: * The string match has been notified.
047: */
048: int STRING_MATCHES_NOTIFIED = 16;
049:
050: /**
051: * The string differs has been notified.
052: */
053: int STRING_DIFFERS_NOTIFIED = 32;
054:
055: // Attributes ----------------------------------------------------
056:
057: /**
058: * The comparison string.
059: */
060: String stringToCompare = new String();
061:
062: /**
063: * Notify Matches.
064: */
065: boolean notifyMatch = false;
066:
067: /**
068: * Notify Differs.
069: */
070: boolean notifyDiffer = false;
071: /**
072: * The runnable monitor.
073: */
074: private MonitorRunnable monitorRunnable;
075:
076: // Static --------------------------------------------------------
077:
078: // Constructors --------------------------------------------------
079:
080: /**
081: * Default Constructor
082: */
083: public StringMonitor() {
084: }
085:
086: // Public --------------------------------------------------------
087:
088: public MBeanNotificationInfo[] getNotificationInfo() {
089: MBeanNotificationInfo[] result = new MBeanNotificationInfo[1];
090: String[] types = new String[] {
091: MonitorNotification.RUNTIME_ERROR,
092: MonitorNotification.OBSERVED_OBJECT_ERROR,
093: MonitorNotification.OBSERVED_ATTRIBUTE_ERROR,
094: MonitorNotification.OBSERVED_ATTRIBUTE_TYPE_ERROR,
095: MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED,
096: MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED };
097: result[0] = new MBeanNotificationInfo(types,
098: "javax.management.monitor.MonitorNotification",
099: "Notifications sent by the String Monitor Service MBean");
100: return result;
101: }
102:
103: // StringMonitorMBean implementation -----------------------------
104:
105: public String getDerivedGauge() {
106: ObservedObject object = getFirstObservedObject();
107: if (object != null)
108: return (String) object.getDerivedGauge();
109: else
110: return null;
111: }
112:
113: public String getDerivedGauge(ObjectName name) {
114: ObservedObject object = retrieveObservedObject(name);
115: if (object != null)
116: return (String) object.getDerivedGauge();
117: else
118: return null;
119: }
120:
121: public long getDerivedGaugeTimeStamp() {
122: ObservedObject object = getFirstObservedObject();
123: if (object != null)
124: return object.getDerivedGaugeTimeStamp();
125: else
126: return 0L;
127: }
128:
129: public long getDerivedGaugeTimeStamp(ObjectName name) {
130: ObservedObject object = retrieveObservedObject(name);
131: if (object != null)
132: return object.getDerivedGaugeTimeStamp();
133: else
134: return 0L;
135: }
136:
137: public String getStringToCompare() {
138: return stringToCompare;
139: }
140:
141: public void setStringToCompare(String value)
142: throws IllegalArgumentException {
143: if (value == null)
144: throw new IllegalArgumentException(
145: "Null string to compare.");
146: this .stringToCompare = value;
147: }
148:
149: public boolean getNotifyMatch() {
150: return notifyMatch;
151: }
152:
153: public void setNotifyMatch(boolean value) {
154: notifyMatch = value;
155: }
156:
157: public boolean getNotifyDiffer() {
158: return notifyDiffer;
159: }
160:
161: public void setNotifyDiffer(boolean value) {
162: notifyDiffer = value;
163: }
164:
165: public synchronized void start() {
166: // Ignore if already active
167: if (active)
168: return;
169: active = true;
170:
171: for (Iterator i = retrieveObservedObjects().values().iterator(); i
172: .hasNext();) {
173: ObservedObject object = (ObservedObject) i.next();
174: initObservedObject(object);
175: }
176:
177: // Start the monitor runnable
178: MonitorCallback callback = new MonitorCallback() {
179: public void monitorCallback(ObservedObject object,
180: MBeanAttributeInfo attributeInfo, Object value)
181: throws Exception {
182: monitor(object, attributeInfo, value);
183: }
184:
185: public MonitorNotification createNotification(String type,
186: Object source, long timeStamp, String message,
187: Object derivedGauge, String observedAttribute,
188: ObjectName observedObject, Object trigger) {
189: return new MonitorNotification(type, source,
190: nextSeqNo(), timeStamp, message, derivedGauge,
191: observedAttribute, observedObject, trigger);
192: }
193: };
194: monitorRunnable = new MonitorRunnable(this , objectName,
195: callback, observedObjects, server);
196: }
197:
198: public synchronized void stop() {
199: // Ignore if not active
200: if (!active)
201: return;
202:
203: // Stop the monitor runnable
204: active = false;
205: monitorRunnable.setScheduler(null);
206: monitorRunnable = null;
207: }
208:
209: // Package protected ---------------------------------------------
210:
211: void initObservedObject(ObservedObject object) {
212: super .initObservedObject(object);
213: object.setDerivedGauge(new String());
214: }
215:
216: void monitor(ObservedObject object,
217: MBeanAttributeInfo attributeInfo, Object value)
218: throws Exception {
219: // Must be a string.
220: if (!(value instanceof String)) {
221: sendAttributeTypeErrorNotification(object,
222: "Not a string attribute");
223: return;
224: }
225:
226: // Get the gauge and record when we got it.
227: object.setDerivedGauge(value);
228: object.setDerivedGaugeTimeStamp(System.currentTimeMillis());
229:
230: // Try to fire the relevant event.
231: if (value.equals(stringToCompare))
232: sendStringMatchesNotification(object, (String) value);
233: else
234: sendStringDiffersNotification(object, (String) value);
235: }
236:
237: /**
238: * Send an string matches notification.<p>
239: *
240: * This is only performed when requested and it has not already been sent.
241: *
242: * @param value the attribute value.
243: */
244: void sendStringMatchesNotification(ObservedObject object,
245: String value) {
246: if (notifyMatch
247: && object.notAlreadyNotified(STRING_MATCHES_NOTIFIED))
248: sendNotification(
249: object,
250: MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED,
251: object.getDerivedGaugeTimeStamp(), "matches",
252: observedAttribute, value, stringToCompare);
253: object.setNotAlreadyNotified(STRING_DIFFERS_NOTIFIED);
254: }
255:
256: /**
257: * Send an string differs notification.<p>
258: *
259: * This is only performed when requested and it has not already been sent.
260: *
261: * @param value the attribute value.
262: */
263: void sendStringDiffersNotification(ObservedObject object,
264: String value) {
265: if (notifyDiffer
266: && object.notAlreadyNotified(STRING_DIFFERS_NOTIFIED))
267: sendNotification(
268: object,
269: MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED,
270: object.getDerivedGaugeTimeStamp(), "differs",
271: observedAttribute, value, stringToCompare);
272: object.setNotAlreadyNotified(STRING_MATCHES_NOTIFIED);
273: }
274:
275: // Protected -----------------------------------------------------
276:
277: // Private -------------------------------------------------------
278:
279: // Inner classes -------------------------------------------------
280: }
|