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 java.util.Timer;
011: import java.util.TimerTask;
012: import java.util.Iterator;
013: import java.util.HashMap;
014: import java.util.Map;
015: import javax.management.NotificationBroadcasterSupport;
016: import javax.management.MBeanRegistration;
017: import javax.management.ObjectName;
018: import javax.management.MBeanServer;
019: import javax.management.InstanceNotFoundException;
020: import javax.management.AttributeNotFoundException;
021: import javax.management.MBeanException;
022: import javax.management.ReflectionException;
023:
024: /**
025: * Defines the common part to all monitor MBeans.
026: * A monitor MBean monitors values of an attribute in an observed MBean. The
027: * observed attribute is monitored at intervals specified by the granularity
028: * period. A gauge value (derived gauge) is derived from the values of
029: * the observed attribute.
030: *
031: * @author <a href="mailto:young_yy@hotmail.org">Young Yang</a>
032: */
033:
034: public abstract class Monitor extends NotificationBroadcasterSupport
035: implements MonitorMBean, MBeanRegistration {
036:
037: // private ObjectName observedObject = null;
038: protected Map observedObjects = new HashMap();
039:
040: private String observedAttribute = null;
041: private long granularityPeriod = 10000L;
042: private MBeanServer server = null;
043:
044: private volatile boolean isActive = false;
045: private volatile long sequenceNumber = 0L;
046:
047: private Timer timer = null;
048: private MonitorTask task = null;
049:
050: public Monitor() {
051:
052: }
053:
054: public ObjectName preRegister(MBeanServer mbeanserver,
055: ObjectName objectname) throws Exception {
056: server = mbeanserver;
057: if (objectname == null)
058: objectname = new ObjectName(server.getDefaultDomain(),
059: "Service", this .toString());
060: return objectname;
061: }
062:
063: public void postRegister(Boolean value) {
064:
065: }
066:
067: public void preDeregister() throws Exception {
068: stop();
069: }
070:
071: public void postDeregister() {
072:
073: }
074:
075: public abstract void addObservedObject(ObjectName objectName)
076: throws java.lang.IllegalArgumentException;
077:
078: // {
079: // if(objectName == null) throw new IllegalArgumentException("The object to observe cannot be null.");
080: // observedObjects.add(objectName);
081: // }
082:
083: public ObjectName[] getObservedObjects() {
084: return (ObjectName[]) observedObjects.keySet().toArray(
085: new ObjectName[0]);
086: }
087:
088: public boolean containsObservedObject(ObjectName objectName) {
089: return observedObjects.containsKey(objectName);
090: }
091:
092: public void removeObservedObject(ObjectName objectName) {
093: observedObjects.remove(objectName);
094: }
095:
096: public String getObservedAttribute() {
097: return observedAttribute;
098: }
099:
100: public void setObservedAttribute(String attribute)
101: throws IllegalArgumentException {
102: if (attribute == null)
103: throw new IllegalArgumentException(
104: "The attribute to observe cannot be null.");
105: observedAttribute = attribute;
106: }
107:
108: public long getGranularityPeriod() {
109: return granularityPeriod;
110: }
111:
112: public void setGranularityPeriod(long period)
113: throws IllegalArgumentException {
114: if (period <= 0L)
115: throw new IllegalArgumentException(
116: "The granularity period must be greater than zero.");
117: granularityPeriod = period;
118: }
119:
120: public boolean isActive() {
121: return isActive;
122: }
123:
124: public abstract void start();
125:
126: public abstract void stop();
127:
128: protected void sendNotification(String type, long timeStamp,
129: String message, Object derivedValue, Object trigger,
130: ObjectName observedObject) {
131: sequenceNumber = sequenceNumber + 1L;
132: MonitorNotification notification = new MonitorNotification(
133: type, this , sequenceNumber, timeStamp, message,
134: observedObject, observedAttribute, derivedValue,
135: trigger);
136: super
137: .sendNotification(((javax.management.Notification) (notification)));
138: }
139:
140: protected void _start() {
141: if (isActive)
142: return;
143: isActive = true;
144: timer = new Timer();
145: task = new MonitorTask();
146: timer.schedule(task, granularityPeriod, granularityPeriod);
147: }
148:
149: protected void _stop() {
150: if (!isActive)
151: return;
152: isActive = false;
153: task.cancel();
154: timer.cancel();
155: // clear the Observed Objects
156: observedObjects.clear();
157: }
158:
159: protected MonitorData getMonitorData(ObjectName observedObject) {
160: return (MonitorData) observedObjects.get(observedObject);
161: }
162:
163: // implemented by every entity monitor
164: protected abstract void doMonitor(ObjectName observedObject,
165: Object value);
166:
167: // inner class MonitorTask for scheduled
168: private class MonitorTask extends TimerTask {
169:
170: public void run() {
171: if (!isActive)
172: return;
173:
174: // if(getObservedObject() == null) {
175: // sendNotification(MonitorNotification.OBSERVED_OBJECT_ERROR,System.currentTimeMillis(),"The observed object must be not null",null,null);
176: // return;
177: // }
178: for (Iterator iter = observedObjects.keySet().iterator(); iter
179: .hasNext();) {
180: ObjectName observedObject = (ObjectName) iter.next();
181: // System.out.println("MonitorTask.run()" + observedObject);
182: if (getObservedAttribute() == null) {
183: sendNotification(
184: MonitorNotification.OBSERVED_ATTRIBUTE_ERROR,
185: System.currentTimeMillis(),
186: "The observed attribute must be not null",
187: null, null, observedObject);
188: return;
189: }
190: try {
191: Object value = server.getAttribute(observedObject,
192: observedAttribute);
193: // call back
194: doMonitor(observedObject, value);
195: } catch (InstanceNotFoundException e) {
196: e.printStackTrace();
197: sendNotification(
198: MonitorNotification.OBSERVED_OBJECT_ERROR,
199: System.currentTimeMillis(),
200: "The observed object must be registered in the MBean server.",
201: null, null, observedObject);
202: } catch (AttributeNotFoundException e) {
203: e.printStackTrace();
204: sendNotification(
205: MonitorNotification.OBSERVED_ATTRIBUTE_ERROR,
206: System.currentTimeMillis(),
207: "The observed attribute must be accessible in the observed object.",
208: null, null, observedObject);
209: } catch (MBeanException e) {
210: e.printStackTrace();
211: sendNotification(MonitorNotification.RUNTIME_ERROR,
212: System.currentTimeMillis(), e
213: .getTargetException().getMessage(),
214: null, null, observedObject);
215: } catch (ReflectionException e) {
216: e.printStackTrace();
217: sendNotification(
218: MonitorNotification.OBSERVED_ATTRIBUTE_ERROR,
219: System.currentTimeMillis(), e
220: .getTargetException().getMessage(),
221: null, null, observedObject);
222: } catch (Exception e) {
223: e.printStackTrace();
224: sendNotification(MonitorNotification.RUNTIME_ERROR,
225: System.currentTimeMillis(), e.getMessage(),
226: null, null, observedObject);
227: }
228: }
229: }
230: }
231: }
232:
233: abstract class MonitorData {
234: protected ObjectName objectName;
235: protected long derivedGaugeTimestamp = System.currentTimeMillis();
236:
237: public MonitorData(ObjectName objectName) {
238: if (objectName == null)
239: throw new IllegalArgumentException(
240: "The object to observe cannot be null.");
241: this .objectName = objectName;
242: }
243:
244: public ObjectName getObjectName() {
245: return objectName;
246: }
247: }
|