src/java.rmi/share/classes/sun/rmi/transport/tcp/TCPTransport.java
author mli
Thu, 28 Nov 2019 13:48:25 +0800
changeset 59300 60bdcb9a7b94
parent 47216 71c04702a3d5
permissions -rw-r--r--
8232446: logging enhancement for rmi when socket closed Reviewed-by: rriggs, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
37892
55da13d60938 8155978: Remove HTTP proxy implementation and tests from RMI
rriggs
parents: 37667
diff changeset
     2
 * Copyright (c) 1996, 2016, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package sun.rmi.transport.tcp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.lang.ref.Reference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.ref.SoftReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.ref.WeakReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.reflect.InvocationTargetException;
13408
7b1c9c089326 7187876: ClassCastException in TCPTransport.executeAcceptLoop
dmocek
parents: 12040
diff changeset
    31
import java.lang.reflect.UndeclaredThrowableException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.DataInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.DataOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.BufferedInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.BufferedOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.net.ServerSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.net.Socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.rmi.RemoteException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.rmi.server.ExportException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.rmi.server.LogStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.rmi.server.RMIFailureHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.rmi.server.RMISocketFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.rmi.server.RemoteCall;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.rmi.server.ServerNotActiveException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.rmi.server.UID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.security.AccessControlContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.security.AccessController;
28552
608626229264 8055309: RMI needs better transportation considerations
smarks
parents: 25859
diff changeset
    52
import java.security.Permissions;
23333
b0af2c7c8c91 8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents: 19211
diff changeset
    53
import java.security.PrivilegedAction;
28552
608626229264 8055309: RMI needs better transportation considerations
smarks
parents: 25859
diff changeset
    54
import java.security.ProtectionDomain;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import java.util.LinkedList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import java.util.WeakHashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import java.util.logging.Level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import java.util.concurrent.ExecutorService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import java.util.concurrent.RejectedExecutionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import java.util.concurrent.SynchronousQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
import java.util.concurrent.ThreadFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
import java.util.concurrent.ThreadPoolExecutor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
import java.util.concurrent.TimeUnit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
import java.util.concurrent.atomic.AtomicInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
import sun.rmi.runtime.Log;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
import sun.rmi.runtime.NewThreadAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
import sun.rmi.transport.Channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
import sun.rmi.transport.Connection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
import sun.rmi.transport.DGCAckHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
import sun.rmi.transport.Endpoint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
import sun.rmi.transport.StreamRemoteCall;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
import sun.rmi.transport.Target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
import sun.rmi.transport.Transport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
import sun.rmi.transport.TransportConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * TCPTransport is the socket-based implementation of the RMI Transport
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * abstraction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * @author Ann Wollrath
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * @author Peter Jones
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 */
19211
32a04c562026 8022440: suppress deprecation warnings in sun.rmi
smarks
parents: 14342
diff changeset
    86
@SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
public class TCPTransport extends Transport {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /* tcp package log */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    static final Log tcpLog = Log.getLog("sun.rmi.transport.tcp", "tcp",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        LogStream.parseLevel(AccessController.doPrivileged(
23333
b0af2c7c8c91 8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents: 19211
diff changeset
    92
            (PrivilegedAction<String>) () -> System.getProperty("sun.rmi.transport.tcp.logLevel"))));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /** maximum number of connection handler threads */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private static final int maxConnectionThreads =     // default no limit
23333
b0af2c7c8c91 8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents: 19211
diff changeset
    96
        AccessController.doPrivileged((PrivilegedAction<Integer>) () ->
b0af2c7c8c91 8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents: 19211
diff changeset
    97
            Integer.getInteger("sun.rmi.transport.tcp.maxConnectionThreads",
b0af2c7c8c91 8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents: 19211
diff changeset
    98
                               Integer.MAX_VALUE));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /** keep alive time for idle connection handler threads */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private static final long threadKeepAliveTime =     // default 1 minute
23333
b0af2c7c8c91 8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents: 19211
diff changeset
   102
        AccessController.doPrivileged((PrivilegedAction<Long>) () ->
b0af2c7c8c91 8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents: 19211
diff changeset
   103
            Long.getLong("sun.rmi.transport.tcp.threadKeepAliveTime", 60000));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /** thread pool for connection handlers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private static final ExecutorService connectionThreadPool =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        new ThreadPoolExecutor(0, maxConnectionThreads,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            threadKeepAliveTime, TimeUnit.MILLISECONDS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            new SynchronousQueue<Runnable>(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            new ThreadFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                public Thread newThread(Runnable runnable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    return AccessController.doPrivileged(new NewThreadAction(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                        runnable, "TCP Connection(idle)", true, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /** total connections handled */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private static final AtomicInteger connectionCount = new AtomicInteger(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /** client host for the current thread's connection */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private static final ThreadLocal<ConnectionHandler>
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   122
        threadConnectionHandler = new ThreadLocal<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
28552
608626229264 8055309: RMI needs better transportation considerations
smarks
parents: 25859
diff changeset
   124
    /** an AccessControlContext with no permissions */
608626229264 8055309: RMI needs better transportation considerations
smarks
parents: 25859
diff changeset
   125
    private static final AccessControlContext NOPERMS_ACC;
608626229264 8055309: RMI needs better transportation considerations
smarks
parents: 25859
diff changeset
   126
    static {
608626229264 8055309: RMI needs better transportation considerations
smarks
parents: 25859
diff changeset
   127
        Permissions perms = new Permissions();
608626229264 8055309: RMI needs better transportation considerations
smarks
parents: 25859
diff changeset
   128
        ProtectionDomain[] pd = { new ProtectionDomain(null, perms) };
608626229264 8055309: RMI needs better transportation considerations
smarks
parents: 25859
diff changeset
   129
        NOPERMS_ACC = new AccessControlContext(pd);
608626229264 8055309: RMI needs better transportation considerations
smarks
parents: 25859
diff changeset
   130
    }
608626229264 8055309: RMI needs better transportation considerations
smarks
parents: 25859
diff changeset
   131
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /** endpoints for this transport */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private final LinkedList<TCPEndpoint> epList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /** number of objects exported on this transport */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private int exportCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /** server socket for this transport */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private ServerSocket server = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /** table mapping endpoints to channels */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    private final Map<TCPEndpoint,Reference<TCPChannel>> channelTable =
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   140
        new WeakHashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    static final RMISocketFactory defaultSocketFactory =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        RMISocketFactory.getDefaultSocketFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /** number of milliseconds in accepted-connection timeout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Warning: this should be greater than 15 seconds (the client-side
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * timeout), and defaults to 2 hours.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * The maximum representable value is slightly more than 24 days
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * and 20 hours.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    private static final int connectionReadTimeout =    // default 2 hours
23333
b0af2c7c8c91 8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents: 19211
diff changeset
   152
        AccessController.doPrivileged((PrivilegedAction<Integer>) () ->
b0af2c7c8c91 8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents: 19211
diff changeset
   153
            Integer.getInteger("sun.rmi.transport.tcp.readTimeout", 2 * 3600 * 1000));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Constructs a TCPTransport.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    TCPTransport(LinkedList<TCPEndpoint> epList)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        // assert ((epList.size() != null) && (epList.size() >= 1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        this.epList = epList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (tcpLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            tcpLog.log(Log.BRIEF, "Version = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                TransportConstants.Version + ", ep = " + getEndpoint());
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
     * Closes all cached connections in every channel subordinated to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * transport.  Currently, this only closes outgoing connections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public void shedConnectionCaches() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        List<TCPChannel> channels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        synchronized (channelTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            channels = new ArrayList<TCPChannel>(channelTable.values().size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            for (Reference<TCPChannel> ref : channelTable.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                TCPChannel ch = ref.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                if (ch != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    channels.add(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        for (TCPChannel channel : channels) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            channel.shedCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Returns a <I>Channel</I> that generates connections to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * endpoint <I>ep</I>. A Channel is an object that creates and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * manages connections of a particular type to some particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * address space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @param ep the endpoint to which connections will be generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @return the channel or null if the transport cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * generate connections to this endpoint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public TCPChannel getChannel(Endpoint ep) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        TCPChannel ch = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (ep instanceof TCPEndpoint) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            synchronized (channelTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                Reference<TCPChannel> ref = channelTable.get(ep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                if (ref != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    ch = ref.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                if (ch == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                    TCPEndpoint tcpEndpoint = (TCPEndpoint) ep;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    ch = new TCPChannel(this, tcpEndpoint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    channelTable.put(tcpEndpoint,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                                     new WeakReference<TCPChannel>(ch));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        return ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Removes the <I>Channel</I> that generates connections to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * endpoint <I>ep</I>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public void free(Endpoint ep) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        if (ep instanceof TCPEndpoint) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            synchronized (channelTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                Reference<TCPChannel> ref = channelTable.remove(ep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                if (ref != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    TCPChannel channel = ref.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    if (channel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                        channel.shedCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Export the object so that it can accept incoming calls.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public void exportObject(Target target) throws RemoteException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
         * Ensure that a server socket is listening, and count this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
         * export while synchronized to prevent the server socket from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
         * being closed due to concurrent unexports.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            listen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            exportCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
         * Try to add the Target to the exported object table; keep
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
         * counting this export (to keep server socket open) only if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
         * that succeeds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        boolean ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            super.exportObject(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            if (!ok) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    decrementExportCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    protected synchronized void targetUnexported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        decrementExportCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * Decrements the count of exported objects, closing the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * server socket if the count reaches zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    private void decrementExportCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        assert Thread.holdsLock(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        exportCount--;
59300
60bdcb9a7b94 8232446: logging enhancement for rmi when socket closed
mli
parents: 47216
diff changeset
   276
        if (tcpLog.isLoggable(Log.VERBOSE)) {
60bdcb9a7b94 8232446: logging enhancement for rmi when socket closed
mli
parents: 47216
diff changeset
   277
            tcpLog.log(Log.VERBOSE,
60bdcb9a7b94 8232446: logging enhancement for rmi when socket closed
mli
parents: 47216
diff changeset
   278
                    "server socket: " + server + ", exportCount: " + exportCount);
60bdcb9a7b94 8232446: logging enhancement for rmi when socket closed
mli
parents: 47216
diff changeset
   279
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if (exportCount == 0 && getEndpoint().getListenPort() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            ServerSocket ss = server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            server = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            try {
59300
60bdcb9a7b94 8232446: logging enhancement for rmi when socket closed
mli
parents: 47216
diff changeset
   284
                if (tcpLog.isLoggable(Log.BRIEF)) {
60bdcb9a7b94 8232446: logging enhancement for rmi when socket closed
mli
parents: 47216
diff changeset
   285
                    tcpLog.log(Log.BRIEF, "server socket close: " + ss);
60bdcb9a7b94 8232446: logging enhancement for rmi when socket closed
mli
parents: 47216
diff changeset
   286
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                ss.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            } catch (IOException e) {
59300
60bdcb9a7b94 8232446: logging enhancement for rmi when socket closed
mli
parents: 47216
diff changeset
   289
                if (tcpLog.isLoggable(Log.BRIEF)) {
60bdcb9a7b94 8232446: logging enhancement for rmi when socket closed
mli
parents: 47216
diff changeset
   290
                    tcpLog.log(Log.BRIEF,
60bdcb9a7b94 8232446: logging enhancement for rmi when socket closed
mli
parents: 47216
diff changeset
   291
                            "server socket close throws: " + e);
60bdcb9a7b94 8232446: logging enhancement for rmi when socket closed
mli
parents: 47216
diff changeset
   292
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * Verify that the current access control context has permission to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * accept the connection being dispatched by the current thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    protected void checkAcceptPermission(AccessControlContext acc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        if (sm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        ConnectionHandler h = threadConnectionHandler.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        if (h == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            throw new Error(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                "checkAcceptPermission not in ConnectionHandler thread");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        h.checkAcceptPermission(sm, acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    private TCPEndpoint getEndpoint() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        synchronized (epList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            return epList.getLast();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * Listen on transport's endpoint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    private void listen() throws RemoteException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        assert Thread.holdsLock(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        TCPEndpoint ep = getEndpoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        int port = ep.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        if (server == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            if (tcpLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                tcpLog.log(Log.BRIEF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                    "(port " + port + ") create server socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                server = ep.newServerSocket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                 * Don't retry ServerSocket if creation fails since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                 * "port in use" will cause export to hang if an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                 * RMIFailureHandler is not installed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                Thread t = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    new NewThreadAction(new AcceptLoop(server),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                                        "TCP Accept-" + port, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                t.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            } catch (java.net.BindException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                throw new ExportException("Port already in use: " + port, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                throw new ExportException("Listen failed on port: " + port, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            // otherwise verify security access to existing server socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                sm.checkListen(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * Worker for accepting connections from a server socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    private class AcceptLoop implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        private final ServerSocket serverSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        // state for throttling loop on exceptions (local to accept thread)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        private long lastExceptionTime = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        private int recentExceptionCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        AcceptLoop(ServerSocket serverSocket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            this.serverSocket = serverSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                executeAcceptLoop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                try {
59300
60bdcb9a7b94 8232446: logging enhancement for rmi when socket closed
mli
parents: 47216
diff changeset
   380
                    if (tcpLog.isLoggable(Log.BRIEF)) {
60bdcb9a7b94 8232446: logging enhancement for rmi when socket closed
mli
parents: 47216
diff changeset
   381
                        tcpLog.log(Log.BRIEF,
60bdcb9a7b94 8232446: logging enhancement for rmi when socket closed
mli
parents: 47216
diff changeset
   382
                                "server socket close: " + serverSocket);
60bdcb9a7b94 8232446: logging enhancement for rmi when socket closed
mli
parents: 47216
diff changeset
   383
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                     * Only one accept loop is started per server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                     * socket, so after no more connections will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                     * accepted, ensure that the server socket is no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                     * longer listening.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                    serverSocket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                } catch (IOException e) {
59300
60bdcb9a7b94 8232446: logging enhancement for rmi when socket closed
mli
parents: 47216
diff changeset
   392
                    if (tcpLog.isLoggable(Log.BRIEF)) {
60bdcb9a7b94 8232446: logging enhancement for rmi when socket closed
mli
parents: 47216
diff changeset
   393
                        tcpLog.log(Log.BRIEF,
60bdcb9a7b94 8232446: logging enhancement for rmi when socket closed
mli
parents: 47216
diff changeset
   394
                                "server socket close throws: " + e);
60bdcb9a7b94 8232446: logging enhancement for rmi when socket closed
mli
parents: 47216
diff changeset
   395
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
         * Accepts connections from the server socket and executes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
         * handlers for them in the thread pool.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
         **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        private void executeAcceptLoop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            if (tcpLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                tcpLog.log(Log.BRIEF, "listening on port " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                           getEndpoint().getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                Socket socket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                    socket = serverSocket.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                     * Find client host name (or "0.0.0.0" if unknown)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    InetAddress clientAddr = socket.getInetAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    String clientHost = (clientAddr != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                                         ? clientAddr.getHostAddress()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                                         : "0.0.0.0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                     * Execute connection handler in the thread pool,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                     * which uses non-system threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                        connectionThreadPool.execute(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                            new ConnectionHandler(socket, clientHost));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                    } catch (RejectedExecutionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                        closeSocket(socket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                        tcpLog.log(Log.BRIEF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                                   "rejected connection from " + clientHost);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                         * If the server socket has been closed, such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                         * as because there are no more exported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                         * objects, then we expect accept to throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                         * exception, so just terminate normally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                        if (serverSocket.isClosed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                            if (tcpLog.isLoggable(Level.WARNING)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                                tcpLog.log(Level.WARNING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                                           "accept loop for " + serverSocket +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                                           " throws", t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                        } catch (Throwable tt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                         * Always close the accepted socket (if any)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                         * if an exception occurs, but only after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                         * logging an unexpected exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                        if (socket != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                            closeSocket(socket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                     * In case we're running out of file descriptors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                     * release resources held in caches.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                    if (!(t instanceof SecurityException)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                            TCPEndpoint.shedConnectionCaches();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                        } catch (Throwable tt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                     * A NoClassDefFoundError can occur if no file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                     * descriptors are available, in which case this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                     * loop should not terminate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                    if (t instanceof Exception ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                        t instanceof OutOfMemoryError ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                        t instanceof NoClassDefFoundError)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                        if (!continueAfterAcceptFailure(t)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                        // continue loop
13408
7b1c9c089326 7187876: ClassCastException in TCPTransport.executeAcceptLoop
dmocek
parents: 12040
diff changeset
   491
                    } else if (t instanceof Error) {
7b1c9c089326 7187876: ClassCastException in TCPTransport.executeAcceptLoop
dmocek
parents: 12040
diff changeset
   492
                        throw (Error) t;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                    } else {
13408
7b1c9c089326 7187876: ClassCastException in TCPTransport.executeAcceptLoop
dmocek
parents: 12040
diff changeset
   494
                        throw new UndeclaredThrowableException(t);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
         * Returns true if the accept loop should continue after the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
         * specified exception has been caught, or false if the accept
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
         * loop should terminate (closing the server socket).  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
         * there is an RMIFailureHandler, this method returns the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
         * result of passing the specified exception to it; otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
         * this method always returns true, after sleeping to throttle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
         * the accept loop if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
         **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        private boolean continueAfterAcceptFailure(Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            RMIFailureHandler fh = RMISocketFactory.getFailureHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            if (fh != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                return fh.failure(t instanceof Exception ? (Exception) t :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                                  new InvocationTargetException(t));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                throttleLoopOnException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                return true;
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
         * Throttles the accept loop after an exception has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
         * caught: if a burst of 10 exceptions in 5 seconds occurs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
         * then wait for 10 seconds to curb busy CPU usage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
         **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        private void throttleLoopOnException() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            long now = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            if (lastExceptionTime == 0L || (now - lastExceptionTime) > 5000) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                // last exception was long ago (or this is the first)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                lastExceptionTime = now;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                recentExceptionCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                // exception burst window was started recently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                if (++recentExceptionCount >= 10) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                        Thread.sleep(10000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                    } catch (InterruptedException ignore) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    /** close socket and eat exception */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    private static void closeSocket(Socket sock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        try {
59300
60bdcb9a7b94 8232446: logging enhancement for rmi when socket closed
mli
parents: 47216
diff changeset
   546
            if (tcpLog.isLoggable(Log.BRIEF)) {
60bdcb9a7b94 8232446: logging enhancement for rmi when socket closed
mli
parents: 47216
diff changeset
   547
                tcpLog.log(Log.BRIEF, "socket close: " + sock);
60bdcb9a7b94 8232446: logging enhancement for rmi when socket closed
mli
parents: 47216
diff changeset
   548
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            sock.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            // eat exception
59300
60bdcb9a7b94 8232446: logging enhancement for rmi when socket closed
mli
parents: 47216
diff changeset
   552
            if (tcpLog.isLoggable(Log.BRIEF)) {
60bdcb9a7b94 8232446: logging enhancement for rmi when socket closed
mli
parents: 47216
diff changeset
   553
                tcpLog.log(Log.BRIEF, "socket close throws: " + ex);
60bdcb9a7b94 8232446: logging enhancement for rmi when socket closed
mli
parents: 47216
diff changeset
   554
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * handleMessages decodes transport operations and handles messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * appropriately.  If an exception occurs during message handling,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * the socket is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    void handleMessages(Connection conn, boolean persistent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        int port = getEndpoint().getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            DataInputStream in = new DataInputStream(conn.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                int op = in.read();     // transport op
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                if (op == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                    if (tcpLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                        tcpLog.log(Log.BRIEF, "(port " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                            port + ") connection closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                if (tcpLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                    tcpLog.log(Log.BRIEF, "(port " + port +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                        ") op = " + op);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                switch (op) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                case TransportConstants.Call:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                    // service incoming RMI call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                    RemoteCall call = new StreamRemoteCall(conn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                    if (serviceCall(call) == false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                case TransportConstants.Ping:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                    // send ack for ping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                    DataOutputStream out =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                        new DataOutputStream(conn.getOutputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                    out.writeByte(TransportConstants.PingAck);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                    conn.releaseOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                case TransportConstants.DGCAck:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                    DGCAckHandler.received(UID.read(in));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                    throw new IOException("unknown transport op " + op);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            } while (persistent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            // exception during processing causes connection to close (below)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            if (tcpLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                tcpLog.log(Log.BRIEF, "(port " + port +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                    ") exception: ", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                conn.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                // eat exception
59300
60bdcb9a7b94 8232446: logging enhancement for rmi when socket closed
mli
parents: 47216
diff changeset
   619
                if (tcpLog.isLoggable(Log.BRIEF)) {
60bdcb9a7b94 8232446: logging enhancement for rmi when socket closed
mli
parents: 47216
diff changeset
   620
                    tcpLog.log(Log.BRIEF, "Connection close throws " + ex);
60bdcb9a7b94 8232446: logging enhancement for rmi when socket closed
mli
parents: 47216
diff changeset
   621
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * Returns the client host for the current thread's connection.  Throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * ServerNotActiveException if no connection is active for this thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    public static String getClientHost() throws ServerNotActiveException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        ConnectionHandler h = threadConnectionHandler.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        if (h != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            return h.getClientHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            throw new ServerNotActiveException("not in a remote call");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * Services messages on accepted connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    private class ConnectionHandler implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        /** int value of "POST" in ASCII (Java's specified data formats
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
         *  make this once-reviled tactic again socially acceptable) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        private static final int POST = 0x504f5354;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        /** most recently accept-authorized AccessControlContext */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        private AccessControlContext okContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        /** cache of accept-authorized AccessControlContexts */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        private Map<AccessControlContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                    Reference<AccessControlContext>> authCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        /** security manager which authorized contexts in authCache */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        private SecurityManager cacheSecurityManager = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        private Socket socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        private String remoteHost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        ConnectionHandler(Socket socket, String remoteHost) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            this.socket = socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            this.remoteHost = remoteHost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        String getClientHost() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            return remoteHost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
         * Verify that the given AccessControlContext has permission to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
         * accept this connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        void checkAcceptPermission(SecurityManager sm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                                   AccessControlContext acc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
             * Note: no need to synchronize on cache-related fields, since this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
             * method only gets called from the ConnectionHandler's thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            if (sm != cacheSecurityManager) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                okContext = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                authCache = new WeakHashMap<AccessControlContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                                            Reference<AccessControlContext>>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                cacheSecurityManager = sm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            if (acc.equals(okContext) || authCache.containsKey(acc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            InetAddress addr = socket.getInetAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            String host = (addr != null) ? addr.getHostAddress() : "*";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            sm.checkAccept(host, socket.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            authCache.put(acc, new SoftReference<AccessControlContext>(acc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            okContext = acc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        public void run() {
28557
ce28716935ce 8062807: Exporting RMI objects fails when run under restrictive SecurityManager
smarks
parents: 28552
diff changeset
   698
            Thread t = Thread.currentThread();
ce28716935ce 8062807: Exporting RMI objects fails when run under restrictive SecurityManager
smarks
parents: 28552
diff changeset
   699
            String name = t.getName();
ce28716935ce 8062807: Exporting RMI objects fails when run under restrictive SecurityManager
smarks
parents: 28552
diff changeset
   700
            try {
ce28716935ce 8062807: Exporting RMI objects fails when run under restrictive SecurityManager
smarks
parents: 28552
diff changeset
   701
                t.setName("RMI TCP Connection(" +
ce28716935ce 8062807: Exporting RMI objects fails when run under restrictive SecurityManager
smarks
parents: 28552
diff changeset
   702
                          connectionCount.incrementAndGet() +
ce28716935ce 8062807: Exporting RMI objects fails when run under restrictive SecurityManager
smarks
parents: 28552
diff changeset
   703
                          ")-" + remoteHost);
ce28716935ce 8062807: Exporting RMI objects fails when run under restrictive SecurityManager
smarks
parents: 28552
diff changeset
   704
                AccessController.doPrivileged((PrivilegedAction<Void>)() -> {
ce28716935ce 8062807: Exporting RMI objects fails when run under restrictive SecurityManager
smarks
parents: 28552
diff changeset
   705
                    run0();
28552
608626229264 8055309: RMI needs better transportation considerations
smarks
parents: 25859
diff changeset
   706
                    return null;
608626229264 8055309: RMI needs better transportation considerations
smarks
parents: 25859
diff changeset
   707
                }, NOPERMS_ACC);
28557
ce28716935ce 8062807: Exporting RMI objects fails when run under restrictive SecurityManager
smarks
parents: 28552
diff changeset
   708
            } finally {
ce28716935ce 8062807: Exporting RMI objects fails when run under restrictive SecurityManager
smarks
parents: 28552
diff changeset
   709
                t.setName(name);
ce28716935ce 8062807: Exporting RMI objects fails when run under restrictive SecurityManager
smarks
parents: 28552
diff changeset
   710
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
47032
98e444a1b204 8087189: RMI server-side multiplex protocol support should be removed
rriggs
parents: 41883
diff changeset
   713
        @SuppressWarnings("fallthrough")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        private void run0() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            TCPEndpoint endpoint = getEndpoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            int port = endpoint.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            threadConnectionHandler.set(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            // set socket to disable Nagle's algorithm (always send
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            // immediately)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            // TBD: should this be left up to socket factory instead?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                socket.setTcpNoDelay(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                // if we fail to set this, ignore and proceed anyway
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            // set socket to timeout after excessive idle time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                if (connectionReadTimeout > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                    socket.setSoTimeout(connectionReadTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                // too bad, continue anyway
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                InputStream sockIn = socket.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                InputStream bufIn = sockIn.markSupported()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                        ? sockIn
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                        : new BufferedInputStream(sockIn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
37892
55da13d60938 8155978: Remove HTTP proxy implementation and tests from RMI
rriggs
parents: 37667
diff changeset
   742
                // Read magic
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                DataInputStream in = new DataInputStream(bufIn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                int magic = in.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                // read and verify transport header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                short version = in.readShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                if (magic != TransportConstants.Magic ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                    version != TransportConstants.Version) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                    // protocol mismatch detected...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                    // just close socket: this would recurse if we marshal an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                    // exception to the client and the protocol at other end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                    // doesn't match.
59300
60bdcb9a7b94 8232446: logging enhancement for rmi when socket closed
mli
parents: 47216
diff changeset
   754
                    if (tcpLog.isLoggable(Log.BRIEF)) {
60bdcb9a7b94 8232446: logging enhancement for rmi when socket closed
mli
parents: 47216
diff changeset
   755
                        tcpLog.log(Log.BRIEF, "magic or version not match: "
60bdcb9a7b94 8232446: logging enhancement for rmi when socket closed
mli
parents: 47216
diff changeset
   756
                                                  + magic + ", " + version);
60bdcb9a7b94 8232446: logging enhancement for rmi when socket closed
mli
parents: 47216
diff changeset
   757
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                    closeSocket(socket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                OutputStream sockOut = socket.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                BufferedOutputStream bufOut =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                    new BufferedOutputStream(sockOut);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                DataOutputStream out = new DataOutputStream(bufOut);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                int remotePort = socket.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                if (tcpLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                    tcpLog.log(Log.BRIEF, "accepted socket from [" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                                     remoteHost + ":" + remotePort + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                TCPEndpoint ep;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                TCPChannel ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                TCPConnection conn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                // send ack (or nack) for protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                byte protocol = in.readByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                switch (protocol) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                case TransportConstants.SingleOpProtocol:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                    // no ack for protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                    // create dummy channel for receiving messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                    ep = new TCPEndpoint(remoteHost, socket.getLocalPort(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                                         endpoint.getClientSocketFactory(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                                         endpoint.getServerSocketFactory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                    ch = new TCPChannel(TCPTransport.this, ep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                    conn = new TCPConnection(ch, socket, bufIn, bufOut);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                    // read input messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                    handleMessages(conn, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                case TransportConstants.StreamProtocol:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                    // send ack
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                    out.writeByte(TransportConstants.ProtocolAck);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                    // suggest endpoint (in case client doesn't know host name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                    if (tcpLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                        tcpLog.log(Log.VERBOSE, "(port " + port +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                            ") " + "suggesting " + remoteHost + ":" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                            remotePort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                    out.writeUTF(remoteHost);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                    out.writeInt(remotePort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                    out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                    // read and discard (possibly bogus) endpoint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                    // REMIND: would be faster to read 2 bytes then skip N+4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                    String clientHost = in.readUTF();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                    int    clientPort = in.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                    if (tcpLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                        tcpLog.log(Log.VERBOSE, "(port " + port +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                            ") client using " + clientHost + ":" + clientPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                    // create dummy channel for receiving messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                    // (why not use clientHost and clientPort?)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                    ep = new TCPEndpoint(remoteHost, socket.getLocalPort(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                                         endpoint.getClientSocketFactory(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                                         endpoint.getServerSocketFactory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                    ch = new TCPChannel(TCPTransport.this, ep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                    conn = new TCPConnection(ch, socket, bufIn, bufOut);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                    // read input messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                    handleMessages(conn, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                case TransportConstants.MultiplexProtocol:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                    if (tcpLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                        tcpLog.log(Log.VERBOSE, "(port " + port +
47032
98e444a1b204 8087189: RMI server-side multiplex protocol support should be removed
rriggs
parents: 41883
diff changeset
   834
                                ") rejecting multiplex protocol");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                    }
47032
98e444a1b204 8087189: RMI server-side multiplex protocol support should be removed
rriggs
parents: 41883
diff changeset
   836
                    // Fall-through to reject use of MultiplexProtocol
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                    // protocol not understood, send nack and close socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                    out.writeByte(TransportConstants.ProtocolNack);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                    out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                // socket in unknown state: destroy socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                tcpLog.log(Log.BRIEF, "terminated with exception:", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                closeSocket(socket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
}