jdk/test/javax/management/remote/mandatory/connection/ConnectionTest.java
author dbuck
Tue, 18 Aug 2015 04:29:28 -0700
changeset 32417 6859107fc6c3
parent 30376 2ccf2cf7ea48
child 43503 bc7f8619ab70
permissions -rw-r--r--
8133666: OperatingSystemMXBean reports abnormally high machine CPU consumption on Linux Reviewed-by: sla, mgronlun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 23010
diff changeset
     2
 * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4865397
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Tests remote JMX connections
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Eamonn McManus
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 23010
diff changeset
    29
 * @modules java.management
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run clean ConnectionTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run build ConnectionTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run main ConnectionTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.LinkedList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.security.Principal;
17425
9259f4bb1c48 7199324: Connection ID for IPv6 addresses is not generated accordingly to the specification
jbachorik
parents: 5506
diff changeset
    48
import java.util.regex.Pattern;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.security.auth.Subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import javax.management.MBeanServerConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import javax.management.MBeanServerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import javax.management.Notification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import javax.management.NotificationListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import javax.management.remote.JMXAuthenticator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import javax.management.remote.JMXConnectionNotification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import javax.management.remote.JMXConnector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import javax.management.remote.JMXConnectorFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import javax.management.remote.JMXConnectorServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import javax.management.remote.JMXConnectorServerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
import javax.management.remote.JMXPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
import javax.management.remote.JMXServiceURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
public class ConnectionTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
//      System.setProperty("java.util.logging.config.file",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
//                         "../../../../logging.properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
//      // we are in <workspace>/build/test/JTwork/scratch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
//      java.util.logging.LogManager.getLogManager().readConfiguration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        boolean ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        String[] protocols = {"rmi", "iiop", "jmxmp"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (args.length > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            protocols = args;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        for (int i = 0; i < protocols.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            final String proto = protocols[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            System.out.println("Testing for protocol " + proto);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                ok &= test(proto);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                System.err.println("Unexpected exception: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                ok = false;
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
        if (ok)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            System.out.println("Test passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            System.out.println("TEST FAILED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private static boolean test(String proto) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        ObjectName serverName = ObjectName.getInstance("d:type=server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        JMXAuthenticator authenticator = new BogusAuthenticator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        Map env = Collections.singletonMap("jmx.remote.authenticator",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                                           authenticator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        JMXServiceURL url = new JMXServiceURL("service:jmx:" + proto + "://");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        JMXConnectorServer server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            server =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                JMXConnectorServerFactory.newJMXConnectorServer(url, env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                                                                null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            System.out.println("Protocol " + proto +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                               " not supported, ignoring");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        System.out.println("Created connector server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        mbs.registerMBean(server, serverName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        System.out.println("Registered connector server in MBean server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        mbs.addNotificationListener(serverName, logListener, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        mbs.invoke(serverName, "start", null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        System.out.println("Started connector server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        JMXServiceURL address =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            (JMXServiceURL) mbs.getAttribute(serverName, "Address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        System.out.println("Retrieved address: " + address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (address.getHost().length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            System.out.println("Generated address has empty hostname");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        JMXConnector client = JMXConnectorFactory.connect(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        System.out.println("Client connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        String clientConnId = client.getConnectionId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        System.out.println("Got connection ID on client: " + clientConnId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        boolean ok = checkConnectionId(proto, clientConnId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (!ok)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        System.out.println("Connection ID is OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        // 4901826: connection ids need some time to be updated using jmxmp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        // we don't get the notif immediately either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        // this was originally timeout 1ms, which was not enough
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        Notification notif = waitForNotification(1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        System.out.println("Server got notification: " + notif);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        ok = mustBeConnectionNotification(notif, clientConnId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                                         JMXConnectionNotification.OPENED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (!ok)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        client.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        System.out.println("Closed client");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        notif = waitForNotification(1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        System.out.println("Got notification: " + notif);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        ok = mustBeConnectionNotification(notif, clientConnId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                                          JMXConnectionNotification.CLOSED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (!ok)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        client = JMXConnectorFactory.connect(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        System.out.println("Second client connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        String clientConnId2 = client.getConnectionId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (clientConnId.equals(clientConnId2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            System.out.println("Same connection ID for two connections: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                               clientConnId2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        System.out.println("Second client connection ID is different");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        notif = waitForNotification(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        ok = mustBeConnectionNotification(notif, clientConnId2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                                          JMXConnectionNotification.OPENED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        if (!ok)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        MBeanServerConnection mbsc = client.getMBeanServerConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        Map attrs = (Map) mbsc.getAttribute(serverName, "Attributes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        System.out.println("Server attributes received by client: " + attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        server.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        System.out.println("Server stopped");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        notif = waitForNotification(1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        System.out.println("Server got connection-closed notification: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                           notif);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        ok = mustBeConnectionNotification(notif, clientConnId2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                                          JMXConnectionNotification.CLOSED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (!ok)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            mbsc.getDefaultDomain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            System.out.println("Connection still working but should not be");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            System.out.println("Connection correctly got exception: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            client = JMXConnectorFactory.connect(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            System.out.println("Connector server still working but should " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                               "not be");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            System.out.println("New connection correctly got exception: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    private static boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        mustBeConnectionNotification(Notification notif,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                                     String requiredConnId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                                     String requiredType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        if (!(notif instanceof JMXConnectionNotification)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            System.out.println("Should have been a " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                               "JMXConnectionNotification: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                               notif.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        JMXConnectionNotification cnotif = (JMXConnectionNotification) notif;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        if (!cnotif.getType().equals(requiredType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            System.out.println("Wrong type notif: is \"" + cnotif.getType() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                               "\", should be \"" + requiredType + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        if (!cnotif.getConnectionId().equals(requiredConnId)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            System.out.println("Wrong connection id: is \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                               cnotif.getConnectionId() + "\", should be \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                               requiredConnId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
17425
9259f4bb1c48 7199324: Connection ID for IPv6 addresses is not generated accordingly to the specification
jbachorik
parents: 5506
diff changeset
   244
    private static final String IPV4_PTN = "^(?:[0-9]{1,3}\\.){3}[0-9]{1,3}(\\:[1-9][0-9]{3})?$";
9259f4bb1c48 7199324: Connection ID for IPv6 addresses is not generated accordingly to the specification
jbachorik
parents: 5506
diff changeset
   245
9259f4bb1c48 7199324: Connection ID for IPv6 addresses is not generated accordingly to the specification
jbachorik
parents: 5506
diff changeset
   246
    /**
9259f4bb1c48 7199324: Connection ID for IPv6 addresses is not generated accordingly to the specification
jbachorik
parents: 5506
diff changeset
   247
     * Checks the connection id for validity.
9259f4bb1c48 7199324: Connection ID for IPv6 addresses is not generated accordingly to the specification
jbachorik
parents: 5506
diff changeset
   248
     * The {@link
9259f4bb1c48 7199324: Connection ID for IPv6 addresses is not generated accordingly to the specification
jbachorik
parents: 5506
diff changeset
   249
     * javax.management.remote package description} describes the
9259f4bb1c48 7199324: Connection ID for IPv6 addresses is not generated accordingly to the specification
jbachorik
parents: 5506
diff changeset
   250
     * conventions for connection IDs.
9259f4bb1c48 7199324: Connection ID for IPv6 addresses is not generated accordingly to the specification
jbachorik
parents: 5506
diff changeset
   251
     * @param proto Connection protocol
9259f4bb1c48 7199324: Connection ID for IPv6 addresses is not generated accordingly to the specification
jbachorik
parents: 5506
diff changeset
   252
     * @param clientConnId The connection ID
9259f4bb1c48 7199324: Connection ID for IPv6 addresses is not generated accordingly to the specification
jbachorik
parents: 5506
diff changeset
   253
     * @return Returns {@code true} if the connection id conforms to the specification; {@code false} otherwise.
9259f4bb1c48 7199324: Connection ID for IPv6 addresses is not generated accordingly to the specification
jbachorik
parents: 5506
diff changeset
   254
     * @throws Exception
9259f4bb1c48 7199324: Connection ID for IPv6 addresses is not generated accordingly to the specification
jbachorik
parents: 5506
diff changeset
   255
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    private static boolean checkConnectionId(String proto, String clientConnId)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        StringTokenizer tok = new StringTokenizer(clientConnId, " ", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        String s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        s = tok.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        if (!s.startsWith(proto + ":")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            System.out.println("Expected \"" + proto + ":\", found \"" + s +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                               "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
17425
9259f4bb1c48 7199324: Connection ID for IPv6 addresses is not generated accordingly to the specification
jbachorik
parents: 5506
diff changeset
   266
9259f4bb1c48 7199324: Connection ID for IPv6 addresses is not generated accordingly to the specification
jbachorik
parents: 5506
diff changeset
   267
        int hostAddrInd = s.indexOf("//");
9259f4bb1c48 7199324: Connection ID for IPv6 addresses is not generated accordingly to the specification
jbachorik
parents: 5506
diff changeset
   268
        if (hostAddrInd > -1) {
9259f4bb1c48 7199324: Connection ID for IPv6 addresses is not generated accordingly to the specification
jbachorik
parents: 5506
diff changeset
   269
            s = s.substring(hostAddrInd + 2);
9259f4bb1c48 7199324: Connection ID for IPv6 addresses is not generated accordingly to the specification
jbachorik
parents: 5506
diff changeset
   270
            if (!Pattern.matches(IPV4_PTN, s)) {
9259f4bb1c48 7199324: Connection ID for IPv6 addresses is not generated accordingly to the specification
jbachorik
parents: 5506
diff changeset
   271
                if (!s.startsWith("[") || !s.endsWith("]")) {
9259f4bb1c48 7199324: Connection ID for IPv6 addresses is not generated accordingly to the specification
jbachorik
parents: 5506
diff changeset
   272
                    System.out.println("IPv6 address must be enclosed in \"[]\"");
9259f4bb1c48 7199324: Connection ID for IPv6 addresses is not generated accordingly to the specification
jbachorik
parents: 5506
diff changeset
   273
                    return false;
9259f4bb1c48 7199324: Connection ID for IPv6 addresses is not generated accordingly to the specification
jbachorik
parents: 5506
diff changeset
   274
                }
9259f4bb1c48 7199324: Connection ID for IPv6 addresses is not generated accordingly to the specification
jbachorik
parents: 5506
diff changeset
   275
            }
9259f4bb1c48 7199324: Connection ID for IPv6 addresses is not generated accordingly to the specification
jbachorik
parents: 5506
diff changeset
   276
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        s = tok.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        if (!s.equals(" ")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            System.out.println("Expected \" \", found \"" + s + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        s = tok.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        StringTokenizer tok2 = new StringTokenizer(s, ";", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        Set principalNames = new HashSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        String s2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        s2 = tok2.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        if (s2.equals(";")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            System.out.println("In identity \"" + s +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                               "\", expected name, found \";\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        principalNames.add(s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        s2 = tok2.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        if (!s2.equals(";"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            throw new Exception("Can't happen");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        s2 = tok2.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        if (s2.equals(";")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            System.out.println("In identity \"" + s +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                               "\", expected name, found \";\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        principalNames.add(s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        if (tok2.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            System.out.println("In identity \"" + s + "\", too many tokens");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        if (principalNames.size() != bogusPrincipals.size()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            System.out.println("Wrong number of principal names: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                               principalNames.size() + " != " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                               bogusPrincipals.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        for (Iterator it = bogusPrincipals.iterator(); it.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            Principal p = (Principal) it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            if (!principalNames.contains(p.getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                System.out.println("Principal names don't contain \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                                   p.getName() + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        s = tok.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        if (!s.equals(" ")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            System.out.println("Expected \" \", found \"" + s + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    private static Notification waitForNotification(long timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        synchronized (log) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            if (log.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                long remainingTime = timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                final long startTime = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                while (log.isEmpty() && remainingTime >0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    log.wait(remainingTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    remainingTime = timeout - (System.currentTimeMillis() - startTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                if (log.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    throw new InterruptedException("Timed out waiting for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                                                   "notification!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            return (Notification) log.remove(0);
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
    private static class LogListener implements NotificationListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        LogListener(List log) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            this.log = log;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        public void handleNotification(Notification n, Object h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            synchronized (log) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                log.add(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                log.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        private final List log;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    private static List log = new LinkedList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    private static NotificationListener logListener = new LogListener(log);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    private static class BogusAuthenticator implements JMXAuthenticator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        public Subject authenticate(Object credentials) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            Subject subject =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                new Subject(true, bogusPrincipals,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                            Collections.EMPTY_SET, Collections.EMPTY_SET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            System.out.println("Authenticator returns: " + subject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            return subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    private static final Set bogusPrincipals = new HashSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        bogusPrincipals.add(new JMXPrincipal("foo"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        bogusPrincipals.add(new JMXPrincipal("bar"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
}