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