/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* Defines the part common to all monitor MBeans.
* A monitor MBean monitors values of an attribute common to a set of observed
* MBeans. The observed attribute is monitored at intervals specified by the
* granularity period. A gauge value (derived gauge) is derived from the values
* of the observed attribute.
*
*
* @since 1.5
*/
public abstract class Monitor
extends NotificationBroadcasterSupport
implements MonitorMBean, MBeanRegistration {
/*
* ------------------------------------------
* PACKAGE CLASSES
* ------------------------------------------
*/
static class ObservedObject {
this.observedObject = observedObject;
}
return observedObject;
}
public final synchronized int getAlreadyNotified() {
return alreadyNotified;
}
this.alreadyNotified = alreadyNotified;
}
return derivedGauge;
}
this.derivedGauge = derivedGauge;
}
public final synchronized long getDerivedGaugeTimeStamp() {
return derivedGaugeTimeStamp;
}
public final synchronized void setDerivedGaugeTimeStamp(
long derivedGaugeTimeStamp) {
}
private int alreadyNotified;
private long derivedGaugeTimeStamp;
}
/*
* ------------------------------------------
* PRIVATE VARIABLES
* ------------------------------------------
*/
/**
* Attribute to observe.
*/
/**
* Monitor granularity period (in milliseconds).
* The default value is set to 10 seconds.
*/
/**
* Monitor state.
* The default value is set to <CODE>false</CODE>.
*/
private boolean isActive = false;
/**
* Monitor sequence number.
* The default value is set to 0.
*/
/**
* Complex type attribute flag.
* The default value is set to <CODE>false</CODE>.
*/
private boolean isComplexTypeAttribute = false;
/**
* First attribute name extracted from complex type attribute name.
*/
/**
* Remaining attribute names extracted from complex type attribute name.
*/
new CopyOnWriteArrayList<String>();
/**
* AccessControlContext of the Monitor.start() caller.
*/
new AccessControlContext(
/**
* Scheduler Service.
*/
new DaemonThreadFactory("Scheduler"));
/**
* Map containing the thread pool executor per thread group.
*/
/**
* Lock for executors map.
*/
/**
* Maximum Pool Size
*/
private static final int maximumPoolSize;
static {
if (maximumPoolSizeStr == null ||
maximumPoolSize = 10;
} else {
int maximumPoolSizeTmp = 10;
try {
} catch (NumberFormatException e) {
"<static initializer>",
"Wrong value for " + maximumPoolSizeSysProp +
" system property", e);
"<static initializer>",
maximumPoolSizeSysProp + " defaults to 10");
}
maximumPoolSizeTmp = 10;
}
if (maximumPoolSizeTmp < 1) {
maximumPoolSize = 1;
} else {
}
}
}
/**
* Future associated to the current monitor task.
*/
/**
* Scheduler task to be executed by the Scheduler Service.
*/
/**
* ScheduledFuture associated to the current scheduler task.
*/
/*
* ------------------------------------------
* PROTECTED VARIABLES
* ------------------------------------------
*/
/**
* The amount by which the capacity of the monitor arrays are
* automatically incremented when their size becomes greater than
* their capacity.
*/
/**
* The number of valid components in the vector of observed objects.
*
*/
/**
* Monitor errors that have already been notified.
* @deprecated equivalent to {@link #alreadyNotifieds}[0].
*/
/**
* <p>Selected monitor errors that have already been notified.</p>
*
* <p>Each element in this array corresponds to an observed object
* in the vector. It contains a bit mask of the flags {@link
* #OBSERVED_OBJECT_ERROR_NOTIFIED} etc, indicating whether the
* corresponding notification has already been sent for the MBean
* being monitored.</p>
*
*/
/**
* Reference to the MBean server. This reference is null when the
* monitor MBean is not registered in an MBean server. This
* reference is initialized before the monitor MBean is registered
* in the MBean server.
* @see #preRegister(MBeanServer server, ObjectName name)
*/
// Flags defining possible monitor errors.
//
/**
* This flag is used to reset the {@link #alreadyNotifieds
* alreadyNotifieds} monitor attribute.
*/
/**
* Flag denoting that a notification has occurred after changing
* the observed object. This flag is used to check that the new
* observed object is registered in the MBean server at the time
* of the first notification.
*/
/**
* Flag denoting that a notification has occurred after changing
* the observed attribute. This flag is used to check that the
* new observed attribute belongs to the observed object at the
* time of the first notification.
*/
/**
* Flag denoting that a notification has occurred after changing
* the observed object or the observed attribute. This flag is
* used to check that the observed attribute type is correct
* (depending on the monitor in use) at the time of the first
* notification.
*/
/**
* Flag denoting that a notification has occurred after changing
* the observed object or the observed attribute. This flag is
* used to notify any exception (except the cases described above)
* when trying to get the value of the observed attribute at the
* time of the first notification.
*/
/**
* This field is retained for compatibility but should not be referenced.
*
* @deprecated No replacement.
*/
/*
* ------------------------------------------
* PACKAGE VARIABLES
* ------------------------------------------
*/
/**
* List of ObservedObjects to which the attribute to observe belongs.
*/
new CopyOnWriteArrayList<ObservedObject>();
/**
* Flag denoting that a notification has occurred after changing
* the threshold. This flag is used to notify any exception
* related to invalid thresholds settings.
*/
/**
* Enumeration used to keep trace of the derived gauge type
* in counter and gauge monitors.
*/
/**
* Constant used to initialize all the numeric values.
*/
/*
* ------------------------------------------
* PUBLIC METHODS
* ------------------------------------------
*/
/**
* Allows the monitor MBean to perform any operations it needs
* before being registered in the MBean server.
* <P>
* Initializes the reference to the MBean server.
*
* @param server The MBean server in which the monitor MBean will
* be registered.
* @param name The object name of the monitor MBean.
*
* @return The name of the monitor MBean registered.
*
* @exception Exception
*/
throws Exception {
"preRegister(MBeanServer, ObjectName)",
"initialize the reference on the MBean server");
return name;
}
/**
* Allows the monitor MBean to perform any operations needed after
* having been registered in the MBean server or after the
* registration has failed.
* <P>
* Not used in this context.
*/
}
/**
* Allows the monitor MBean to perform any operations it needs
* before being unregistered by the MBean server.
* <P>
* Stops the monitor.
*
* @exception Exception
*/
"preDeregister()", "stop the monitor");
// Stop the Monitor.
//
stop();
}
/**
* Allows the monitor MBean to perform any operations needed after
* having been unregistered by the MBean server.
* <P>
* Not used in this context.
*/
public void postDeregister() {
}
/**
* Starts the monitor.
*/
public abstract void start();
/**
* Stops the monitor.
*/
public abstract void stop();
// GETTERS AND SETTERS
//--------------------
/**
* Returns the object name of the first object in the set of observed
* MBeans, or <code>null</code> if there is no such object.
*
* @return The object being observed.
*
* @see #setObservedObject(ObjectName)
*
* @deprecated As of JMX 1.2, replaced by {@link #getObservedObjects}
*/
if (observedObjects.isEmpty()) {
return null;
} else {
}
}
/**
* Removes all objects from the set of observed objects, and then adds the
* specified object.
*
* @param object The object to observe.
* @exception IllegalArgumentException The specified
* object is null.
*
* @see #getObservedObject()
*
* @deprecated As of JMX 1.2, replaced by {@link #addObservedObject}
*/
throws IllegalArgumentException {
throw new IllegalArgumentException("Null observed object");
return;
}
/**
* Adds the specified object in the set of observed MBeans, if this object
* is not already present.
*
* @param object The object to observe.
* @exception IllegalArgumentException The specified object is null.
*
*/
throws IllegalArgumentException {
throw new IllegalArgumentException("Null observed object");
}
// Check that the specified object is not already contained.
//
if (containsObservedObject(object))
return;
// Add the specified object in the list.
//
observedObjects.add(o);
// Update legacy protected stuff.
//
}
/**
* Removes the specified object from the set of observed MBeans.
*
* @param object The object to remove.
*
*/
// Check for null object.
//
return;
if (o != null) {
// Remove the specified object from the list.
//
// Update legacy protected stuff.
//
}
}
/**
* Tests whether the specified object is in the set of observed MBeans.
*
* @param object The object to check.
* @return <CODE>true</CODE> if the specified object is present,
* <CODE>false</CODE> otherwise.
*
*/
}
/**
* Returns an array containing the objects being observed.
*
* @return The objects being observed.
*
*/
return names;
}
/**
* Gets the attribute being observed.
* <BR>The observed attribute is not initialized by default (set to null).
*
* @return The attribute being observed.
*
* @see #setObservedAttribute
*/
return observedAttribute;
}
/**
* Sets the attribute to observe.
* <BR>The observed attribute is not initialized by default (set to null).
*
* @param attribute The attribute to observe.
* @exception IllegalArgumentException The specified
* attribute is null.
*
* @see #getObservedAttribute
*/
throws IllegalArgumentException {
throw new IllegalArgumentException("Null observed attribute");
}
// Update alreadyNotified array.
//
synchronized (this) {
if (observedAttribute != null &&
return;
// Reset the complex type attribute information
// such that it is recalculated again.
//
int index = 0;
for (ObservedObject o : observedObjects) {
resetAlreadyNotified(o, index++,
}
}
}
/**
* Gets the granularity period (in milliseconds).
* <BR>The default value of the granularity period is 10 seconds.
*
* @return The granularity period value.
*
* @see #setGranularityPeriod
*/
public synchronized long getGranularityPeriod() {
return granularityPeriod;
}
/**
* Sets the granularity period (in milliseconds).
* <BR>The default value of the granularity period is 10 seconds.
*
* @param period The granularity period value.
* @exception IllegalArgumentException The granularity
* period is less than or equal to zero.
*
* @see #getGranularityPeriod
*/
throws IllegalArgumentException {
if (period <= 0) {
throw new IllegalArgumentException("Nonpositive granularity " +
"period");
}
if (granularityPeriod == period)
return;
// Reschedule the scheduler task if the monitor is active.
//
if (isActive()) {
}
}
/**
* Tests whether the monitor MBean is active. A monitor MBean is
* marked active when the {@link #start start} method is called.
* It becomes inactive when the {@link #stop stop} method is
* called.
*
* @return <CODE>true</CODE> if the monitor MBean is active,
* <CODE>false</CODE> otherwise.
*/
/* This method must be synchronized so that the monitoring thread will
correctly see modifications to the isActive variable. See the MonitorTask
action executed by the Scheduled Executor Service. */
public synchronized boolean isActive() {
return isActive;
}
/*
* ------------------------------------------
* PACKAGE METHODS
* ------------------------------------------
*/
/**
* Starts the monitor.
*/
void doStart() {
"doStart()", "start the monitor");
synchronized (this) {
if (isActive()) {
"doStart()", "the monitor is already active");
return;
}
isActive = true;
// Reset the complex type attribute information
// such that it is recalculated again.
//
// Cache the AccessControlContext of the Monitor.start() caller.
// The monitor tasks will be executed within this context.
//
// Start the scheduler.
//
}
}
/**
* Stops the monitor.
*/
void doStop() {
"doStop()", "stop the monitor");
synchronized (this) {
if (!isActive()) {
"doStop()", "the monitor is not active");
return;
}
isActive = false;
// Cancel the scheduler task associated with the
// scheduler and its associated monitor task.
//
// Reset the AccessControlContext.
//
// Reset the complex type attribute information
// such that it is recalculated again.
//
}
}
/**
* Gets the derived gauge of the specified object, if this object is
* contained in the set of observed MBeans, or <code>null</code> otherwise.
*
* @param object the name of the object whose derived gauge is to
* be returned.
*
* @return The derived gauge of the specified object.
*
* @since 1.6
*/
}
/**
* Gets the derived gauge timestamp of the specified object, if
* this object is contained in the set of observed MBeans, or
* <code>0</code> otherwise.
*
* @param object the name of the object whose derived gauge
* timestamp is to be returned.
*
* @return The derived gauge timestamp of the specified object.
*
*/
}
throws AttributeNotFoundException,
// Check for "ObservedAttribute" replacement.
// This could happen if a thread A called setObservedAttribute()
// while other thread B was in the middle of the monitor() method
// and received the old observed attribute value.
//
final boolean lookupMBeanInfo;
synchronized (this) {
if (!isActive())
throw new IllegalArgumentException(
"The monitor has been stopped");
throw new IllegalArgumentException(
"The observed attribute has been changed");
}
// Look up MBeanInfo if needed
//
if (lookupMBeanInfo) {
try {
} catch (IntrospectionException e) {
throw new IllegalArgumentException(e);
}
} else {
}
// Check for complex type attribute
//
synchronized (this) {
if (!isActive())
throw new IllegalArgumentException(
"The monitor has been stopped");
throw new IllegalArgumentException(
"The observed attribute has been changed");
if (firstAttribute == null) {
break;
}
}
if (firstAttribute == null) {
isComplexTypeAttribute = true;
}
} else {
}
}
fa = firstAttribute;
}
}
throws AttributeNotFoundException {
if (isComplexTypeAttribute) {
return (Comparable<?>) v;
} else {
return (Comparable<?>) value;
}
}
Comparable<?> value) {
return true;
}
Comparable<?> value) {
return null;
}
}
Comparable<?> value) {
return (Comparable<?>) value;
}
Comparable<?> value){
return null;
}
Comparable<?> value) {
return true;
}
switch (type) {
case BYTE:
return Byte.class;
case SHORT:
return Short.class;
case INTEGER:
return Integer.class;
case LONG:
return Long.class;
case FLOAT:
return Float.class;
case DOUBLE:
return Double.class;
default:
throw new IllegalArgumentException(
"Unsupported numerical type");
}
}
}
/**
* Get the specified {@code ObservedObject} if this object is
* contained in the set of observed MBeans, or {@code null}
* otherwise.
*
* @param object the name of the {@code ObservedObject} to retrieve.
*
* @return The {@code ObservedObject} associated to the supplied
* {@code ObjectName}.
*
* @since 1.6
*/
for (ObservedObject o : observedObjects)
return o;
return null;
}
/**
* Factory method for ObservedObject creation.
*
* @since 1.6
*/
return new ObservedObject(object);
}
/**
* Create the {@link #alreadyNotified} array from
* the {@code ObservedObject} array list.
*/
synchronized void createAlreadyNotified() {
// Update elementCount.
//
// Update arrays.
//
alreadyNotifieds = new int[elementCount];
for (int i = 0; i < elementCount; i++) {
}
}
/**
* Update the deprecated {@link #alreadyNotified} field.
*/
synchronized void updateDeprecatedAlreadyNotified() {
if (elementCount > 0)
else
alreadyNotified = 0;
}
/**
* Update the {@link #alreadyNotifieds} array element at the given index
* with the already notified flag in the given {@code ObservedObject}.
* Ensure the deprecated {@link #alreadyNotified} field is updated
* if appropriate.
*/
if (index == 0)
}
/**
* Check if the given bits in the given element of {@link #alreadyNotifieds}
* are set.
*/
}
/**
* Set the given bits in the given element of {@link #alreadyNotifieds}.
* Ensure the deprecated {@link #alreadyNotified} field is updated
* if appropriate.
*/
if (i == -1)
return;
updateAlreadyNotified(o, i);
}
/**
* Reset the given bits in the given element of {@link #alreadyNotifieds}.
* Ensure the deprecated {@link #alreadyNotified} field is updated
* if appropriate.
*/
}
/**
* Reset all bits in the given element of {@link #alreadyNotifieds}.
* Ensure the deprecated {@link #alreadyNotified} field is updated
* if appropriate.
*/
if (i == -1)
return;
}
/**
* Check if the {@link #alreadyNotifieds} array has been modified.
* If true recompute the index for the given observed object.
*/
if (an == alreadyNotifieds) {
return index;
} else {
return observedObjects.indexOf(o);
}
}
/*
* ------------------------------------------
* PRIVATE METHODS
* ------------------------------------------
*/
/**
* This method is used by the monitor MBean to create and send a
* monitor notification to all the listeners registered for this
* kind of notification.
*
* @param type The notification type.
* @param timeStamp The notification emission date.
* @param msg The notification message.
* @param derGauge The derived gauge.
* type) that triggered off the notification.
* @param object The ObjectName of the observed object that triggered
* off the notification.
* @param onError Flag indicating if this monitor notification is
* an error notification or an alarm notification.
*/
if (!isActive())
return;
"sendNotification", "send notification: " +
"\n\tNotification observed object = " + object +
"\n\tNotification observed attribute = " + observedAttribute +
"\n\tNotification derived gauge = " + derGauge);
}
new MonitorNotification(type,
this,
msg,
trigger);
if (onError)
}
/**
* This method is called by the monitor each time
* the granularity period has been exceeded.
* @param o The observed object.
*/
if (!isActive())
return;
// Check that neither the observed object nor the
// observed attribute are null. If the observed
// object or observed attribute is null, this means
// that the monitor started before a complete
// initialization and nothing is done.
//
synchronized (this) {
object = o.getObservedObject();
return;
}
}
// Check that the observed object is registered in the
// MBean server and that the observed attribute
// belongs to the observed object.
//
try {
if (attributeValue == null)
if (isAlreadyNotified(
return;
else {
msg = "The observed attribute value is null.";
"monitor", msg);
}
} catch (NullPointerException np_ex) {
if (isAlreadyNotified(o, RUNTIME_ERROR_NOTIFIED))
return;
else {
msg =
"The monitor must be registered in the MBean " +
"server or an MBeanServerConnection must be " +
"explicitly supplied.";
"monitor", msg);
}
} catch (InstanceNotFoundException inf_ex) {
return;
else {
msg =
"The observed object must be accessible in " +
"the MBeanServerConnection.";
"monitor", msg);
}
} catch (AttributeNotFoundException anf_ex) {
return;
else {
msg =
"The observed attribute must be accessible in " +
"the observed object.";
"monitor", msg);
}
} catch (MBeanException mb_ex) {
if (isAlreadyNotified(o, RUNTIME_ERROR_NOTIFIED))
return;
else {
"monitor", msg);
}
} catch (ReflectionException ref_ex) {
if (isAlreadyNotified(o, RUNTIME_ERROR_NOTIFIED)) {
return;
} else {
"monitor", msg);
}
} catch (IOException io_ex) {
if (isAlreadyNotified(o, RUNTIME_ERROR_NOTIFIED))
return;
else {
"monitor", msg);
}
} catch (RuntimeException rt_ex) {
if (isAlreadyNotified(o, RUNTIME_ERROR_NOTIFIED))
return;
else {
"monitor", msg);
}
}
synchronized (this) {
// Check if the monitor has been stopped.
//
if (!isActive())
return;
// Check if the observed attribute has been changed.
//
// Avoid race condition where mbs.getAttribute() succeeded but
// another thread replaced the observed attribute meanwhile.
//
// Avoid setting computed derived gauge on erroneous attribute.
//
return;
// Derive a Comparable object from the ObservedAttribute value
// if the type of the ObservedAttribute value is a complex type.
//
try {
} catch (ClassCastException e) {
if (isAlreadyNotified(
return;
else {
msg =
"The observed attribute value does not " +
"implement the Comparable interface.";
}
} catch (AttributeNotFoundException e) {
return;
else {
msg =
"The observed attribute must be accessible in " +
"the observed object.";
}
} catch (RuntimeException e) {
if (isAlreadyNotified(o, RUNTIME_ERROR_NOTIFIED))
return;
else {
}
}
}
// Check that the observed attribute type is supported by this
// monitor.
//
if (isAlreadyNotified(
return;
else {
msg = "The observed attribute type is not valid.";
}
}
}
// Check that threshold type is supported by this monitor.
//
return;
else {
msg = "The threshold type is not valid.";
}
}
}
// Let someone subclassing the monitor to perform additional
// monitor consistency checks and report errors if necessary.
//
if (isAlreadyNotified(o, RUNTIME_ERROR_NOTIFIED))
return;
else {
}
}
}
// If no errors were found then clear all error flags and
// let the monitor decide if a notification must be sent.
//
// Clear all already notified flags.
//
// Get derived gauge from comparable value.
//
value);
// Check if an alarm must be fired.
//
(Comparable<?>) derGauge);
}
}
// Notify monitor errors
//
msg,
true);
// Notify monitor alarms
//
alarm.getMessage(),
alarm.getTrigger(),
false);
}
/**
* Cleanup the scheduler and monitor tasks futures.
*/
private synchronized void cleanupFutures() {
if (schedulerFuture != null) {
schedulerFuture.cancel(false);
}
if (monitorFuture != null) {
monitorFuture.cancel(false);
}
}
/**
* Cleanup the "is complex type attribute" info.
*/
private synchronized void cleanupIsComplexTypeAttribute() {
isComplexTypeAttribute = false;
}
/**
* SchedulerTask nested class: This class implements the Runnable interface.
*
* The SchedulerTask is executed periodically with a given fixed delay by
* the Scheduled Executor Service.
*/
/*
* ------------------------------------------
* CONSTRUCTORS
* ------------------------------------------
*/
public SchedulerTask() {
}
/*
* ------------------------------------------
* ------------------------------------------
*/
}
/*
* ------------------------------------------
* PUBLIC METHODS
* ------------------------------------------
*/
public void run() {
synchronized (Monitor.this) {
}
}
}
/**
* MonitorTask nested class: This class implements the Runnable interface.
*
* The MonitorTask is executed periodically with a given fixed delay by the
* Scheduled Executor Service.
*/
/*
* ------------------------------------------
* CONSTRUCTORS
* ------------------------------------------
*/
public MonitorTask() {
// Find out if there's already an existing executor for the calling
// thread and reuse it. Otherwise, create a new one and store it in
// the executors map. If there is a SecurityManager, the group of
// System.getSecurityManager() is used, else the group of the thread
// instantiating this MonitorTask, i.e. the group of the thread that
// calls "Monitor.start()".
synchronized (executorsLock) {
executor = e;
break;
}
}
executor = new ThreadPoolExecutor(
60L,
new LinkedBlockingQueue<Runnable>(),
new DaemonThreadFactory("ThreadGroup<" +
executor.allowCoreThreadTimeOut(true);
}
}
}
/*
* ------------------------------------------
* PUBLIC METHODS
* ------------------------------------------
*/
}
public void run() {
final ScheduledFuture<?> sf;
final AccessControlContext ac;
synchronized (Monitor.this) {
}
final int an[] = alreadyNotifieds;
int index = 0;
}
}
}
return null;
}
};
throw new SecurityException("AccessControlContext cannot be null");
}
synchronized (Monitor.this) {
Monitor.this.schedulerFuture =
Monitor.this.getGranularityPeriod(),
}
}
}
}
/**
* Daemon thread factory used by the monitor executors.
* <P>
* This factory creates all new threads used by an Executor in
* the same ThreadGroup. If there is a SecurityManager, it uses
* the group of System.getSecurityManager(), else the group of
* the thread instantiating this DaemonThreadFactory. Each new
* thread is created as a daemon thread with priority
* Thread.NORM_PRIORITY. New threads have names accessible via
* Thread.getName() of "JMX Monitor <pool-name> Pool [Thread-M]",
* where M is the sequence number of the thread created by this
* factory.
*/
}
group = threadGroup;
}
return group;
}
r,
0);
t.setDaemon(true);
return t;
}
}
}