jdk/src/share/classes/sun/management/jmxremote/ConnectorBootstrap.java
author mchung
Tue, 17 Jan 2012 15:55:40 -0800
changeset 11530 a9d059c15b80
parent 11125 99b115114fa3
child 11991 800d0ff7b043
permissions -rw-r--r--
7117570: Warnings in sun.mangement.* and its subpackages Reviewed-by: mchung, dsamersoff Contributed-by: kurchi.subhra.hazra@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1448
diff changeset
     2
 * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1448
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1448
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1448
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1448
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1448
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.management.jmxremote;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.BufferedInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.FileInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.net.UnknownHostException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.rmi.NoSuchObjectException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.rmi.Remote;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.rmi.RemoteException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.rmi.registry.Registry;
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
    42
import java.rmi.server.RemoteObject;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.rmi.server.RMIClientSocketFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.rmi.server.RMIServerSocketFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.rmi.server.UnicastRemoteObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.security.KeyStore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.security.Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import java.util.Properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import java.lang.management.ManagementFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import javax.management.remote.JMXAuthenticator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
import javax.management.remote.JMXConnectorServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
import javax.management.remote.JMXConnectorServerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
import javax.management.remote.JMXServiceURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
import javax.management.remote.rmi.RMIConnectorServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
import javax.rmi.ssl.SslRMIClientSocketFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
import javax.rmi.ssl.SslRMIServerSocketFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
import javax.security.auth.Subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
    74
import sun.rmi.server.UnicastRef;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
import sun.rmi.server.UnicastServerRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
import sun.rmi.server.UnicastServerRef2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
import sun.management.Agent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
import sun.management.AgentConfigurationError;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
import static sun.management.AgentConfigurationError.*;
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
    81
import sun.management.ConnectorAddressLink;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
import sun.management.FileSystem;
1447
95d5bd511ec2 6332953: JMX agent should bind to loopback address when starting the local connector server
dfuchs
parents: 52
diff changeset
    83
import com.sun.jmx.remote.util.ClassLogger;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
import com.sun.jmx.remote.internal.RMIExporter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
import com.sun.jmx.remote.security.JMXPluggableAuthenticator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * This class initializes and starts the RMIConnectorServer for JSR 163
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * JMX Monitoring.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
public final class ConnectorBootstrap {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Default values for JMX configuration properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public static interface DefaultValues {
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
    98
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
    99
        public static final String PORT = "0";
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   100
        public static final String CONFIG_FILE_NAME = "management.properties";
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   101
        public static final String USE_SSL = "true";
1448
86d46701261b 6758165: ConnectorBootstrap.DefaultValues should have a default value for USE_LOCAL_ONLY
dfuchs
parents: 1447
diff changeset
   102
        public static final String USE_LOCAL_ONLY = "true";
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   103
        public static final String USE_REGISTRY_SSL = "false";
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   104
        public static final String USE_AUTHENTICATION = "true";
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   105
        public static final String PASSWORD_FILE_NAME = "jmxremote.password";
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   106
        public static final String ACCESS_FILE_NAME = "jmxremote.access";
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   107
        public static final String SSL_NEED_CLIENT_AUTH = "false";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Names of JMX configuration properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public static interface PropertyNames {
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   114
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        public static final String PORT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                "com.sun.management.jmxremote.port";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        public static final String CONFIG_FILE_NAME =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                "com.sun.management.config.file";
1447
95d5bd511ec2 6332953: JMX agent should bind to loopback address when starting the local connector server
dfuchs
parents: 52
diff changeset
   119
        public static final String USE_LOCAL_ONLY =
95d5bd511ec2 6332953: JMX agent should bind to loopback address when starting the local connector server
dfuchs
parents: 52
diff changeset
   120
                "com.sun.management.jmxremote.local.only";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        public static final String USE_SSL =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                "com.sun.management.jmxremote.ssl";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        public static final String USE_REGISTRY_SSL =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                "com.sun.management.jmxremote.registry.ssl";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        public static final String USE_AUTHENTICATION =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                "com.sun.management.jmxremote.authenticate";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        public static final String PASSWORD_FILE_NAME =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                "com.sun.management.jmxremote.password.file";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        public static final String ACCESS_FILE_NAME =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                "com.sun.management.jmxremote.access.file";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        public static final String LOGIN_CONFIG_NAME =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                "com.sun.management.jmxremote.login.config";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        public static final String SSL_ENABLED_CIPHER_SUITES =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                "com.sun.management.jmxremote.ssl.enabled.cipher.suites";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        public static final String SSL_ENABLED_PROTOCOLS =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                "com.sun.management.jmxremote.ssl.enabled.protocols";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        public static final String SSL_NEED_CLIENT_AUTH =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                "com.sun.management.jmxremote.ssl.need.client.auth";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        public static final String SSL_CONFIG_FILE_NAME =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                "com.sun.management.jmxremote.ssl.config.file";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   144
     * JMXConnectorServer associated data.
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   145
     */
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   146
    private static class JMXConnectorServerData {
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   147
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   148
        public JMXConnectorServerData(
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   149
                JMXConnectorServer jmxConnectorServer,
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   150
                JMXServiceURL jmxRemoteURL) {
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   151
            this.jmxConnectorServer = jmxConnectorServer;
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   152
            this.jmxRemoteURL = jmxRemoteURL;
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   153
        }
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   154
        JMXConnectorServer jmxConnectorServer;
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   155
        JMXServiceURL jmxRemoteURL;
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   156
    }
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   157
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   158
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * <p>Prevents our RMI server objects from keeping the JVM alive.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * <p>We use a private interface in Sun's JMX Remote API implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * that allows us to specify how to export RMI objects.  We do so using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * UnicastServerRef, a class in Sun's RMI implementation.  This is all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * non-portable, of course, so this is only valid because we are inside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Sun's JRE.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * <p>Objects are exported using {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * UnicastServerRef#exportObject(Remote, Object, boolean)}.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * boolean parameter is called <code>permanent</code> and means
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * both that the object is not eligible for Distributed Garbage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * Collection, and that its continued existence will not prevent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * the JVM from exiting.  It is the latter semantics we want (we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * already have the former because of the way the JMX Remote API
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * works).  Hence the somewhat misleading name of this class.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    private static class PermanentExporter implements RMIExporter {
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   177
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        public Remote exportObject(Remote obj,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                RMIClientSocketFactory csf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                RMIServerSocketFactory ssf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                throws RemoteException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            synchronized (this) {
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   185
                if (firstExported == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    firstExported = obj;
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   187
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            final UnicastServerRef ref;
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   191
            if (csf == null && ssf == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                ref = new UnicastServerRef(port);
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   193
            } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                ref = new UnicastServerRef2(port, csf, ssf);
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   195
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            return ref.exportObject(obj, null, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        // Nothing special to be done for this case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        public boolean unexportObject(Remote obj, boolean force)
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   201
                throws NoSuchObjectException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            return UnicastRemoteObject.unexportObject(obj, force);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        Remote firstExported;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * This JMXAuthenticator wraps the JMXPluggableAuthenticator and verifies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * that at least one of the principal names contained in the authenticated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Subject is present in the access file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    private static class AccessFileCheckerAuthenticator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            implements JMXAuthenticator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        public AccessFileCheckerAuthenticator(Map<String, Object> env) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            environment = env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            accessFile = (String) env.get("jmx.remote.x.access.file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            properties = propertiesFromFile(accessFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        public Subject authenticate(Object credentials) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            final JMXAuthenticator authenticator =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    new JMXPluggableAuthenticator(environment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            final Subject subject = authenticator.authenticate(credentials);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            checkAccessFileEntries(subject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            return subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        private void checkAccessFileEntries(Subject subject) {
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   230
            if (subject == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                throw new SecurityException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                        "Access denied! No matching entries found in " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                        "the access file [" + accessFile + "] as the " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                        "authenticated Subject is null");
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   235
            }
11125
99b115114fa3 7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents: 5506
diff changeset
   236
            final Set<Principal> principals = subject.getPrincipals();
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 11125
diff changeset
   237
            for (Principal p1: principals) {
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 11125
diff changeset
   238
                if (properties.containsKey(p1.getName())) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    return;
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   240
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            }
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 11125
diff changeset
   242
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 11125
diff changeset
   243
            final Set<String> principalsStr = new HashSet<>();
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 11125
diff changeset
   244
            for (Principal p2: principals) {
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 11125
diff changeset
   245
                principalsStr.add(p2.getName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            throw new SecurityException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    "Access denied! No entries found in the access file [" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    accessFile + "] for any of the authenticated identities " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    principalsStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        private static Properties propertiesFromFile(String fname)
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   254
                throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            Properties p = new Properties();
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   256
            if (fname == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                return p;
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   258
            }
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 11125
diff changeset
   259
            try (FileInputStream fin = new FileInputStream(fname)) {
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 11125
diff changeset
   260
                p.load(fin);
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 11125
diff changeset
   261
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        private final Map<String, Object> environment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        private final Properties properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        private final String accessFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * Initializes and starts the JMX Connector Server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * If the com.sun.management.jmxremote.port property is not defined,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * simply return. Otherwise, attempts to load the config file, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * then calls {@link #initialize(java.lang.String, java.util.Properties)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    public static synchronized JMXConnectorServer initialize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        // Load a new management properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        final Properties props = Agent.loadManagementProperties();
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   280
        if (props == null) {
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   281
            return null;
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   282
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        final String portStr = props.getProperty(PropertyNames.PORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        // System.out.println("initializing: {port=" + portStr + ",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        //                     properties="+props+"}");
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   289
        return initialize(portStr, props);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * Initializes and starts a JMX Connector Server for remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * monitoring and management.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     **/
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   296
    public static synchronized JMXConnectorServer initialize(String portStr, Properties props) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        // Get port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        final int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            port = Integer.parseInt(portStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        } catch (NumberFormatException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            throw new AgentConfigurationError(INVALID_JMXREMOTE_PORT, x, portStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        if (port < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            throw new AgentConfigurationError(INVALID_JMXREMOTE_PORT, portStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        // Do we use authentication?
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   310
        final String useAuthenticationStr =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                props.getProperty(PropertyNames.USE_AUTHENTICATION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                DefaultValues.USE_AUTHENTICATION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        final boolean useAuthentication =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                Boolean.valueOf(useAuthenticationStr).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        // Do we use SSL?
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   317
        final String useSslStr =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                props.getProperty(PropertyNames.USE_SSL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                DefaultValues.USE_SSL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        final boolean useSsl =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                Boolean.valueOf(useSslStr).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        // Do we use RMI Registry SSL?
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   324
        final String useRegistrySslStr =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                props.getProperty(PropertyNames.USE_REGISTRY_SSL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                DefaultValues.USE_REGISTRY_SSL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        final boolean useRegistrySsl =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                Boolean.valueOf(useRegistrySslStr).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        final String enabledCipherSuites =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                props.getProperty(PropertyNames.SSL_ENABLED_CIPHER_SUITES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        String enabledCipherSuitesList[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        if (enabledCipherSuites != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            StringTokenizer st = new StringTokenizer(enabledCipherSuites, ",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            int tokens = st.countTokens();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            enabledCipherSuitesList = new String[tokens];
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   337
            for (int i = 0; i < tokens; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                enabledCipherSuitesList[i] = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        final String enabledProtocols =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                props.getProperty(PropertyNames.SSL_ENABLED_PROTOCOLS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        String enabledProtocolsList[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        if (enabledProtocols != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            StringTokenizer st = new StringTokenizer(enabledProtocols, ",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            int tokens = st.countTokens();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            enabledProtocolsList = new String[tokens];
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   349
            for (int i = 0; i < tokens; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                enabledProtocolsList[i] = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   354
        final String sslNeedClientAuthStr =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                props.getProperty(PropertyNames.SSL_NEED_CLIENT_AUTH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                DefaultValues.SSL_NEED_CLIENT_AUTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        final boolean sslNeedClientAuth =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                Boolean.valueOf(sslNeedClientAuthStr).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        // Read SSL config file name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        final String sslConfigFileName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                props.getProperty(PropertyNames.SSL_CONFIG_FILE_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        String loginConfigName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        String passwordFileName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        String accessFileName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        // Initialize settings when authentication is active
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        if (useAuthentication) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            // Get non-default login configuration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            loginConfigName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                    props.getProperty(PropertyNames.LOGIN_CONFIG_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            if (loginConfigName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                // Get password file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                passwordFileName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                        props.getProperty(PropertyNames.PASSWORD_FILE_NAME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                        getDefaultFileName(DefaultValues.PASSWORD_FILE_NAME));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                checkPasswordFile(passwordFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            // Get access file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            accessFileName = props.getProperty(PropertyNames.ACCESS_FILE_NAME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                    getDefaultFileName(DefaultValues.ACCESS_FILE_NAME));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            checkAccessFile(accessFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
1447
95d5bd511ec2 6332953: JMX agent should bind to loopback address when starting the local connector server
dfuchs
parents: 52
diff changeset
   389
        if (log.debugOn()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            log.debug("initialize",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                    Agent.getText("jmxremote.ConnectorBootstrap.initialize") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                    "\n\t" + PropertyNames.PORT + "=" + port +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                    "\n\t" + PropertyNames.USE_SSL + "=" + useSsl +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                    "\n\t" + PropertyNames.USE_REGISTRY_SSL + "=" + useRegistrySsl +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                    "\n\t" + PropertyNames.SSL_CONFIG_FILE_NAME + "=" + sslConfigFileName +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                    "\n\t" + PropertyNames.SSL_ENABLED_CIPHER_SUITES + "=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                    enabledCipherSuites +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                    "\n\t" + PropertyNames.SSL_ENABLED_PROTOCOLS + "=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    enabledProtocols +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                    "\n\t" + PropertyNames.SSL_NEED_CLIENT_AUTH + "=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    sslNeedClientAuth +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                    "\n\t" + PropertyNames.USE_AUTHENTICATION + "=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    useAuthentication +
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   404
                    (useAuthentication ? (loginConfigName == null ? ("\n\t" + PropertyNames.PASSWORD_FILE_NAME + "=" +
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   405
                    passwordFileName) : ("\n\t" + PropertyNames.LOGIN_CONFIG_NAME + "=" +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    loginConfigName)) : "\n\t" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                    Agent.getText("jmxremote.ConnectorBootstrap.initialize.noAuthentication")) +
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   408
                    (useAuthentication ? ("\n\t" + PropertyNames.ACCESS_FILE_NAME + "=" +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                    accessFileName) : "") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                    "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        JMXConnectorServer cs = null;
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   415
        JMXServiceURL url = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        try {
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   417
            final JMXConnectorServerData data = exportMBeanServer(
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   418
                    mbs, port, useSsl, useRegistrySsl,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    sslConfigFileName, enabledCipherSuitesList,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                    enabledProtocolsList, sslNeedClientAuth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                    useAuthentication, loginConfigName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    passwordFileName, accessFileName);
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   423
            cs = data.jmxConnectorServer;
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   424
            url = data.jmxRemoteURL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            log.config("initialize",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                    Agent.getText("jmxremote.ConnectorBootstrap.initialize.ready",
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   427
                    url.toString()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            throw new AgentConfigurationError(AGENT_EXCEPTION, e, e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   431
        try {
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   432
            // Export remote connector address and associated configuration
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   433
            // properties to the instrumentation buffer.
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 11125
diff changeset
   434
            Map<String, String> properties = new HashMap<>();
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   435
            properties.put("remoteAddress", url.toString());
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   436
            properties.put("authenticate", useAuthenticationStr);
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   437
            properties.put("ssl", useSslStr);
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   438
            properties.put("sslRegistry", useRegistrySslStr);
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   439
            properties.put("sslNeedClientAuth", sslNeedClientAuthStr);
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   440
            ConnectorAddressLink.exportRemote(properties);
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   441
        } catch (Exception e) {
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   442
            // Remote connector server started but unable to export remote
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   443
            // connector address and associated configuration properties to
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   444
            // the instrumentation buffer - non-fatal error.
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   445
            log.debug("initialize", e);
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   446
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        return cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * Creates and starts a RMI Connector Server for "local" monitoring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * and management.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    public static JMXConnectorServer startLocalConnectorServer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        // Ensure cryptographically strong random number generater used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        // to choose the object number - see java.rmi.server.ObjID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        System.setProperty("java.rmi.server.randomIDs", "true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        // This RMI server should not keep the VM alive
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 11125
diff changeset
   460
        Map<String, Object> env = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        env.put(RMIExporter.EXPORTER_ATTRIBUTE, new PermanentExporter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        // The local connector server need only be available via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        // loopback connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        String localhost = "localhost";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        InetAddress lh = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            lh = InetAddress.getByName(localhost);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            localhost = lh.getHostAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        } catch (UnknownHostException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        // localhost unknown or (somehow) didn't resolve to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        // a loopback address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        if (lh == null || !lh.isLoopbackAddress()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            localhost = "127.0.0.1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            JMXServiceURL url = new JMXServiceURL("rmi", localhost, 0);
1447
95d5bd511ec2 6332953: JMX agent should bind to loopback address when starting the local connector server
dfuchs
parents: 52
diff changeset
   482
            // Do we accept connections from local interfaces only?
95d5bd511ec2 6332953: JMX agent should bind to loopback address when starting the local connector server
dfuchs
parents: 52
diff changeset
   483
            Properties props = Agent.getManagementProperties();
95d5bd511ec2 6332953: JMX agent should bind to loopback address when starting the local connector server
dfuchs
parents: 52
diff changeset
   484
            if (props ==  null) {
95d5bd511ec2 6332953: JMX agent should bind to loopback address when starting the local connector server
dfuchs
parents: 52
diff changeset
   485
                props = new Properties();
95d5bd511ec2 6332953: JMX agent should bind to loopback address when starting the local connector server
dfuchs
parents: 52
diff changeset
   486
            }
95d5bd511ec2 6332953: JMX agent should bind to loopback address when starting the local connector server
dfuchs
parents: 52
diff changeset
   487
            String useLocalOnlyStr = props.getProperty(
95d5bd511ec2 6332953: JMX agent should bind to loopback address when starting the local connector server
dfuchs
parents: 52
diff changeset
   488
                    PropertyNames.USE_LOCAL_ONLY, DefaultValues.USE_LOCAL_ONLY);
95d5bd511ec2 6332953: JMX agent should bind to loopback address when starting the local connector server
dfuchs
parents: 52
diff changeset
   489
            boolean useLocalOnly = Boolean.valueOf(useLocalOnlyStr).booleanValue();
95d5bd511ec2 6332953: JMX agent should bind to loopback address when starting the local connector server
dfuchs
parents: 52
diff changeset
   490
            if (useLocalOnly) {
95d5bd511ec2 6332953: JMX agent should bind to loopback address when starting the local connector server
dfuchs
parents: 52
diff changeset
   491
                env.put(RMIConnectorServer.RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE,
95d5bd511ec2 6332953: JMX agent should bind to loopback address when starting the local connector server
dfuchs
parents: 52
diff changeset
   492
                        new LocalRMIServerSocketFactory());
95d5bd511ec2 6332953: JMX agent should bind to loopback address when starting the local connector server
dfuchs
parents: 52
diff changeset
   493
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            JMXConnectorServer server =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                    JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            server.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            return server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            throw new AgentConfigurationError(AGENT_EXCEPTION, e, e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    private static void checkPasswordFile(String passwordFileName) {
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   504
        if (passwordFileName == null || passwordFileName.length() == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            throw new AgentConfigurationError(PASSWORD_FILE_NOT_SET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        File file = new File(passwordFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        if (!file.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            throw new AgentConfigurationError(PASSWORD_FILE_NOT_FOUND, passwordFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        if (!file.canRead()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            throw new AgentConfigurationError(PASSWORD_FILE_NOT_READABLE, passwordFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        FileSystem fs = FileSystem.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            if (fs.supportsFileSecurity(file)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                if (!fs.isAccessUserOnly(file)) {
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   520
                    final String msg = Agent.getText("jmxremote.ConnectorBootstrap.initialize.password.readonly",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                            passwordFileName);
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   522
                    log.config("initialize", msg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                    throw new AgentConfigurationError(PASSWORD_FILE_ACCESS_NOT_RESTRICTED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                            passwordFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            throw new AgentConfigurationError(PASSWORD_FILE_READ_FAILED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                    e, passwordFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    private static void checkAccessFile(String accessFileName) {
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   534
        if (accessFileName == null || accessFileName.length() == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            throw new AgentConfigurationError(ACCESS_FILE_NOT_SET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        File file = new File(accessFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        if (!file.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            throw new AgentConfigurationError(ACCESS_FILE_NOT_FOUND, accessFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        if (!file.canRead()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            throw new AgentConfigurationError(ACCESS_FILE_NOT_READABLE, accessFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    private static void checkRestrictedFile(String restrictedFileName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        if (restrictedFileName == null || restrictedFileName.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            throw new AgentConfigurationError(FILE_NOT_SET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        File file = new File(restrictedFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        if (!file.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            throw new AgentConfigurationError(FILE_NOT_FOUND, restrictedFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        if (!file.canRead()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            throw new AgentConfigurationError(FILE_NOT_READABLE, restrictedFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        FileSystem fs = FileSystem.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            if (fs.supportsFileSecurity(file)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                if (!fs.isAccessUserOnly(file)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                    final String msg = Agent.getText(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                            "jmxremote.ConnectorBootstrap.initialize.file.readonly",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                            restrictedFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                    log.config("initialize", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                    throw new AgentConfigurationError(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                            FILE_ACCESS_NOT_RESTRICTED, restrictedFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            throw new AgentConfigurationError(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                    FILE_READ_FAILED, e, restrictedFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * Compute the full path name for a default file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * @param basename basename (with extension) of the default file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * @return ${JRE}/lib/management/${basename}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    private static String getDefaultFileName(String basename) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        final String fileSeparator = File.separator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        return System.getProperty("java.home") + fileSeparator + "lib" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                fileSeparator + "management" + fileSeparator +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                basename;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    private static SslRMIServerSocketFactory createSslRMIServerSocketFactory(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            String sslConfigFileName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            String[] enabledCipherSuites,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            String[] enabledProtocols,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            boolean sslNeedClientAuth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        if (sslConfigFileName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            return new SslRMIServerSocketFactory(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                    enabledCipherSuites,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                    enabledProtocols,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                    sslNeedClientAuth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            checkRestrictedFile(sslConfigFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                // Load the SSL keystore properties from the config file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                Properties p = new Properties();
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 11125
diff changeset
   603
                try (InputStream in = new FileInputStream(sslConfigFileName)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                    BufferedInputStream bin = new BufferedInputStream(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                    p.load(bin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                String keyStore =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                        p.getProperty("javax.net.ssl.keyStore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                String keyStorePassword =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                        p.getProperty("javax.net.ssl.keyStorePassword", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                String trustStore =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                        p.getProperty("javax.net.ssl.trustStore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                String trustStorePassword =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                        p.getProperty("javax.net.ssl.trustStorePassword", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                char[] keyStorePasswd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                if (keyStorePassword.length() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                    keyStorePasswd = keyStorePassword.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                char[] trustStorePasswd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                if (trustStorePassword.length() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                    trustStorePasswd = trustStorePassword.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                KeyStore ks = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                if (keyStore != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                    ks = KeyStore.getInstance(KeyStore.getDefaultType());
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 11125
diff changeset
   629
                    try (FileInputStream ksfis = new FileInputStream(keyStore)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                        ks.load(ksfis, keyStorePasswd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                KeyManagerFactory kmf = KeyManagerFactory.getInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                        KeyManagerFactory.getDefaultAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                kmf.init(ks, keyStorePasswd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                KeyStore ts = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                if (trustStore != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                    ts = KeyStore.getInstance(KeyStore.getDefaultType());
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 11125
diff changeset
   640
                    try (FileInputStream tsfis = new FileInputStream(trustStore)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                        ts.load(tsfis, trustStorePasswd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                TrustManagerFactory tmf = TrustManagerFactory.getInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                        TrustManagerFactory.getDefaultAlgorithm());
11125
99b115114fa3 7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents: 5506
diff changeset
   646
                tmf.init(ts);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                SSLContext ctx = SSLContext.getInstance("SSL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                return new SslRMIServerSocketFactory(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                        ctx,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                        enabledCipherSuites,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                        enabledProtocols,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                        sslNeedClientAuth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                throw new AgentConfigurationError(AGENT_EXCEPTION, e, e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   662
    private static JMXConnectorServerData exportMBeanServer(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            MBeanServer mbs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            int port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            boolean useSsl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            boolean useRegistrySsl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            String sslConfigFileName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            String[] enabledCipherSuites,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            String[] enabledProtocols,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            boolean sslNeedClientAuth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            boolean useAuthentication,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            String loginConfigName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            String passwordFileName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            String accessFileName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            throws IOException, MalformedURLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        /* Make sure we use non-guessable RMI object IDs.  Otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
         * attackers could hijack open connections by guessing their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
         * IDs.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        System.setProperty("java.rmi.server.randomIDs", "true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 11125
diff changeset
   684
        Map<String, Object> env = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        PermanentExporter exporter = new PermanentExporter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        env.put(RMIExporter.EXPORTER_ATTRIBUTE, exporter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        if (useAuthentication) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            if (loginConfigName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                env.put("jmx.remote.x.login.config", loginConfigName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            if (passwordFileName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                env.put("jmx.remote.x.password.file", passwordFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            env.put("jmx.remote.x.access.file", accessFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            if (env.get("jmx.remote.x.password.file") != null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                    env.get("jmx.remote.x.login.config") != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                env.put(JMXConnectorServer.AUTHENTICATOR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                        new AccessFileCheckerAuthenticator(env));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        RMIClientSocketFactory csf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        RMIServerSocketFactory ssf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        if (useSsl || useRegistrySsl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            csf = new SslRMIClientSocketFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            ssf = createSslRMIServerSocketFactory(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                    sslConfigFileName, enabledCipherSuites,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                    enabledProtocols, sslNeedClientAuth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        if (useSsl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            env.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                    csf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            env.put(RMIConnectorServer.RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                    ssf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        JMXConnectorServer connServer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            connServer =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                    JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            connServer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            if (connServer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                throw new AgentConfigurationError(CONNECTOR_SERVER_IO_ERROR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                        e, url.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                throw new AgentConfigurationError(CONNECTOR_SERVER_IO_ERROR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                        e, connServer.getAddress().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        final Registry registry;
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   740
        if (useRegistrySsl) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            registry =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                    new SingleEntryRegistry(port, csf, ssf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                    "jmxrmi", exporter.firstExported);
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   744
        } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            registry =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                    new SingleEntryRegistry(port,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                    "jmxrmi", exporter.firstExported);
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   748
        }
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   749
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   750
        JMXServiceURL remoteURL = new JMXServiceURL(
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   751
                "service:jmx:rmi:///jndi/rmi://" + url.getHost() + ":" +
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   752
                ((UnicastRef) ((RemoteObject) registry).getRef()).getLiveRef().getPort() +
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   753
                "/jmxrmi");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        /* Our exporter remembers the first object it was asked to
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   756
        export, which will be an RMIServerImpl appropriate for
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   757
        publication in our special registry.  We could
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   758
        alternatively have constructed the RMIServerImpl explicitly
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   759
        and then constructed an RMIConnectorServer passing it as a
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   760
        parameter, but that's quite a bit more verbose and pulls in
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   761
        lots of knowledge of the RMI connector.  */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
52
752ebbd838ad 4981215: Publishing a port number for management console to access
lmalvent
parents: 2
diff changeset
   763
        return new JMXConnectorServerData(connServer, remoteURL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * This class cannot be instantiated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    private ConnectorBootstrap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
1447
95d5bd511ec2 6332953: JMX agent should bind to loopback address when starting the local connector server
dfuchs
parents: 52
diff changeset
   772
    private static final ClassLogger log =
95d5bd511ec2 6332953: JMX agent should bind to loopback address when starting the local connector server
dfuchs
parents: 52
diff changeset
   773
        new ClassLogger(ConnectorBootstrap.class.getPackage().getName(),
95d5bd511ec2 6332953: JMX agent should bind to loopback address when starting the local connector server
dfuchs
parents: 52
diff changeset
   774
                        "ConnectorBootstrap");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
}