src/java.management.rmi/share/classes/javax/management/remote/rmi/RMIServerImpl.java
author pconcannon
Wed, 27 Nov 2019 16:01:34 +0000
changeset 59299 17d242844fc9
parent 47216 71c04702a3d5
permissions -rw-r--r--
8233018: Add a new test to verify that DatagramSocket is not interruptible Summary: Test added to check the interruptability of DatagramSocket, MulticastSocket and DatagramSocketAdaptor. Reviewed-by: chegar, dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
32639
339de1317e84 8043937: Drop support for the IIOP transport from the JMX RMIConnector
jbachorik
parents: 25859
diff changeset
     2
 * Copyright (c) 2002, 2015, 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: 4156
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: 4156
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: 4156
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
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
import com.sun.jmx.remote.internal.ArrayNotificationBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import com.sun.jmx.remote.internal.NotificationBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import com.sun.jmx.remote.security.JMXPluggableAuthenticator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import com.sun.jmx.remote.util.ClassLogger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.Closeable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.lang.ref.WeakReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.rmi.Remote;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.rmi.server.RemoteServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.rmi.server.ServerNotActiveException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.List;
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.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.management.remote.JMXAuthenticator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.management.remote.JMXConnectorServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import javax.security.auth.Subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <p>An RMI object representing a connector server.  Remote clients
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * can make connections using the {@link #newClient(Object)} method.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * method returns an RMI object representing the connection.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <p>User code does not usually reference this class directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * RMI connection servers are usually created with the class {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * RMIConnectorServer}.  Remote clients usually create connections
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * either with {@link javax.management.remote.JMXConnectorFactory}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * or by instantiating {@link RMIConnector}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <p>This is an abstract class.  Concrete subclasses define the
32639
339de1317e84 8043937: Drop support for the IIOP transport from the JMX RMIConnector
jbachorik
parents: 25859
diff changeset
    64
 * details of the client connection objects.</p>
2
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 abstract class RMIServerImpl implements Closeable, RMIServer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * <p>Constructs a new <code>RMIServerImpl</code>.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * @param env the environment containing attributes for the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * <code>RMIServerImpl</code>.  Can be null, which is equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * to an empty Map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public RMIServerImpl(Map<String,?> env) {
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 2
diff changeset
    77
        this.env = (env == null) ? Collections.<String,Object>emptyMap() : env;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    void setRMIConnectorServer(RMIConnectorServer connServer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        this.connServer = connServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * <p>Exports this RMI object.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @exception IOException if this RMI object cannot be exported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    protected abstract void export() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Returns a remotable stub for this server object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @return a remotable stub.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @exception IOException if the stub cannot be obtained - e.g the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *            RMIServerImpl has not been exported yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public abstract Remote toStub() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * <p>Sets the default <code>ClassLoader</code> for this connector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * server. New client connections will use this classloader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Existing client connections are unaffected.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @param cl the new <code>ClassLoader</code> to be used by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * connector server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @see #getDefaultClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public synchronized void setDefaultClassLoader(ClassLoader cl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        this.cl = cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * <p>Gets the default <code>ClassLoader</code> used by this connector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * server.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @return the default <code>ClassLoader</code> used by this
21656
d4c777ccb1db 8028014: Doclint warning/error cleanup in javax.management
rriggs
parents: 17425
diff changeset
   119
     * connector server.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @see #setDefaultClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public synchronized ClassLoader getDefaultClassLoader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * <p>Sets the <code>MBeanServer</code> to which this connector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * server is attached. New client connections will interact
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * with this <code>MBeanServer</code>. Existing client connections are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * unaffected.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param mbs the new <code>MBeanServer</code>.  Can be null, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * new client connections will be refused as long as it is.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @see #getMBeanServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public synchronized void setMBeanServer(MBeanServer mbs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        this.mbeanServer = mbs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * <p>The <code>MBeanServer</code> to which this connector server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * is attached.  This is the last value passed to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * #setMBeanServer} on this object, or null if that method has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * never been called.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @return the <code>MBeanServer</code> to which this connector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * is attached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @see #setMBeanServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public synchronized MBeanServer getMBeanServer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return mbeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public String getVersion() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        // Expected format is: "protocol-version implementation-name"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            return "1.0 java_runtime_" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    System.getProperty("java.runtime.version");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            return "1.0 ";
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * <p>Creates a new client connection.  This method calls {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * #makeClient makeClient} and adds the returned client connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * object to an internal list.  When this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * <code>RMIServerImpl</code> is shut down via its {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * #close()} method, the {@link RMIConnection#close() close()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * method of each object remaining in the list is called.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * <p>The fact that a client connection object is in this internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * list does not prevent it from being garbage collected.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @param credentials this object specifies the user-defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * credentials to be passed in to the server in order to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * authenticate the caller before creating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * <code>RMIConnection</code>.  Can be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @return the newly-created <code>RMIConnection</code>.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * usually the object created by <code>makeClient</code>, though
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * an implementation may choose to wrap that object in another
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * object implementing <code>RMIConnection</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @exception IOException if the new client object cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * created or exported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @exception SecurityException if the given credentials do not allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * the server to authenticate the user successfully.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @exception IllegalStateException if {@link #getMBeanServer()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public RMIConnection newClient(Object credentials) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        return doNewClient(credentials);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * This method could be overridden by subclasses defined in this package
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * to perform additional operations specific to the underlying transport
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * before creating the new client connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    RMIConnection doNewClient(Object credentials) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        final boolean tracing = logger.traceOn();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (tracing) logger.trace("newClient","making new client");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        if (getMBeanServer() == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            throw new IllegalStateException("Not attached to an MBean server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        Subject subject = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        JMXAuthenticator authenticator =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            (JMXAuthenticator) env.get(JMXConnectorServer.AUTHENTICATOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if (authenticator == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
             * Create the JAAS-based authenticator only if authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
             * has been enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            if (env.get("jmx.remote.x.password.file") != null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                env.get("jmx.remote.x.login.config") != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                authenticator = new JMXPluggableAuthenticator(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (authenticator != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            if (tracing) logger.trace("newClient","got authenticator: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                               authenticator.getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                subject = authenticator.authenticate(credentials);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                logger.trace("newClient", "Authentication failed: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        if (tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            if (subject != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                logger.trace("newClient","subject is not null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            else logger.trace("newClient","no subject");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        final String connectionId = makeConnectionId(getProtocol(), subject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (tracing)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            logger.trace("newClient","making new connection: " + connectionId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        RMIConnection client = makeClient(connectionId, subject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        dropDeadReferences();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        WeakReference<RMIConnection> wr = new WeakReference<RMIConnection>(client);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        synchronized (clientList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            clientList.add(wr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
1708
4e1939e6e6b5 6332907: Add ability for connector server to close individual connections
sjiang
parents: 1510
diff changeset
   257
        connServer.connectionOpened(connectionId, "Connection opened", null);
4e1939e6e6b5 6332907: Add ability for connector server to close individual connections
sjiang
parents: 1510
diff changeset
   258
4e1939e6e6b5 6332907: Add ability for connector server to close individual connections
sjiang
parents: 1510
diff changeset
   259
        synchronized (clientList) {
4e1939e6e6b5 6332907: Add ability for connector server to close individual connections
sjiang
parents: 1510
diff changeset
   260
            if (!clientList.contains(wr)) {
4e1939e6e6b5 6332907: Add ability for connector server to close individual connections
sjiang
parents: 1510
diff changeset
   261
                // can be removed only by a JMXConnectionNotification listener
4e1939e6e6b5 6332907: Add ability for connector server to close individual connections
sjiang
parents: 1510
diff changeset
   262
                throw new IOException("The connection is refused.");
4e1939e6e6b5 6332907: Add ability for connector server to close individual connections
sjiang
parents: 1510
diff changeset
   263
            }
4e1939e6e6b5 6332907: Add ability for connector server to close individual connections
sjiang
parents: 1510
diff changeset
   264
        }
4e1939e6e6b5 6332907: Add ability for connector server to close individual connections
sjiang
parents: 1510
diff changeset
   265
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        if (tracing)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            logger.trace("newClient","new connection done: " + connectionId );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        return client;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * <p>Creates a new client connection.  This method is called by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * the public method {@link #newClient(Object)}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @param connectionId the ID of the new connection.  Every
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * connection opened by this connector server will have a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * different ID.  The behavior is unspecified if this parameter is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @param subject the authenticated subject.  Can be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @return the newly-created <code>RMIConnection</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @exception IOException if the new client object cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * created or exported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    protected abstract RMIConnection makeClient(String connectionId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                                                Subject subject)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * <p>Closes a client connection made by {@link #makeClient makeClient}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @param client a connection previously returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * <code>makeClient</code> on which the <code>closeClient</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * method has not previously been called.  The behavior is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * unspecified if these conditions are violated, including the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * case where <code>client</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @exception IOException if the client connection cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    protected abstract void closeClient(RMIConnection client)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * <p>Returns the protocol string for this object.  The string is
32639
339de1317e84 8043937: Drop support for the IIOP transport from the JMX RMIConnector
jbachorik
parents: 25859
diff changeset
   309
     * <code>rmi</code> for RMI/JRMP.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @return the protocol string for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    protected abstract String getProtocol();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * <p>Method called when a client connection created by {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * #makeClient makeClient} is closed.  A subclass that defines
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * <code>makeClient</code> must arrange for this method to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * called when the resultant object's {@link RMIConnection#close()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * close} method is called.  This enables it to be removed from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * the <code>RMIServerImpl</code>'s list of connections.  It is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * not an error for <code>client</code> not to be in that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * list.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * <p>After removing <code>client</code> from the list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * connections, this method calls {@link #closeClient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * closeClient(client)}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @param client the client connection that has been closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @exception IOException if {@link #closeClient} throws this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * @exception NullPointerException if <code>client</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    protected void clientClosed(RMIConnection client) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        final boolean debug = logger.debugOn();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        if (debug) logger.trace("clientClosed","client="+client);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        if (client == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            throw new NullPointerException("Null client");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        synchronized (clientList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            dropDeadReferences();
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 2
diff changeset
   346
            for (Iterator<WeakReference<RMIConnection>> it = clientList.iterator();
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 2
diff changeset
   347
                 it.hasNext(); ) {
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 2
diff changeset
   348
                WeakReference<RMIConnection> wr = it.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                if (wr.get() == client) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            /* It is not a bug for this loop not to find the client.  In
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
               our close() method, we remove a client from the list before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
               calling its close() method.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        if (debug) logger.trace("clientClosed", "closing client.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        closeClient(client);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        if (debug) logger.trace("clientClosed", "sending notif");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        connServer.connectionClosed(client.getConnectionId(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                                    "Client connection closed", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        if (debug) logger.trace("clientClosed","done");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * <p>Closes this connection server.  This method first calls the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * {@link #closeServer()} method so that no new client connections
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * will be accepted.  Then, for each remaining {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * RMIConnection} object returned by {@link #makeClient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * makeClient}, its {@link RMIConnection#close() close} method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * called.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * <p>The behavior when this method is called more than once is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * unspecified.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * <p>If {@link #closeServer()} throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * <code>IOException</code>, the individual connections are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * nevertheless closed, and then the <code>IOException</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * thrown from this method.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * <p>If {@link #closeServer()} returns normally but one or more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * of the individual connections throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * <code>IOException</code>, then, after closing all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * connections, one of those <code>IOException</code>s is thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * from this method.  If more than one connection throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * <code>IOException</code>, it is unspecified which one is thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * from this method.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @exception IOException if {@link #closeServer()} or one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * {@link RMIConnection#close()} calls threw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * <code>IOException</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    public synchronized void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        final boolean tracing = logger.traceOn();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        final boolean debug   = logger.debugOn();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        if (tracing) logger.trace("close","closing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        IOException ioException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            if (debug)   logger.debug("close","closing Server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            closeServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            if (tracing) logger.trace("close","Failed to close server: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            if (debug)   logger.debug("close",e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            ioException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        if (debug)   logger.debug("close","closing Clients");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        // Loop to close all clients
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            synchronized (clientList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                if (debug) logger.debug("close","droping dead references");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                dropDeadReferences();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                if (debug) logger.debug("close","client count: "+clientList.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                if (clientList.size() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                /* Loop until we find a non-null client.  Because we called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                   dropDeadReferences(), this will usually be the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                   element of the list, but a garbage collection could have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                   happened in between.  */
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 2
diff changeset
   427
                for (Iterator<WeakReference<RMIConnection>> it = clientList.iterator();
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 2
diff changeset
   428
                     it.hasNext(); ) {
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 2
diff changeset
   429
                    WeakReference<RMIConnection> wr = it.next();
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 2
diff changeset
   430
                    RMIConnection client = wr.get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                    it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    if (client != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                            client.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                            if (tracing)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                                logger.trace("close","Failed to close client: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                            if (debug) logger.debug("close",e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                            if (ioException == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                                ioException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        if(notifBuffer != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            notifBuffer.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        if (ioException != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            if (tracing) logger.trace("close","close failed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            throw ioException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        if (tracing) logger.trace("close","closed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * <p>Called by {@link #close()} to close the connector server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * After returning from this method, the connector server must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * not accept any new connections.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * @exception IOException if the attempt to close the connector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * server failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    protected abstract void closeServer() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    private static synchronized String makeConnectionId(String protocol,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                                                        Subject subject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        connectionIdNumber++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        String clientHost = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            clientHost = RemoteServer.getClientHost();
17425
9259f4bb1c48 7199324: Connection ID for IPv6 addresses is not generated accordingly to the specification
jbachorik
parents: 5506
diff changeset
   476
            /*
9259f4bb1c48 7199324: Connection ID for IPv6 addresses is not generated accordingly to the specification
jbachorik
parents: 5506
diff changeset
   477
             * According to the rules specified in the javax.management.remote
9259f4bb1c48 7199324: Connection ID for IPv6 addresses is not generated accordingly to the specification
jbachorik
parents: 5506
diff changeset
   478
             * package description, a numeric IPv6 address (detected by the
9259f4bb1c48 7199324: Connection ID for IPv6 addresses is not generated accordingly to the specification
jbachorik
parents: 5506
diff changeset
   479
             * presence of otherwise forbidden ":" character) forming a part
9259f4bb1c48 7199324: Connection ID for IPv6 addresses is not generated accordingly to the specification
jbachorik
parents: 5506
diff changeset
   480
             * of the connection id must be enclosed in square brackets.
9259f4bb1c48 7199324: Connection ID for IPv6 addresses is not generated accordingly to the specification
jbachorik
parents: 5506
diff changeset
   481
             */
9259f4bb1c48 7199324: Connection ID for IPv6 addresses is not generated accordingly to the specification
jbachorik
parents: 5506
diff changeset
   482
            if (clientHost.contains(":")) {
9259f4bb1c48 7199324: Connection ID for IPv6 addresses is not generated accordingly to the specification
jbachorik
parents: 5506
diff changeset
   483
                clientHost = "[" + clientHost + "]";
9259f4bb1c48 7199324: Connection ID for IPv6 addresses is not generated accordingly to the specification
jbachorik
parents: 5506
diff changeset
   484
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        } catch (ServerNotActiveException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            logger.trace("makeConnectionId", "getClientHost", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        final StringBuilder buf = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        buf.append(protocol).append(":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        if (clientHost.length() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            buf.append("//").append(clientHost);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        buf.append(" ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        if (subject != null) {
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 2
diff changeset
   495
            Set<Principal> principals = subject.getPrincipals();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            String sep = "";
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 2
diff changeset
   497
            for (Iterator<Principal> it = principals.iterator(); it.hasNext(); ) {
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 2
diff changeset
   498
                Principal p = it.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                String name = p.getName().replace(' ', '_').replace(';', ':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                buf.append(sep).append(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                sep = ";";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        buf.append(" ").append(connectionIdNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        if (logger.traceOn())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            logger.trace("newConnectionId","connectionId="+buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    private void dropDeadReferences() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        synchronized (clientList) {
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 2
diff changeset
   512
            for (Iterator<WeakReference<RMIConnection>> it = clientList.iterator();
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 2
diff changeset
   513
                 it.hasNext(); ) {
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 2
diff changeset
   514
                WeakReference<RMIConnection> wr = it.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                if (wr.get() == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                    it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    synchronized NotificationBuffer getNotifBuffer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        //Notification buffer is lazily created when the first client connects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        if(notifBuffer == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            notifBuffer =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                ArrayNotificationBuffer.getNotificationBuffer(mbeanServer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                                                              env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        return notifBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    private static final ClassLogger logger =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        new ClassLogger("javax.management.remote.rmi", "RMIServerImpl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    /** List of WeakReference values.  Each one references an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        RMIConnection created by this object, or null if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        RMIConnection has been garbage-collected.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    private final List<WeakReference<RMIConnection>> clientList =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            new ArrayList<WeakReference<RMIConnection>>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    private ClassLoader cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    private MBeanServer mbeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 2
diff changeset
   543
    private final Map<String, ?> env;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    private RMIConnectorServer connServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    private static int connectionIdNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    private NotificationBuffer notifBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
}