001: /**
002: * The XMOJO Project 5
003: * Copyright © 2003 XMOJO.org. All rights reserved.
004:
005: * NO WARRANTY
006:
007: * BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR
008: * THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
009: * OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
010: * PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
011: * OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
012: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
013: * TO THE QUALITY AND PERFORMANCE OF THE LIBRARY IS WITH YOU. SHOULD THE
014: * LIBRARY PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
015: * REPAIR OR CORRECTION.
016:
017: * IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL
018: * ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE
019: * THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
020: * GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
021: * USE OR INABILITY TO USE THE LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF
022: * DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
023: * PARTIES OR A FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE),
024: * EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
025: * SUCH DAMAGES.
026: **/package javax.management.monitor;
027:
028: import java.io.Serializable;
029:
030: import javax.management.ObjectName;
031: import javax.management.MBeanServer;
032: import javax.management.MBeanNotificationInfo;
033: import javax.management.MBeanRegistration;
034: import javax.management.NotificationBroadcasterSupport;
035:
036: /**
037: * This class is the base class for all types of monitor mbeans.Defines the
038: * common part to all monitor MBeans. Using Monitor MBeans, the observed
039: * attribute of another MBean (the observed MBean) is monitored at intervals
040: * specified by the granularity period. A gauge value (derived gauge) is
041: * derived from the values of the observed attribute.
042: */
043: public abstract class Monitor extends NotificationBroadcasterSupport
044: implements MonitorMBean, MBeanRegistration, Serializable {
045: /**
046: * Selected monitor errors that have already been notified
047: */
048: protected int alreadyNotified = 0;
049:
050: /**
051: * Flag denoting that a notification has occurred after changing the
052: * observed attribute. This flag is used to check that the new observed
053: * attribute belongs to the observed object at the time of the first notification.
054: */
055: protected static int OBSERVED_ATTRIBUTE_ERROR_NOTIFIED = 1;
056:
057: /**
058: * Flag denoting that a notification has occurred after changing the
059: * observed object or the observed attribute. This flag is used to check
060: * that the observed attribute type is correct (depending on the monitor in
061: * use) at the time of the first notification.
062: */
063: protected static int OBSERVED_ATTRIBUTE_TYPE_ERROR_NOTIFIED = 1;
064:
065: /**
066: * Flag denoting that a notification has occurred after changing the
067: * observed object or the observed attribute. This flag is used to notify
068: * any exception (except the cases described above) when trying to get the
069: * value of the observed attribute at the time of the first notification.
070: */
071: protected static int OBSERVED_OBJECT_ERROR_NOTIFIED = 1;
072:
073: /**
074: * This flag is used to reset the alreadyNotified monitor attribute.
075: */
076: protected static int RESET_FLAGS_ALREADY_NOTIFIED = 1;
077:
078: /**
079: * Flag denoting that a notification has occurred after changing the
080: * observed object or the observed attribute. This flag is used to notify
081: * any exception (except the cases described above) when trying to get the
082: * value of the observed attribute at the time of the first notification.
083: */
084: protected static int RUNTIME_ERROR_NOTIFIED = 1;
085:
086: /**
087: * Reference on the MBean server.
088: * This reference is null when the monitor MBean is not registered in an
089: * MBean server. This reference is initialized before the monitor MBean is
090: * registered in the MBean server.
091: *
092: * @see #preRegister(MBeanServer server, ObjectName name)
093: */
094: protected MBeanServer server = null;
095:
096: /**
097: * Monitor granularity period (in milliseconds).
098: */
099: long granularityPeriod = 1000;
100:
101: /**
102: * Object to which the attribute to observe belongs to.
103: * <BR>The default value is set to null.
104: */
105: ObjectName observedObject = null;
106:
107: /**
108: * Attribute to observe.
109: * <BR>The default value is set to null.
110: */
111: String attributeName = null;
112:
113: /**
114: * Monitor state.
115: * The default value is set to <CODE>false</CODE>.
116: */
117: boolean isActive = false;
118:
119: long derivedGaugeTimeStamp = 0;
120:
121: protected String dgbTag = null;
122:
123: /**
124: * Default constructor
125: */
126: public Monitor() {
127: }
128:
129: /**
130: * This method gets the granularity period (in milliseconds).
131: *
132: * @return long value representing the value of the granularity period
133: * (in milliseconds).
134: */
135: public long getGranularityPeriod() {
136: return granularityPeriod;
137: }
138:
139: /**
140: * This method sets the granularity period (in milliseconds). The default
141: * value is one second.
142: *
143: * @param period the granularity period value.
144: *
145: * @exception java.lang.IllegalArgumentException - The granularity period
146: * is less than or equal to zero.
147: */
148: public void setGranularityPeriod(long period)
149: throws IllegalArgumentException {
150: if (period <= 0)
151: throw new IllegalArgumentException(
152: "Invalid Granularity Period!!!!"
153: + " Granularity Period cannot be negative or Zero");
154: else
155: this .granularityPeriod = period;
156: }
157:
158: /**
159: * This method gets the name of the attribute being observed.
160: *
161: * @return String The name of the attribute that is being observed.
162: */
163: public String getObservedAttribute() {
164: return attributeName;
165: }
166:
167: /**
168: * This method sets the attribute being observed.
169: *
170: * @param attribute The attribute to be observed.
171: */
172: public void setObservedAttribute(String attribute)
173: throws IllegalArgumentException {
174: if (attribute == null || attribute.equals("")) {
175: throw new IllegalArgumentException(
176: "Attribute cannot be null");
177: }
178:
179: this .attributeName = attribute;
180: }
181:
182: /**
183: * This method gets the object name of the object being observed.
184: *
185: * @return The ObjectName of the object being observed.
186: */
187: public ObjectName getObservedObject() {
188: return observedObject;
189: }
190:
191: /**
192: * This method sets the object name of the object being observed.
193: *
194: * @param object The ObjectName of the object to be observed.
195: *
196: * @throws IllegalArgumentException.
197: */
198: public void setObservedObject(ObjectName object)
199: throws IllegalArgumentException {
200: if (object == null) {
201: throw new IllegalArgumentException(
202: "ObjectName cannot be null");
203: }
204:
205: observedObject = object;
206: }
207:
208: /**
209: * This method tests whether the monitor MBean is active. A monitor MBean
210: * is marked active when the start method is called. It becomes inactive
211: * when the stop method is called.
212: *
213: * @return boolean value indicating whether the MBean is active or not.
214: */
215: public boolean isActive() {
216: return isActive;
217: }
218:
219: /**
220: * This method allows the monitor MBean to perform any operations needed
221: * after having been de-registered by the MBean server.
222: * Not used in this context.
223: */
224: public void postDeregister() {
225: }
226:
227: /**
228: * This method allows the monitor MBean to perform any operations needed
229: * after having been registered in the MBean server or after the
230: * registration has failed.
231: *
232: * Not used in this context.
233: */
234: public void postRegister(Boolean registrationDone) {
235: }
236:
237: /**
238: * This method allows the monitor MBean to perform any operations it needs
239: * before being registered in the MBean server.
240: * Initializes the reference to the MBean server.
241: *
242: * @param server - The MBean server in which the monitor MBean will be registered.
243: *
244: * @param name - The object name of the monitor MBean.
245: *
246: * @return This method allows the monitor MBean to perform any operations
247: * it needs before being registered in the MBean server.
248: *
249: * @exception - java.lang.Exception.
250: */
251: public ObjectName preRegister(MBeanServer server, ObjectName name)
252: throws Exception {
253: this .server = server;
254: return name;
255: }
256:
257: /**
258: * This method allows the monitor MBean to perform any operations it needs
259: * before being de-registered by the MBean server.
260: * Stops the monitor.
261: *
262: * @throws This operation throws java.lang.Exception
263: */
264: public void preDeregister() throws Exception {
265: }
266:
267: /**
268: * This method starts the monitor.
269: */
270: public abstract void start();
271:
272: /**
273: * This method stops the monitor.
274: */
275: public abstract void stop();
276:
277: /**
278: * This method returns a NotificationInfo object containing the name of the
279: * Java class of the notification and the notification types sent by the
280: * counter monitor.
281: *
282: * @return An Array of MBeanNotificationInfo objects.
283: */
284: public MBeanNotificationInfo[] getNotificationInfo() {
285: MBeanNotificationInfo[] notifInfo = new MBeanNotificationInfo[1];
286:
287: String[] types = { "jmx.monitor.error.mbean",
288: "jmx.monitor.error.attribute",
289: "jmx.monitor.error.type", "jmx.monitor.error.runtime" };
290:
291: notifInfo[0] = new MBeanNotificationInfo(types,
292: "MonitorErrorNotification",
293: "These types of notification are emitted on Monitor Error Cases");
294:
295: return notifInfo;
296: }
297: }
|