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