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