jdk/src/share/classes/javax/management/remote/JMXConnectorFactory.java
author emcmanus
Mon, 27 Oct 2008 14:02:40 +0100
changeset 1510 e747d3193ef2
parent 1156 bbc2d15aaf7a
child 1570 4165709c91e3
permissions -rw-r--r--
6763639: Remove "rawtypes" warnings from JMX code Reviewed-by: dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 526
diff changeset
     2
 * Copyright 2002-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * <p>Every implementation must support the RMI connector protocols,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * specified with the string <code>rmi</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * <code>iiop</code>.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * <p>Once a provider is found, the result of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * <code>newJMXConnector</code> method is the result of calling {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * JMXConnectorProvider#newJMXConnector(JMXServiceURL,Map) newJMXConnector}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * on the provider.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * <p>The <code>Map</code> parameter passed to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * <code>JMXConnectorProvider</code> is a new read-only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * <code>Map</code> that contains all the entries that were in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * <code>environment</code> parameter to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * #newJMXConnector(JMXServiceURL,Map)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * JMXConnectorFactory.newJMXConnector}, if there was one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * Additionally, if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * <code>jmx.remote.protocol.provider.class.loader</code> key is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * present in the <code>environment</code> parameter, it is added to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * the new read-only <code>Map</code>.  The associated value is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * calling thread's context class loader.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
public class JMXConnectorFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * <p>Name of the attribute that specifies the default class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * loader. This class loader is used to deserialize return values and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * exceptions from remote <code>MBeanServerConnection</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * calls.  The value associated with this attribute is an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * of {@link ClassLoader}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    public static final String DEFAULT_CLASS_LOADER =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        "jmx.remote.default.class.loader";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * <p>Name of the attribute that specifies the provider packages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * that are consulted when looking for the handler for a protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * The value associated with this attribute is a string with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * package names separated by vertical bars (<code>|</code>).</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public static final String PROTOCOL_PROVIDER_PACKAGES =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        "jmx.remote.protocol.provider.pkgs";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * <p>Name of the attribute that specifies the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * loader for loading protocol providers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * The value associated with this attribute is an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * of {@link ClassLoader}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public static final String PROTOCOL_PROVIDER_CLASS_LOADER =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        "jmx.remote.protocol.provider.class.loader";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    private static final String PROTOCOL_PROVIDER_DEFAULT_PACKAGE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        "com.sun.jmx.remote.protocol";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    private static final ClassLogger logger =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        new ClassLogger("javax.management.remote.misc", "JMXConnectorFactory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /** There are no instances of this class.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    private JMXConnectorFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * <p>Creates a connection to the connector server at the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * address.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * <p>This method is equivalent to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * #connect(JMXServiceURL,Map) connect(serviceURL, null)}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @param serviceURL the address of the connector server to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * connect to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @return a <code>JMXConnector</code> whose {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * JMXConnector#connect connect} method has been called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @exception NullPointerException if <code>serviceURL</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @exception IOException if the connector client or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * connection cannot be made because of a communication problem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @exception SecurityException if the connection cannot be made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * for security reasons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    public static JMXConnector connect(JMXServiceURL serviceURL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        return connect(serviceURL, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * <p>Creates a connection to the connector server at the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * address.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * <p>This method is equivalent to:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * JMXConnector conn = JMXConnectorFactory.newJMXConnector(serviceURL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *                                                         environment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * conn.connect(environment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @param serviceURL the address of the connector server to connect to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @param environment a set of attributes to determine how the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * connection is made.  This parameter can be null.  Keys in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * map must be Strings.  The appropriate type of each associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * value depends on the attribute.  The contents of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * <code>environment</code> are not changed by this call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @return a <code>JMXConnector</code> representing the newly-made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * connection.  Each successful call to this method produces a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * different object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @exception NullPointerException if <code>serviceURL</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @exception IOException if the connector client or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * connection cannot be made because of a communication problem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @exception SecurityException if the connection cannot be made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * for security reasons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public static JMXConnector connect(JMXServiceURL serviceURL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                                       Map<String,?> environment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        if (serviceURL == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            throw new NullPointerException("Null JMXServiceURL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        JMXConnector conn = newJMXConnector(serviceURL, environment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        conn.connect(environment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        return conn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   271
    private static <K,V> Map<K,V> newHashMap() {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   272
        return new HashMap<K,V>();
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   273
    }
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   274
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   275
    private static <K> Map<K,Object> newHashMap(Map<K,?> map) {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   276
        return new HashMap<K,Object>(map);
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
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * <p>Creates a connector client for the connector server at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * given address.  The resultant client is not connected until its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * {@link JMXConnector#connect(Map) connect} method is called.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @param serviceURL the address of the connector server to connect to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @param environment a set of attributes to determine how the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * connection is made.  This parameter can be null.  Keys in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * map must be Strings.  The appropriate type of each associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * value depends on the attribute.  The contents of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * <code>environment</code> are not changed by this call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @return a <code>JMXConnector</code> representing the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * connector client.  Each successful call to this method produces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * a different object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @exception NullPointerException if <code>serviceURL</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @exception IOException if the connector client cannot be made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * because of a communication problem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @exception MalformedURLException if there is no provider for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * protocol in <code>serviceURL</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @exception JMXProviderException if there is a provider for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * protocol in <code>serviceURL</code> but it cannot be used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * some reason.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    public static JMXConnector newJMXConnector(JMXServiceURL serviceURL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                                               Map<String,?> environment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            throws IOException {
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   311
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   312
        final Map<String,Object> envcopy;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        if (environment == null)
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   314
            envcopy = newHashMap();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            EnvHelp.checkAttributes(environment);
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   317
            envcopy = newHashMap(environment);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        final ClassLoader loader = resolveClassLoader(envcopy);
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   321
        final Class<JMXConnectorProvider> targetInterface =
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   322
                JMXConnectorProvider.class;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        final String protocol = serviceURL.getProtocol();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        final String providerClassName = "ClientProvider";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        JMXConnectorProvider provider =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            getProvider(serviceURL, envcopy, providerClassName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                        targetInterface, loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        IOException exception = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        if (provider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            // Loader is null when context class loader is set to null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            // and no loader has been provided in map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            // com.sun.jmx.remote.util.Service class extracted from j2se
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            // provider search algorithm doesn't handle well null classloader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            if (loader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    JMXConnector connection =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                        getConnectorAsService(loader, serviceURL, envcopy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    if (connection != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                        return connection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                } catch (JMXProviderException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                    exception = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            provider =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                getProvider(protocol, PROTOCOL_PROVIDER_DEFAULT_PACKAGE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                            JMXConnectorFactory.class.getClassLoader(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                            providerClassName, targetInterface);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        if (provider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            MalformedURLException e =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                new MalformedURLException("Unsupported protocol: " + protocol);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            if (exception == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                throw EnvHelp.initCause(e, exception);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   364
        final Map<String,Object> fixedenv =
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   365
                Collections.unmodifiableMap(envcopy);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   367
        return provider.newJMXConnector(serviceURL, fixedenv);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1156
diff changeset
   370
    private static String resolvePkgs(Map<String, ?> env)
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1156
diff changeset
   371
            throws JMXProviderException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        Object pkgsObject = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        if (env != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            pkgsObject = env.get(PROTOCOL_PROVIDER_PACKAGES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        if (pkgsObject == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            pkgsObject =
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   380
                AccessController.doPrivileged(new PrivilegedAction<String>() {
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   381
                    public String run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                        return System.getProperty(PROTOCOL_PROVIDER_PACKAGES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        if (pkgsObject == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        if (!(pkgsObject instanceof String)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            final String msg = "Value of " + PROTOCOL_PROVIDER_PACKAGES +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                " parameter is not a String: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                pkgsObject.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            throw new JMXProviderException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        final String pkgs = (String) pkgsObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if (pkgs.trim().equals(""))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        // pkgs may not contain an empty element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        if (pkgs.startsWith("|") || pkgs.endsWith("|") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            pkgs.indexOf("||") >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            final String msg = "Value of " + PROTOCOL_PROVIDER_PACKAGES +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                " contains an empty element: " + pkgs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            throw new JMXProviderException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        return pkgs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    static <T> T getProvider(JMXServiceURL serviceURL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                             Map<String, Object> environment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                             String providerClassName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                             Class<T> targetInterface,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                             ClassLoader loader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        final String protocol = serviceURL.getProtocol();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        final String pkgs = resolvePkgs(environment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        T instance = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        if (pkgs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            environment.put(PROTOCOL_PROVIDER_CLASS_LOADER, loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            instance =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                getProvider(protocol, pkgs, loader, providerClassName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                            targetInterface);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        return instance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    static <T> Iterator<T> getProviderIterator(final Class<T> providerClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                                               final ClassLoader loader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
       ServiceLoader<T> serviceLoader =
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   438
                ServiceLoader.load(providerClass, loader);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
       return serviceLoader.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    private static JMXConnector getConnectorAsService(ClassLoader loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                                                      JMXServiceURL url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                                                      Map<String, ?> map)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        Iterator<JMXConnectorProvider> providers =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                getProviderIterator(JMXConnectorProvider.class, loader);
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
   449
        JMXConnector connection;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        IOException exception = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        while(providers.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                connection = providers.next().newJMXConnector(url, map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                return connection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            } catch (JMXProviderException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                if (logger.traceOn())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                    logger.trace("getConnectorAsService",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                                 "URL[" + url +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                                 "] Service provider exception: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                if (!(e instanceof MalformedURLException)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                    if (exception == null) {
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
   464
                        if (e instanceof IOException) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                            exception = (IOException) e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                            exception = EnvHelp.initCause(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                                new IOException(e.getMessage()), e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        if (exception == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            throw exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    static <T> T getProvider(String protocol,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                              String pkgs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                              ClassLoader loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                              String providerClassName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                              Class<T> targetInterface)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        StringTokenizer tokenizer = new StringTokenizer(pkgs, "|");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        while (tokenizer.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            String pkg = tokenizer.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            String className = (pkg + "." + protocol2package(protocol) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                                "." + providerClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            Class<?> providerClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                providerClass = Class.forName(className, true, loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                //Add trace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            if (!targetInterface.isAssignableFrom(providerClass)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                    "Provider class does not implement " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                    targetInterface.getName() + ": " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                    providerClass.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                throw new JMXProviderException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            // We have just proved that this cast is correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            Class<? extends T> providerClassT = Util.cast(providerClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                return providerClassT.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                    "Exception when instantiating provider [" + className +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                    "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                throw new JMXProviderException(msg, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1156
diff changeset
   525
    static ClassLoader resolveClassLoader(Map<String, ?> environment) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        ClassLoader loader = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        if (environment != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                loader = (ClassLoader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                    environment.get(PROTOCOL_PROVIDER_CLASS_LOADER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            } catch (ClassCastException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                    "The ClassLoader supplied in the environment map using " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                    "the " + PROTOCOL_PROVIDER_CLASS_LOADER +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                    " attribute is not an instance of java.lang.ClassLoader";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        if (loader == null)
1156
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   542
            loader = AccessController.doPrivileged(
bbc2d15aaf7a 5072476: RFE: support cascaded (federated) MBean Servers
dfuchs
parents: 715
diff changeset
   543
                    new PrivilegedAction<ClassLoader>() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                        public ClassLoader run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                            return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                                Thread.currentThread().getContextClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        return loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    private static String protocol2package(String protocol) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        return protocol.replace('+', '.').replace('-', '_');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
}