jdk/src/share/classes/javax/management/remote/JMXConnectorServerFactory.java
author yan
Mon, 12 May 2014 14:33:13 +0400
changeset 24368 2b4801b94265
parent 23010 6dadb192ad81
permissions -rw-r--r--
8038795: Tidy warnings cleanup for javax.management Reviewed-by: dfuchs Contributed-by: Alexander Stepanov <alexander.v.stepanov@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
24368
2b4801b94265 8038795: Tidy warnings cleanup for javax.management
yan
parents: 23010
diff changeset
     2
 * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1004
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: 1004
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: 1004
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1004
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1004
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import com.sun.jmx.remote.util.ClassLogger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import com.sun.jmx.remote.util.EnvHelp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p>Factory to create JMX API connector servers.  There
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * are no instances of this class.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p>Each connector server is created by an instance of {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * JMXConnectorServerProvider}.  This instance is found as follows.  Suppose
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * the given {@link JMXServiceURL} looks like
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <code>"service:jmx:<em>protocol</em>:<em>remainder</em>"</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Then the factory will attempt to find the appropriate {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * JMXConnectorServerProvider} for <code><em>protocol</em></code>.  Each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * occurrence of the character <code>+</code> or <code>-</code> in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <code><em>protocol</em></code> is replaced by <code>.</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <code>_</code>, respectively.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <p>A <em>provider package list</em> is searched for as follows:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <li>If the <code>environment</code> parameter to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * #newJMXConnectorServer(JMXServiceURL,Map,MBeanServer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * newJMXConnectorServer} contains the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <code>jmx.remote.protocol.provider.pkgs</code> then the associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * value is the provider package list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <li>Otherwise, if the system property
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <code>jmx.remote.protocol.provider.pkgs</code> exists, then its value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * is the provider package list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <li>Otherwise, there is no provider package list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <p>The provider package list is a string that is interpreted as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * list of non-empty Java package names separated by vertical bars
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * (<code>|</code>).  If the string is empty, then so is the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * package list.  If the provider package list is not a String, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * it contains an element that is an empty string, a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * JMXProviderException} is thrown.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * <p>If the provider package list exists and is not empty, then for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * each element <code><em>pkg</em></code> of the list, the factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * will attempt to load the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * <code><em>pkg</em>.<em>protocol</em>.ServerProvider</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * <p>If the <code>environment</code> parameter to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * #newJMXConnectorServer(JMXServiceURL, Map, MBeanServer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * newJMXConnectorServer} contains the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * <code>jmx.remote.protocol.provider.class.loader</code> then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * associated value is the class loader to use to load the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * If the associated value is not an instance of {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * java.lang.ClassLoader}, an {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * java.lang.IllegalArgumentException} is thrown.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * <p>If the <code>jmx.remote.protocol.provider.class.loader</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * key is not present in the <code>environment</code> parameter, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * calling thread's context class loader is used.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * <p>If the attempt to load this class produces a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * ClassNotFoundException}, the search for a handler continues with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * the next element of the list.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * <p>Otherwise, a problem with the provider found is signalled by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * {@link JMXProviderException} whose {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * JMXProviderException#getCause() <em>cause</em>} indicates the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * underlying exception, as follows:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * <li>if the attempt to load the class produces an exception other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * than <code>ClassNotFoundException</code>, that is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * <em>cause</em>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * <li>if {@link Class#newInstance()} for the class produces an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * exception, that is the <em>cause</em>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * <p>If no provider is found by the above steps, including the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * default case where there is no provider package list, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * implementation will use its own provider for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * <code><em>protocol</em></code>, or it will throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * <code>MalformedURLException</code> if there is none.  An
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * implementation may choose to find providers by other means.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * example, it may support the <a
24368
2b4801b94265 8038795: Tidy warnings cleanup for javax.management
yan
parents: 23010
diff changeset
   128
 * href="{@docRoot}/../technotes/guides/jar/jar.html#Service%20Provider">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * JAR conventions for service providers</a>, where the service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * interface is <code>JMXConnectorServerProvider</code>.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 *
14917
bf08557604f8 8001048: JSR-160: Allow IIOP transport to be optional
alanb
parents: 5506
diff changeset
   132
 * <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
   133
 * 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
   134
 * An implementation may optionally support the RMI connector protocol
bf08557604f8 8001048: JSR-160: Allow IIOP transport to be optional
alanb
parents: 5506
diff changeset
   135
 * with the RMI/IIOP transport, specified with the string
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * <code>iiop</code>.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * <p>Once a provider is found, the result of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * <code>newJMXConnectorServer</code> method is the result of calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * JMXConnectorServerProvider#newJMXConnectorServer(JMXServiceURL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * Map, MBeanServer) newJMXConnectorServer} on the provider.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * <p>The <code>Map</code> parameter passed to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * <code>JMXConnectorServerProvider</code> is a new read-only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * <code>Map</code> that contains all the entries that were in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * <code>environment</code> parameter to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * #newJMXConnectorServer(JMXServiceURL,Map,MBeanServer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * JMXConnectorServerFactory.newJMXConnectorServer}, if there was one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * Additionally, if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * <code>jmx.remote.protocol.provider.class.loader</code> key is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * present in the <code>environment</code> parameter, it is added to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * the new read-only <code>Map</code>. The associated value is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * calling thread's context class loader.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
public class JMXConnectorServerFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * <p>Name of the attribute that specifies the default class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * loader.  This class loader is used to deserialize objects in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * requests received from the client, possibly after consulting an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * MBean-specific class loader.  The value associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * attribute is an instance of {@link ClassLoader}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public static final String DEFAULT_CLASS_LOADER =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        JMXConnectorFactory.DEFAULT_CLASS_LOADER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * <p>Name of the attribute that specifies the default class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * loader MBean name.  This class loader is used to deserialize objects in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * requests received from the client, possibly after consulting an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * MBean-specific class loader.  The value associated with this
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 715
diff changeset
   175
     * attribute is an instance of {@link javax.management.ObjectName
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 715
diff changeset
   176
     * ObjectName}.</p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public static final String DEFAULT_CLASS_LOADER_NAME =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        "jmx.remote.default.class.loader.name";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * <p>Name of the attribute that specifies the provider packages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * that are consulted when looking for the handler for a protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * The value associated with this attribute is a string with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * package names separated by vertical bars (<code>|</code>).</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public static final String PROTOCOL_PROVIDER_PACKAGES =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        "jmx.remote.protocol.provider.pkgs";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * <p>Name of the attribute that specifies the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * loader for loading protocol providers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * The value associated with this attribute is an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * of {@link ClassLoader}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public static final String PROTOCOL_PROVIDER_CLASS_LOADER =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        "jmx.remote.protocol.provider.class.loader";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    private static final String PROTOCOL_PROVIDER_DEFAULT_PACKAGE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        "com.sun.jmx.remote.protocol";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    private static final ClassLogger logger =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        new ClassLogger("javax.management.remote.misc","JMXConnectorServerFactory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /** There are no instances of this class.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    private JMXConnectorServerFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    private static JMXConnectorServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        getConnectorServerAsService(ClassLoader loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                                    JMXServiceURL url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                                    Map<String, ?> map,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                                    MBeanServer mbs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        Iterator<JMXConnectorServerProvider> providers =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                JMXConnectorFactory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                getProviderIterator(JMXConnectorServerProvider.class, loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        IOException exception = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        while (providers.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            try {
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
   222
                return providers.next().newJMXConnectorServer(url, map, mbs);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            } catch (JMXProviderException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                if (logger.traceOn())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                    logger.trace("getConnectorAsService",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                                 "URL[" + url +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                                 "] Service provider exception: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                if (!(e instanceof MalformedURLException)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    if (exception == null) {
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
   232
                        if (e instanceof IOException) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                            exception = (IOException) e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                            exception = EnvHelp.initCause(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                                new IOException(e.getMessage()), e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        if (exception == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            throw exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * <p>Creates a connector server at the given address.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * resultant server is not started until its {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * JMXConnectorServer#start() start} method is called.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @param serviceURL the address of the new connector server.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * actual address of the new connector server, as returned by its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * {@link JMXConnectorServer#getAddress() getAddress} method, will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * not necessarily be exactly the same.  For example, it might
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * include a port number if the original address did not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @param environment a set of attributes to control the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * connector server's behavior.  This parameter can be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * Keys in this map must be Strings.  The appropriate type of each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * associated value depends on the attribute.  The contents of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * <code>environment</code> are not changed by this call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @param mbeanServer the MBean server that this connector server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * is attached to.  Null if this connector server will be attached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * to an MBean server by being registered in it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @return a <code>JMXConnectorServer</code> representing the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * connector server.  Each successful call to this method produces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * a different object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @exception NullPointerException if <code>serviceURL</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @exception IOException if the connector server cannot be made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * because of a communication problem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @exception MalformedURLException if there is no provider for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * protocol in <code>serviceURL</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @exception JMXProviderException if there is a provider for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * protocol in <code>serviceURL</code> but it cannot be used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * some reason.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    public static JMXConnectorServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        newJMXConnectorServer(JMXServiceURL serviceURL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                              Map<String,?> environment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                              MBeanServer mbeanServer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        Map<String, Object> envcopy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        if (environment == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            envcopy = new HashMap<String, Object>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            EnvHelp.checkAttributes(environment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            envcopy = new HashMap<String, Object>(environment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        final Class<JMXConnectorServerProvider> targetInterface =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                JMXConnectorServerProvider.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        final ClassLoader loader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            JMXConnectorFactory.resolveClassLoader(envcopy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        final String protocol = serviceURL.getProtocol();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        final String providerClassName = "ServerProvider";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        JMXConnectorServerProvider provider =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            JMXConnectorFactory.getProvider(serviceURL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                                            envcopy,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                                            providerClassName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                                            targetInterface,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                                            loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        IOException exception = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        if (provider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            // Loader is null when context class loader is set to null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            // and no loader has been provided in map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            // com.sun.jmx.remote.util.Service class extracted from j2se
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            // provider search algorithm doesn't handle well null classloader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            if (loader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                    JMXConnectorServer connection =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                        getConnectorServerAsService(loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                                                    serviceURL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                                                    envcopy,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                                                    mbeanServer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                    if (connection != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                        return connection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                } catch (JMXProviderException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                    throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                    exception = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            provider =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                JMXConnectorFactory.getProvider(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    protocol,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    PROTOCOL_PROVIDER_DEFAULT_PACKAGE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    JMXConnectorFactory.class.getClassLoader(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    providerClassName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    targetInterface);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        if (provider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            MalformedURLException e =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                new MalformedURLException("Unsupported protocol: " + protocol);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            if (exception == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                throw EnvHelp.initCause(e, exception);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        envcopy = Collections.unmodifiableMap(envcopy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        return provider.newJMXConnectorServer(serviceURL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                                              envcopy,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                                              mbeanServer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
}