jdk/src/share/classes/javax/management/remote/JMXConnectorFactory.java
author yan
Mon, 12 May 2014 14:33:13 +0400
changeset 24368 2b4801b94265
parent 23010 6dadb192ad81
permissions -rw-r--r--
8038795: Tidy warnings cleanup for javax.management Reviewed-by: dfuchs Contributed-by: Alexander Stepanov <alexander.v.stepanov@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
24368
2b4801b94265 8038795: Tidy warnings cleanup for javax.management
yan
parents: 23010
diff changeset
     2
 * Copyright (c) 2002, 2014, 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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.ServiceLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import com.sun.jmx.remote.util.ClassLogger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import com.sun.jmx.remote.util.EnvHelp;
18190
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
    42
import sun.reflect.misc.ReflectUtil;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>Factory to create JMX API connector clients.  There
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * are no instances of this class.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <p>Connections are usually made using the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * #connect(JMXServiceURL) connect} method of this class.  More
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * advanced applications can separate the creation of the connector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * client, using {@link #newJMXConnector(JMXServiceURL, Map)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * newJMXConnector} and the establishment of the connection itself, using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * {@link JMXConnector#connect(Map)}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <p>Each client is created by an instance of {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * JMXConnectorProvider}.  This instance is found as follows.  Suppose
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * the given {@link JMXServiceURL} looks like
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <code>"service:jmx:<em>protocol</em>:<em>remainder</em>"</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * Then the factory will attempt to find the appropriate {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * JMXConnectorProvider} for <code><em>protocol</em></code>.  Each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * occurrence of the character <code>+</code> or <code>-</code> in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <code><em>protocol</em></code> is replaced by <code>.</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <code>_</code>, respectively.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p>A <em>provider package list</em> is searched for as follows:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <li>If the <code>environment</code> parameter to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * #newJMXConnector(JMXServiceURL, Map) newJMXConnector} contains the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * key <code>jmx.remote.protocol.provider.pkgs</code> then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * associated value is the provider package list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <li>Otherwise, if the system property
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <code>jmx.remote.protocol.provider.pkgs</code> exists, then its value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * is the provider package list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <li>Otherwise, there is no provider package list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * <p>The provider package list is a string that is interpreted as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * list of non-empty Java package names separated by vertical bars
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * (<code>|</code>).  If the string is empty, then so is the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * package list.  If the provider package list is not a String, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * it contains an element that is an empty string, a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * JMXProviderException} is thrown.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * <p>If the provider package list exists and is not empty, then for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * each element <code><em>pkg</em></code> of the list, the factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * will attempt to load the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * <code><em>pkg</em>.<em>protocol</em>.ClientProvider</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * <p>If the <code>environment</code> parameter to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * #newJMXConnector(JMXServiceURL, Map) newJMXConnector} contains the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * key <code>jmx.remote.protocol.provider.class.loader</code> then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * associated value is the class loader to use to load the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * If the associated value is not an instance of {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * java.lang.ClassLoader}, an {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * java.lang.IllegalArgumentException} is thrown.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * <p>If the <code>jmx.remote.protocol.provider.class.loader</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * key is not present in the <code>environment</code> parameter, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * calling thread's context class loader is used.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * <p>If the attempt to load this class produces a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * ClassNotFoundException}, the search for a handler continues with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * the next element of the list.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * <p>Otherwise, a problem with the provider found is signalled by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * {@link JMXProviderException} whose {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * JMXProviderException#getCause() <em>cause</em>} indicates the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * exception, as follows:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * <li>if the attempt to load the class produces an exception other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * than <code>ClassNotFoundException</code>, that is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * <em>cause</em>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * <li>if {@link Class#newInstance()} for the class produces an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * exception, that is the <em>cause</em>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * <p>If no provider is found by the above steps, including the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * default case where there is no provider package list, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * implementation will use its own provider for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * <code><em>protocol</em></code>, or it will throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * <code>MalformedURLException</code> if there is none.  An
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * implementation may choose to find providers by other means.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * example, it may support the <a
24368
2b4801b94265 8038795: Tidy warnings cleanup for javax.management
yan
parents: 23010
diff changeset
   137
 * href="{@docRoot}/../technotes/guides/jar/jar.html#Service%20Provider">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * JAR conventions for service providers</a>, where the service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * interface is <code>JMXConnectorProvider</code>.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 *
14917
bf08557604f8 8001048: JSR-160: Allow IIOP transport to be optional
alanb
parents: 5506
diff changeset
   141
 * <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
   142
 * the default RMI transport, specified with string <code>rmi</code>.
bf08557604f8 8001048: JSR-160: Allow IIOP transport to be optional
alanb
parents: 5506
diff changeset
   143
 * An implementation may optionally support the RMI connector protocol
bf08557604f8 8001048: JSR-160: Allow IIOP transport to be optional
alanb
parents: 5506
diff changeset
   144
 * with the RMI/IIOP transport, specified with the string
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * <code>iiop</code>.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * <p>Once a provider is found, the result of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * <code>newJMXConnector</code> method is the result of calling {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * JMXConnectorProvider#newJMXConnector(JMXServiceURL,Map) newJMXConnector}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * on the provider.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * <p>The <code>Map</code> parameter passed to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * <code>JMXConnectorProvider</code> is a new read-only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * <code>Map</code> that contains all the entries that were in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * <code>environment</code> parameter to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * #newJMXConnector(JMXServiceURL,Map)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * JMXConnectorFactory.newJMXConnector}, if there was one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * Additionally, if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * <code>jmx.remote.protocol.provider.class.loader</code> key is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * present in the <code>environment</code> parameter, it is added to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * the new read-only <code>Map</code>.  The associated value is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * calling thread's context class loader.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
public class JMXConnectorFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * <p>Name of the attribute that specifies the default class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * loader. This class loader is used to deserialize return values and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * exceptions from remote <code>MBeanServerConnection</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * calls.  The value associated with this attribute is an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * of {@link ClassLoader}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public static final String DEFAULT_CLASS_LOADER =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        "jmx.remote.default.class.loader";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * <p>Name of the attribute that specifies the provider packages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * that are consulted when looking for the handler for a protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * The value associated with this attribute is a string with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * package names separated by vertical bars (<code>|</code>).</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public static final String PROTOCOL_PROVIDER_PACKAGES =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        "jmx.remote.protocol.provider.pkgs";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * <p>Name of the attribute that specifies the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * loader for loading protocol providers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * The value associated with this attribute is an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * of {@link ClassLoader}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    public static final String PROTOCOL_PROVIDER_CLASS_LOADER =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        "jmx.remote.protocol.provider.class.loader";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    private static final String PROTOCOL_PROVIDER_DEFAULT_PACKAGE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        "com.sun.jmx.remote.protocol";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    private static final ClassLogger logger =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        new ClassLogger("javax.management.remote.misc", "JMXConnectorFactory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /** There are no instances of this class.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    private JMXConnectorFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * <p>Creates a connection to the connector server at the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * address.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * <p>This method is equivalent to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * #connect(JMXServiceURL,Map) connect(serviceURL, null)}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @param serviceURL the address of the connector server to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * connect to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @return a <code>JMXConnector</code> whose {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * JMXConnector#connect connect} method has been called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @exception NullPointerException if <code>serviceURL</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @exception IOException if the connector client or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * connection cannot be made because of a communication problem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @exception SecurityException if the connection cannot be made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * for security reasons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public static JMXConnector connect(JMXServiceURL serviceURL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        return connect(serviceURL, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * <p>Creates a connection to the connector server at the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * address.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * <p>This method is equivalent to:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * JMXConnector conn = JMXConnectorFactory.newJMXConnector(serviceURL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *                                                         environment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * conn.connect(environment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @param serviceURL the address of the connector server to connect to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @param environment a set of attributes to determine how the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * connection is made.  This parameter can be null.  Keys in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * map must be Strings.  The appropriate type of each associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * value depends on the attribute.  The contents of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * <code>environment</code> are not changed by this call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @return a <code>JMXConnector</code> representing the newly-made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * connection.  Each successful call to this method produces a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * different object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @exception NullPointerException if <code>serviceURL</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @exception IOException if the connector client or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * connection cannot be made because of a communication problem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @exception SecurityException if the connection cannot be made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * for security reasons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    public static JMXConnector connect(JMXServiceURL serviceURL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                                       Map<String,?> environment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if (serviceURL == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            throw new NullPointerException("Null JMXServiceURL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        JMXConnector conn = newJMXConnector(serviceURL, environment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        conn.connect(environment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        return conn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   274
    private static <K,V> Map<K,V> newHashMap() {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   275
        return new HashMap<K,V>();
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   276
    }
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
    private static <K> Map<K,Object> newHashMap(Map<K,?> map) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   279
        return new HashMap<K,Object>(map);
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   280
    }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   281
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * <p>Creates a connector client for the connector server at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * given address.  The resultant client is not connected until its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * {@link JMXConnector#connect(Map) connect} method is called.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @param serviceURL the address of the connector server to connect to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @param environment a set of attributes to determine how the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * connection is made.  This parameter can be null.  Keys in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * map must be Strings.  The appropriate type of each associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * value depends on the attribute.  The contents of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * <code>environment</code> are not changed by this call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @return a <code>JMXConnector</code> representing the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * connector client.  Each successful call to this method produces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * a different object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @exception NullPointerException if <code>serviceURL</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @exception IOException if the connector client cannot be made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * because of a communication problem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @exception MalformedURLException if there is no provider for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * protocol in <code>serviceURL</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @exception JMXProviderException if there is a provider for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * protocol in <code>serviceURL</code> but it cannot be used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * some reason.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    public static JMXConnector newJMXConnector(JMXServiceURL serviceURL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                                               Map<String,?> environment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            throws IOException {
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   314
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   315
        final Map<String,Object> envcopy;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (environment == null)
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   317
            envcopy = newHashMap();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            EnvHelp.checkAttributes(environment);
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   320
            envcopy = newHashMap(environment);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        final ClassLoader loader = resolveClassLoader(envcopy);
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   324
        final Class<JMXConnectorProvider> targetInterface =
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   325
                JMXConnectorProvider.class;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        final String protocol = serviceURL.getProtocol();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        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
   328
        final JMXServiceURL providerURL = serviceURL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1510
diff changeset
   330
        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
   331
                                               providerClassName,
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1510
diff changeset
   332
                                               targetInterface,
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1510
diff changeset
   333
                                               loader);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        IOException exception = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        if (provider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            // Loader is null when context class loader is set to null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            // and no loader has been provided in map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            // com.sun.jmx.remote.util.Service class extracted from j2se
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            // provider search algorithm doesn't handle well null classloader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            if (loader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    JMXConnector connection =
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1510
diff changeset
   344
                        getConnectorAsService(loader, providerURL, envcopy);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    if (connection != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                        return connection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                } catch (JMXProviderException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    exception = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            }
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1510
diff changeset
   353
            provider = getProvider(protocol, PROTOCOL_PROVIDER_DEFAULT_PACKAGE,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                            JMXConnectorFactory.class.getClassLoader(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                            providerClassName, targetInterface);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        if (provider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            MalformedURLException e =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                new MalformedURLException("Unsupported protocol: " + protocol);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            if (exception == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                throw EnvHelp.initCause(e, exception);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   368
        final Map<String,Object> fixedenv =
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   369
                Collections.unmodifiableMap(envcopy);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   371
        return provider.newJMXConnector(serviceURL, fixedenv);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1156
diff changeset
   374
    private static String resolvePkgs(Map<String, ?> env)
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1156
diff changeset
   375
            throws JMXProviderException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        Object pkgsObject = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        if (env != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            pkgsObject = env.get(PROTOCOL_PROVIDER_PACKAGES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        if (pkgsObject == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            pkgsObject =
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   384
                AccessController.doPrivileged(new PrivilegedAction<String>() {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   385
                    public String run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                        return System.getProperty(PROTOCOL_PROVIDER_PACKAGES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                });
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
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        if (!(pkgsObject instanceof String)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            final String msg = "Value of " + PROTOCOL_PROVIDER_PACKAGES +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                " parameter is not a String: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                pkgsObject.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            throw new JMXProviderException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        final String pkgs = (String) pkgsObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        if (pkgs.trim().equals(""))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        // pkgs may not contain an empty element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        if (pkgs.startsWith("|") || pkgs.endsWith("|") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            pkgs.indexOf("||") >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            final String msg = "Value of " + PROTOCOL_PROVIDER_PACKAGES +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                " contains an empty element: " + pkgs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            throw new JMXProviderException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        return pkgs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    static <T> T getProvider(JMXServiceURL serviceURL,
18190
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   416
                             final Map<String, Object> environment,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                             String providerClassName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                             Class<T> targetInterface,
18190
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   419
                             final ClassLoader loader)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        final String protocol = serviceURL.getProtocol();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        final String pkgs = resolvePkgs(environment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        T instance = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        if (pkgs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            instance =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                getProvider(protocol, pkgs, loader, providerClassName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                            targetInterface);
18190
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   432
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   433
            if (instance != null) {
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   434
                boolean needsWrap = (loader != instance.getClass().getClassLoader());
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   435
                environment.put(PROTOCOL_PROVIDER_CLASS_LOADER, needsWrap ? wrap(loader) : loader);
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   436
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        return instance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    static <T> Iterator<T> getProviderIterator(final Class<T> providerClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                                               final ClassLoader loader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
       ServiceLoader<T> serviceLoader =
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   445
                ServiceLoader.load(providerClass, loader);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
       return serviceLoader.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
18190
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   449
    private static ClassLoader wrap(final ClassLoader parent) {
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   450
        return parent != null ? AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   451
            @Override
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   452
            public ClassLoader run() {
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   453
                return new ClassLoader(parent) {
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   454
                    @Override
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   455
                    protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   456
                        ReflectUtil.checkPackageAccess(name);
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   457
                        return super.loadClass(name, resolve);
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   458
                    }
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
        }) : null;
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   462
    }
e45d26861c25 8008585: Better JMX data handling
jbachorik
parents: 18187
diff changeset
   463
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    private static JMXConnector getConnectorAsService(ClassLoader loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                                                      JMXServiceURL url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                                                      Map<String, ?> map)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        Iterator<JMXConnectorProvider> providers =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                getProviderIterator(JMXConnectorProvider.class, loader);
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
   471
        JMXConnector connection;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        IOException exception = null;
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1510
diff changeset
   473
        while (providers.hasNext()) {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1510
diff changeset
   474
            JMXConnectorProvider provider = providers.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            try {
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1510
diff changeset
   476
                connection = provider.newJMXConnector(url, map);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                return connection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            } catch (JMXProviderException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                if (logger.traceOn())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                    logger.trace("getConnectorAsService",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                                 "URL[" + url +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                                 "] Service provider exception: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                if (!(e instanceof MalformedURLException)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                    if (exception == null) {
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
   487
                        if (e instanceof IOException) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                            exception = (IOException) e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                            exception = EnvHelp.initCause(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                                new IOException(e.getMessage()), e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        if (exception == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            throw exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    static <T> T getProvider(String protocol,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                              String pkgs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                              ClassLoader loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                              String providerClassName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                              Class<T> targetInterface)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        StringTokenizer tokenizer = new StringTokenizer(pkgs, "|");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        while (tokenizer.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            String pkg = tokenizer.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            String className = (pkg + "." + protocol2package(protocol) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                                "." + providerClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            Class<?> providerClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                providerClass = Class.forName(className, true, loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                //Add trace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            if (!targetInterface.isAssignableFrom(providerClass)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                    "Provider class does not implement " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                    targetInterface.getName() + ": " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                    providerClass.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                throw new JMXProviderException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            // We have just proved that this cast is correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            Class<? extends T> providerClassT = Util.cast(providerClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                return providerClassT.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                    "Exception when instantiating provider [" + className +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                    "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                throw new JMXProviderException(msg, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1156
diff changeset
   548
    static ClassLoader resolveClassLoader(Map<String, ?> environment) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        ClassLoader loader = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        if (environment != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                loader = (ClassLoader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                    environment.get(PROTOCOL_PROVIDER_CLASS_LOADER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            } catch (ClassCastException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                    "The ClassLoader supplied in the environment map using " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                    "the " + PROTOCOL_PROVIDER_CLASS_LOADER +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                    " attribute is not an instance of java.lang.ClassLoader";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
18187
a798516cf58d 8008603: Improve provision of JMX providers
jbachorik
parents: 14917
diff changeset
   564
        if (loader == null) {
a798516cf58d 8008603: Improve provision of JMX providers
jbachorik
parents: 14917
diff changeset
   565
            loader = Thread.currentThread().getContextClassLoader();
a798516cf58d 8008603: Improve provision of JMX providers
jbachorik
parents: 14917
diff changeset
   566
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        return loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    private static String protocol2package(String protocol) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        return protocol.replace('+', '.').replace('-', '_');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
}