jdk/test/sun/management/jmxremote/bootstrap/RmiBootstrapTest.java
author chegar
Wed, 03 Dec 2014 14:22:58 +0000
changeset 27565 729f9700483a
parent 23031 995cf5c2eb55
child 31715 4e942fbc0bf6
permissions -rw-r--r--
8049367: Modular Run-Time Images Reviewed-by: chegar, dfuchs, ihse, joehw, mullan, psandoz, wetmore Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, bradford.wetmore@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, james.laskey@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com, sundararajan.athijegannathan@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
21408
6468b29c9877 8027058: sun/management/jmxremote/bootstrap/RmiBootstrapTest.sh Failed to initialize connector
jbachorik
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2013, 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
import sun.management.jmxremote.ConnectorBootstrap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
import java.io.FileInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.FilenameFilter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
21408
6468b29c9877 8027058: sun/management/jmxremote/bootstrap/RmiBootstrapTest.sh Failed to initialize connector
jbachorik
parents: 5506
diff changeset
    29
import java.net.BindException;
6468b29c9877 8027058: sun/management/jmxremote/bootstrap/RmiBootstrapTest.sh Failed to initialize connector
jbachorik
parents: 5506
diff changeset
    30
import java.rmi.server.ExportException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.management.remote.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import sun.management.AgentConfigurationError;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>This class implements unit test for RMI Bootstrap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * When called with no arguments main() looks in the directory indicated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * by the "test.src" system property for files called management*ok.properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * or management*ko.properties. The *ok.properties files are assumed to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * valid Java M&M config files for which the bootstrap should succeed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * The *ko.properties files are assumed to be configurations for which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * bootstrap & connection test will fail.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <p>The rmi port number can be specified with the "rmi.port" system property.
21408
6468b29c9877 8027058: sun/management/jmxremote/bootstrap/RmiBootstrapTest.sh Failed to initialize connector
jbachorik
parents: 5506
diff changeset
    55
 * If not, this test will use the first available port</p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <p>When called with some argument, the main() will interprete its args to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * be Java M&M configuration file names. The filenames are expected to end
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * with ok.properties or ko.properties - and are interpreted as above.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <p>Note that a limitation of the RMI registry (bug 4267864) prevent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * this test from succeeding if more than 1 configuration is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * As long as 4267864 isn't fix, this test must be called as many times
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * as needed but with a single argument (no arguments, or several arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * will fail).</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <p>Debug traces are logged in "sun.management.test"</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
public class RmiBootstrapTest {
21408
6468b29c9877 8027058: sun/management/jmxremote/bootstrap/RmiBootstrapTest.sh Failed to initialize connector
jbachorik
parents: 5506
diff changeset
    70
    // the number of consecutive ports to test for availability
23031
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
    71
    private static int MAX_GET_FREE_PORT_TRIES = 10;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    static TestLogger log =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        new TestLogger("RmiBootstrapTest");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Default values for RMI configuration properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public static interface DefaultValues {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        public static final String PORT="0";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        public static final String CONFIG_FILE_NAME="management.properties";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        public static final String USE_SSL="true";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        public static final String USE_AUTHENTICATION="true";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        public static final String PASSWORD_FILE_NAME="jmxremote.password";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        public static final String ACCESS_FILE_NAME="jmxremote.access";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        public static final String KEYSTORE="keystore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        public static final String KEYSTORE_PASSWD="password";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        public static final String TRUSTSTORE="truststore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        public static final String TRUSTSTORE_PASSWD="trustword";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        public static final String SSL_NEED_CLIENT_AUTH="false";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Names of RMI configuration properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public static interface PropertyNames {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        public static final String PORT=
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            "com.sun.management.jmxremote.port";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        public static final String CONFIG_FILE_NAME=
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            "com.sun.management.config.file";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        public static final String USE_SSL=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            "com.sun.management.jmxremote.ssl";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        public static final String USE_AUTHENTICATION=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            "com.sun.management.jmxremote.authenticate";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        public static final String PASSWORD_FILE_NAME=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            "com.sun.management.jmxremote.password.file";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        public static final String ACCESS_FILE_NAME=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            "com.sun.management.jmxremote.access.file";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        public static final String INSTRUMENT_ALL=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            "com.sun.management.instrumentall";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        public static final String CREDENTIALS =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            "jmx.remote.credentials";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        public static final String KEYSTORE=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            "javax.net.ssl.keyStore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        public static final String KEYSTORE_PASSWD=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            "javax.net.ssl.keyStorePassword";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        public static final String TRUSTSTORE=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            "javax.net.ssl.trustStore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        public static final String TRUSTSTORE_PASSWD=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            "javax.net.ssl.trustStorePassword";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        public static final String SSL_ENABLED_CIPHER_SUITES =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            "com.sun.management.jmxremote.ssl.enabled.cipher.suites";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        public static final String SSL_ENABLED_PROTOCOLS =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            "com.sun.management.jmxremote.ssl.enabled.protocols";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        public static final String SSL_NEED_CLIENT_AUTH =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            "com.sun.management.jmxremote.ssl.need.client.auth";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * A filter to find all filenames who match <prefix>*<suffix>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Note that <prefix> and <suffix> can overlap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    private static class ConfigFilenameFilter implements FilenameFilter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        final String suffix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        final String prefix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        ConfigFilenameFilter(String prefix, String suffix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            this.suffix=suffix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            this.prefix=prefix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        public boolean accept(File dir, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            return (name.startsWith(prefix) && name.endsWith(suffix));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Get all "management*ok.properties" files in the directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * indicated by the "test.src" management property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    private static File[] findConfigurationFilesOk() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        final String testSrc = System.getProperty("test.src");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        final File dir = new File(testSrc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        final FilenameFilter filter =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            new ConfigFilenameFilter("management_test","ok.properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return dir.listFiles(filter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Get all "management*ko.properties" files in the directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * indicated by the "test.src" management property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    private static File[] findConfigurationFilesKo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        final String testSrc = System.getProperty("test.src");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        final File dir = new File(testSrc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        final FilenameFilter filter =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            new ConfigFilenameFilter("management_test","ko.properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return dir.listFiles(filter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * List all MBeans and their attributes. Used to test communication
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * with the Java M&M MBean Server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @return the number of queried MBeans.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public static int listMBeans(MBeanServerConnection server)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return listMBeans(server,null,null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * List all matching MBeans and their attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Used to test communication with the Java M&M MBean Server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @return the number of matching MBeans.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public static int listMBeans(MBeanServerConnection server,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                                  ObjectName pattern, QueryExp query)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        final Set names = server.queryNames(pattern,query);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        for (final Iterator i=names.iterator(); i.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            ObjectName name = (ObjectName)i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            log.trace("listMBeans","Got MBean: "+name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                MBeanInfo info =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                    server.getMBeanInfo((ObjectName)name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                MBeanAttributeInfo[] attrs = info.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                if (attrs == null) continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                for (int j=0; j<attrs.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    if (attrs[j].isReadable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                            Object o =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                                server.getAttribute(name,attrs[j].getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                            if (log.isDebugOn())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                                log.debug("listMBeans","\t\t" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                                          attrs[j].getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                                          " = "+o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                            log.trace("listMBeans","JmxClient failed to get " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                                      attrs[j].getName() + ": " + x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                            final IOException io =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                                new IOException("JmxClient failed to get " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                                                attrs[j].getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                            io.initCause(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                            throw io;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
               log.trace("listMBeans",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                         "JmxClient failed to get MBeanInfo: "  + x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                final IOException io =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    new IOException("JmxClient failed to get MBeanInfo: "+x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                io.initCause(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                throw io;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        return names.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * Compute the full path name for a default file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @param basename basename (with extension) of the default file.
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 23031
diff changeset
   231
     * @return ${JRE}/conf/management/${basename}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    private static String getDefaultFileName(String basename) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        final String fileSeparator = File.separator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        final StringBuffer defaultFileName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            new StringBuffer(System.getProperty("java.home")).
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 23031
diff changeset
   237
            append(fileSeparator).append("conf").append(fileSeparator).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            append("management").append(fileSeparator).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            append(basename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        return defaultFileName.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * Compute the full path name for a default file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @param basename basename (with extension) of the default file.
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 23031
diff changeset
   246
     * @return ${JRE}/conf/management/${basename}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    private static String getDefaultStoreName(String basename) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        final String fileSeparator = File.separator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        final StringBuffer defaultFileName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            new StringBuffer(System.getProperty("test.src")).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            append(fileSeparator).append("ssl").append(fileSeparator).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            append(basename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        return defaultFileName.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * Parses the password file to read the credentials.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * Returns an ArrayList of arrays of 2 string:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * {<subject>, <password>}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * If the password file does not exists, return an empty list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * (File not found = empty file).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    private ArrayList readCredentials(String passwordFileName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        final Properties pws = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        final ArrayList  result = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        final File f = new File(passwordFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        if (!f.exists()) return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        FileInputStream fin = new FileInputStream(passwordFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        try {pws.load(fin);}finally{fin.close();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        for (Enumeration en=pws.propertyNames();en.hasMoreElements();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            final String[] cred = new String[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            cred[0]=(String)en.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            cred[1]=pws.getProperty(cred[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            result.add(cred);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * Connect with the given url, using all given credentials in turn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * A null entry in the useCredentials arrays indicate a connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * where no credentials are used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @param url JMXServiceURL of the server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @param useCredentials An array of credentials (a credential
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *        is a two String array, so this is an array of arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *        of strings:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *           useCredentials[i][0]=subject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *           useCredentials[i][1]=password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *        if useCredentials[i] == null means no credentials.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @param expectConnectOk true if connection is expected to succeed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *    Note: if expectConnectOk=false and the test fails to connect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *          the number of failure is not incremented. Conversely,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *          if expectConnectOk=false and the test does not fail to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *          connect the number of failure is incremented.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @param expectReadOk true if communication (listMBeans) is expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *        to succeed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *    Note: if expectReadOk=false and the test fails to read MBeans
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *          the number of failure is not incremented. Conversely,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *          if expectReadOk=false and the test does not fail to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *          read MBeans the number of failure is incremented.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @return number of failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    public int connectAndRead(JMXServiceURL url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                              Object[] useCredentials,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                              boolean  expectConnectOk,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                              boolean  expectReadOk)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        int errorCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        for (int i=0 ; i<useCredentials.length ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            final Map m = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            final String[] credentials = (String[])useCredentials[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            final String   crinfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            if (credentials != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                crinfo = "{"+credentials[0] + ", " + credentials[1] + "}";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                m.put(PropertyNames.CREDENTIALS,credentials);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                crinfo="no credentials";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            log.trace("testCommunication","using credentials: " + crinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            final JMXConnector c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                c = JMXConnectorFactory.connect(url,m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            } catch (IOException x ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                if (expectConnectOk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    final String err = "Connection failed for " + crinfo +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                        ": " + x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    System.out.println(err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                    log.trace("testCommunication",err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    log.debug("testCommunication",x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    errorCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    System.out.println("Connection failed as expected for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                                       crinfo + ": " + x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            } catch (RuntimeException x ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                if (expectConnectOk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    final String err = "Connection failed for " + crinfo +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                        ": " + x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    System.out.println(err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    log.trace("testCommunication",err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    log.debug("testCommunication",x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    errorCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    System.out.println("Connection failed as expected for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                                       crinfo + ": " + x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                MBeanServerConnection conn =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    c.getMBeanServerConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                if (log.isDebugOn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    log.debug("testCommunication","Connection is:" + conn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    log.debug("testCommunication","Server domain is: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                              conn.getDefaultDomain());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                final ObjectName pattern =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    new ObjectName("java.lang:type=Memory,*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                final int count = listMBeans(conn,pattern,null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                if (count == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    throw new Exception("Expected at least one matching "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                                        "MBean for "+pattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                if (expectReadOk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                    System.out.println("Communication succeeded " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                                       "as expected for "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                                       crinfo + ": found " + count
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                                       + ((count<2)?"MBean":"MBeans"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    final String err = "Expected failure didn't occur for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                        crinfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                    System.out.println(err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                    errorCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            } catch (IOException x ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                if (expectReadOk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                    final String err = "Communication failed with " + crinfo +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                        ": " + x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                    System.out.println(err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                    log.trace("testCommunication",err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                    log.debug("testCommunication",x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                    errorCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                    System.out.println("Communication failed as expected for "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                                       crinfo + ": " + x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            } catch (RuntimeException x ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                if (expectReadOk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                    final String err = "Communication failed with " + crinfo +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                        ": " + x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                    System.out.println(err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    log.trace("testCommunication",err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                    log.debug("testCommunication",x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    errorCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                    System.out.println("Communication failed as expected for "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                                       crinfo + ": " + x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                final String err = "Failed to read MBeans with " + crinfo +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                    ": " + x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                System.out.println(err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                log.trace("testCommunication",err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                log.debug("testCommunication",x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                errorCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                c.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        return errorCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    private void setSslProperties() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        final String defaultKeyStore =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            getDefaultStoreName(DefaultValues.KEYSTORE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        final String defaultTrustStore =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            getDefaultStoreName(DefaultValues.TRUSTSTORE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        final String keyStore =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            System.getProperty(PropertyNames.KEYSTORE, defaultKeyStore);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        System.setProperty(PropertyNames.KEYSTORE,keyStore);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        log.trace("setSslProperties",PropertyNames.KEYSTORE+"="+keyStore);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        final String password =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            System.getProperty(PropertyNames.KEYSTORE_PASSWD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                               DefaultValues.KEYSTORE_PASSWD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        System.setProperty(PropertyNames.KEYSTORE_PASSWD,password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        log.trace("setSslProperties",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                  PropertyNames.KEYSTORE_PASSWD+"="+password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        final String trustStore =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            System.getProperty(PropertyNames.TRUSTSTORE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                               defaultTrustStore);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        System.setProperty(PropertyNames.TRUSTSTORE,trustStore);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        log.trace("setSslProperties",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                  PropertyNames.TRUSTSTORE+"="+trustStore);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        final String trustword =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            System.getProperty(PropertyNames.TRUSTSTORE_PASSWD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                               DefaultValues.TRUSTSTORE_PASSWD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        System.setProperty(PropertyNames.TRUSTSTORE_PASSWD,trustword);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        log.trace("setSslProperties",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                  PropertyNames.TRUSTSTORE_PASSWD+"="+trustword);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    private void checkSslConfiguration() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            final String defaultConf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                getDefaultFileName(DefaultValues.CONFIG_FILE_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            final String confname =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                System.getProperty(PropertyNames.CONFIG_FILE_NAME,defaultConf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            final Properties props = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            final File conf = new File(confname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            if (conf.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                FileInputStream fin = new FileInputStream(conf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                try {props.load(fin);} finally {fin.close();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            // Do we use SSL?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            final String  useSslStr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                props.getProperty(PropertyNames.USE_SSL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                                  DefaultValues.USE_SSL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            final boolean useSsl =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                Boolean.valueOf(useSslStr).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            log.debug("checkSslConfiguration",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                      PropertyNames.USE_SSL+"="+useSsl+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                      ": setting SSL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            // Do we use SSL client authentication?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            final String  useSslClientAuthStr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                props.getProperty(PropertyNames.SSL_NEED_CLIENT_AUTH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                                  DefaultValues.SSL_NEED_CLIENT_AUTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            final boolean useSslClientAuth =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                Boolean.valueOf(useSslClientAuthStr).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            log.debug("checkSslConfiguration",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                      PropertyNames.SSL_NEED_CLIENT_AUTH+"="+useSslClientAuth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            // Do we use customized SSL cipher suites?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            final String sslCipherSuites =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                props.getProperty(PropertyNames.SSL_ENABLED_CIPHER_SUITES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            log.debug("checkSslConfiguration",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                      PropertyNames.SSL_ENABLED_CIPHER_SUITES + "=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                      sslCipherSuites);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            // Do we use customized SSL protocols?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            final String sslProtocols =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                props.getProperty(PropertyNames.SSL_ENABLED_PROTOCOLS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            log.debug("checkSslConfiguration",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                      PropertyNames.SSL_ENABLED_PROTOCOLS + "=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                      sslProtocols);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            if (useSsl) setSslProperties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            System.out.println("Failed to setup SSL configuration: " + x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            log.debug("checkSslConfiguration",x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * Tests the server bootstraped at the given URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * Uses the system properties to determine which config file is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * Loads the config file to determine which password file is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * Loads the password file to find out wich credentials to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * Also checks that unregistered user/passwords are not allowed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * connect when a password file is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * This method calls connectAndRead().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    public void testCommunication(JMXServiceURL url)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        final String defaultConf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            getDefaultFileName(DefaultValues.CONFIG_FILE_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        final String confname =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            System.getProperty(PropertyNames.CONFIG_FILE_NAME,defaultConf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        final Properties props = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        final File conf = new File(confname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        if (conf.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            FileInputStream fin = new FileInputStream(conf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            try {props.load(fin);} finally {fin.close();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        // Do we use authentication?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        final String  useAuthenticationStr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            props.getProperty(PropertyNames.USE_AUTHENTICATION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                              DefaultValues.USE_AUTHENTICATION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        final boolean useAuthentication =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            Boolean.valueOf(useAuthenticationStr).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        // Get Password File
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        final String defaultPasswordFileName = Utils.convertPath(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            getDefaultFileName(DefaultValues.PASSWORD_FILE_NAME));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        final String passwordFileName = Utils.convertPath(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            props.getProperty(PropertyNames.PASSWORD_FILE_NAME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                              defaultPasswordFileName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        // Get Access File
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        final String defaultAccessFileName = Utils.convertPath(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            getDefaultFileName(DefaultValues.ACCESS_FILE_NAME));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        final String accessFileName = Utils.convertPath(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            props.getProperty(PropertyNames.ACCESS_FILE_NAME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                              defaultAccessFileName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        if (useAuthentication) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            System.out.println("PasswordFileName: " + passwordFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            System.out.println("accessFileName: " + accessFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        final Object[] allCredentials;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        final Object[] noCredentials = { null };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        if (useAuthentication) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            final ArrayList l = readCredentials(passwordFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            if (l.size() == 0) allCredentials = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            else allCredentials = l.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        } else allCredentials = noCredentials;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        int errorCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        if (allCredentials!=null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            // Tests that the registered user/passwords are allowed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            // connect & read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            errorCount += connectAndRead(url,allCredentials,true,true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            // Tests that no one is allowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            // connect & read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            final String[][] someCredentials = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                { "modify", "R&D" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                { "measure", "QED" }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            errorCount += connectAndRead(url,someCredentials,false,false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        if (useAuthentication && allCredentials != noCredentials) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            // Tests that the registered user/passwords are not allowed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            // connect & read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            final String[][] badCredentials = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                { "bad.user", "R&D" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                { "measure", "bad.password" }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            errorCount += connectAndRead(url,badCredentials,false,false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        if (errorCount > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            final String err = "Test " + confname + " failed with " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                errorCount + " error(s)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            log.debug("testCommunication",err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            throw new RuntimeException(err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * Test the configuration indicated by `file'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * Sets the appropriate System properties for config file and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * port and then calls ConnectorBootstrap.initialize().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * eventually cleans up by calling ConnectorBootstrap.terminate().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * @return null if the test succeeds, an error message otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     **/
23031
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   620
    private String testConfiguration(File file) throws IOException, InterruptedException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
23031
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   622
        for (int i = 0; i < MAX_GET_FREE_PORT_TRIES; i++) {
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   623
            try {
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   624
                int port = jdk.testlibrary.Utils.getFreePort();
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   625
                final String path;
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   626
                try {
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   627
                    path=(file==null)?null:file.getCanonicalPath();
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   628
                } catch(IOException x) {
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   629
                    final String err = "Failed to test configuration " + file +
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   630
                        ": " + x;
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   631
                    log.trace("testConfiguration",err);
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   632
                    log.debug("testConfiguration",x);
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   633
                    return err;
21408
6468b29c9877 8027058: sun/management/jmxremote/bootstrap/RmiBootstrapTest.sh Failed to initialize connector
jbachorik
parents: 5506
diff changeset
   634
                }
23031
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   635
                final String config = (path==null)?"Default config file":path;
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   636
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   637
                System.out.println("***");
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   638
                System.out.println("*** Testing configuration (port=" + port + "): "
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   639
                                   + path);
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   640
                System.out.println("***");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
23031
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   642
                System.setProperty("com.sun.management.jmxremote.port",
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   643
                                    Integer.toString(port));
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   644
                if (path != null)
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   645
                    System.setProperty("com.sun.management.config.file", path);
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   646
                else
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   647
                    System.getProperties().remove("com.sun.management.config.file");
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   648
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   649
                log.trace("testConfiguration","com.sun.management.jmxremote.port="+port);
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   650
                if (path != null && log.isDebugOn())
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   651
                    log.trace("testConfiguration",
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   652
                              "com.sun.management.config.file="+path);
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   653
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   654
                checkSslConfiguration();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
23031
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   656
                final JMXConnectorServer cs;
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   657
                try {
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   658
                    cs = ConnectorBootstrap.initialize();
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   659
                } catch (AgentConfigurationError x) {
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   660
                    if (x.getCause() instanceof ExportException) {
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   661
                        if (x.getCause().getCause() instanceof BindException) {
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   662
                            throw (BindException)x.getCause().getCause();
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   663
                        }
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   664
                    }
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   665
                    final String err = "Failed to initialize connector:" +
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   666
                        "\n\tcom.sun.management.jmxremote.port=" + port +
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   667
                        ((path!=null)?"\n\tcom.sun.management.config.file="+path:
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   668
                         "\n\t"+config) +
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   669
                        "\n\tError is: " + x;
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   670
                    log.trace("testConfiguration",err);
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   671
                    log.debug("testConfiguration",x);
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   672
                    return err;
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   673
                } catch (Exception x) {
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   674
                    log.debug("testConfiguration",x);
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   675
                    return x.toString();
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   676
                }
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   677
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   678
                try {
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   679
                    JMXServiceURL url =
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   680
                        new JMXServiceURL("rmi",null,0,"/jndi/rmi://localhost:"+
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   681
                                          port+"/jmxrmi");
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   682
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   683
                    try {
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   684
                        testCommunication(url);
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   685
                    } catch (Exception x) {
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   686
                        final String err = "Failed to connect to agent {url="+url+
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   687
                            "}: " + x;
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   688
                        log.trace("testConfiguration",err);
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   689
                        log.debug("testConfiguration",x);
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   690
                        return err;
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   691
                    }
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   692
                } catch (Exception x) {
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   693
                    final String err = "Failed to test configuration "+config+
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   694
                        ": "+x;
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   695
                    log.trace("testConfiguration",err);
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   696
                    log.debug("testConfiguration",x);
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   697
                    return err;
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   698
                } finally {
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   699
                    try {
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   700
                        cs.stop();
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   701
                    } catch (Exception x) {
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   702
                        final String err = "Failed to terminate: "+x;
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   703
                        log.trace("testConfiguration",err);
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   704
                        log.debug("testConfiguration",x);
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   705
                    }
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   706
                }
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   707
                System.out.println("Configuration " + config + " successfully tested");
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   708
                return null;
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   709
            } catch(BindException ex) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        }
23031
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   712
        System.err.println("Cannot find a free port after " + MAX_GET_FREE_PORT_TRIES + " tries");
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   713
        return "Failed: cannot find a free port after " + MAX_GET_FREE_PORT_TRIES + " tries";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * Test a configuration file which should make the bootstrap fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * The test is assumed to have succeeded if the bootstrap fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * @return null if the test succeeds, an error message otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     **/
23031
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   721
    private String testConfigurationKo(File conf) throws InterruptedException, IOException {
21408
6468b29c9877 8027058: sun/management/jmxremote/bootstrap/RmiBootstrapTest.sh Failed to initialize connector
jbachorik
parents: 5506
diff changeset
   722
        String errStr = null;
23031
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   723
        errStr = testConfiguration(conf);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        if (errStr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            return "Configuration " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                conf + " should have failed!";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        System.out.println("Configuration " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                           conf + " failed as expected");
23031
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   730
        log.debug("runko", "Error was: " + errStr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * Test a configuration file. Determines whether the bootstrap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * should succeed or fail depending on the file name:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     *     *ok.properties: bootstrap should succeed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     *     *ko.properties: bootstrap or connection should fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * @return null if the test succeeds, an error message otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     **/
23031
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   741
    private String testConfigurationFile(String fileName) throws InterruptedException, IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        File file = new File(fileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        if (fileName.endsWith("ok.properties")) {
21408
6468b29c9877 8027058: sun/management/jmxremote/bootstrap/RmiBootstrapTest.sh Failed to initialize connector
jbachorik
parents: 5506
diff changeset
   745
            String errStr = null;
23031
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   746
            errStr = testConfiguration(file);
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   747
            return errStr;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        if (fileName.endsWith("ko.properties")) {
23031
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   750
            return testConfigurationKo(file);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        return fileName +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            ": test file suffix must be one of [ko|ok].properties";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * Find all *ko.property files and test them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * (see findConfigurationFilesKo() and testConfigurationKo())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * @throws RuntimeException if the test fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     **/
23031
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   761
    public void runko() throws InterruptedException, IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        final File[] conf = findConfigurationFilesKo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        if ((conf == null)||(conf.length == 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            throw new RuntimeException("No configuration found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        String errStr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        for (int i=0;i<conf.length;i++) {
23031
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   768
            errStr = testConfigurationKo(conf[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            if (errStr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                throw new RuntimeException(errStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * Find all *ok.property files and test them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * (see findConfigurationFilesOk() and testConfiguration())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * @throws RuntimeException if the test fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     **/
23031
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   781
    public void runok() throws InterruptedException, IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        final File[] conf = findConfigurationFilesOk();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        if ((conf == null)||(conf.length == 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            throw new RuntimeException("No configuration found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
21408
6468b29c9877 8027058: sun/management/jmxremote/bootstrap/RmiBootstrapTest.sh Failed to initialize connector
jbachorik
parents: 5506
diff changeset
   786
        String errStr = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        for (int i=0;i<conf.length;i++) {
23031
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   788
            errStr = testConfiguration(conf[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            if (errStr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                throw new RuntimeException(errStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        // FIXME: No jmxremote.password is not installed in JRE by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        // - disable the following test case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        // Test default config
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        // errStr = testConfiguration(null,port+testPort++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        // if (errStr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        //    throw new RuntimeException(errStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        // }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * Finds all configuration files (*ok.properties and *ko.properties)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * and tests them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * (see runko() and runok()).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * @throws RuntimeException if the test fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     **/
23031
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   811
    public void run() throws InterruptedException, IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        runok();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        runko();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * Tests the specified configuration files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * If args[] is not empty, each element in args[] is expected to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * a filename ending either by ok.properties or ko.properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * Otherwise, the configuration files will be automatically determined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * by looking at all *.properties files located in the directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * indicated by the System property "test.src".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * @throws RuntimeException if the test fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     **/
23031
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   825
    public void run(String args[]) throws InterruptedException, IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        if (args.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            run() ; return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        for (int i=0; i<args.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            final String errStr =testConfigurationFile(args[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            if (errStr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                throw new RuntimeException(errStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * Calls run(args[]).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * exit(1) if the test fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     **/
21408
6468b29c9877 8027058: sun/management/jmxremote/bootstrap/RmiBootstrapTest.sh Failed to initialize connector
jbachorik
parents: 5506
diff changeset
   841
    public static void main(String args[]) throws Exception {
23031
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   842
        try {
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   843
            MAX_GET_FREE_PORT_TRIES = Integer.parseInt(System.getProperty("test.getfreeport.max.tries", "10"));
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   844
        } catch (NumberFormatException ex) {
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   845
        }
995cf5c2eb55 7195249: Some jtreg tests use hard coded ports
sla
parents: 21408
diff changeset
   846
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        RmiBootstrapTest manager = new RmiBootstrapTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            manager.run(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        } catch (RuntimeException r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            System.out.println("Test Failed: "+ r.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            System.out.println("Test Failed: "+ t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            t.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            System.exit(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        System.out.println("**** Test  RmiBootstrap Passed ****");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
}