jdk/test/javax/management/monitor/RuntimeExceptionTest.java
author tschatzl
Wed, 16 Apr 2014 10:55:26 +0200
changeset 24100 7e71ac14ec06
parent 5506 202f599c92aa
child 26623 11289cf5c8fc
permissions -rw-r--r--
8027553: Change the in_cset_fast_test functionality to use the G1BiasedArray abstraction Summary: Instead of using a manually managed array for the in_cset_fast_test array, use a G1BiasedArray instance. Reviewed-by: brutisso, mgerdin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2005, 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 6200391
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 RuntimeException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Luis-Miguel Alventosa
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run clean RuntimeExceptionTest MBeanServerBuilderImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *            MBeanServerForwarderInvocationHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run build RuntimeExceptionTest MBeanServerBuilderImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *            MBeanServerForwarderInvocationHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @run main RuntimeExceptionTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.lang.reflect.Proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.management.MBeanServerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.management.Notification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.management.NotificationListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.management.monitor.CounterMonitor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.management.monitor.GaugeMonitor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.management.monitor.MonitorNotification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.management.monitor.StringMonitor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public class RuntimeExceptionTest implements NotificationListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // MBean class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public class ObservedObject implements ObservedObjectMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        public Integer getIntegerAttribute() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        public void setIntegerAttribute(Integer i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            this.i = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        public String getStringAttribute() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        public void setStringAttribute(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            this.s = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        private Integer i = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        private String s = "dummy";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // MBean interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public interface ObservedObjectMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        public Integer getIntegerAttribute();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        public void setIntegerAttribute(Integer i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        public String getStringAttribute();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        public void setStringAttribute(String s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // Notification handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public void handleNotification(Notification notification, Object handback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        echo(">>> Received notification: " + notification);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if (notification instanceof MonitorNotification) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            String type = notification.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            if (type.equals(MonitorNotification.RUNTIME_ERROR)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                MonitorNotification mn = (MonitorNotification) notification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                echo("\tType: " + mn.getType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                echo("\tTimeStamp: " + mn.getTimeStamp());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                echo("\tObservedObject: " + mn.getObservedObject());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                echo("\tObservedAttribute: " + mn.getObservedAttribute());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                echo("\tDerivedGauge: " + mn.getDerivedGauge());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                echo("\tTrigger: " + mn.getTrigger());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                messageReceived = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Update the counter and check for notifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public int counterMonitorNotification() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        CounterMonitor counterMonitor = new CounterMonitor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            // Create a new CounterMonitor MBean and add it to the MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            echo(">>> CREATE a new CounterMonitor MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            ObjectName counterMonitorName = new ObjectName(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                            domain + ":type=" + CounterMonitor.class.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            server.registerMBean(counterMonitor, counterMonitorName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            echo(">>> ADD a listener to the CounterMonitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            counterMonitor.addNotificationListener(this, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            // MANAGEMENT OF A STANDARD MBEAN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            echo(">>> SET the attributes of the CounterMonitor:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            counterMonitor.addObservedObject(obsObjName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            echo("\tATTRIBUTE \"ObservedObject\"    = " + obsObjName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            counterMonitor.setObservedAttribute("IntegerAttribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            echo("\tATTRIBUTE \"ObservedAttribute\" = IntegerAttribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            counterMonitor.setNotify(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            echo("\tATTRIBUTE \"NotifyFlag\"        = false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            Integer threshold = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            counterMonitor.setInitThreshold(threshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            echo("\tATTRIBUTE \"Threshold\"         = " + threshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            int granularityperiod = 500;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            counterMonitor.setGranularityPeriod(granularityperiod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            echo(">>> START the CounterMonitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            counterMonitor.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            // Wait for granularity period (multiplied by 2 for sure)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            Thread.sleep(granularityperiod * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            // Check if notification was received
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            if (messageReceived) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                echo("\tOK: CounterMonitor got RUNTIME_ERROR notification!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                echo("\tKO: CounterMonitor did not get " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                     "RUNTIME_ERROR notification!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            messageReceived = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            if (counterMonitor != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                counterMonitor.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Update the gauge and check for notifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public int gaugeMonitorNotification() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        GaugeMonitor gaugeMonitor = new GaugeMonitor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            // Create a new GaugeMonitor MBean and add it to the MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            echo(">>> CREATE a new GaugeMonitor MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            ObjectName gaugeMonitorName = new ObjectName(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                            domain + ":type=" + GaugeMonitor.class.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            server.registerMBean(gaugeMonitor, gaugeMonitorName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            echo(">>> ADD a listener to the GaugeMonitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            gaugeMonitor.addNotificationListener(this, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            // MANAGEMENT OF A STANDARD MBEAN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            echo(">>> SET the attributes of the GaugeMonitor:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            gaugeMonitor.addObservedObject(obsObjName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            echo("\tATTRIBUTE \"ObservedObject\"    = " + obsObjName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            gaugeMonitor.setObservedAttribute("IntegerAttribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            echo("\tATTRIBUTE \"ObservedAttribute\" = IntegerAttribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            gaugeMonitor.setNotifyLow(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            gaugeMonitor.setNotifyHigh(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            echo("\tATTRIBUTE \"Notify Low  Flag\"  = false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            echo("\tATTRIBUTE \"Notify High Flag\"  = false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            Integer highThreshold = 3, lowThreshold = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            gaugeMonitor.setThresholds(highThreshold, lowThreshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            echo("\tATTRIBUTE \"Low  Threshold\"    = " + lowThreshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            echo("\tATTRIBUTE \"High Threshold\"    = " + highThreshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            int granularityperiod = 500;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            gaugeMonitor.setGranularityPeriod(granularityperiod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            echo(">>> START the GaugeMonitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            gaugeMonitor.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            // Wait for granularity period (multiplied by 2 for sure)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            Thread.sleep(granularityperiod * 2);
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
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            if (messageReceived) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                echo("\tOK: GaugeMonitor got RUNTIME_ERROR notification!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                echo("\tKO: GaugeMonitor did not get " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                     "RUNTIME_ERROR notification!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            messageReceived = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            if (gaugeMonitor != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                gaugeMonitor.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        return 0;
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
     * Update the string and check for notifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    public int stringMonitorNotification() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        StringMonitor stringMonitor = new StringMonitor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            // Create a new StringMonitor MBean and add it to the MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            echo(">>> CREATE a new StringMonitor MBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            ObjectName stringMonitorName = new ObjectName(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                            domain + ":type=" + StringMonitor.class.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            server.registerMBean(stringMonitor, stringMonitorName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            echo(">>> ADD a listener to the StringMonitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            stringMonitor.addNotificationListener(this, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            // MANAGEMENT OF A STANDARD MBEAN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            echo(">>> SET the attributes of the StringMonitor:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            stringMonitor.addObservedObject(obsObjName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            echo("\tATTRIBUTE \"ObservedObject\"    = " + obsObjName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            stringMonitor.setObservedAttribute("StringAttribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            echo("\tATTRIBUTE \"ObservedAttribute\" = StringAttribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            stringMonitor.setNotifyMatch(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            echo("\tATTRIBUTE \"NotifyMatch\"       = false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            stringMonitor.setNotifyDiffer(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            echo("\tATTRIBUTE \"NotifyDiffer\"      = false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            stringMonitor.setStringToCompare("dummy");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            echo("\tATTRIBUTE \"StringToCompare\"   = \"dummy\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            int granularityperiod = 500;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            stringMonitor.setGranularityPeriod(granularityperiod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            echo(">>> START the StringMonitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            stringMonitor.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            // Wait for granularity period (multiplied by 2 for sure)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            Thread.sleep(granularityperiod * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            // Check if notification was received
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            if (messageReceived) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                echo("\tOK: StringMonitor got RUNTIME_ERROR notification!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                echo("\tKO: StringMonitor did not get " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                     "RUNTIME_ERROR notification!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            messageReceived = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            if (stringMonitor != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                stringMonitor.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * Test the monitor notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    public int monitorNotifications() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        server = MBeanServerFactory.newMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        MBeanServerForwarderInvocationHandler mbsfih =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            (MBeanServerForwarderInvocationHandler)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            Proxy.getInvocationHandler(server);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        mbsfih.setGetAttributeException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            new RuntimeException("Test RuntimeException"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        domain = server.getDefaultDomain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        obsObjName = ObjectName.getInstance(domain + ":type=ObservedObject");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        server.registerMBean(new ObservedObject(), obsObjName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        echo(">>> ----------------------------------------");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        int error = counterMonitorNotification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        echo(">>> ----------------------------------------");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        error += gaugeMonitorNotification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        echo(">>> ----------------------------------------");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        error += stringMonitorNotification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        echo(">>> ----------------------------------------");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        return error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * Print message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    private static void echo(String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        System.out.println(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * Standalone entry point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * Run the test and report to stdout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    public static void main (String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        System.setProperty("javax.management.builder.initial",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                           MBeanServerBuilderImpl.class.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        RuntimeExceptionTest test = new RuntimeExceptionTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        int error = test.monitorNotifications();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        if (error > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            echo(">>> Unhappy Bye, Bye!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            throw new IllegalStateException("Test FAILED: Didn't get all " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                                            "the notifications that were " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                                            "expected by the test!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            echo(">>> Happy Bye, Bye!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    // Flag to notify that a message has been received
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    private boolean messageReceived = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    private MBeanServer server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    private ObjectName obsObjName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    private String domain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
}