jdk/src/share/classes/javax/management/remote/rmi/RMIConnectorServer.java
author rriggs
Tue, 12 Nov 2013 14:03:28 -0500
changeset 21656 d4c777ccb1db
parent 14917 bf08557604f8
child 23010 6dadb192ad81
permissions -rw-r--r--
8028014: Doclint warning/error cleanup in javax.management Summary: Improve generated html by fixing doclint warnings Reviewed-by: sla, jbachorik
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 14210
diff changeset
     2
 * Copyright (c) 2002, 2012, 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: 4167
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: 4167
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: 4167
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4167
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4167
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.rmi;
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.security.MBeanServerFileAccessController;
4167
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 4156
diff changeset
    30
import com.sun.jmx.remote.internal.IIOPHelper;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import com.sun.jmx.remote.util.ClassLogger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import com.sun.jmx.remote.util.EnvHelp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.rmi.server.RMIClientSocketFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.rmi.server.RMIServerSocketFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.management.InstanceNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import javax.management.remote.JMXConnectionNotification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import javax.management.remote.JMXConnector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import javax.management.remote.JMXConnectorServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import javax.management.remote.JMXServiceURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import javax.management.remote.MBeanServerForwarder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import javax.naming.InitialContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import javax.naming.NamingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <p>A JMX API connector server that creates RMI-based connections
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * from remote clients.  Usually, such connector servers are made
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * using {@link javax.management.remote.JMXConnectorServerFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * JMXConnectorServerFactory}.  However, specialized applications can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * use this class directly, for example with an {@link RMIServerImpl}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * object.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
public class RMIConnectorServer extends JMXConnectorServer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * <p>Name of the attribute that specifies whether the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * RMIServer} stub that represents an RMI connector server should
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * override an existing stub at the same address.  The value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * associated with this attribute, if any, should be a string that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * is equal, ignoring case, to <code>"true"</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * <code>"false"</code>.  The default value is false.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public static final String JNDI_REBIND_ATTRIBUTE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        "jmx.remote.jndi.rebind";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * <p>Name of the attribute that specifies the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * RMIClientSocketFactory} for the RMI objects created in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * conjunction with this connector. The value associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * attribute must be of type <code>RMIClientSocketFactory</code> and can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * only be specified in the <code>Map</code> argument supplied when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * creating a connector server.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public static final String RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        "jmx.remote.rmi.client.socket.factory";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * <p>Name of the attribute that specifies the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * RMIServerSocketFactory} for the RMI objects created in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * conjunction with this connector. The value associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * attribute must be of type <code>RMIServerSocketFactory</code> and can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * only be specified in the <code>Map</code> argument supplied when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * creating a connector server.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public static final String RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        "jmx.remote.rmi.server.socket.factory";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * <p>Makes an <code>RMIConnectorServer</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * This is equivalent to calling {@link #RMIConnectorServer(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * JMXServiceURL,Map,RMIServerImpl,MBeanServer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * RMIConnectorServer(directoryURL,environment,null,null)}</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @param url the URL defining how to create the connector server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Cannot be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @param environment attributes governing the creation and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * storing of the RMI object.  Can be null, which is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * an empty Map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @exception IllegalArgumentException if <code>url</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @exception MalformedURLException if <code>url</code> does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * conform to the syntax for an RMI connector, or if its protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * is not recognized by this implementation. Only "rmi" and "iiop"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * are valid when this constructor is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @exception IOException if the connector server cannot be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * for some reason or if it is inevitable that its {@link #start()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * start} method will fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public RMIConnectorServer(JMXServiceURL url, Map<String,?> environment)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        this(url, environment, (MBeanServer) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * <p>Makes an <code>RMIConnectorServer</code> for the given MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * This is equivalent to calling {@link #RMIConnectorServer(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * JMXServiceURL,Map,RMIServerImpl,MBeanServer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * RMIConnectorServer(directoryURL,environment,null,mbeanServer)}</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @param url the URL defining how to create the connector server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Cannot be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @param environment attributes governing the creation and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * storing of the RMI object.  Can be null, which is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * an empty Map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @param mbeanServer the MBean server to which the new connector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * server is attached, or null if it will be attached by being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * registered as an MBean in the MBean server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @exception IllegalArgumentException if <code>url</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @exception MalformedURLException if <code>url</code> does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * conform to the syntax for an RMI connector, or if its protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * is not recognized by this implementation. Only "rmi" and "iiop"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * are valid when this constructor is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @exception IOException if the connector server cannot be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * for some reason or if it is inevitable that its {@link #start()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * start} method will fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public RMIConnectorServer(JMXServiceURL url, Map<String,?> environment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                              MBeanServer mbeanServer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        this(url, environment, (RMIServerImpl) null, mbeanServer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * <p>Makes an <code>RMIConnectorServer</code> for the given MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * server.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @param url the URL defining how to create the connector server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Cannot be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @param environment attributes governing the creation and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * storing of the RMI object.  Can be null, which is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * an empty Map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @param rmiServerImpl An implementation of the RMIServer interface,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *  consistent with the protocol type specified in <var>url</var>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *  If this parameter is non null, the protocol type specified by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *  <var>url</var> is not constrained, and is assumed to be valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *  Otherwise, only "rmi" and "iiop" will be recognized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @param mbeanServer the MBean server to which the new connector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * server is attached, or null if it will be attached by being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * registered as an MBean in the MBean server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @exception IllegalArgumentException if <code>url</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @exception MalformedURLException if <code>url</code> does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * conform to the syntax for an RMI connector, or if its protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * is not recognized by this implementation. Only "rmi" and "iiop"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * are recognized when <var>rmiServerImpl</var> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @exception IOException if the connector server cannot be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * for some reason or if it is inevitable that its {@link #start()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * start} method will fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @see #start
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    public RMIConnectorServer(JMXServiceURL url, Map<String,?> environment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                              RMIServerImpl rmiServerImpl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                              MBeanServer mbeanServer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        super(mbeanServer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        if (url == null) throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            IllegalArgumentException("Null JMXServiceURL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (rmiServerImpl == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            final String prt = url.getProtocol();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            if (prt == null || !(prt.equals("rmi") || prt.equals("iiop"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                final String msg = "Invalid protocol type: " + prt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                throw new MalformedURLException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            final String urlPath = url.getURLPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            if (!urlPath.equals("")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                && !urlPath.equals("/")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                && !urlPath.startsWith("/jndi/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                final String msg = "URL path must be empty or start with " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    "/jndi/";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                throw new MalformedURLException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        if (environment == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            this.attributes = Collections.emptyMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            EnvHelp.checkAttributes(environment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            this.attributes = Collections.unmodifiableMap(environment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        this.address = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        this.rmiServerImpl = rmiServerImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * <p>Returns a client stub for this connector server.  A client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * stub is a serializable object whose {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * JMXConnector#connect(Map) connect} method can be used to make
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * one new connection to this connector server.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @param env client connection parameters of the same sort that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * could be provided to {@link JMXConnector#connect(Map)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * JMXConnector.connect(Map)}.  Can be null, which is equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * to an empty map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @return a client stub that can be used to make a new connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * to this connector server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @exception UnsupportedOperationException if this connector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * server does not support the generation of client stubs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @exception IllegalStateException if the JMXConnectorServer is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * not started (see {@link #isActive()}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @exception IOException if a communications problem means that a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * stub cannot be created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    public JMXConnector toJMXConnector(Map<String,?> env) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        // The serialized for of rmiServerImpl is automatically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        // a RMI server stub.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        if (!isActive()) throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            IllegalStateException("Connector is not active");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        // Merge maps
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        Map<String, Object> usemap = new HashMap<String, Object>(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                (this.attributes==null)?Collections.<String, Object>emptyMap():
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    this.attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        if (env != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            EnvHelp.checkAttributes(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            usemap.putAll(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        usemap = EnvHelp.filterAttributes(usemap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        final RMIServer stub=(RMIServer)rmiServerImpl.toStub();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        return new RMIConnector(stub, usemap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * <p>Activates the connector server, that is starts listening for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * client connections.  Calling this method when the connector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * server is already active has no effect.  Calling this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * when the connector server has been stopped will generate an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * <code>IOException</code>.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * <p>The behavior of this method when called for the first time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * depends on the parameters that were supplied at construction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * as described below.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * <p>First, an object of a subclass of {@link RMIServerImpl} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * required, to export the connector server through RMI:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * <li>If an <code>RMIServerImpl</code> was supplied to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * constructor, it is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * <li>Otherwise, if the protocol part of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * <code>JMXServiceURL</code> supplied to the constructor was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * <code>iiop</code>, an object of type {@link RMIIIOPServerImpl}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * is created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * <li>Otherwise, if the <code>JMXServiceURL</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * was null, or its protocol part was <code>rmi</code>, an object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * of type {@link RMIJRMPServerImpl} is created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * <li>Otherwise, the implementation can create an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * implementation-specific {@link RMIServerImpl} or it can throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * {@link MalformedURLException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * <p>If the given address includes a JNDI directory URL as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * specified in the package documentation for {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * javax.management.remote.rmi}, then this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * <code>RMIConnectorServer</code> will bootstrap by binding the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * <code>RMIServerImpl</code> to the given address.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * <p>If the URL path part of the <code>JMXServiceURL</code> was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * empty or a single slash (<code>/</code>), then the RMI object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * will not be bound to a directory.  Instead, a reference to it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * will be encoded in the URL path of the RMIConnectorServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * address (returned by {@link #getAddress()}).  The encodings for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * <code>rmi</code> and <code>iiop</code> are described in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * package documentation for {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * javax.management.remote.rmi}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * <p>The behavior when the URL path is neither empty nor a JNDI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * directory URL, or when the protocol is neither <code>rmi</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * nor <code>iiop</code>, is implementation defined, and may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * include throwing {@link MalformedURLException} when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * connector server is created or when it is started.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @exception IllegalStateException if the connector server has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * not been attached to an MBean server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @exception IOException if the connector server cannot be
14917
bf08557604f8 8001048: JSR-160: Allow IIOP transport to be optional
alanb
parents: 14342
diff changeset
   340
     * started, or in the case of the {@code iiop} protocol, that
bf08557604f8 8001048: JSR-160: Allow IIOP transport to be optional
alanb
parents: 14342
diff changeset
   341
     * RMI/IIOP is not supported.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    public synchronized void start() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        final boolean tracing = logger.traceOn();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        if (state == STARTED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            if (tracing) logger.trace("start", "already started");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        } else if (state == STOPPED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            if (tracing) logger.trace("start", "already stopped");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            throw new IOException("The server has been stopped.");
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 (getMBeanServer() == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            throw new IllegalStateException("This connector server is not " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                                            "attached to an MBean server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        // Check the internal access file property to see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        // if an MBeanServerForwarder is to be provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        if (attributes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            // Check if access file property is specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            String accessFile =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                (String) attributes.get("jmx.remote.x.access.file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            if (accessFile != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                // Access file property specified, create an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                // of the MBeanServerFileAccessController class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                //
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
   370
                MBeanServerForwarder mbsf;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    mbsf = new MBeanServerFileAccessController(accessFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                    throw EnvHelp.initCause(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                        new IllegalArgumentException(e.getMessage()), e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                // Set the MBeanServerForwarder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                setMBeanServerForwarder(mbsf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            if (tracing) logger.trace("start", "setting default class loader");
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 715
diff changeset
   385
            defaultClassLoader = EnvHelp.resolveServerClassLoader(
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1708
diff changeset
   386
                    attributes, getMBeanServer());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        } catch (InstanceNotFoundException infc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            IllegalArgumentException x = new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                IllegalArgumentException("ClassLoader not found: "+infc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            throw EnvHelp.initCause(x,infc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        if (tracing) logger.trace("start", "setting RMIServer object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        final RMIServerImpl rmiServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        if (rmiServerImpl != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            rmiServer = rmiServerImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            rmiServer = newServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1708
diff changeset
   401
        rmiServer.setMBeanServer(getMBeanServer());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        rmiServer.setDefaultClassLoader(defaultClassLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        rmiServer.setRMIConnectorServer(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        rmiServer.export();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            if (tracing) logger.trace("start", "getting RMIServer object to export");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            final RMIServer objref = objectToBind(rmiServer, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            if (address != null && address.getURLPath().startsWith("/jndi/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                final String jndiUrl = address.getURLPath().substring(6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                if (tracing)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                    logger.trace("start", "Using external directory: " + jndiUrl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
14210
042f1a001234 7158796: Tighten properties checking in EnvHelp
dsamersoff
parents: 5506
diff changeset
   416
                String stringBoolean = (String) attributes.get(JNDI_REBIND_ATTRIBUTE);
042f1a001234 7158796: Tighten properties checking in EnvHelp
dsamersoff
parents: 5506
diff changeset
   417
                final boolean rebind = EnvHelp.computeBooleanFromString( stringBoolean );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                if (tracing)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                    logger.trace("start", JNDI_REBIND_ATTRIBUTE + "=" + rebind);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    if (tracing) logger.trace("start", "binding to " + jndiUrl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1004
diff changeset
   425
                    final Hashtable<?, ?> usemap = EnvHelp.mapToHashtable(attributes);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                    bind(jndiUrl, usemap, objref, rebind);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                    boundJndiUrl = jndiUrl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                    // fit e in the nested exception if we are on 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    throw newIOException("Cannot bind to URL ["+jndiUrl+"]: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                                         + e, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                // if jndiURL is null, we must encode the stub into the URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                if (tracing) logger.trace("start", "Encoding URL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                encodeStubInAddress(objref, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                if (tracing) logger.trace("start", "Encoded URL: " + this.address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                rmiServer.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                // OK: we are already throwing another exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            if (e instanceof RuntimeException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                throw (RuntimeException) e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            else if (e instanceof IOException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                throw (IOException) e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                throw newIOException("Got unexpected exception while " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                                     "starting the connector server: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                                     + e, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        rmiServerImpl = rmiServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        synchronized(openedServers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            openedServers.add(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        state = STARTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            logger.trace("start", "Connector Server Address = " + address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            logger.trace("start", "started.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * <p>Deactivates the connector server, that is, stops listening for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * client connections.  Calling this method will also close all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * client connections that were made by this server.  After this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * method returns, whether normally or with an exception, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * connector server will not create any new client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * connections.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * <p>Once a connector server has been stopped, it cannot be started
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * again.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * <p>Calling this method when the connector server has already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * been stopped has no effect.  Calling this method when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * connector server has not yet been started will disable the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * connector server object permanently.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * <p>If closing a client connection produces an exception, that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * exception is not thrown from this method.  A {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * JMXConnectionNotification} is emitted from this MBean with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * connection ID of the connection that could not be closed.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * <p>Closing a connector server is a potentially slow operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * For example, if a client machine with an open connection has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * crashed, the close operation might have to wait for a network
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * protocol timeout.  Callers that do not want to block in a close
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * operation should do it in a separate thread.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * <p>This method calls the method {@link RMIServerImpl#close()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * close} on the connector server's <code>RMIServerImpl</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * object.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * <p>If the <code>RMIServerImpl</code> was bound to a JNDI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * directory by the {@link #start() start} method, it is unbound
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * from the directory by this method.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * @exception IOException if the server cannot be closed cleanly,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * or if the <code>RMIServerImpl</code> cannot be unbound from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * directory.  When this exception is thrown, the server has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * already attempted to close all client connections, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * appropriate; to call {@link RMIServerImpl#close()}; and to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * unbind the <code>RMIServerImpl</code> from its directory, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * appropriate.  All client connections are closed except possibly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * those that generated exceptions when the server attempted to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * close them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    public void stop() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        final boolean tracing = logger.traceOn();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            if (state == STOPPED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                if (tracing) logger.trace("stop","already stopped.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            } else if (state == CREATED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                if (tracing) logger.trace("stop","not started yet.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            if (tracing) logger.trace("stop", "stopping.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            state = STOPPED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        synchronized(openedServers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            openedServers.remove(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        IOException exception = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        // rmiServerImpl can be null if stop() called without start()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        if (rmiServerImpl != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                if (tracing) logger.trace("stop", "closing RMI server.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                rmiServerImpl.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                if (tracing) logger.trace("stop", "failed to close RMI server: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                if (logger.debugOn()) logger.debug("stop",e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                exception = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        if (boundJndiUrl != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                if (tracing)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                    logger.trace("stop",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                          "unbind from external directory: " + boundJndiUrl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1004
diff changeset
   557
                final Hashtable<?, ?> usemap = EnvHelp.mapToHashtable(attributes);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                InitialContext ctx =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                    new InitialContext(usemap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                ctx.unbind(boundJndiUrl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                ctx.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                if (tracing) logger.trace("stop", "failed to unbind RMI server: "+e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                if (logger.debugOn()) logger.debug("stop",e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                // fit e in as the nested exception if we are on 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                if (exception == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                    exception = newIOException("Cannot bind to URL: " + e, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        if (exception != null) throw exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        if (tracing) logger.trace("stop", "stopped");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    public synchronized boolean isActive() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        return (state == STARTED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    public JMXServiceURL getAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        if (!isActive())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        return address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    public Map<String,?> getAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        Map<String, ?> map = EnvHelp.filterAttributes(attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        return Collections.unmodifiableMap(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
1004
5ba8217eb504 5108776: Add reliable event handling to the JMX API
sjiang
parents: 715
diff changeset
   594
    @Override
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1708
diff changeset
   595
    public synchronized
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1708
diff changeset
   596
        void setMBeanServerForwarder(MBeanServerForwarder mbsf) {
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1708
diff changeset
   597
        super.setMBeanServerForwarder(mbsf);
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1708
diff changeset
   598
        if (rmiServerImpl != null)
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1708
diff changeset
   599
            rmiServerImpl.setMBeanServer(getMBeanServer());
1708
4e1939e6e6b5 6332907: Add ability for connector server to close individual connections
sjiang
parents: 1570
diff changeset
   600
    }
4e1939e6e6b5 6332907: Add ability for connector server to close individual connections
sjiang
parents: 1570
diff changeset
   601
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    /* We repeat the definitions of connection{Opened,Closed,Failed}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
       here so that they are accessible to other classes in this package
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
       even though they have protected access.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1510
diff changeset
   606
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    protected void connectionOpened(String connectionId, String message,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                                    Object userData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        super.connectionOpened(connectionId, message, userData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1510
diff changeset
   612
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    protected void connectionClosed(String connectionId, String message,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                                    Object userData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        super.connectionClosed(connectionId, message, userData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents: 1510
diff changeset
   618
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    protected void connectionFailed(String connectionId, String message,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                                    Object userData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        super.connectionFailed(connectionId, message, userData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * Bind a stub to a registry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * @param jndiUrl URL of the stub in the registry, extracted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     *        from the <code>JMXServiceURL</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * @param attributes A Hashtable containing environment parameters,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     *        built from the Map specified at this object creation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * @param rmiServer The object to bind in the registry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * @param rebind true if the object must be rebound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     **/
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1004
diff changeset
   633
    void bind(String jndiUrl, Hashtable<?, ?> attributes,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
              RMIServer rmiServer, boolean rebind)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        throws NamingException, MalformedURLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        // if jndiURL is not null, we nust bind the stub to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        // directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        InitialContext ctx =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            new InitialContext(attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        if (rebind)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            ctx.rebind(jndiUrl, rmiServer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            ctx.bind(jndiUrl, rmiServer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        ctx.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * Creates a new RMIServerImpl.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    RMIServerImpl newServer() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        final boolean iiop = isIiopURL(address,true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        final int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        if (address == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            port = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            port = address.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        if (iiop)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            return newIIOPServer(attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            return newJRMPServer(attributes, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * Encode a stub into the JMXServiceURL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * @param rmiServer The stub object to encode in the URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * @param attributes A Map containing environment parameters,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     *        built from the Map specified at this object creation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     **/
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1004
diff changeset
   670
    private void encodeStubInAddress(
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1004
diff changeset
   671
            RMIServer rmiServer, Map<String, ?> attributes)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        final String protocol, host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        final int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        if (address == null) {
4167
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 4156
diff changeset
   678
            if (IIOPHelper.isStub(rmiServer))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                protocol = "iiop";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                protocol = "rmi";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            host = null; // will default to local host name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            port = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            protocol = address.getProtocol();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            host = (address.getHost().equals("")) ? null : address.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            port = address.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        final String urlPath = encodeStub(rmiServer, attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        address = new JMXServiceURL(protocol, host, port, urlPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    static boolean isIiopURL(JMXServiceURL directoryURL, boolean strict)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        throws MalformedURLException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        String protocol = directoryURL.getProtocol();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        if (protocol.equals("rmi"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        else if (protocol.equals("iiop"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        else if (strict) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            throw new MalformedURLException("URL must have protocol " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                                            "\"rmi\" or \"iiop\": \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                                            protocol + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * Returns the IOR of the given rmiServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     **/
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1004
diff changeset
   714
    static String encodeStub(
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1004
diff changeset
   715
            RMIServer rmiServer, Map<String, ?> env) throws IOException {
4167
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 4156
diff changeset
   716
        if (IIOPHelper.isStub(rmiServer))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            return "/ior/" + encodeIIOPStub(rmiServer, env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            return "/stub/" + encodeJRMPStub(rmiServer, env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1004
diff changeset
   722
    static String encodeJRMPStub(
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1004
diff changeset
   723
            RMIServer rmiServer, Map<String, ?> env)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        ObjectOutputStream oout = new ObjectOutputStream(bout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        oout.writeObject(rmiServer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        oout.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        byte[] bytes = bout.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        return byteArrayToBase64(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1004
diff changeset
   733
    static String encodeIIOPStub(
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1004
diff changeset
   734
            RMIServer rmiServer, Map<String, ?> env)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        try {
4167
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 4156
diff changeset
   737
            Object orb = IIOPHelper.getOrb(rmiServer);
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 4156
diff changeset
   738
            return IIOPHelper.objectToString(orb, rmiServer);
76f44f2d5d4d 6888179: Separate out dependency on CORBA
alanb
parents: 4156
diff changeset
   739
        } catch (RuntimeException x) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            throw newIOException(x.getMessage(), x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * Object that we will bind to the registry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * This object is a stub connected to our RMIServerImpl.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     **/
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1004
diff changeset
   748
    private static RMIServer objectToBind(
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 1004
diff changeset
   749
            RMIServerImpl rmiServer, Map<String, ?> env)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        return RMIConnector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            connectStub((RMIServer)rmiServer.toStub(),env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    private static RMIServerImpl newJRMPServer(Map<String, ?> env, int port)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        RMIClientSocketFactory csf = (RMIClientSocketFactory)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            env.get(RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        RMIServerSocketFactory ssf = (RMIServerSocketFactory)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            env.get(RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        return new RMIJRMPServerImpl(port, csf, ssf, env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    private static RMIServerImpl newIIOPServer(Map<String, ?> env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        return new RMIIIOPServerImpl(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    private static String byteArrayToBase64(byte[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        int aLen = a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        int numFullGroups = aLen/3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        int numBytesInPartialGroup = aLen - 3*numFullGroups;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        int resultLen = 4*((aLen + 2)/3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        final StringBuilder result = new StringBuilder(resultLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        // Translate all full groups from byte array elements to Base64
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        int inCursor = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        for (int i=0; i<numFullGroups; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            int byte0 = a[inCursor++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            int byte1 = a[inCursor++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            int byte2 = a[inCursor++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            result.append(intToAlpha[byte0 >> 2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            result.append(intToAlpha[(byte0 << 4)&0x3f | (byte1 >> 4)]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            result.append(intToAlpha[(byte1 << 2)&0x3f | (byte2 >> 6)]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            result.append(intToAlpha[byte2 & 0x3f]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        // Translate partial group if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        if (numBytesInPartialGroup != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            int byte0 = a[inCursor++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            result.append(intToAlpha[byte0 >> 2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            if (numBytesInPartialGroup == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                result.append(intToAlpha[(byte0 << 4) & 0x3f]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                result.append("==");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                // assert numBytesInPartialGroup == 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                int byte1 = a[inCursor++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                result.append(intToAlpha[(byte0 << 4)&0x3f | (byte1 >> 4)]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                result.append(intToAlpha[(byte1 << 2)&0x3f]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                result.append('=');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        // assert inCursor == a.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        // assert result.length() == resultLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        return result.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * This array is a lookup table that translates 6-bit positive integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * index values into their "Base64 Alphabet" equivalents as specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * in Table 1 of RFC 2045.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    private static final char intToAlpha[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * Construct a new IOException with a nested exception.
21656
d4c777ccb1db 8028014: Doclint warning/error cleanup in javax.management
rriggs
parents: 14917
diff changeset
   823
     * The nested exception is set only if JDK {@literal >= 1.4}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    private static IOException newIOException(String message,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                                              Throwable cause) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        final IOException x = new IOException(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        return EnvHelp.initCause(x,cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    // Private variables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    // -----------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    private static ClassLogger logger =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        new ClassLogger("javax.management.remote.rmi", "RMIConnectorServer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    private JMXServiceURL address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    private RMIServerImpl rmiServerImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    private final Map<String, ?> attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    private ClassLoader defaultClassLoader = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
    private String boundJndiUrl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
    // state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    private static final int CREATED = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    private static final int STARTED = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    private static final int STOPPED = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    private int state = CREATED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    private final static Set<RMIConnectorServer> openedServers =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            new HashSet<RMIConnectorServer>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
}