jdk/src/java.management/share/classes/javax/management/remote/JMXConnectorFactory.java
author alanb
Fri, 07 Apr 2017 08:05:54 +0000
changeset 44545 83b611b88ac8
parent 43503 bc7f8619ab70
child 45663 4a0cbf8f2474
permissions -rw-r--r--
8177530: Module system implementation refresh (4/2017) Reviewed-by: mchung, alanb Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
32639
339de1317e84 8043937: Drop support for the IIOP transport from the JMX RMIConnector
jbachorik
parents: 25859
diff changeset
     2
 * Copyright (c) 2002, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1570
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: 1570
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: 1570
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1570
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1570
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 javax.management.remote;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import com.sun.jmx.mbeanserver.Util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
43503
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
    30
import java.io.UncheckedIOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.ServiceLoader;
43503
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
    36
import java.util.ServiceLoader.Provider;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.StringTokenizer;
43503
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
    38
import java.util.function.Predicate;
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
    39
import java.util.stream.Stream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import com.sun.jmx.remote.util.ClassLogger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import com.sun.jmx.remote.util.EnvHelp;
18190
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
    45
import sun.reflect.misc.ReflectUtil;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <p>Factory to create JMX API connector clients.  There
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * are no instances of this class.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <p>Connections are usually made using the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * #connect(JMXServiceURL) connect} method of this class.  More
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * advanced applications can separate the creation of the connector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * client, using {@link #newJMXConnector(JMXServiceURL, Map)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * newJMXConnector} and the establishment of the connection itself, using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * {@link JMXConnector#connect(Map)}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <p>Each client is created by an instance of {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * JMXConnectorProvider}.  This instance is found as follows.  Suppose
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * the given {@link JMXServiceURL} looks like
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <code>"service:jmx:<em>protocol</em>:<em>remainder</em>"</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * Then the factory will attempt to find the appropriate {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * JMXConnectorProvider} for <code><em>protocol</em></code>.  Each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * occurrence of the character <code>+</code> or <code>-</code> in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <code><em>protocol</em></code> is replaced by <code>.</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <code>_</code>, respectively.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <p>A <em>provider package list</em> is searched for as follows:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <li>If the <code>environment</code> parameter to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * #newJMXConnector(JMXServiceURL, Map) newJMXConnector} contains the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * key <code>jmx.remote.protocol.provider.pkgs</code> then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * associated value is the provider package list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <li>Otherwise, if the system property
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <code>jmx.remote.protocol.provider.pkgs</code> exists, then its value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * is the provider package list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <li>Otherwise, there is no provider package list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <p>The provider package list is a string that is interpreted as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * list of non-empty Java package names separated by vertical bars
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * (<code>|</code>).  If the string is empty, then so is the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * package list.  If the provider package list is not a String, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * it contains an element that is an empty string, a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * JMXProviderException} is thrown.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * <p>If the provider package list exists and is not empty, then for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * each element <code><em>pkg</em></code> of the list, the factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * will attempt to load the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * <code><em>pkg</em>.<em>protocol</em>.ClientProvider</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * <p>If the <code>environment</code> parameter to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * #newJMXConnector(JMXServiceURL, Map) newJMXConnector} contains the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * key <code>jmx.remote.protocol.provider.class.loader</code> then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * associated value is the class loader to use to load the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * If the associated value is not an instance of {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * java.lang.ClassLoader}, an {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * java.lang.IllegalArgumentException} is thrown.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * <p>If the <code>jmx.remote.protocol.provider.class.loader</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * key is not present in the <code>environment</code> parameter, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * calling thread's context class loader is used.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * <p>If the attempt to load this class produces a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * ClassNotFoundException}, the search for a handler continues with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * the next element of the list.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * <p>Otherwise, a problem with the provider found is signalled by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * {@link JMXProviderException} whose {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * JMXProviderException#getCause() <em>cause</em>} indicates the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * exception, as follows:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * <li>if the attempt to load the class produces an exception other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * than <code>ClassNotFoundException</code>, that is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * <em>cause</em>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * <li>if {@link Class#newInstance()} for the class produces an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * exception, that is the <em>cause</em>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * <p>If no provider is found by the above steps, including the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * default case where there is no provider package list, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * implementation will use its own provider for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * <code><em>protocol</em></code>, or it will throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * <code>MalformedURLException</code> if there is none.  An
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * implementation may choose to find providers by other means.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * example, it may support the <a
24368
2b4801b94265 8038795: Tidy warnings cleanup for javax.management
yan
parents: 23010
diff changeset
   140
 * href="{@docRoot}/../technotes/guides/jar/jar.html#Service%20Provider">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * JAR conventions for service providers</a>, where the service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * interface is <code>JMXConnectorProvider</code>.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 *
14917
bf08557604f8 8001048: JSR-160: Allow IIOP transport to be optional
alanb
parents: 5506
diff changeset
   144
 * <p>Every implementation must support the RMI connector protocol with
bf08557604f8 8001048: JSR-160: Allow IIOP transport to be optional
alanb
parents: 5506
diff changeset
   145
 * the default RMI transport, specified with string <code>rmi</code>.
32639
339de1317e84 8043937: Drop support for the IIOP transport from the JMX RMIConnector
jbachorik
parents: 25859
diff changeset
   146
 * </p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * <p>Once a provider is found, the result of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * <code>newJMXConnector</code> method is the result of calling {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * JMXConnectorProvider#newJMXConnector(JMXServiceURL,Map) newJMXConnector}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * on the provider.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * <p>The <code>Map</code> parameter passed to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * <code>JMXConnectorProvider</code> is a new read-only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * <code>Map</code> that contains all the entries that were in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * <code>environment</code> parameter to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * #newJMXConnector(JMXServiceURL,Map)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * JMXConnectorFactory.newJMXConnector}, if there was one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * Additionally, if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * <code>jmx.remote.protocol.provider.class.loader</code> key is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * present in the <code>environment</code> parameter, it is added to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * the new read-only <code>Map</code>.  The associated value is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * calling thread's context class loader.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
public class JMXConnectorFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * <p>Name of the attribute that specifies the default class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * loader. This class loader is used to deserialize return values and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * exceptions from remote <code>MBeanServerConnection</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * calls.  The value associated with this attribute is an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * of {@link ClassLoader}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public static final String DEFAULT_CLASS_LOADER =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        "jmx.remote.default.class.loader";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * <p>Name of the attribute that specifies the provider packages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * that are consulted when looking for the handler for a protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * The value associated with this attribute is a string with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * package names separated by vertical bars (<code>|</code>).</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    public static final String PROTOCOL_PROVIDER_PACKAGES =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        "jmx.remote.protocol.provider.pkgs";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * <p>Name of the attribute that specifies the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * loader for loading protocol providers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * The value associated with this attribute is an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * of {@link ClassLoader}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public static final String PROTOCOL_PROVIDER_CLASS_LOADER =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        "jmx.remote.protocol.provider.class.loader";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    private static final String PROTOCOL_PROVIDER_DEFAULT_PACKAGE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        "com.sun.jmx.remote.protocol";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    private static final ClassLogger logger =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        new ClassLogger("javax.management.remote.misc", "JMXConnectorFactory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    /** There are no instances of this class.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    private JMXConnectorFactory() {
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
     * <p>Creates a connection to the connector server at the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * address.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * <p>This method is equivalent to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * #connect(JMXServiceURL,Map) connect(serviceURL, null)}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @param serviceURL the address of the connector server to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * connect to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @return a <code>JMXConnector</code> whose {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * JMXConnector#connect connect} method has been called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @exception NullPointerException if <code>serviceURL</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @exception IOException if the connector client or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * connection cannot be made because of a communication problem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @exception SecurityException if the connection cannot be made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * for security reasons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public static JMXConnector connect(JMXServiceURL serviceURL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        return connect(serviceURL, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * <p>Creates a connection to the connector server at the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * address.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * <p>This method is equivalent to:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * JMXConnector conn = JMXConnectorFactory.newJMXConnector(serviceURL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *                                                         environment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * conn.connect(environment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @param serviceURL the address of the connector server to connect to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @param environment a set of attributes to determine how the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * connection is made.  This parameter can be null.  Keys in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * map must be Strings.  The appropriate type of each associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * value depends on the attribute.  The contents of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * <code>environment</code> are not changed by this call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @return a <code>JMXConnector</code> representing the newly-made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * connection.  Each successful call to this method produces a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * different object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @exception NullPointerException if <code>serviceURL</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @exception IOException if the connector client or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * connection cannot be made because of a communication problem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @exception SecurityException if the connection cannot be made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * for security reasons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    public static JMXConnector connect(JMXServiceURL serviceURL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                                       Map<String,?> environment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        if (serviceURL == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            throw new NullPointerException("Null JMXServiceURL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        JMXConnector conn = newJMXConnector(serviceURL, environment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        conn.connect(environment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        return conn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   275
    private static <K,V> Map<K,V> newHashMap() {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   276
        return new HashMap<K,V>();
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   277
    }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   278
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   279
    private static <K> Map<K,Object> newHashMap(Map<K,?> map) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   280
        return new HashMap<K,Object>(map);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   281
    }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   282
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * <p>Creates a connector client for the connector server at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * given address.  The resultant client is not connected until its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * {@link JMXConnector#connect(Map) connect} method is called.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @param serviceURL the address of the connector server to connect to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @param environment a set of attributes to determine how the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * connection is made.  This parameter can be null.  Keys in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * map must be Strings.  The appropriate type of each associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * value depends on the attribute.  The contents of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * <code>environment</code> are not changed by this call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @return a <code>JMXConnector</code> representing the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * connector client.  Each successful call to this method produces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * a different object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @exception NullPointerException if <code>serviceURL</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @exception IOException if the connector client cannot be made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * because of a communication problem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @exception MalformedURLException if there is no provider for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * protocol in <code>serviceURL</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @exception JMXProviderException if there is a provider for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * protocol in <code>serviceURL</code> but it cannot be used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * some reason.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    public static JMXConnector newJMXConnector(JMXServiceURL serviceURL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                                               Map<String,?> environment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            throws IOException {
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   315
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   316
        final Map<String,Object> envcopy;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        if (environment == null)
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   318
            envcopy = newHashMap();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            EnvHelp.checkAttributes(environment);
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   321
            envcopy = newHashMap(environment);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        final ClassLoader loader = resolveClassLoader(envcopy);
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   325
        final Class<JMXConnectorProvider> targetInterface =
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   326
                JMXConnectorProvider.class;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        final String protocol = serviceURL.getProtocol();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        final String providerClassName = "ClientProvider";
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1510
diff changeset
   329
        final JMXServiceURL providerURL = serviceURL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1510
diff changeset
   331
        JMXConnectorProvider provider = getProvider(providerURL, envcopy,
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1510
diff changeset
   332
                                               providerClassName,
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1510
diff changeset
   333
                                               targetInterface,
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1510
diff changeset
   334
                                               loader);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        IOException exception = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        if (provider == null) {
43503
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   338
            Predicate<Provider<?>> systemProvider =
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   339
                    JMXConnectorFactory::isSystemProvider;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            // Loader is null when context class loader is set to null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            // and no loader has been provided in map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            // com.sun.jmx.remote.util.Service class extracted from j2se
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            // provider search algorithm doesn't handle well null classloader.
43503
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   344
            JMXConnector connection = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            if (loader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                try {
43503
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   347
                    connection = getConnectorAsService(loader,
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   348
                                                       providerURL,
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   349
                                                       envcopy,
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   350
                                                       systemProvider.negate());
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   351
                    if (connection != null) return connection;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                } catch (JMXProviderException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                    throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    exception = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            }
43503
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   358
            connection = getConnectorAsService(
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   359
                             JMXConnectorFactory.class.getClassLoader(),
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   360
                             providerURL,
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   361
                             Collections.unmodifiableMap(envcopy),
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   362
                             systemProvider);
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   363
            if (connection != null) return connection;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        if (provider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            MalformedURLException e =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                new MalformedURLException("Unsupported protocol: " + protocol);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            if (exception == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                throw EnvHelp.initCause(e, exception);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   376
        final Map<String,Object> fixedenv =
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   377
                Collections.unmodifiableMap(envcopy);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   379
        return provider.newJMXConnector(serviceURL, fixedenv);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1156
diff changeset
   382
    private static String resolvePkgs(Map<String, ?> env)
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1156
diff changeset
   383
            throws JMXProviderException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        Object pkgsObject = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        if (env != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            pkgsObject = env.get(PROTOCOL_PROVIDER_PACKAGES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        if (pkgsObject == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            pkgsObject =
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   392
                AccessController.doPrivileged(new PrivilegedAction<String>() {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   393
                    public String run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                        return System.getProperty(PROTOCOL_PROVIDER_PACKAGES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        if (pkgsObject == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        if (!(pkgsObject instanceof String)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            final String msg = "Value of " + PROTOCOL_PROVIDER_PACKAGES +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                " parameter is not a String: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                pkgsObject.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            throw new JMXProviderException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        final String pkgs = (String) pkgsObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        if (pkgs.trim().equals(""))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        // pkgs may not contain an empty element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        if (pkgs.startsWith("|") || pkgs.endsWith("|") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            pkgs.indexOf("||") >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            final String msg = "Value of " + PROTOCOL_PROVIDER_PACKAGES +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                " contains an empty element: " + pkgs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            throw new JMXProviderException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        return pkgs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    static <T> T getProvider(JMXServiceURL serviceURL,
18190
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   424
                             final Map<String, Object> environment,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                             String providerClassName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                             Class<T> targetInterface,
18190
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   427
                             final ClassLoader loader)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        final String protocol = serviceURL.getProtocol();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        final String pkgs = resolvePkgs(environment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        T instance = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        if (pkgs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            instance =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                getProvider(protocol, pkgs, loader, providerClassName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                            targetInterface);
18190
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   440
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   441
            if (instance != null) {
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   442
                boolean needsWrap = (loader != instance.getClass().getClassLoader());
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   443
                environment.put(PROTOCOL_PROVIDER_CLASS_LOADER, needsWrap ? wrap(loader) : loader);
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   444
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        return instance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
18190
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   450
    private static ClassLoader wrap(final ClassLoader parent) {
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   451
        return parent != null ? AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   452
            @Override
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   453
            public ClassLoader run() {
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   454
                return new ClassLoader(parent) {
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   455
                    @Override
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   456
                    protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   457
                        ReflectUtil.checkPackageAccess(name);
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   458
                        return super.loadClass(name, resolve);
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   459
                    }
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   460
                };
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   461
            }
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   462
        }) : null;
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   463
    }
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   464
43503
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   465
    /**
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   466
     * Checks whether the given provider is our system provider for
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   467
     * the RMI connector.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   468
     * If providers for additional protocols are added in the future
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   469
     * then the name of their modules may need to be added here.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   470
     * System providers will be loaded only if no other provider is found.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   471
     * @param provider the provider to test.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   472
     * @return true if this provider is a default system provider.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   473
     */
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   474
    static boolean isSystemProvider(Provider<?> provider) {
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   475
        Module providerModule = provider.type().getModule();
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   476
        return providerModule.isNamed()
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   477
           && providerModule.getName().equals("java.management.rmi");
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   478
    }
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   479
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   480
    /**
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   481
     * Creates a JMXConnector from the first JMXConnectorProvider service
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   482
     * supporting the given url that can be loaded from the given loader.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   483
     * <p>
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   484
     * Parses the list of JMXConnectorProvider services that can be loaded
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   485
     * from the given loader, only retaining those that satisfy the given filter.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   486
     * Then for each provider, attempts to create a new JMXConnector.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   487
     * The first JMXConnector successfully created is returned.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   488
     * <p>
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   489
     * The filter predicate is usually used to either exclude system providers
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   490
     * or only retain system providers (see isSystemProvider(...) above).
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   491
     *
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   492
     * @param loader The ClassLoader to use when looking up an implementation
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   493
     *        of the service. If null, then only installed services will be
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   494
     *        considered.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   495
     *
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   496
     * @param url The JMXServiceURL of the connector for which a provider is
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   497
     *        requested.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   498
     *
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   499
     * @param filter A filter used to exclude or return provider
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   500
     *        implementations. Typically the filter will either exclude
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   501
     *        system services (system default implementations) or only
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   502
     *        retain those.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   503
     *        This can allow to first look for custom implementations (e.g.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   504
     *        deployed on the CLASSPATH with META-INF/services) and
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   505
     *        then only default to system implementations.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   506
     *
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   507
     * @throws IOException if no connector could not be instantiated, and
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   508
     *         at least one provider threw an exception that wasn't a
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   509
     *         {@code MalformedURLException} or a {@code JMProviderException}.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   510
     *
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   511
     * @throws JMXProviderException if a provider for the protocol in
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   512
     *         <code>url</code> was found, but couldn't create the connector
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   513
     *         some reason.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   514
     *
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   515
     * @return an instance of JMXConnector if a provider was found from
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   516
     *         which one could be instantiated, {@code null} otherwise.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   517
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    private static JMXConnector getConnectorAsService(ClassLoader loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                                                      JMXServiceURL url,
43503
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   520
                                                      Map<String, ?> map,
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   521
                                                      Predicate<Provider<?>> filter)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
43503
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   524
        final ConnectorFactory<JMXConnectorProvider, JMXConnector> factory =
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   525
                (p) -> p.newJMXConnector(url, map);
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   526
        return getConnectorAsService(JMXConnectorProvider.class, loader, url,
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   527
                                     filter, factory);
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   528
    }
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   529
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   530
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   531
    /**
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   532
     * A factory function that can create a connector from a provider.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   533
     * The pair (P,C) will be either one of:
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   534
     * a. (JMXConnectorProvider, JMXConnector) or
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   535
     * b. (JMXConnectorServerProvider, JMXConnectorServer)
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   536
     */
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   537
    @FunctionalInterface
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   538
    static interface ConnectorFactory<P,C> {
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   539
        public C apply(P provider) throws Exception;
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   540
    }
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   541
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   542
    /**
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   543
     * An instance of ProviderFinder is used to traverse a
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   544
     * {@code Stream<Provider<P>>} and find the first implementation of P
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   545
     * that supports creating a connector C from the given JMXServiceURL.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   546
     * <p>
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   547
     * The pair (P,C) will be either one of: <br>
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   548
     * a. (JMXConnectorProvider, JMXConnector) or <br>
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   549
     * b. (JMXConnectorServerProvider, JMXConnectorServer)
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   550
     * <p>
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   551
     * The first connector successfully created while traversing the stream
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   552
     * is stored in the ProviderFinder instance. After that, the
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   553
     * ProviderFinder::test method, if called, will always return false, skipping
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   554
     * the remaining providers.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   555
     * <p>
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   556
     * An instance of ProviderFinder is always expected to be used in conjunction
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   557
     * with Stream::findFirst, so that the stream traversal is stopped as soon
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   558
     * as a matching provider is found.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   559
     * <p>
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   560
     * At the end of the stream traversal, the ProviderFinder::get method can be
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   561
     * used to obtain the connector instance (an instance of C) that was created.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   562
     * If no connector could be created, and an exception was encountered while
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   563
     * traversing the stream and attempting to create the connector, then that
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   564
     * exception will be thrown by ProviderFinder::get, wrapped, if needed,
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   565
     * inside an IOException.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   566
     * <p>
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   567
     * If any JMXProviderException is encountered while traversing the stream and
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   568
     * attempting to create the connector, that exception will be wrapped in an
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   569
     * UncheckedIOException and thrown immediately within the stream, thus
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   570
     * interrupting the traversal.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   571
     * <p>
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   572
     * If no matching provider was found (no provider found or attempting
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   573
     * factory.apply always returned null or threw a MalformedURLException,
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   574
     * indicating the provider didn't support the protocol asked for by
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   575
     * the JMXServiceURL), then ProviderFinder::get will simply return null.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   576
     */
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   577
    private static final class ProviderFinder<P,C> implements Predicate<Provider<P>> {
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   578
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   579
        final ConnectorFactory<P,C> factory;
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   580
        final JMXServiceURL  url;
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   581
        private IOException  exception = null;
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   582
        private C connection = null;
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   583
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   584
        ProviderFinder(ConnectorFactory<P,C> factory, JMXServiceURL url) {
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   585
            this.factory = factory;
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   586
            this.url = url;
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   587
        }
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   588
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   589
        /**
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   590
         * Returns {@code true} for the first provider {@code sp} that can
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   591
         * be used to obtain an instance of {@code C} from the given
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   592
         * {@code factory}.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   593
         *
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   594
         * @param sp a candidate provider for instantiating {@code C}.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   595
         *
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   596
         * @throws UncheckedIOException if {@code sp} throws a
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   597
         *         JMXProviderException. The JMXProviderException is set as the
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   598
         *         root cause.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   599
         *
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   600
         * @return {@code true} for the first provider {@code sp} for which
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   601
         *         {@code C} could be instantiated, {@code false} otherwise.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   602
         */
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   603
        public boolean test(Provider<P> sp) {
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   604
            if (connection == null) {
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   605
                P provider = sp.get();
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   606
                try {
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   607
                    connection = factory.apply(provider);
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   608
                    return connection != null;
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   609
                } catch (JMXProviderException e) {
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   610
                    throw new UncheckedIOException(e);
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   611
                } catch (Exception e) {
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   612
                    if (logger.traceOn())
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   613
                        logger.trace("getConnectorAsService",
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   614
                             "URL[" + url +
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   615
                             "] Service provider exception: " + e);
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   616
                    if (!(e instanceof MalformedURLException)) {
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   617
                        if (exception == null) {
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   618
                            if (e instanceof IOException) {
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   619
                                exception = (IOException) e;
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   620
                            } else {
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   621
                                exception = EnvHelp.initCause(
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   622
                                    new IOException(e.getMessage()), e);
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   623
                            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                }
43503
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   627
            }
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   628
            return false;
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   629
        }
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   630
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   631
        /**
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   632
         * Returns an instance of {@code C} if a provider was found from
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   633
         * which {@code C} could be instantiated.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   634
         *
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   635
         * @throws IOException if {@code C} could not be instantiated, and
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   636
         *         at least one provider threw an exception that wasn't a
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   637
         *         {@code MalformedURLException} or a {@code JMProviderException}.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   638
         *
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   639
         * @return an instance of {@code C} if a provider was found from
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   640
         *         which {@code C} could be instantiated, {@code null} otherwise.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   641
         */
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   642
        C get() throws IOException {
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   643
            if (connection != null) return connection;
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   644
            else if (exception != null) throw exception;
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   645
            else return null;
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   646
        }
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   647
    }
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   648
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   649
    /**
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   650
     * Creates a connector from a provider loaded from the ServiceLoader.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   651
     * <p>
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   652
     * The pair (P,C) will be either one of: <br>
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   653
     * a. (JMXConnectorProvider, JMXConnector) or <br>
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   654
     * b. (JMXConnectorServerProvider, JMXConnectorServer)
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   655
     *
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   656
     * @param providerClass The service type for which an implementation
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   657
     *        should be looked up from the {@code ServiceLoader}. This will
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   658
     *        be either {@code JMXConnectorProvider.class} or
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   659
     *        {@code JMXConnectorServerProvider.class}
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   660
     *
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   661
     * @param loader The ClassLoader to use when looking up an implementation
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   662
     *        of the service. If null, then only installed services will be
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   663
     *        considered.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   664
     *
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   665
     * @param url The JMXServiceURL of the connector for which a provider is
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   666
     *        requested.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   667
     *
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   668
     * @param filter A filter used to exclude or return provider
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   669
     *        implementations. Typically the filter will either exclude
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   670
     *        system services (system default implementations) or only
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   671
     *        retain those.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   672
     *        This can allow to first look for custom implementations (e.g.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   673
     *        deployed on the CLASSPATH with META-INF/services) and
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   674
     *        then only default to system implementations.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   675
     *
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   676
     * @param factory A functional factory that can attempt to create an
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   677
     *        instance of connector {@code C} from a provider {@code P}.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   678
     *        Typically, this is a simple wrapper over {@code
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   679
     *        JMXConnectorProvider::newJMXConnector} or {@code
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   680
     *        JMXConnectorProviderServer::newJMXConnectorServer}.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   681
     *
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   682
     * @throws IOException if {@code C} could not be instantiated, and
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   683
     *         at least one provider {@code P} threw an exception that wasn't a
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   684
     *         {@code MalformedURLException} or a {@code JMProviderException}.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   685
     *
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   686
     * @throws JMXProviderException if a provider {@code P} for the protocol in
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   687
     *         <code>url</code> was found, but couldn't create the connector
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   688
     *         {@code C} for some reason.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   689
     *
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   690
     * @return an instance of {@code C} if a provider {@code P} was found from
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   691
     *         which one could be instantiated, {@code null} otherwise.
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   692
     */
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   693
    static <P,C> C getConnectorAsService(Class<P> providerClass,
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   694
                                         ClassLoader loader,
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   695
                                         JMXServiceURL url,
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   696
                                         Predicate<Provider<?>> filter,
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   697
                                         ConnectorFactory<P,C> factory)
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   698
        throws IOException {
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   699
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   700
        // sanity check
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   701
        if (JMXConnectorProvider.class != providerClass
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   702
            && JMXConnectorServerProvider.class != providerClass) {
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   703
            // should never happen
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   704
            throw new InternalError("Unsupported service interface: "
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   705
                                    + providerClass.getName());
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   706
        }
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   707
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   708
        ServiceLoader<P> serviceLoader = loader == null
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   709
                ? ServiceLoader.loadInstalled(providerClass)
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   710
                : ServiceLoader.load(providerClass, loader);
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   711
        Stream<Provider<P>> stream = serviceLoader.stream().filter(filter);
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   712
        ProviderFinder<P,C> finder = new ProviderFinder<>(factory, url);
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   713
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   714
        try {
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   715
            stream.filter(finder).findFirst();
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   716
            return finder.get();
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   717
        } catch (UncheckedIOException e) {
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   718
            if (e.getCause() instanceof JMXProviderException) {
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   719
                throw (JMXProviderException) e.getCause();
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   720
            } else {
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 37782
diff changeset
   721
                throw e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    static <T> T getProvider(String protocol,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                              String pkgs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                              ClassLoader loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                              String providerClassName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                              Class<T> targetInterface)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        StringTokenizer tokenizer = new StringTokenizer(pkgs, "|");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        while (tokenizer.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            String pkg = tokenizer.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            String className = (pkg + "." + protocol2package(protocol) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                                "." + providerClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            Class<?> providerClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                providerClass = Class.forName(className, true, loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                //Add trace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            if (!targetInterface.isAssignableFrom(providerClass)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                    "Provider class does not implement " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                    targetInterface.getName() + ": " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                    providerClass.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                throw new JMXProviderException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            // We have just proved that this cast is correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            Class<? extends T> providerClassT = Util.cast(providerClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            try {
37782
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 32639
diff changeset
   758
                @SuppressWarnings("deprecation")
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 32639
diff changeset
   759
                T result = providerClassT.newInstance();
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 32639
diff changeset
   760
                return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                    "Exception when instantiating provider [" + className +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                    "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                throw new JMXProviderException(msg, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1156
diff changeset
   772
    static ClassLoader resolveClassLoader(Map<String, ?> environment) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        ClassLoader loader = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        if (environment != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                loader = (ClassLoader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                    environment.get(PROTOCOL_PROVIDER_CLASS_LOADER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            } catch (ClassCastException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                    "The ClassLoader supplied in the environment map using " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                    "the " + PROTOCOL_PROVIDER_CLASS_LOADER +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                    " attribute is not an instance of java.lang.ClassLoader";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
18187
a798516cf58d 8008603: Improve provision of JMX providers
jbachorik
parents: 14917
diff changeset
   788
        if (loader == null) {
a798516cf58d 8008603: Improve provision of JMX providers
jbachorik
parents: 14917
diff changeset
   789
            loader = Thread.currentThread().getContextClassLoader();
a798516cf58d 8008603: Improve provision of JMX providers
jbachorik
parents: 14917
diff changeset
   790
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        return loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
    private static String protocol2package(String protocol) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        return protocol.replace('+', '.').replace('-', '_');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
}