jdk/src/share/classes/javax/management/monitor/Monitor.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 526 61ba2d5ea9da
child 1446 605e30465515
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 526
diff changeset
     2
 * Copyright 1999-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.management.monitor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import static com.sun.jmx.defaults.JmxProperties.MONITOR_LOGGER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import com.sun.jmx.mbeanserver.GetPropertyAction;
34
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
    30
import com.sun.jmx.mbeanserver.Introspector;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.AccessControlContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.concurrent.CopyOnWriteArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.concurrent.ExecutorService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.concurrent.Executors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.concurrent.Future;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.concurrent.LinkedBlockingQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.concurrent.ScheduledExecutorService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.concurrent.ScheduledFuture;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.concurrent.ThreadFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.util.concurrent.ThreadPoolExecutor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.util.concurrent.TimeUnit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.concurrent.atomic.AtomicInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.util.concurrent.atomic.AtomicLong;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.util.logging.Level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.management.AttributeNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import javax.management.InstanceNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import javax.management.IntrospectionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import javax.management.MBeanAttributeInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import javax.management.MBeanException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import javax.management.MBeanInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import javax.management.MBeanRegistration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import javax.management.MBeanServerConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import javax.management.NotificationBroadcasterSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import javax.management.ReflectionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import static javax.management.monitor.MonitorNotification.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * Defines the part common to all monitor MBeans.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * A monitor MBean monitors values of an attribute common to a set of observed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * MBeans. The observed attribute is monitored at intervals specified by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * granularity period. A gauge value (derived gauge) is derived from the values
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * of the observed attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
public abstract class Monitor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    extends NotificationBroadcasterSupport
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    implements MonitorMBean, MBeanRegistration {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     *  PACKAGE CLASSES
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    static class ObservedObject {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        public ObservedObject(ObjectName observedObject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            this.observedObject = observedObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        public final ObjectName getObservedObject() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            return observedObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        public final synchronized int getAlreadyNotified() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            return alreadyNotified;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        public final synchronized void setAlreadyNotified(int alreadyNotified) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            this.alreadyNotified = alreadyNotified;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        public final synchronized Object getDerivedGauge() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            return derivedGauge;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        public final synchronized void setDerivedGauge(Object derivedGauge) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            this.derivedGauge = derivedGauge;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        public final synchronized long getDerivedGaugeTimeStamp() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            return derivedGaugeTimeStamp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        public final synchronized void setDerivedGaugeTimeStamp(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                                                 long derivedGaugeTimeStamp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            this.derivedGaugeTimeStamp = derivedGaugeTimeStamp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        private final ObjectName observedObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        private int alreadyNotified;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        private Object derivedGauge;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        private long derivedGaugeTimeStamp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *  PRIVATE VARIABLES
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Attribute to observe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    private String observedAttribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Monitor granularity period (in milliseconds).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * The default value is set to 10 seconds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private long granularityPeriod = 10000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Monitor state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * The default value is set to <CODE>false</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    private boolean isActive = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Monitor sequence number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * The default value is set to 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private final AtomicLong sequenceNumber = new AtomicLong();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Complex type attribute flag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * The default value is set to <CODE>false</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    private boolean isComplexTypeAttribute = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * First attribute name extracted from complex type attribute name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    private String firstAttribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Remaining attribute names extracted from complex type attribute name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    private final List<String> remainingAttributes =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        new CopyOnWriteArrayList<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * AccessControlContext of the Monitor.start() caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    private AccessControlContext acc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Scheduler Service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    private static final ScheduledExecutorService scheduler =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        Executors.newSingleThreadScheduledExecutor(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            new DaemonThreadFactory("Scheduler"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * Maximum Pool Size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    private static final int maximumPoolSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * Executor Service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    private static final ExecutorService executor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        final String maximumPoolSizeSysProp = "jmx.x.monitor.maximum.pool.size";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        final String maximumPoolSizeStr = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            new GetPropertyAction(maximumPoolSizeSysProp));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (maximumPoolSizeStr == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            maximumPoolSizeStr.trim().length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            maximumPoolSize = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            int maximumPoolSizeTmp = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                maximumPoolSizeTmp = Integer.parseInt(maximumPoolSizeStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                if (MONITOR_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    MONITOR_LOGGER.logp(Level.FINER, Monitor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                            "<static initializer>",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                            "Wrong value for " + maximumPoolSizeSysProp +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                            " system property", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    MONITOR_LOGGER.logp(Level.FINER, Monitor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                            "<static initializer>",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                            maximumPoolSizeSysProp + " defaults to 10");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                maximumPoolSizeTmp = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            if (maximumPoolSizeTmp < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                maximumPoolSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                maximumPoolSize = maximumPoolSizeTmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        executor = new ThreadPoolExecutor(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                maximumPoolSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                maximumPoolSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                60L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                TimeUnit.SECONDS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                new LinkedBlockingQueue<Runnable>(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                new DaemonThreadFactory("Executor"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        ((ThreadPoolExecutor)executor).allowCoreThreadTimeOut(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Monitor task to be executed by the Executor Service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    private final MonitorTask monitorTask = new MonitorTask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * Future associated to the current monitor task.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    private Future<?> monitorFuture;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * Scheduler task to be executed by the Scheduler Service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    private final SchedulerTask schedulerTask = new SchedulerTask(monitorTask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * ScheduledFuture associated to the current scheduler task.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    private ScheduledFuture<?> schedulerFuture;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *  PROTECTED VARIABLES
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * The amount by which the capacity of the monitor arrays are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * automatically incremented when their size becomes greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * their capacity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    protected final static int capacityIncrement = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * The number of valid components in the vector of observed objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    protected int elementCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * Monitor errors that have already been notified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @deprecated equivalent to {@link #alreadyNotifieds}[0].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    protected int alreadyNotified = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * <p>Selected monitor errors that have already been notified.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * <p>Each element in this array corresponds to an observed object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * in the vector.  It contains a bit mask of the flags {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * #OBSERVED_OBJECT_ERROR_NOTIFIED} etc, indicating whether the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * corresponding notification has already been sent for the MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * being monitored.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    protected int alreadyNotifieds[] = new int[capacityIncrement];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * Reference to the MBean server.  This reference is null when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * monitor MBean is not registered in an MBean server.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * reference is initialized before the monitor MBean is registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * in the MBean server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @see #preRegister(MBeanServer server, ObjectName name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    protected MBeanServer server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    // Flags defining possible monitor errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * This flag is used to reset the {@link #alreadyNotifieds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * alreadyNotifieds} monitor attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    protected static final int RESET_FLAGS_ALREADY_NOTIFIED             = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * Flag denoting that a notification has occurred after changing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * the observed object.  This flag is used to check that the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * observed object is registered in the MBean server at the time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * of the first notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    protected static final int OBSERVED_OBJECT_ERROR_NOTIFIED           = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * Flag denoting that a notification has occurred after changing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * the observed attribute.  This flag is used to check that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * new observed attribute belongs to the observed object at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * time of the first notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    protected static final int OBSERVED_ATTRIBUTE_ERROR_NOTIFIED        = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * Flag denoting that a notification has occurred after changing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * the observed object or the observed attribute.  This flag is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * used to check that the observed attribute type is correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * (depending on the monitor in use) at the time of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    protected static final int OBSERVED_ATTRIBUTE_TYPE_ERROR_NOTIFIED   = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * Flag denoting that a notification has occurred after changing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * the observed object or the observed attribute.  This flag is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * used to notify any exception (except the cases described above)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * when trying to get the value of the observed attribute at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * time of the first notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    protected static final int RUNTIME_ERROR_NOTIFIED                   = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * This field is retained for compatibility but should not be referenced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @deprecated No replacement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    protected String dbgTag = Monitor.class.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *  PACKAGE VARIABLES
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * List of ObservedObjects to which the attribute to observe belongs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    final List<ObservedObject> observedObjects =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        new CopyOnWriteArrayList<ObservedObject>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * Flag denoting that a notification has occurred after changing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * the threshold. This flag is used to notify any exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * related to invalid thresholds settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    static final int THRESHOLD_ERROR_NOTIFIED                           = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * Enumeration used to keep trace of the derived gauge type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * in counter and gauge monitors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    enum NumericalType { BYTE, SHORT, INTEGER, LONG, FLOAT, DOUBLE };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * Constant used to initialize all the numeric values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     */
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 34
diff changeset
   370
    static final Integer INTEGER_ZERO = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *  PUBLIC METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * Allows the monitor MBean to perform any operations it needs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * before being registered in the MBean server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * Initializes the reference to the MBean server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @param server The MBean server in which the monitor MBean will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * be registered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * @param name The object name of the monitor MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * @return The name of the monitor MBean registered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @exception Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    public ObjectName preRegister(MBeanServer server, ObjectName name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        MONITOR_LOGGER.logp(Level.FINER, Monitor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                "preRegister(MBeanServer, ObjectName)",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                "initialize the reference on the MBean server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        this.server = server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * Allows the monitor MBean to perform any operations needed after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * having been registered in the MBean server or after the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * registration has failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * Not used in this context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    public void postRegister(Boolean registrationDone) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * Allows the monitor MBean to perform any operations it needs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * before being unregistered by the MBean server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * Stops the monitor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @exception Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    public void preDeregister() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        MONITOR_LOGGER.logp(Level.FINER, Monitor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                "preDeregister()", "stop the monitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        // Stop the Monitor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * Allows the monitor MBean to perform any operations needed after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * having been unregistered by the MBean server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * Not used in this context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    public void postDeregister() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * Starts the monitor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    public abstract void start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * Stops the monitor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    public abstract void stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    // GETTERS AND SETTERS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    //--------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * Returns the object name of the first object in the set of observed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * MBeans, or <code>null</code> if there is no such object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * @return The object being observed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * @see #setObservedObject(ObjectName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * @deprecated As of JMX 1.2, replaced by {@link #getObservedObjects}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    public synchronized ObjectName getObservedObject() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        if (observedObjects.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            return observedObjects.get(0).getObservedObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * Removes all objects from the set of observed objects, and then adds the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * specified object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * @param object The object to observe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * @exception IllegalArgumentException The specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * object is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * @see #getObservedObject()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * @deprecated As of JMX 1.2, replaced by {@link #addObservedObject}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    public synchronized void setObservedObject(ObjectName object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        throws IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        if (object == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            throw new IllegalArgumentException("Null observed object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        if (observedObjects.size() == 1 && containsObservedObject(object))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        observedObjects.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        addObservedObject(object);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * Adds the specified object in the set of observed MBeans, if this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * is not already present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * @param object The object to observe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * @exception IllegalArgumentException The specified object is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    public synchronized void addObservedObject(ObjectName object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        throws IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        if (object == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            throw new IllegalArgumentException("Null observed object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        // Check that the specified object is not already contained.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        if (containsObservedObject(object))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        // Add the specified object in the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        ObservedObject o = createObservedObject(object);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        o.setAlreadyNotified(RESET_FLAGS_ALREADY_NOTIFIED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        o.setDerivedGauge(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        o.setDerivedGaugeTimeStamp(System.currentTimeMillis());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        observedObjects.add(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        // Update legacy protected stuff.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        createAlreadyNotified();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * Removes the specified object from the set of observed MBeans.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * @param object The object to remove.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    public synchronized void removeObservedObject(ObjectName object) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        // Check for null object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        if (object == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        final ObservedObject o = getObservedObject(object);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        if (o != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            // Remove the specified object from the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            observedObjects.remove(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            // Update legacy protected stuff.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            createAlreadyNotified();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * Tests whether the specified object is in the set of observed MBeans.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * @param object The object to check.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * @return <CODE>true</CODE> if the specified object is present,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * <CODE>false</CODE> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    public synchronized boolean containsObservedObject(ObjectName object) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        return getObservedObject(object) != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * Returns an array containing the objects being observed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * @return The objects being observed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    public synchronized ObjectName[] getObservedObjects() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        ObjectName[] names = new ObjectName[observedObjects.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        for (int i = 0; i < names.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            names[i] = observedObjects.get(i).getObservedObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        return names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * Gets the attribute being observed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * <BR>The observed attribute is not initialized by default (set to null).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * @return The attribute being observed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * @see #setObservedAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    public synchronized String getObservedAttribute() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        return observedAttribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * Sets the attribute to observe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * <BR>The observed attribute is not initialized by default (set to null).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * @param attribute The attribute to observe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * @exception IllegalArgumentException The specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * attribute is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * @see #getObservedAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    public void setObservedAttribute(String attribute)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        throws IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        if (attribute == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            throw new IllegalArgumentException("Null observed attribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        // Update alreadyNotified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            if (observedAttribute != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                observedAttribute.equals(attribute))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            observedAttribute = attribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            // Reset the complex type attribute information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            // such that it is recalculated again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            cleanupIsComplexTypeAttribute();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            for (ObservedObject o : observedObjects) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                resetAlreadyNotified(o, index++,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                                     OBSERVED_ATTRIBUTE_ERROR_NOTIFIED |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                                     OBSERVED_ATTRIBUTE_TYPE_ERROR_NOTIFIED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * Gets the granularity period (in milliseconds).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * <BR>The default value of the granularity period is 10 seconds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * @return The granularity period value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * @see #setGranularityPeriod
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    public synchronized long getGranularityPeriod() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        return granularityPeriod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * Sets the granularity period (in milliseconds).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * <BR>The default value of the granularity period is 10 seconds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * @param period The granularity period value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * @exception IllegalArgumentException The granularity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * period is less than or equal to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * @see #getGranularityPeriod
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    public synchronized void setGranularityPeriod(long period)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        throws IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        if (period <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            throw new IllegalArgumentException("Nonpositive granularity " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                                               "period");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        if (granularityPeriod == period)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        granularityPeriod = period;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        // Reschedule the scheduler task if the monitor is active.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        if (isActive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            cleanupFutures();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            schedulerFuture = scheduler.schedule(schedulerTask,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                                                 period,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                                                 TimeUnit.MILLISECONDS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * Tests whether the monitor MBean is active.  A monitor MBean is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * marked active when the {@link #start start} method is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * It becomes inactive when the {@link #stop stop} method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * @return <CODE>true</CODE> if the monitor MBean is active,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * <CODE>false</CODE> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    /* This method must be synchronized so that the monitoring thread will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
       correctly see modifications to the isActive variable. See the MonitorTask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
       action executed by the Scheduled Executor Service. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    public synchronized boolean isActive() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        return isActive;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     *  PACKAGE METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * Starts the monitor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    void doStart() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            MONITOR_LOGGER.logp(Level.FINER, Monitor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                "doStart()", "start the monitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            if (isActive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                MONITOR_LOGGER.logp(Level.FINER, Monitor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                        "doStart()", "the monitor is already active");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            isActive = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            // Reset the complex type attribute information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            // such that it is recalculated again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            cleanupIsComplexTypeAttribute();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            // Cache the AccessControlContext of the Monitor.start() caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            // The monitor tasks will be executed within this context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            acc = AccessController.getContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            // Start the scheduler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            cleanupFutures();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            schedulerFuture = scheduler.schedule(schedulerTask,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                                                 getGranularityPeriod(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                                                 TimeUnit.MILLISECONDS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * Stops the monitor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    void doStop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        MONITOR_LOGGER.logp(Level.FINER, Monitor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                "doStop()", "stop the monitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            if (!isActive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                MONITOR_LOGGER.logp(Level.FINER, Monitor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                        "doStop()", "the monitor is not active");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            isActive = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            // Cancel the scheduler task associated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            // scheduler and its associated monitor task.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            cleanupFutures();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            // Reset the AccessControlContext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            acc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            // Reset the complex type attribute information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            // such that it is recalculated again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            cleanupIsComplexTypeAttribute();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * Gets the derived gauge of the specified object, if this object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * contained in the set of observed MBeans, or <code>null</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * @param object the name of the object whose derived gauge is to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * @return The derived gauge of the specified object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    synchronized Object getDerivedGauge(ObjectName object) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        final ObservedObject o = getObservedObject(object);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        return o == null ? null : o.getDerivedGauge();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * Gets the derived gauge timestamp of the specified object, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * this object is contained in the set of observed MBeans, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * <code>0</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * @param object the name of the object whose derived gauge
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * timestamp is to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * @return The derived gauge timestamp of the specified object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    synchronized long getDerivedGaugeTimeStamp(ObjectName object) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        final ObservedObject o = getObservedObject(object);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        return o == null ? 0 : o.getDerivedGaugeTimeStamp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    Object getAttribute(MBeanServerConnection mbsc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                        ObjectName object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                        String attribute)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        throws AttributeNotFoundException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
               InstanceNotFoundException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
               MBeanException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
               ReflectionException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
               IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        // Check for "ObservedAttribute" replacement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        // This could happen if a thread A called setObservedAttribute()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        // while other thread B was in the middle of the monitor() method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        // and received the old observed attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        final boolean lookupMBeanInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            if (!isActive())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                    "The monitor has been stopped");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            if (!attribute.equals(getObservedAttribute()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                    "The observed attribute has been changed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            lookupMBeanInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                (firstAttribute == null && attribute.indexOf('.') != -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        // Look up MBeanInfo if needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        final MBeanInfo mbi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        if (lookupMBeanInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                mbi = mbsc.getMBeanInfo(object);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            } catch (IntrospectionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                throw new IllegalArgumentException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            mbi = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        // Check for complex type attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        final String fa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            if (!isActive())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                    "The monitor has been stopped");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            if (!attribute.equals(getObservedAttribute()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                    "The observed attribute has been changed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            if (firstAttribute == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                if (attribute.indexOf('.') != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                    MBeanAttributeInfo mbaiArray[] = mbi.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                    for (MBeanAttributeInfo mbai : mbaiArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                        if (attribute.equals(mbai.getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                            firstAttribute = attribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                    if (firstAttribute == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                        String tokens[] = attribute.split("\\.", -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                        firstAttribute = tokens[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                        for (int i = 1; i < tokens.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                            remainingAttributes.add(tokens[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                        isComplexTypeAttribute = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                    firstAttribute = attribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            fa = firstAttribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        return mbsc.getAttribute(object, fa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    Comparable<?> getComparableFromAttribute(ObjectName object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                                             String attribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                                             Object value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        throws AttributeNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        if (isComplexTypeAttribute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            Object v = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            for (String attr : remainingAttributes)
34
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents: 2
diff changeset
   873
                v = Introspector.elementFromComplex(v, attr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            return (Comparable<?>) v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            return (Comparable<?>) value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    boolean isComparableTypeValid(ObjectName object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                                  String attribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                                  Comparable<?> value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
    String buildErrorNotification(ObjectName object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                                  String attribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                                  Comparable<?> value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    void onErrorNotification(MonitorNotification notification) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
    Comparable<?> getDerivedGaugeFromComparable(ObjectName object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                                                String attribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                                                Comparable<?> value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        return (Comparable<?>) value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
    MonitorNotification buildAlarmNotification(ObjectName object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                                               String attribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                                               Comparable<?> value){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
    boolean isThresholdTypeValid(ObjectName object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                                 String attribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                                 Comparable<?> value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    static Class<? extends Number> classForType(NumericalType type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            case BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                return Byte.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            case SHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                return Short.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            case INTEGER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                return Integer.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            case LONG:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                return Long.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            case FLOAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                return Float.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            case DOUBLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                return Double.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                    "Unsupported numerical type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
    static boolean isValidForType(Object value, Class<? extends Number> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        return ((value == INTEGER_ZERO) || c.isInstance(value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * Get the specified {@code ObservedObject} if this object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * contained in the set of observed MBeans, or {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * @param object the name of the {@code ObservedObject} to retrieve.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * @return The {@code ObservedObject} associated to the supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * {@code ObjectName}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
    synchronized ObservedObject getObservedObject(ObjectName object) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        for (ObservedObject o : observedObjects)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            if (o.getObservedObject().equals(object))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                return o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * Factory method for ObservedObject creation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
    ObservedObject createObservedObject(ObjectName object) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        return new ObservedObject(object);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * Create the {@link #alreadyNotified} array from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * the {@code ObservedObject} array list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
    synchronized void createAlreadyNotified() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        // Update elementCount.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        elementCount = observedObjects.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        // Update arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        alreadyNotifieds = new int[elementCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        for (int i = 0; i < elementCount; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
            alreadyNotifieds[i] = observedObjects.get(i).getAlreadyNotified();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        updateDeprecatedAlreadyNotified();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * Update the deprecated {@link #alreadyNotified} field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
    synchronized void updateDeprecatedAlreadyNotified() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        if (elementCount > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
            alreadyNotified = alreadyNotifieds[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            alreadyNotified = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     * Update the {@link #alreadyNotifieds} array element at the given index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     * with the already notified flag in the given {@code ObservedObject}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * Ensure the deprecated {@link #alreadyNotified} field is updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     * if appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
    synchronized void updateAlreadyNotified(ObservedObject o, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        alreadyNotifieds[index] = o.getAlreadyNotified();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        if (index == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            updateDeprecatedAlreadyNotified();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * Check if the given bits in the given element of {@link #alreadyNotifieds}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * are set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
    synchronized boolean isAlreadyNotified(ObservedObject o, int mask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        return ((o.getAlreadyNotified() & mask) != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * Set the given bits in the given element of {@link #alreadyNotifieds}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * Ensure the deprecated {@link #alreadyNotified} field is updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * if appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
    synchronized void setAlreadyNotified(ObservedObject o, int index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                                         int mask, int an[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        final int i = computeAlreadyNotifiedIndex(o, index, an);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        if (i == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        o.setAlreadyNotified(o.getAlreadyNotified() | mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        updateAlreadyNotified(o, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     * Reset the given bits in the given element of {@link #alreadyNotifieds}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * Ensure the deprecated {@link #alreadyNotified} field is updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * if appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
    synchronized void resetAlreadyNotified(ObservedObject o,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                                           int index, int mask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        o.setAlreadyNotified(o.getAlreadyNotified() & ~mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        updateAlreadyNotified(o, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     * Reset all bits in the given element of {@link #alreadyNotifieds}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * Ensure the deprecated {@link #alreadyNotified} field is updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * if appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
    synchronized void resetAllAlreadyNotified(ObservedObject o,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                                              int index, int an[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        final int i = computeAlreadyNotifiedIndex(o, index, an);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        if (i == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        o.setAlreadyNotified(RESET_FLAGS_ALREADY_NOTIFIED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        updateAlreadyNotified(o, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * Check if the {@link #alreadyNotifieds} array has been modified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * If true recompute the index for the given observed object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
    synchronized int computeAlreadyNotifiedIndex(ObservedObject o,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                                                 int index, int an[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        if (an == alreadyNotifieds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
            return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
            return observedObjects.indexOf(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     *  PRIVATE METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     * This method is used by the monitor MBean to create and send a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     * monitor notification to all the listeners registered for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * kind of notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * @param type The notification type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     * @param timeStamp The notification emission date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * @param msg The notification message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     * @param derGauge The derived gauge.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * @param trigger The threshold/string (depending on the monitor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     * type) that triggered off the notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     * @param object The ObjectName of the observed object that triggered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     * off the notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     * @param onError Flag indicating if this monitor notification is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     * an error notification or an alarm notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
    private void sendNotification(String type, long timeStamp, String msg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                                  Object derGauge, Object trigger,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
                                  ObjectName object, boolean onError) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        if (!isActive())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        if (MONITOR_LOGGER.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            MONITOR_LOGGER.logp(Level.FINER, Monitor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                    "sendNotification", "send notification: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                    "\n\tNotification observed object = " + object +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                    "\n\tNotification observed attribute = " + observedAttribute +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                    "\n\tNotification derived gauge = " + derGauge);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        long seqno = sequenceNumber.getAndIncrement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        MonitorNotification mn =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            new MonitorNotification(type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                                    this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                                    seqno,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                                    timeStamp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                                    msg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
                                    object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                                    observedAttribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                                    derGauge,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                                    trigger);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        if (onError)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            onErrorNotification(mn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        sendNotification(mn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     * This method is called by the monitor each time
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     * the granularity period has been exceeded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     * @param o The observed object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
    private void monitor(ObservedObject o, int index, int an[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 34
diff changeset
  1125
        String attribute;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        String notifType = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        String msg = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        Object derGauge = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        Object trigger = null;
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 34
diff changeset
  1130
        ObjectName object;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        Comparable<?> value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        MonitorNotification alarm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        if (!isActive())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        // Check that neither the observed object nor the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        // observed attribute are null.  If the observed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        // object or observed attribute is null, this means
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        // that the monitor started before a complete
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        // initialization and nothing is done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
            object = o.getObservedObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            attribute = getObservedAttribute();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
            if (object == null || attribute == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        // Check that the observed object is registered in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        // MBean server and that the observed attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        // belongs to the observed object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        Object attributeValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
            attributeValue = getAttribute(server, object, attribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
            if (attributeValue == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
                if (isAlreadyNotified(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
                        o, OBSERVED_ATTRIBUTE_TYPE_ERROR_NOTIFIED))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
                    notifType = OBSERVED_ATTRIBUTE_TYPE_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
                    setAlreadyNotified(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
                        o, index, OBSERVED_ATTRIBUTE_TYPE_ERROR_NOTIFIED, an);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
                    msg = "The observed attribute value is null.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
                    MONITOR_LOGGER.logp(Level.FINEST, Monitor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
                            "monitor", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        } catch (NullPointerException np_ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
            if (isAlreadyNotified(o, RUNTIME_ERROR_NOTIFIED))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
                notifType = RUNTIME_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
                setAlreadyNotified(o, index, RUNTIME_ERROR_NOTIFIED, an);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
                msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
                    "The monitor must be registered in the MBean " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
                    "server or an MBeanServerConnection must be " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
                    "explicitly supplied.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
                MONITOR_LOGGER.logp(Level.FINEST, Monitor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
                        "monitor", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
                MONITOR_LOGGER.logp(Level.FINEST, Monitor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
                        "monitor", np_ex.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        } catch (InstanceNotFoundException inf_ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
            if (isAlreadyNotified(o, OBSERVED_OBJECT_ERROR_NOTIFIED))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
                notifType = OBSERVED_OBJECT_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
                setAlreadyNotified(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
                    o, index, OBSERVED_OBJECT_ERROR_NOTIFIED, an);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
                msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                    "The observed object must be accessible in " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
                    "the MBeanServerConnection.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
                MONITOR_LOGGER.logp(Level.FINEST, Monitor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
                        "monitor", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
                MONITOR_LOGGER.logp(Level.FINEST, Monitor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
                        "monitor", inf_ex.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        } catch (AttributeNotFoundException anf_ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
            if (isAlreadyNotified(o, OBSERVED_ATTRIBUTE_ERROR_NOTIFIED))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
                notifType = OBSERVED_ATTRIBUTE_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
                setAlreadyNotified(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
                    o, index, OBSERVED_ATTRIBUTE_ERROR_NOTIFIED, an);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
                msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
                    "The observed attribute must be accessible in " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                    "the observed object.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                MONITOR_LOGGER.logp(Level.FINEST, Monitor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                        "monitor", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                MONITOR_LOGGER.logp(Level.FINEST, Monitor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                        "monitor", anf_ex.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        } catch (MBeanException mb_ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
            if (isAlreadyNotified(o, RUNTIME_ERROR_NOTIFIED))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
                notifType = RUNTIME_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
                setAlreadyNotified(o, index, RUNTIME_ERROR_NOTIFIED, an);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
                msg = mb_ex.getMessage() == null ? "" : mb_ex.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
                MONITOR_LOGGER.logp(Level.FINEST, Monitor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
                        "monitor", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
                MONITOR_LOGGER.logp(Level.FINEST, Monitor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
                        "monitor", mb_ex.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        } catch (ReflectionException ref_ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
            if (isAlreadyNotified(o, RUNTIME_ERROR_NOTIFIED)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
                notifType = RUNTIME_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
                setAlreadyNotified(o, index, RUNTIME_ERROR_NOTIFIED, an);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
                msg = ref_ex.getMessage() == null ? "" : ref_ex.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
                MONITOR_LOGGER.logp(Level.FINEST, Monitor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
                        "monitor", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
                MONITOR_LOGGER.logp(Level.FINEST, Monitor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
                        "monitor", ref_ex.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        } catch (IOException io_ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
            if (isAlreadyNotified(o, RUNTIME_ERROR_NOTIFIED))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
                notifType = RUNTIME_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
                setAlreadyNotified(o, index, RUNTIME_ERROR_NOTIFIED, an);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
                msg = io_ex.getMessage() == null ? "" : io_ex.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
                MONITOR_LOGGER.logp(Level.FINEST, Monitor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
                        "monitor", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
                MONITOR_LOGGER.logp(Level.FINEST, Monitor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
                        "monitor", io_ex.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
        } catch (RuntimeException rt_ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
            if (isAlreadyNotified(o, RUNTIME_ERROR_NOTIFIED))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
                notifType = RUNTIME_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
                setAlreadyNotified(o, index, RUNTIME_ERROR_NOTIFIED, an);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
                msg = rt_ex.getMessage() == null ? "" : rt_ex.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
                MONITOR_LOGGER.logp(Level.FINEST, Monitor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
                        "monitor", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
                MONITOR_LOGGER.logp(Level.FINEST, Monitor.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
                        "monitor", rt_ex.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
            // Check if the monitor has been stopped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
            if (!isActive())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
            // Check if the observed attribute has been changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
            // Avoid race condition where mbs.getAttribute() succeeded but
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
            // another thread replaced the observed attribute meanwhile.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
            // Avoid setting computed derived gauge on erroneous attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
            if (!attribute.equals(getObservedAttribute()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
            // Derive a Comparable object from the ObservedAttribute value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
            // if the type of the ObservedAttribute value is a complex type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
            if (msg == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
                    value = getComparableFromAttribute(object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                                                       attribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
                                                       attributeValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
                } catch (ClassCastException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
                    if (isAlreadyNotified(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
                            o, OBSERVED_ATTRIBUTE_TYPE_ERROR_NOTIFIED))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
                        notifType = OBSERVED_ATTRIBUTE_TYPE_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
                        setAlreadyNotified(o, index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
                            OBSERVED_ATTRIBUTE_TYPE_ERROR_NOTIFIED, an);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
                        msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
                            "The observed attribute value does not " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
                            "implement the Comparable interface.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
                        MONITOR_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                                Monitor.class.getName(), "monitor", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
                        MONITOR_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
                                Monitor.class.getName(), "monitor", e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
                } catch (AttributeNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
                    if (isAlreadyNotified(o, OBSERVED_ATTRIBUTE_ERROR_NOTIFIED))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
                        notifType = OBSERVED_ATTRIBUTE_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
                        setAlreadyNotified(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
                            o, index, OBSERVED_ATTRIBUTE_ERROR_NOTIFIED, an);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
                        msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                            "The observed attribute must be accessible in " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                            "the observed object.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                        MONITOR_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
                                Monitor.class.getName(), "monitor", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
                        MONITOR_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
                                Monitor.class.getName(), "monitor", e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
                    if (isAlreadyNotified(o, RUNTIME_ERROR_NOTIFIED))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
                        notifType = RUNTIME_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
                        setAlreadyNotified(o, index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
                            RUNTIME_ERROR_NOTIFIED, an);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                        msg = e.getMessage() == null ? "" : e.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                        MONITOR_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
                                Monitor.class.getName(), "monitor", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
                        MONITOR_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
                                Monitor.class.getName(), "monitor", e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
            // Check that the observed attribute type is supported by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
            // monitor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
            if (msg == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
                if (!isComparableTypeValid(object, attribute, value)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
                    if (isAlreadyNotified(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
                            o, OBSERVED_ATTRIBUTE_TYPE_ERROR_NOTIFIED))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                        notifType = OBSERVED_ATTRIBUTE_TYPE_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
                        setAlreadyNotified(o, index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
                            OBSERVED_ATTRIBUTE_TYPE_ERROR_NOTIFIED, an);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
                        msg = "The observed attribute type is not valid.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
                        MONITOR_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
                                Monitor.class.getName(), "monitor", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
            // Check that threshold type is supported by this monitor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
            if (msg == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
                if (!isThresholdTypeValid(object, attribute, value)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
                    if (isAlreadyNotified(o, THRESHOLD_ERROR_NOTIFIED))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
                        notifType = THRESHOLD_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
                        setAlreadyNotified(o, index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
                            THRESHOLD_ERROR_NOTIFIED, an);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
                        msg = "The threshold type is not valid.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
                        MONITOR_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
                                Monitor.class.getName(), "monitor", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
            // Let someone subclassing the monitor to perform additional
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
            // monitor consistency checks and report errors if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
            if (msg == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                msg = buildErrorNotification(object, attribute, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                if (msg != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
                    if (isAlreadyNotified(o, RUNTIME_ERROR_NOTIFIED))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
                        notifType = RUNTIME_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
                        setAlreadyNotified(o, index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
                            RUNTIME_ERROR_NOTIFIED, an);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                        MONITOR_LOGGER.logp(Level.FINEST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
                                Monitor.class.getName(), "monitor", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
            // If no errors were found then clear all error flags and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
            // let the monitor decide if a notification must be sent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
            if (msg == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
                // Clear all already notified flags.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
                resetAllAlreadyNotified(o, index, an);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
                // Get derived gauge from comparable value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
                derGauge = getDerivedGaugeFromComparable(object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
                                                         attribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
                                                         value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
                o.setDerivedGauge(derGauge);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
                o.setDerivedGaugeTimeStamp(System.currentTimeMillis());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
                // Check if an alarm must be fired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
                alarm = buildAlarmNotification(object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
                                               attribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
                                               (Comparable<?>) derGauge);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
        // Notify monitor errors
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
        if (msg != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
            sendNotification(notifType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
                             System.currentTimeMillis(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
                             msg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
                             derGauge,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
                             trigger,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
                             object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
                             true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
        // Notify monitor alarms
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
        if (alarm != null && alarm.getType() != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
            sendNotification(alarm.getType(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
                             System.currentTimeMillis(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
                             alarm.getMessage(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
                             derGauge,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
                             alarm.getTrigger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
                             object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
                             false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
     * Cleanup the scheduler and monitor tasks futures.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
    private synchronized void cleanupFutures() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
        if (schedulerFuture != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
            schedulerFuture.cancel(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
            schedulerFuture = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
        if (monitorFuture != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
            monitorFuture.cancel(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
            monitorFuture = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
     * Cleanup the "is complex type attribute" info.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
    private synchronized void cleanupIsComplexTypeAttribute() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
        firstAttribute = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
        remainingAttributes.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
        isComplexTypeAttribute = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
     * SchedulerTask nested class: This class implements the Runnable interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
     * The SchedulerTask is executed periodically with a given fixed delay by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
     * the Scheduled Executor Service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
    private class SchedulerTask implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
        private Runnable task = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
         * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
         *  CONSTRUCTORS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
         * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
        public SchedulerTask(Runnable task) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
            this.task = task;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
         * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
         *  PUBLIC METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
         * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
            synchronized (Monitor.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
                Monitor.this.monitorFuture = executor.submit(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
     * MonitorTask nested class: This class implements the Runnable interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
     * The MonitorTask is executed periodically with a given fixed delay by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
     * Scheduled Executor Service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
    private class MonitorTask implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
         * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
         *  CONSTRUCTORS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
         * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
        public MonitorTask() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
         * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
         *  PUBLIC METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
         * ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
            final ScheduledFuture<?> sf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
            synchronized (Monitor.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
                sf = Monitor.this.schedulerFuture;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
            AccessController.doPrivileged(new PrivilegedAction<Void>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
                public Void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
                    if (Monitor.this.isActive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
                        final int an[] = alreadyNotifieds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
                        int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
                        for (ObservedObject o : Monitor.this.observedObjects) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
                            if (Monitor.this.isActive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
                                Monitor.this.monitor(o, index++, an);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
            }, Monitor.this.acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
            synchronized (Monitor.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
                if (Monitor.this.isActive() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
                    Monitor.this.schedulerFuture == sf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
                    Monitor.this.monitorFuture = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
                    Monitor.this.schedulerFuture =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
                        scheduler.schedule(Monitor.this.schedulerTask,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
                                           Monitor.this.getGranularityPeriod(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
                                           TimeUnit.MILLISECONDS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
     * Daemon thread factory used by the monitor executors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
     * This factory creates all new threads used by an Executor in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
     * the same ThreadGroup. If there is a SecurityManager, it uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
     * the group of System.getSecurityManager(), else the group of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
     * the thread instantiating this DaemonThreadFactory. Each new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
     * thread is created as a daemon thread with priority
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
     * Thread.NORM_PRIORITY. New threads have names accessible via
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
     * Thread.getName() of "JMX Monitor <pool-name> Pool [Thread-M]",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
     * where M is the sequence number of the thread created by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
     * factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
    private static class DaemonThreadFactory implements ThreadFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
        final ThreadGroup group;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
        final AtomicInteger threadNumber = new AtomicInteger(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
        final String namePrefix;
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 34
diff changeset
  1568
        static final String nameSuffix = "]";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
        public DaemonThreadFactory(String poolName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
            SecurityManager s = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
            group = (s != null) ? s.getThreadGroup() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
                                  Thread.currentThread().getThreadGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
            namePrefix = "JMX Monitor " + poolName + " Pool [Thread-";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
        public Thread newThread(Runnable r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
            Thread t = new Thread(group,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
                                  r,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
                                  namePrefix +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
                                  threadNumber.getAndIncrement() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
                                  nameSuffix,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
                                  0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
            t.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
            if (t.getPriority() != Thread.NORM_PRIORITY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
                t.setPriority(Thread.NORM_PRIORITY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
            return t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
}