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