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