jdk/test/javax/management/monitor/ReflectionExceptionTest.java
author dbuck
Tue, 18 Aug 2015 04:29:28 -0700
changeset 32417 6859107fc6c3
parent 32018 91c91b3d50a0
child 44423 306c020eb154
permissions -rw-r--r--
8133666: OperatingSystemMXBean reports abnormally high machine CPU consumption on Linux Reviewed-by: sla, mgronlun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 26623
diff changeset
     2
 * Copyright (c) 2005, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6205072
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test that the jmx.monitor.error.runtime monitor notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          is emitted when getAttribute throws ReflectionException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Luis-Miguel Alventosa
32018
91c91b3d50a0 8132094: Mark intermittently failuring core-svc tests
ykantser
parents: 31909
diff changeset
    30
 * @key intermittent
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 26623
diff changeset
    31
 * @modules java.management
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run clean ReflectionExceptionTest MBeanServerBuilderImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *            MBeanServerForwarderInvocationHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @run build ReflectionExceptionTest MBeanServerBuilderImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *            MBeanServerForwarderInvocationHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @run main ReflectionExceptionTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.lang.reflect.Proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.management.MBeanServerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.management.Notification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.management.NotificationListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.management.ReflectionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.management.monitor.CounterMonitor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.management.monitor.GaugeMonitor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.management.monitor.MonitorNotification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.management.monitor.StringMonitor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
public class ReflectionExceptionTest implements NotificationListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // MBean class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public class ObservedObject implements ObservedObjectMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        public Integer getIntegerAttribute() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        public void setIntegerAttribute(Integer i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            this.i = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        public String getStringAttribute() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        public void setStringAttribute(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            this.s = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        private Integer i = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        private String s = "dummy";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // MBean interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public interface ObservedObjectMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        public Integer getIntegerAttribute();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        public void setIntegerAttribute(Integer i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        public String getStringAttribute();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        public void setStringAttribute(String s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    // Notification handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public void handleNotification(Notification notification, Object handback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        echo(">>> Received notification: " + notification);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (notification instanceof MonitorNotification) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            String type = notification.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            if (type.equals(MonitorNotification.RUNTIME_ERROR)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                MonitorNotification mn = (MonitorNotification) notification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                echo("\tType: " + mn.getType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                echo("\tTimeStamp: " + mn.getTimeStamp());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                echo("\tObservedObject: " + mn.getObservedObject());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                echo("\tObservedAttribute: " + mn.getObservedAttribute());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                echo("\tDerivedGauge: " + mn.getDerivedGauge());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                echo("\tTrigger: " + mn.getTrigger());
26623
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
    92
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
    93
                synchronized (this) {
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
    94
                    messageReceived = true;
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
    95
                    notifyAll();
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
    96
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Update the counter and check for notifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public int counterMonitorNotification() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        CounterMonitor counterMonitor = new CounterMonitor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            // Create a new CounterMonitor MBean and add it to the MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            echo(">>> CREATE a new CounterMonitor MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            ObjectName counterMonitorName = new ObjectName(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                            domain + ":type=" + CounterMonitor.class.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            server.registerMBean(counterMonitor, counterMonitorName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            echo(">>> ADD a listener to the CounterMonitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            counterMonitor.addNotificationListener(this, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            // MANAGEMENT OF A STANDARD MBEAN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            echo(">>> SET the attributes of the CounterMonitor:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            counterMonitor.addObservedObject(obsObjName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            echo("\tATTRIBUTE \"ObservedObject\"    = " + obsObjName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            counterMonitor.setObservedAttribute("IntegerAttribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            echo("\tATTRIBUTE \"ObservedAttribute\" = IntegerAttribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            counterMonitor.setNotify(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            echo("\tATTRIBUTE \"NotifyFlag\"        = false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            Integer threshold = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            counterMonitor.setInitThreshold(threshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            echo("\tATTRIBUTE \"Threshold\"         = " + threshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            int granularityperiod = 500;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            counterMonitor.setGranularityPeriod(granularityperiod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            echo(">>> START the CounterMonitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            counterMonitor.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            // Check if notification was received
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            //
26623
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
   146
            doWait();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            if (messageReceived) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                echo("\tOK: CounterMonitor got RUNTIME_ERROR notification!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                echo("\tKO: CounterMonitor did not get " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                     "RUNTIME_ERROR notification!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            messageReceived = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            if (counterMonitor != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                counterMonitor.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Update the gauge and check for notifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public int gaugeMonitorNotification() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        GaugeMonitor gaugeMonitor = new GaugeMonitor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            // Create a new GaugeMonitor MBean and add it to the MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            echo(">>> CREATE a new GaugeMonitor MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            ObjectName gaugeMonitorName = new ObjectName(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                            domain + ":type=" + GaugeMonitor.class.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            server.registerMBean(gaugeMonitor, gaugeMonitorName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            echo(">>> ADD a listener to the GaugeMonitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            gaugeMonitor.addNotificationListener(this, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            // MANAGEMENT OF A STANDARD MBEAN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            echo(">>> SET the attributes of the GaugeMonitor:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            gaugeMonitor.addObservedObject(obsObjName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            echo("\tATTRIBUTE \"ObservedObject\"    = " + obsObjName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            gaugeMonitor.setObservedAttribute("IntegerAttribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            echo("\tATTRIBUTE \"ObservedAttribute\" = IntegerAttribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            gaugeMonitor.setNotifyLow(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            gaugeMonitor.setNotifyHigh(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            echo("\tATTRIBUTE \"Notify Low  Flag\"  = false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            echo("\tATTRIBUTE \"Notify High Flag\"  = false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            Integer highThreshold = 3, lowThreshold = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            gaugeMonitor.setThresholds(highThreshold, lowThreshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            echo("\tATTRIBUTE \"Low  Threshold\"    = " + lowThreshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            echo("\tATTRIBUTE \"High Threshold\"    = " + highThreshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            int granularityperiod = 500;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            gaugeMonitor.setGranularityPeriod(granularityperiod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            echo(">>> START the GaugeMonitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            gaugeMonitor.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            // Check if notification was received
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            //
26623
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
   211
            doWait();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            if (messageReceived) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                echo("\tOK: GaugeMonitor got RUNTIME_ERROR notification!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                echo("\tKO: GaugeMonitor did not get " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                     "RUNTIME_ERROR notification!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            messageReceived = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            if (gaugeMonitor != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                gaugeMonitor.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        return 0;
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
     * Update the string and check for notifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    public int stringMonitorNotification() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        StringMonitor stringMonitor = new StringMonitor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            // Create a new StringMonitor MBean and add it to the MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            echo(">>> CREATE a new StringMonitor MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            ObjectName stringMonitorName = new ObjectName(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                            domain + ":type=" + StringMonitor.class.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            server.registerMBean(stringMonitor, stringMonitorName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            echo(">>> ADD a listener to the StringMonitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            stringMonitor.addNotificationListener(this, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            // MANAGEMENT OF A STANDARD MBEAN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            echo(">>> SET the attributes of the StringMonitor:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            stringMonitor.addObservedObject(obsObjName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            echo("\tATTRIBUTE \"ObservedObject\"    = " + obsObjName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            stringMonitor.setObservedAttribute("StringAttribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            echo("\tATTRIBUTE \"ObservedAttribute\" = StringAttribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            stringMonitor.setNotifyMatch(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            echo("\tATTRIBUTE \"NotifyMatch\"       = false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            stringMonitor.setNotifyDiffer(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            echo("\tATTRIBUTE \"NotifyDiffer\"      = false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            stringMonitor.setStringToCompare("dummy");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            echo("\tATTRIBUTE \"StringToCompare\"   = \"dummy\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            int granularityperiod = 500;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            stringMonitor.setGranularityPeriod(granularityperiod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            echo(">>> START the StringMonitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            stringMonitor.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            // Check if notification was received
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            //
26623
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
   275
            doWait();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            if (messageReceived) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                echo("\tOK: StringMonitor got RUNTIME_ERROR notification!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                echo("\tKO: StringMonitor did not get " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                     "RUNTIME_ERROR notification!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            messageReceived = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            if (stringMonitor != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                stringMonitor.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * Test the monitor notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    public int monitorNotifications() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        server = MBeanServerFactory.newMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        MBeanServerForwarderInvocationHandler mbsfih =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            (MBeanServerForwarderInvocationHandler)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            Proxy.getInvocationHandler(server);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        mbsfih.setGetAttributeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            new ReflectionException(new RuntimeException(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                                    "Test ReflectionException"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        domain = server.getDefaultDomain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        obsObjName = ObjectName.getInstance(domain + ":type=ObservedObject");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        server.registerMBean(new ObservedObject(), obsObjName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        echo(">>> ----------------------------------------");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        int error = counterMonitorNotification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        echo(">>> ----------------------------------------");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        error += gaugeMonitorNotification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        echo(">>> ----------------------------------------");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        error += stringMonitorNotification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        echo(">>> ----------------------------------------");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        return error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * Print message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    private static void echo(String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        System.out.println(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * Standalone entry point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * Run the test and report to stdout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    public static void main (String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        System.setProperty("javax.management.builder.initial",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                           MBeanServerBuilderImpl.class.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        ReflectionExceptionTest test = new ReflectionExceptionTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        int error = test.monitorNotifications();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        if (error > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            echo(">>> Unhappy Bye, Bye!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            throw new IllegalStateException("Test FAILED: Didn't get all " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                                            "the notifications that were " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                                            "expected by the test!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            echo(">>> Happy Bye, Bye!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
26623
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
   349
    /*
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
   350
     * Wait messageReceived to be true
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
   351
     */
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
   352
    synchronized void doWait() {
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
   353
        while (!messageReceived) {
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
   354
            try {
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
   355
                wait();
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
   356
            } catch (InterruptedException e) {
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
   357
                System.err.println("Got unexpected exception: " + e);
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
   358
                e.printStackTrace();
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
   359
                break;
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
   360
            }
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
   361
        }
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
   362
    }
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
   363
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    // Flag to notify that a message has been received
26623
11289cf5c8fc 8042205: javax/management/monitor/*: some tests didn't get all the notifications
sjiang
parents: 5506
diff changeset
   365
    private volatile boolean messageReceived = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    private MBeanServer server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    private ObjectName obsObjName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    private String domain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
}