jdk/test/javax/management/remote/mandatory/notif/NotificationEmissionTest.java
author sjiang
Fri, 28 Dec 2012 16:44:27 +0100
changeset 14931 60d0e9160ae4
parent 5506 202f599c92aa
child 30376 2ccf2cf7ea48
permissions -rw-r--r--
7120365: DiffHBTest.java fails due to ConcurrentModificationException Summary: The problem is from the server notification forwarder, it should use a copy of listener set to do iterate. Reviewed-by: alanb
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: 4156
diff changeset
     2
 * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
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: 4156
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
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 5106721
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Check the emission of notifications when a Security Manager is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * installed. Test the property "jmx.remote.x.check.notification.emission".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Luis-Miguel Alventosa
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run clean NotificationEmissionTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run build NotificationEmissionTest
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1247
diff changeset
    32
 * @run main NotificationEmissionTest 1
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1247
diff changeset
    33
 * @run main NotificationEmissionTest 2
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1247
diff changeset
    34
 * @run main NotificationEmissionTest 3
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1247
diff changeset
    35
 * @run main NotificationEmissionTest 4
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1247
diff changeset
    36
 * @run main NotificationEmissionTest 5
2
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.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.management.MBeanServerConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.management.MBeanServerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.management.Notification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.management.NotificationBroadcasterSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import javax.management.NotificationListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import javax.management.remote.JMXAuthenticator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import javax.management.remote.JMXConnector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import javax.management.remote.JMXConnectorFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import javax.management.remote.JMXConnectorServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import javax.management.remote.JMXConnectorServerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import javax.management.remote.JMXPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import javax.management.remote.JMXServiceURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import javax.security.auth.Subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
public class NotificationEmissionTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public class CustomJMXAuthenticator implements JMXAuthenticator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        public Subject authenticate(Object credentials) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            String role = ((String[]) credentials)[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            echo("Create principal with name = " + role);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            return new Subject(true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                               Collections.singleton(new JMXPrincipal(role)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                               Collections.EMPTY_SET,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                               Collections.EMPTY_SET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public interface NBMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        public void emitNotification(int seqnum, ObjectName name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public static class NB
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        extends NotificationBroadcasterSupport
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        implements NBMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        public void emitNotification(int seqnum, ObjectName name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                sendNotification(new Notification("nb", this, seqnum));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                sendNotification(new Notification("nb", name, seqnum));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public class Listener implements NotificationListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        public List<Notification> notifs = new ArrayList<Notification>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        public void handleNotification(Notification n, Object h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            echo("handleNotification:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            echo("\tNotification = " + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            echo("\tNotification.SeqNum = " + n.getSequenceNumber());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            echo("\tHandback = " + h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            notifs.add(n);
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
    public int checkNotifs(int size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                           List<Notification> received,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                           List<ObjectName> expected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (received.size() != size) {
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   105
            echo("Error: expecting " + size + " notifications, got " +
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   106
                    received.size());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            for (Notification n : received) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                echo("Received notification: " + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                if (!n.getType().equals("nb")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    echo("Notification type must be \"nb\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                ObjectName o = (ObjectName) n.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                int index = (int) n.getSequenceNumber();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                ObjectName nb = expected.get(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                if (!o.equals(nb)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    echo("Notification source must be " + nb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public int runTest(int testcase) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        echo("\n=-=-= Running testcase " + testcase + " =-=-=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        switch (testcase) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                return testNotificationEmissionProperty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                return testNotificationEmissionPositive(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                return testNotificationEmissionNegative(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            case 4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                return testNotificationEmissionPositive(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            case 5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                return testNotificationEmissionNegative(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                echo("Invalid testcase");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public int testNotificationEmissionProperty(boolean exception,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                                                Object propValue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            testNotificationEmission(propValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            if (exception) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                echo("Did not get expected exception for value: " + propValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                echo("Property has been correctly set to value: " + propValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            if (exception) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                echo("Got expected exception for value: " + propValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                echo("Exception: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                echo("Got unexpected exception for value: " + propValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                echo("Exception: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public int testNotificationEmissionProperty() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        int error = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        error += testNotificationEmissionProperty(true, new Boolean(false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        error += testNotificationEmissionProperty(true, new Boolean(true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        error += testNotificationEmissionProperty(true, "dummy");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        error += testNotificationEmissionProperty(false, "false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        error += testNotificationEmissionProperty(false, "true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        error += testNotificationEmissionProperty(false, "FALSE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        error += testNotificationEmissionProperty(false, "TRUE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        return error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public int testNotificationEmissionPositive(boolean prop) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        return testNotificationEmission(prop, "true", true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    public int testNotificationEmissionNegative(boolean prop) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        return testNotificationEmission(prop, "true", true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public int testNotificationEmission(Object propValue) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        return testNotificationEmission(true, propValue, false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public int testNotificationEmission(boolean prop,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                                        Object propValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                                        boolean sm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                                        boolean policyPositive)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        JMXConnectorServer server = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        JMXConnector client = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        // Set policy file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        String policyFile =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            System.getProperty("test.src") + File.separator +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            (policyPositive ? "policy.positive" : "policy.negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        echo("\nSetting policy file " + policyFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        System.setProperty("java.security.policy", policyFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        // Create a new MBeanServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            // Create server environment map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            final Map<String,Object> env = new HashMap<String,Object>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            env.put("jmx.remote.authenticator", new CustomJMXAuthenticator());
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1247
diff changeset
   220
            if (prop)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                env.put("jmx.remote.x.check.notification.emission", propValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            // Create the JMXServiceURL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            final JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            // Create a JMXConnectorServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            server = JMXConnectorServerFactory.newJMXConnectorServer(url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                                                                     env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                                                                     mbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            // Start the JMXConnectorServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            server.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            // Create server environment map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            final Map<String,Object> cenv = new HashMap<String,Object>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            String[] credentials = new String[] { "role" , "password" };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            cenv.put("jmx.remote.credentials", credentials);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            // Create JMXConnector and connect to JMXConnectorServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            client = JMXConnectorFactory.connect(server.getAddress(), cenv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            // Get non-secure MBeanServerConnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            final MBeanServerConnection mbsc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                client.getMBeanServerConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            // Create NB MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            ObjectName nb1 = ObjectName.getInstance("domain:type=NB,name=1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            ObjectName nb2 = ObjectName.getInstance("domain:type=NB,name=2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            ObjectName nb3 = ObjectName.getInstance("domain:type=NB,name=3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            mbsc.createMBean(NB.class.getName(), nb1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            mbsc.createMBean(NB.class.getName(), nb2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            mbsc.createMBean(NB.class.getName(), nb3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            // Add notification listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            Listener li = new Listener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            mbsc.addNotificationListener(nb1, li, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            mbsc.addNotificationListener(nb2, li, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            // Set security manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            if (sm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                echo("Setting SM");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                System.setSecurityManager(new SecurityManager());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            // Invoke the "sendNotification" method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            mbsc.invoke(nb1, "emitNotification",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                new Object[] {0, null},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                new String[] {"int", "javax.management.ObjectName"});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            mbsc.invoke(nb2, "emitNotification",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                new Object[] {1, null},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                new String[] {"int", "javax.management.ObjectName"});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            mbsc.invoke(nb2, "emitNotification",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                new Object[] {2, nb3},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                new String[] {"int", "javax.management.ObjectName"});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   286
            // If the check is effective and we're using policy.negative,
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   287
            // then we should see the two notifs sent by nb2 (of which one
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   288
            // has a getSource() that is nb3), but not the notif sent by nb1.
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1247
diff changeset
   289
            // Otherwise we should see all three notifs.  The check is only
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1247
diff changeset
   290
            // effective if the property jmx.remote.x.check.notification.emission
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1247
diff changeset
   291
            // is explicitly true and there is a security manager.
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   292
            int expectedNotifs =
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1247
diff changeset
   293
                    (prop && sm && !policyPositive) ? 2 : 3;
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   294
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            // Wait for notifications to be emitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            //
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   297
            long deadline = System.currentTimeMillis() + 2000;
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   298
            while (li.notifs.size() < expectedNotifs &&
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   299
                    System.currentTimeMillis() < deadline)
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   300
                Thread.sleep(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            // Remove notification listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            mbsc.removeNotificationListener(nb1, li);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            mbsc.removeNotificationListener(nb2, li);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            int result = 0;
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1247
diff changeset
   308
            List<ObjectName> sources = new ArrayList<ObjectName>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            sources.add(nb1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            sources.add(nb2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            sources.add(nb3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 2
diff changeset
   313
            result = checkNotifs(expectedNotifs, li.notifs, sources);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            if (result > 0) {
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1247
diff changeset
   315
                echo("...SecurityManager=" + sm + "; policy=" + policyPositive);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            // Close the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            if (client != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                client.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            // Stop the connector server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            if (server != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                server.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            // Release the MBeanServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            if (mbs != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                MBeanServerFactory.releaseMBeanServer(mbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    private static void echo(String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        System.out.println(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        echo("\n--- Check the emission of notifications " +
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1247
diff changeset
   345
             "when a Security Manager is installed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1247
diff changeset
   347
        NotificationEmissionTest net = new NotificationEmissionTest();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        int error = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        error += net.runTest(Integer.parseInt(args[0]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        if (error > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            final String msg = "\nTest FAILED! Got " + error + " error(s)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            echo(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            echo("\nTest PASSED!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
}