feat(jdk8): move files to new folder to avoid resources compiled.

This commit is contained in:
2025-09-07 15:25:52 +08:00
parent 3f0047bf6f
commit 8c35cfb1c0
17415 changed files with 217 additions and 213 deletions

View File

@@ -0,0 +1,447 @@
/*
* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport.tcp;
import java.io.*;
import java.util.*;
import java.rmi.server.LogStream;
import sun.rmi.runtime.Log;
/**
* ConnectionMultiplexer manages the transparent multiplexing of
* multiple virtual connections from one endpoint to another through
* one given real connection to that endpoint. The input and output
* streams for the the underlying real connection must be supplied.
* A callback object is also supplied to be informed of new virtual
* connections opened by the remote endpoint. After creation, the
* run() method must be called in a thread created for demultiplexing
* the connections. The openConnection() method is called to
* initiate a virtual connection from this endpoint.
*
* @author Peter Jones
*/
@SuppressWarnings("deprecation")
final class ConnectionMultiplexer {
/** "multiplex" log level */
static int logLevel = LogStream.parseLevel(getLogLevel());
private static String getLogLevel() {
return java.security.AccessController.doPrivileged(
new sun.security.action.GetPropertyAction("sun.rmi.transport.tcp.multiplex.logLevel"));
}
/* multiplex system log */
static final Log multiplexLog =
Log.getLog("sun.rmi.transport.tcp.multiplex",
"multiplex", ConnectionMultiplexer.logLevel);
/** multiplexing protocol operation codes */
private final static int OPEN = 0xE1;
private final static int CLOSE = 0xE2;
private final static int CLOSEACK = 0xE3;
private final static int REQUEST = 0xE4;
private final static int TRANSMIT = 0xE5;
/** object to notify for new connections from remote endpoint */
private TCPChannel channel;
/** input stream for underlying single connection */
private InputStream in;
/** output stream for underlying single connection */
private OutputStream out;
/** true if underlying connection originated from this endpoint
(used for generating unique connection IDs) */
private boolean orig;
/** layered stream for reading formatted data from underlying connection */
private DataInputStream dataIn;
/** layered stream for writing formatted data to underlying connection */
private DataOutputStream dataOut;
/** table holding currently open connection IDs and related info */
private Hashtable<Integer, MultiplexConnectionInfo> connectionTable = new Hashtable<>(7);
/** number of currently open connections */
private int numConnections = 0;
/** maximum allowed open connections */
private final static int maxConnections = 256;
/** ID of last connection opened */
private int lastID = 0x1001;
/** true if this mechanism is still alive */
private boolean alive = true;
/**
* Create a new ConnectionMultiplexer using the given underlying
* input/output stream pair. The run method must be called
* (possibly on a new thread) to handle the demultiplexing.
* @param channel object to notify when new connection is received
* @param in input stream of underlying connection
* @param out output stream of underlying connection
* @param orig true if this endpoint intiated the underlying
* connection (needs to be set differently at both ends)
*/
public ConnectionMultiplexer(
TCPChannel channel,
InputStream in,
OutputStream out,
boolean orig)
{
this.channel = channel;
this.in = in;
this.out = out;
this.orig = orig;
dataIn = new DataInputStream(in);
dataOut = new DataOutputStream(out);
}
/**
* Process multiplexing protocol received from underlying connection.
*/
public void run() throws IOException
{
try {
int op, id, length;
MultiplexConnectionInfo info;
while (true) {
// read next op code from remote endpoint
op = dataIn.readUnsignedByte();
switch (op) {
// remote endpoint initiating new connection
case OPEN:
id = dataIn.readUnsignedShort();
if (multiplexLog.isLoggable(Log.VERBOSE)) {
multiplexLog.log(Log.VERBOSE, "operation OPEN " + id);
}
info = connectionTable.get(id);
if (info != null)
throw new IOException(
"OPEN: Connection ID already exists");
info = new MultiplexConnectionInfo(id);
info.in = new MultiplexInputStream(this, info, 2048);
info.out = new MultiplexOutputStream(this, info, 2048);
synchronized (connectionTable) {
connectionTable.put(id, info);
++ numConnections;
}
sun.rmi.transport.Connection conn;
conn = new TCPConnection(channel, info.in, info.out);
channel.acceptMultiplexConnection(conn);
break;
// remote endpoint closing connection
case CLOSE:
id = dataIn.readUnsignedShort();
if (multiplexLog.isLoggable(Log.VERBOSE)) {
multiplexLog.log(Log.VERBOSE, "operation CLOSE " + id);
}
info = connectionTable.get(id);
if (info == null)
throw new IOException(
"CLOSE: Invalid connection ID");
info.in.disconnect();
info.out.disconnect();
if (!info.closed)
sendCloseAck(info);
synchronized (connectionTable) {
connectionTable.remove(id);
-- numConnections;
}
break;
// remote endpoint acknowledging close of connection
case CLOSEACK:
id = dataIn.readUnsignedShort();
if (multiplexLog.isLoggable(Log.VERBOSE)) {
multiplexLog.log(Log.VERBOSE,
"operation CLOSEACK " + id);
}
info = connectionTable.get(id);
if (info == null)
throw new IOException(
"CLOSEACK: Invalid connection ID");
if (!info.closed)
throw new IOException(
"CLOSEACK: Connection not closed");
info.in.disconnect();
info.out.disconnect();
synchronized (connectionTable) {
connectionTable.remove(id);
-- numConnections;
}
break;
// remote endpoint declaring additional bytes receivable
case REQUEST:
id = dataIn.readUnsignedShort();
info = connectionTable.get(id);
if (info == null)
throw new IOException(
"REQUEST: Invalid connection ID");
length = dataIn.readInt();
if (multiplexLog.isLoggable(Log.VERBOSE)) {
multiplexLog.log(Log.VERBOSE,
"operation REQUEST " + id + ": " + length);
}
info.out.request(length);
break;
// remote endpoint transmitting data packet
case TRANSMIT:
id = dataIn.readUnsignedShort();
info = connectionTable.get(id);
if (info == null)
throw new IOException("SEND: Invalid connection ID");
length = dataIn.readInt();
if (multiplexLog.isLoggable(Log.VERBOSE)) {
multiplexLog.log(Log.VERBOSE,
"operation TRANSMIT " + id + ": " + length);
}
info.in.receive(length, dataIn);
break;
default:
throw new IOException("Invalid operation: " +
Integer.toHexString(op));
}
}
} finally {
shutDown();
}
}
/**
* Initiate a new multiplexed connection through the underlying
* connection.
*/
public synchronized TCPConnection openConnection() throws IOException
{
// generate ID that should not be already used
// If all possible 32768 IDs are used,
// this method will block searching for a new ID forever.
int id;
do {
lastID = (++ lastID) & 0x7FFF;
id = lastID;
// The orig flag (copied to the high bit of the ID) is used
// to have two distinct ranges to choose IDs from for the
// two endpoints.
if (orig)
id |= 0x8000;
} while (connectionTable.get(id) != null);
// create multiplexing streams and bookkeeping information
MultiplexConnectionInfo info = new MultiplexConnectionInfo(id);
info.in = new MultiplexInputStream(this, info, 2048);
info.out = new MultiplexOutputStream(this, info, 2048);
// add to connection table if multiplexer has not died
synchronized (connectionTable) {
if (!alive)
throw new IOException("Multiplexer connection dead");
if (numConnections >= maxConnections)
throw new IOException("Cannot exceed " + maxConnections +
" simultaneous multiplexed connections");
connectionTable.put(id, info);
++ numConnections;
}
// inform remote endpoint of new connection
synchronized (dataOut) {
try {
dataOut.writeByte(OPEN);
dataOut.writeShort(id);
dataOut.flush();
} catch (IOException e) {
multiplexLog.log(Log.BRIEF, "exception: ", e);
shutDown();
throw e;
}
}
return new TCPConnection(channel, info.in, info.out);
}
/**
* Shut down all connections and clean up.
*/
public void shutDown()
{
// inform all associated streams
synchronized (connectionTable) {
// return if multiplexer already officially dead
if (!alive)
return;
alive = false;
Enumeration<MultiplexConnectionInfo> enum_ =
connectionTable.elements();
while (enum_.hasMoreElements()) {
MultiplexConnectionInfo info = enum_.nextElement();
info.in.disconnect();
info.out.disconnect();
}
connectionTable.clear();
numConnections = 0;
}
// close underlying connection, if possible (and not already done)
try {
in.close();
} catch (IOException e) {
}
try {
out.close();
} catch (IOException e) {
}
}
/**
* Send request for more data on connection to remote endpoint.
* @param info connection information structure
* @param len number of more bytes that can be received
*/
void sendRequest(MultiplexConnectionInfo info, int len) throws IOException
{
synchronized (dataOut) {
if (alive && !info.closed)
try {
dataOut.writeByte(REQUEST);
dataOut.writeShort(info.id);
dataOut.writeInt(len);
dataOut.flush();
} catch (IOException e) {
multiplexLog.log(Log.BRIEF, "exception: ", e);
shutDown();
throw e;
}
}
}
/**
* Send packet of requested data on connection to remote endpoint.
* @param info connection information structure
* @param buf array containing bytes to send
* @param off offset of first array index of packet
* @param len number of bytes in packet to send
*/
void sendTransmit(MultiplexConnectionInfo info,
byte buf[], int off, int len) throws IOException
{
synchronized (dataOut) {
if (alive && !info.closed)
try {
dataOut.writeByte(TRANSMIT);
dataOut.writeShort(info.id);
dataOut.writeInt(len);
dataOut.write(buf, off, len);
dataOut.flush();
} catch (IOException e) {
multiplexLog.log(Log.BRIEF, "exception: ", e);
shutDown();
throw e;
}
}
}
/**
* Inform remote endpoint that connection has been closed.
* @param info connection information structure
*/
void sendClose(MultiplexConnectionInfo info) throws IOException
{
info.out.disconnect();
synchronized (dataOut) {
if (alive && !info.closed)
try {
dataOut.writeByte(CLOSE);
dataOut.writeShort(info.id);
dataOut.flush();
info.closed = true;
} catch (IOException e) {
multiplexLog.log(Log.BRIEF, "exception: ", e);
shutDown();
throw e;
}
}
}
/**
* Acknowledge remote endpoint's closing of connection.
* @param info connection information structure
*/
void sendCloseAck(MultiplexConnectionInfo info) throws IOException
{
synchronized (dataOut) {
if (alive && !info.closed)
try {
dataOut.writeByte(CLOSEACK);
dataOut.writeShort(info.id);
dataOut.flush();
info.closed = true;
} catch (IOException e) {
multiplexLog.log(Log.BRIEF, "exception: ", e);
shutDown();
throw e;
}
}
}
/**
* Shut down connection upon finalization.
*/
protected void finalize() throws Throwable
{
super.finalize();
shutDown();
}
}

View File

@@ -0,0 +1,55 @@
/*
* Copyright (c) 1996, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport.tcp;
/**
* MultiplexConnectionInfo groups related information about a
* virtual connection managed by a ConnectionMultiplexer object.
*
* @author Peter Jones
*/
class MultiplexConnectionInfo {
/** integer that uniquely identifies this connection */
int id;
/** input stream for reading from connection */
MultiplexInputStream in = null;
/** output stream for writing to connection */
MultiplexOutputStream out = null;
/** true if this connection has been closed */
boolean closed = false;
/**
* Create information structure for given connection identifier.
* @param id connection identifier
*/
MultiplexConnectionInfo(int id)
{
this.id = id;
}
}

View File

@@ -0,0 +1,213 @@
/*
* Copyright (c) 1996, 1997, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport.tcp;
import java.io.*;
/**
* MultiplexInputStream manages receiving data over a connection managed
* by a ConnectionMultiplexer object. This object is responsible for
* requesting more bytes of data as space in its internal buffer becomes
* available.
*
* @author Peter Jones
*/
final class MultiplexInputStream extends InputStream {
/** object managing multiplexed connection */
private ConnectionMultiplexer manager;
/** information about the connection this is the input stream for */
private MultiplexConnectionInfo info;
/** input buffer */
private byte buffer[];
/** number of real data bytes present in buffer */
private int present = 0;
/** current position to read from in input buffer */
private int pos = 0;
/** pending number of bytes this stream has requested */
private int requested = 0;
/** true if this connection has been disconnected */
private boolean disconnected = false;
/**
* lock acquired to access shared variables:
* buffer, present, pos, requested, & disconnected
* WARNING: Any of the methods manager.send*() should not be
* invoked while this lock is held, since they could potentially
* block if the underlying connection's transport buffers are
* full, and the manager may need to acquire this lock to process
* and consume data coming over the underlying connection.
*/
private Object lock = new Object();
/** level at which more data is requested when read past */
private int waterMark;
/** data structure for holding reads of one byte */
private byte temp[] = new byte[1];
/**
* Create a new MultiplexInputStream for the given manager.
* @param manager object that manages this connection
* @param info structure for connection this stream reads from
* @param bufferLength length of input buffer
*/
MultiplexInputStream(
ConnectionMultiplexer manager,
MultiplexConnectionInfo info,
int bufferLength)
{
this.manager = manager;
this.info = info;
buffer = new byte[bufferLength];
waterMark = bufferLength / 2;
}
/**
* Read a byte from the connection.
*/
public synchronized int read() throws IOException
{
int n = read(temp, 0, 1);
if (n != 1)
return -1;
return temp[0] & 0xFF;
}
/**
* Read a subarray of bytes from connection. This method blocks for
* at least one byte, and it returns the number of bytes actually read,
* or -1 if the end of the stream was detected.
* @param b array to read bytes into
* @param off offset of beginning of bytes to read into
* @param len number of bytes to read
*/
public synchronized int read(byte b[], int off, int len) throws IOException
{
if (len <= 0)
return 0;
int moreSpace;
synchronized (lock) {
if (pos >= present)
pos = present = 0;
else if (pos >= waterMark) {
System.arraycopy(buffer, pos, buffer, 0, present - pos);
present -= pos;
pos = 0;
}
int freeSpace = buffer.length - present;
moreSpace = Math.max(freeSpace - requested, 0);
}
if (moreSpace > 0)
manager.sendRequest(info, moreSpace);
synchronized (lock) {
requested += moreSpace;
while ((pos >= present) && !disconnected) {
try {
lock.wait();
} catch (InterruptedException e) {
}
}
if (disconnected && pos >= present)
return -1;
int available = present - pos;
if (len < available) {
System.arraycopy(buffer, pos, b, off, len);
pos += len;
return len;
}
else {
System.arraycopy(buffer, pos, b, off, available);
pos = present = 0;
// could send another request here, if len > available??
return available;
}
}
}
/**
* Return the number of bytes immediately available for reading.
*/
public int available() throws IOException
{
synchronized (lock) {
return present - pos;
}
}
/**
* Close this connection.
*/
public void close() throws IOException
{
manager.sendClose(info);
}
/**
* Receive bytes transmitted from connection at remote endpoint.
* @param length number of bytes transmitted
* @param in input stream with those bytes ready to be read
*/
void receive(int length, DataInputStream in)
throws IOException
{
/* TO DO: Optimize so that data received from stream can be loaded
* directly into user's buffer if there is a pending read().
*/
synchronized (lock) {
if ((pos > 0) && ((buffer.length - present) < length)) {
System.arraycopy(buffer, pos, buffer, 0, present - pos);
present -= pos;
pos = 0;
}
if ((buffer.length - present) < length)
throw new IOException("Receive buffer overflow");
in.readFully(buffer, present, length);
present += length;
requested -= length;
lock.notifyAll();
}
}
/**
* Disconnect this stream from all connection activity.
*/
void disconnect()
{
synchronized (lock) {
disconnected = true;
lock.notifyAll();
}
}
}

View File

@@ -0,0 +1,231 @@
/*
* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport.tcp;
import java.io.*;
/**
* MultiplexOutputStream manages sending data over a connection managed
* by a ConnectionMultiplexer object. Data written is buffered until the
* internal buffer is full or the flush() method is called, at which
* point it attempts to push a packet of bytes through to the remote
* endpoint. This will never push more bytes than the amount already
* requested by the remote endpoint (to prevent receive buffer from
* overflowing), so if the write() and flush() methods will block
* until their operation can complete if enough bytes cannot be
* pushed immediately.
*
* @author Peter Jones
*/
final class MultiplexOutputStream extends OutputStream {
/** object managing multiplexed connection */
private ConnectionMultiplexer manager;
/** information about the connection this is the output stream for */
private MultiplexConnectionInfo info;
/** output buffer */
private byte buffer[];
/** current position to write to in output buffer */
private int pos = 0;
/** pending number of bytes requested by remote endpoint */
private int requested = 0;
/** true if this connection has been disconnected */
private boolean disconnected = false;
/**
* lock acquired to access shared variables:
* requested & disconnected
* WARNING: Any of the methods manager.send*() should not be
* invoked while this lock is held, since they could potentially
* block if the underlying connection's transport buffers are
* full, and the manager may need to acquire this lock to process
* and consume data coming over the underlying connection.
*/
private Object lock = new Object();
/**
* Create a new MultiplexOutputStream for the given manager.
* @param manager object that manages this connection
* @param info structure for connection this stream writes to
* @param bufferLength length of output buffer
*/
MultiplexOutputStream(
ConnectionMultiplexer manager,
MultiplexConnectionInfo info,
int bufferLength)
{
this.manager = manager;
this.info = info;
buffer = new byte[bufferLength];
pos = 0;
}
/**
* Write a byte over connection.
* @param b byte of data to write
*/
public synchronized void write(int b) throws IOException
{
while (pos >= buffer.length)
push();
buffer[pos ++] = (byte) b;
}
/**
* Write a subarray of bytes over connection.
* @param b array containing bytes to write
* @param off offset of beginning of bytes to write
* @param len number of bytes to write
*/
public synchronized void write(byte b[], int off, int len)
throws IOException
{
if (len <= 0)
return;
// if enough free space in output buffer, just copy into there
int freeSpace = buffer.length - pos;
if (len <= freeSpace) {
System.arraycopy(b, off, buffer, pos, len);
pos += len;
return;
}
// else, flush buffer and send rest directly to avoid array copy
flush();
int local_requested;
while (true) {
synchronized (lock) {
while ((local_requested = requested) < 1 && !disconnected) {
try {
lock.wait();
} catch (InterruptedException e) {
}
}
if (disconnected)
throw new IOException("Connection closed");
}
if (local_requested < len) {
manager.sendTransmit(info, b, off, local_requested);
off += local_requested;
len -= local_requested;
synchronized (lock) {
requested -= local_requested;
}
}
else {
manager.sendTransmit(info, b, off, len);
synchronized (lock) {
requested -= len;
}
// len = 0;
break;
}
}
}
/**
* Guarantee that all data written to this stream has been pushed
* over and made available to the remote endpoint.
*/
public synchronized void flush() throws IOException {
while (pos > 0)
push();
}
/**
* Close this connection.
*/
public void close() throws IOException
{
manager.sendClose(info);
}
/**
* Take note of more bytes requested by connection at remote endpoint.
* @param num number of additional bytes requested
*/
void request(int num)
{
synchronized (lock) {
requested += num;
lock.notifyAll();
}
}
/**
* Disconnect this stream from all connection activity.
*/
void disconnect()
{
synchronized (lock) {
disconnected = true;
lock.notifyAll();
}
}
/**
* Push bytes in output buffer to connection at remote endpoint.
* This method blocks until at least one byte has been pushed across.
*/
private void push() throws IOException
{
int local_requested;
synchronized (lock) {
while ((local_requested = requested) < 1 && !disconnected) {
try {
lock.wait();
} catch (InterruptedException e) {
}
}
if (disconnected)
throw new IOException("Connection closed");
}
if (local_requested < pos) {
manager.sendTransmit(info, buffer, 0, local_requested);
System.arraycopy(buffer, local_requested,
buffer, 0, pos - local_requested);
pos -= local_requested;
synchronized (lock) {
requested -= local_requested;
}
}
else {
manager.sendTransmit(info, buffer, 0, pos);
synchronized (lock) {
requested -= pos;
}
pos = 0;
}
}
}

View File

@@ -0,0 +1,540 @@
/*
* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport.tcp;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
import java.net.Socket;
import java.rmi.ConnectIOException;
import java.rmi.RemoteException;
import java.security.AccessControlContext;
import java.security.AccessController;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.WeakHashMap;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import sun.rmi.runtime.Log;
import sun.rmi.runtime.NewThreadAction;
import sun.rmi.runtime.RuntimeUtil;
import sun.rmi.transport.Channel;
import sun.rmi.transport.Connection;
import sun.rmi.transport.Endpoint;
import sun.rmi.transport.TransportConstants;
import sun.security.action.GetIntegerAction;
import sun.security.action.GetLongAction;
/**
* TCPChannel is the socket-based implementation of the RMI Channel
* abstraction.
*
* @author Ann Wollrath
*/
public class TCPChannel implements Channel {
/** endpoint for this channel */
private final TCPEndpoint ep;
/** transport for this channel */
private final TCPTransport tr;
/** list of cached connections */
private final List<TCPConnection> freeList =
new ArrayList<>();
/** frees cached connections that have expired (guarded by freeList) */
private Future<?> reaper = null;
/** using multiplexer (for bi-directional applet communication */
private boolean usingMultiplexer = false;
/** connection multiplexer, if used */
private ConnectionMultiplexer multiplexer = null;
/** connection acceptor (should be in TCPTransport) */
private ConnectionAcceptor acceptor;
/** most recently authorized AccessControlContext */
private AccessControlContext okContext;
/** cache of authorized AccessControlContexts */
private WeakHashMap<AccessControlContext,
Reference<AccessControlContext>> authcache;
/** the SecurityManager which authorized okContext and authcache */
private SecurityManager cacheSecurityManager = null;
/** client-side connection idle usage timeout */
private static final long idleTimeout = // default 15 seconds
AccessController.doPrivileged(
new GetLongAction("sun.rmi.transport.connectionTimeout", 15000));
/** client-side connection handshake read timeout */
private static final int handshakeTimeout = // default 1 minute
AccessController.doPrivileged(
new GetIntegerAction("sun.rmi.transport.tcp.handshakeTimeout",
60000));
/** client-side connection response read timeout (after handshake) */
private static final int responseTimeout = // default infinity
AccessController.doPrivileged(
new GetIntegerAction("sun.rmi.transport.tcp.responseTimeout", 0));
/** thread pool for scheduling delayed tasks */
private static final ScheduledExecutorService scheduler =
AccessController.doPrivileged(
new RuntimeUtil.GetInstanceAction()).getScheduler();
/**
* Create channel for endpoint.
*/
TCPChannel(TCPTransport tr, TCPEndpoint ep) {
this.tr = tr;
this.ep = ep;
}
/**
* Return the endpoint for this channel.
*/
public Endpoint getEndpoint() {
return ep;
}
/**
* Checks if the current caller has sufficient privilege to make
* a connection to the remote endpoint.
* @exception SecurityException if caller is not allowed to use this
* Channel.
*/
private void checkConnectPermission() throws SecurityException {
SecurityManager security = System.getSecurityManager();
if (security == null)
return;
if (security != cacheSecurityManager) {
// The security manager changed: flush the cache
okContext = null;
authcache = new WeakHashMap<AccessControlContext,
Reference<AccessControlContext>>();
cacheSecurityManager = security;
}
AccessControlContext ctx = AccessController.getContext();
// If ctx is the same context as last time, or if it
// appears in the cache, bypass the checkConnect.
if (okContext == null ||
!(okContext.equals(ctx) || authcache.containsKey(ctx)))
{
security.checkConnect(ep.getHost(), ep.getPort());
authcache.put(ctx, new SoftReference<AccessControlContext>(ctx));
// A WeakHashMap is transformed into a SoftHashSet by making
// each value softly refer to its own key (Peter's idea).
}
okContext = ctx;
}
/**
* Supplies a connection to the endpoint of the address space
* for which this is a channel. The returned connection may
* be one retrieved from a cache of idle connections.
*/
public Connection newConnection() throws RemoteException {
TCPConnection conn;
// loop until we find a free live connection (in which case
// we return) or until we run out of freelist (in which case
// the loop exits)
do {
conn = null;
// try to get a free connection
synchronized (freeList) {
int elementPos = freeList.size()-1;
if (elementPos >= 0) {
// If there is a security manager, make sure
// the caller is allowed to connect to the
// requested endpoint.
checkConnectPermission();
conn = freeList.get(elementPos);
freeList.remove(elementPos);
}
}
// at this point, conn is null iff the freelist is empty,
// and nonnull if a free connection of uncertain vitality
// has been found.
if (conn != null) {
// check to see if the connection has closed since last use
if (!conn.isDead()) {
TCPTransport.tcpLog.log(Log.BRIEF, "reuse connection");
return conn;
}
// conn is dead, and cannot be reused (reuse => false)
this.free(conn, false);
}
} while (conn != null);
// none free, so create a new connection
return (createConnection());
}
/**
* Create a new connection to the remote endpoint of this channel.
* The returned connection is new. The caller must already have
* passed a security checkConnect or equivalent.
*/
private Connection createConnection() throws RemoteException {
Connection conn;
TCPTransport.tcpLog.log(Log.BRIEF, "create connection");
if (!usingMultiplexer) {
Socket sock = ep.newSocket();
conn = new TCPConnection(this, sock);
try {
DataOutputStream out =
new DataOutputStream(conn.getOutputStream());
writeTransportHeader(out);
// choose protocol (single op if not reusable socket)
if (!conn.isReusable()) {
out.writeByte(TransportConstants.SingleOpProtocol);
} else {
out.writeByte(TransportConstants.StreamProtocol);
out.flush();
/*
* Set socket read timeout to configured value for JRMP
* connection handshake; this also serves to guard against
* non-JRMP servers that do not respond (see 4322806).
*/
int originalSoTimeout = 0;
try {
originalSoTimeout = sock.getSoTimeout();
sock.setSoTimeout(handshakeTimeout);
} catch (Exception e) {
// if we fail to set this, ignore and proceed anyway
}
DataInputStream in =
new DataInputStream(conn.getInputStream());
byte ack = in.readByte();
if (ack != TransportConstants.ProtocolAck) {
throw new ConnectIOException(
ack == TransportConstants.ProtocolNack ?
"JRMP StreamProtocol not supported by server" :
"non-JRMP server at remote endpoint");
}
String suggestedHost = in.readUTF();
int suggestedPort = in.readInt();
if (TCPTransport.tcpLog.isLoggable(Log.VERBOSE)) {
TCPTransport.tcpLog.log(Log.VERBOSE,
"server suggested " + suggestedHost + ":" +
suggestedPort);
}
// set local host name, if unknown
TCPEndpoint.setLocalHost(suggestedHost);
// do NOT set the default port, because we don't
// know if we can't listen YET...
// write out default endpoint to match protocol
// (but it serves no purpose)
TCPEndpoint localEp =
TCPEndpoint.getLocalEndpoint(0, null, null);
out.writeUTF(localEp.getHost());
out.writeInt(localEp.getPort());
if (TCPTransport.tcpLog.isLoggable(Log.VERBOSE)) {
TCPTransport.tcpLog.log(Log.VERBOSE, "using " +
localEp.getHost() + ":" + localEp.getPort());
}
/*
* After JRMP handshake, set socket read timeout to value
* configured for the rest of the lifetime of the
* connection. NOTE: this timeout, if configured to a
* finite duration, places an upper bound on the time
* that a remote method call is permitted to execute.
*/
try {
/*
* If socket factory had set a non-zero timeout on its
* own, then restore it instead of using the property-
* configured value.
*/
sock.setSoTimeout((originalSoTimeout != 0 ?
originalSoTimeout :
responseTimeout));
} catch (Exception e) {
// if we fail to set this, ignore and proceed anyway
}
out.flush();
}
} catch (IOException e) {
try {
conn.close();
} catch (Exception ex) {}
if (e instanceof RemoteException) {
throw (RemoteException) e;
} else {
throw new ConnectIOException(
"error during JRMP connection establishment", e);
}
}
} else {
try {
conn = multiplexer.openConnection();
} catch (IOException e) {
synchronized (this) {
usingMultiplexer = false;
multiplexer = null;
}
throw new ConnectIOException(
"error opening virtual connection " +
"over multiplexed connection", e);
}
}
return conn;
}
/**
* Free the connection generated by this channel.
* @param conn The connection
* @param reuse If true, the connection is in a state in which it
* can be reused for another method call.
*/
public void free(Connection conn, boolean reuse) {
if (conn == null) return;
if (reuse && conn.isReusable()) {
long lastuse = System.currentTimeMillis();
TCPConnection tcpConnection = (TCPConnection) conn;
TCPTransport.tcpLog.log(Log.BRIEF, "reuse connection");
/*
* Cache connection; if reaper task for expired
* connections isn't scheduled, then schedule it.
*/
synchronized (freeList) {
freeList.add(tcpConnection);
if (reaper == null) {
TCPTransport.tcpLog.log(Log.BRIEF, "create reaper");
reaper = scheduler.scheduleWithFixedDelay(
new Runnable() {
public void run() {
TCPTransport.tcpLog.log(Log.VERBOSE,
"wake up");
freeCachedConnections();
}
}, idleTimeout, idleTimeout, TimeUnit.MILLISECONDS);
}
}
tcpConnection.setLastUseTime(lastuse);
tcpConnection.setExpiration(lastuse + idleTimeout);
} else {
TCPTransport.tcpLog.log(Log.BRIEF, "close connection");
try {
conn.close();
} catch (IOException ignored) {
}
}
}
/**
* Send transport header over stream.
*/
private void writeTransportHeader(DataOutputStream out)
throws RemoteException
{
try {
// write out transport header
DataOutputStream dataOut =
new DataOutputStream(out);
dataOut.writeInt(TransportConstants.Magic);
dataOut.writeShort(TransportConstants.Version);
} catch (IOException e) {
throw new ConnectIOException(
"error writing JRMP transport header", e);
}
}
/**
* Use given connection multiplexer object to obtain new connections
* through this channel.
*/
synchronized void useMultiplexer(ConnectionMultiplexer newMultiplexer) {
// for now, always just use the last one given
multiplexer = newMultiplexer;
usingMultiplexer = true;
}
/**
* Accept a connection provided over a multiplexed channel.
*/
void acceptMultiplexConnection(Connection conn) {
if (acceptor == null) {
acceptor = new ConnectionAcceptor(tr);
acceptor.startNewAcceptor();
}
acceptor.accept(conn);
}
/**
* Closes all the connections in the cache, whether timed out or not.
*/
public void shedCache() {
// Build a list of connections, to avoid holding the freeList
// lock during (potentially long-running) close() calls.
Connection[] conn;
synchronized (freeList) {
conn = freeList.toArray(new Connection[freeList.size()]);
freeList.clear();
}
// Close all the connections that were free
for (int i = conn.length; --i >= 0; ) {
Connection c = conn[i];
conn[i] = null; // help gc
try {
c.close();
} catch (java.io.IOException e) {
// eat exception
}
}
}
private void freeCachedConnections() {
/*
* Remove each connection whose time out has expired.
*/
synchronized (freeList) {
int size = freeList.size();
if (size > 0) {
long time = System.currentTimeMillis();
ListIterator<TCPConnection> iter = freeList.listIterator(size);
while (iter.hasPrevious()) {
TCPConnection conn = iter.previous();
if (conn.expired(time)) {
TCPTransport.tcpLog.log(Log.VERBOSE,
"connection timeout expired");
try {
conn.close();
} catch (java.io.IOException e) {
// eat exception
}
iter.remove();
}
}
}
if (freeList.isEmpty()) {
reaper.cancel(false);
reaper = null;
}
}
}
}
/**
* ConnectionAcceptor manages accepting new connections and giving them
* to TCPTransport's message handler on new threads.
*
* Since this object only needs to know which transport to give new
* connections to, it doesn't need to be per-channel as currently
* implemented.
*/
class ConnectionAcceptor implements Runnable {
/** transport that will handle message on accepted connections */
private TCPTransport transport;
/** queue of connections to be accepted */
private List<Connection> queue = new ArrayList<>();
/** thread ID counter */
private static int threadNum = 0;
/**
* Create a new ConnectionAcceptor that will give connections
* to the specified transport on a new thread.
*/
public ConnectionAcceptor(TCPTransport transport) {
this.transport = transport;
}
/**
* Start a new thread to accept connections.
*/
public void startNewAcceptor() {
Thread t = AccessController.doPrivileged(
new NewThreadAction(ConnectionAcceptor.this,
"Multiplex Accept-" + ++ threadNum,
true));
t.start();
}
/**
* Add connection to queue of connections to be accepted.
*/
public void accept(Connection conn) {
synchronized (queue) {
queue.add(conn);
queue.notify();
}
}
/**
* Give transport next accepted connection, when available.
*/
public void run() {
Connection conn;
synchronized (queue) {
while (queue.size() == 0) {
try {
queue.wait();
} catch (InterruptedException e) {
}
}
startNewAcceptor();
conn = queue.remove(0);
}
transport.handleMessages(conn, true);
}
}

View File

@@ -0,0 +1,236 @@
/*
* Copyright (c) 1996, 2001, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport.tcp;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
import java.rmi.*;
import java.rmi.server.RMISocketFactory;
import sun.rmi.runtime.Log;
import sun.rmi.transport.*;
import sun.rmi.transport.proxy.*;
public class TCPConnection implements Connection {
private Socket socket;
private Channel channel;
private InputStream in = null;
private OutputStream out = null;
private long expiration = Long.MAX_VALUE;
private long lastuse = Long.MIN_VALUE;
private long roundtrip = 5; // round-trip time for ping
/**
* Constructor used for creating a connection to accept call
* (an input connection)
*/
TCPConnection(TCPChannel ch, Socket s, InputStream in, OutputStream out)
{
socket = s;
channel = ch;
this.in = in;
this.out = out;
}
/**
* Constructor used by subclass when underlying input and output streams
* are already available.
*/
TCPConnection(TCPChannel ch, InputStream in, OutputStream out)
{
this(ch, null, in, out);
}
/**
* Constructor used when socket is available, but not underlying
* streams.
*/
TCPConnection(TCPChannel ch, Socket s)
{
this(ch, s, null, null);
}
/**
* Gets the output stream for this connection
*/
public OutputStream getOutputStream() throws IOException
{
if (out == null)
out = new BufferedOutputStream(socket.getOutputStream());
return out;
}
/**
* Release the output stream for this connection.
*/
public void releaseOutputStream() throws IOException
{
if (out != null)
out.flush();
}
/**
* Gets the input stream for this connection.
*/
public InputStream getInputStream() throws IOException
{
if (in == null)
in = new BufferedInputStream(socket.getInputStream());
return in;
}
/**
* Release the input stream for this connection.
*/
public void releaseInputStream()
{
}
/**
* Determine if this connection can be used for multiple operations.
* If the socket implements RMISocketInfo, then we can query it about
* this; otherwise, assume that it does provide a full-duplex
* persistent connection like java.net.Socket.
*/
public boolean isReusable()
{
if ((socket != null) && (socket instanceof RMISocketInfo))
return ((RMISocketInfo) socket).isReusable();
else
return true;
}
/**
* Set the expiration time of this connection.
* @param time The time at which the time out expires.
*/
void setExpiration(long time)
{
expiration = time;
}
/**
* Set the timestamp at which this connection was last used successfully.
* The connection will be pinged for liveness if reused long after
* this time.
* @param time The time at which the connection was last active.
*/
void setLastUseTime(long time)
{
lastuse = time;
}
/**
* Returns true if the timeout has expired on this connection;
* otherwise returns false.
* @param time The current time.
*/
boolean expired(long time)
{
return expiration <= time;
}
/**
* Probes the connection to see if it still alive and connected to
* a responsive server. If the connection has been idle for too
* long, the server is pinged. ``Too long'' means ``longer than the
* last ping round-trip time''.
* <P>
* This method may misdiagnose a dead connection as live, but it
* will never misdiagnose a live connection as dead.
* @return true if the connection and server are recently alive
*/
public boolean isDead()
{
InputStream i;
OutputStream o;
// skip ping if recently used within 1 RTT
long start = System.currentTimeMillis();
if ((roundtrip > 0) && (start < lastuse + roundtrip))
return (false); // still alive and warm
// Get the streams
try {
i = getInputStream();
o = getOutputStream();
} catch (IOException e) {
return (true); // can't even get a stream, must be very dead
}
// Write the ping byte and read the reply byte
int response = 0;
try {
o.write(TransportConstants.Ping);
o.flush();
response = i.read();
} catch (IOException ex) {
TCPTransport.tcpLog.log(Log.VERBOSE, "exception: ", ex);
TCPTransport.tcpLog.log(Log.BRIEF, "server ping failed");
return (true); // server failed the ping test
}
if (response == TransportConstants.PingAck) {
// save most recent RTT for future use
roundtrip = (System.currentTimeMillis() - start) * 2;
// clock-correction may make roundtrip < 0; doesn't matter
return (false); // it's alive and 5-by-5
}
if (TCPTransport.tcpLog.isLoggable(Log.BRIEF)) {
TCPTransport.tcpLog.log(Log.BRIEF,
(response == -1 ? "server has been deactivated" :
"server protocol error: ping response = " + response));
}
return (true);
}
/**
* Close the connection. */
public void close() throws IOException
{
TCPTransport.tcpLog.log(Log.BRIEF, "close connection");
if (socket != null)
socket.close();
else {
in.close();
out.close();
}
}
/**
* Returns the channel for this connection.
*/
public Channel getChannel()
{
return channel;
}
}

View File

@@ -0,0 +1,790 @@
/*
* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport.tcp;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.lang.reflect.Proxy;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.rmi.ConnectIOException;
import java.rmi.RemoteException;
import java.rmi.server.RMIClientSocketFactory;
import java.rmi.server.RMIServerSocketFactory;
import java.rmi.server.RMISocketFactory;
import java.security.AccessController;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
import sun.rmi.runtime.Log;
import sun.rmi.runtime.NewThreadAction;
import sun.rmi.transport.Channel;
import sun.rmi.transport.Endpoint;
import sun.rmi.transport.Target;
import sun.rmi.transport.Transport;
import sun.security.action.GetBooleanAction;
import sun.security.action.GetIntegerAction;
import sun.security.action.GetPropertyAction;
/**
* TCPEndpoint represents some communication endpoint for an address
* space (VM).
*
* @author Ann Wollrath
*/
public class TCPEndpoint implements Endpoint {
/** IP address or host name */
private String host;
/** port number */
private int port;
/** custom client socket factory (null if not custom factory) */
private final RMIClientSocketFactory csf;
/** custom server socket factory (null if not custom factory) */
private final RMIServerSocketFactory ssf;
/** if local, the port number to listen on */
private int listenPort = -1;
/** if local, the transport object associated with this endpoint */
private TCPTransport transport = null;
/** the local host name */
private static String localHost;
/** true if real local host name is known yet */
private static boolean localHostKnown;
// this should be a *private* method since it is privileged
private static int getInt(String name, int def) {
return AccessController.doPrivileged(new GetIntegerAction(name, def));
}
// this should be a *private* method since it is privileged
private static boolean getBoolean(String name) {
return AccessController.doPrivileged(new GetBooleanAction(name));
}
/**
* Returns the value of the java.rmi.server.hostname property.
*/
private static String getHostnameProperty() {
return AccessController.doPrivileged(
new GetPropertyAction("java.rmi.server.hostname"));
}
/**
* Find host name of local machine. Property "java.rmi.server.hostname"
* is used if set, so server administrator can compensate for the possible
* inablility to get fully qualified host name from VM.
*/
static {
localHostKnown = true;
localHost = getHostnameProperty();
// could try querying CGI program here?
if (localHost == null) {
try {
InetAddress localAddr = InetAddress.getLocalHost();
byte[] raw = localAddr.getAddress();
if ((raw[0] == 127) &&
(raw[1] == 0) &&
(raw[2] == 0) &&
(raw[3] == 1)) {
localHostKnown = false;
}
/* if the user wishes to use a fully qualified domain
* name then attempt to find one.
*/
if (getBoolean("java.rmi.server.useLocalHostName")) {
localHost = FQDN.attemptFQDN(localAddr);
} else {
/* default to using ip addresses, names will
* work across seperate domains.
*/
localHost = localAddr.getHostAddress();
}
} catch (Exception e) {
localHostKnown = false;
localHost = null;
}
}
if (TCPTransport.tcpLog.isLoggable(Log.BRIEF)) {
TCPTransport.tcpLog.log(Log.BRIEF,
"localHostKnown = " + localHostKnown +
", localHost = " + localHost);
}
}
/** maps an endpoint key containing custom socket factories to
* their own unique endpoint */
// TBD: should this be a weak hash table?
private static final
Map<TCPEndpoint,LinkedList<TCPEndpoint>> localEndpoints =
new HashMap<>();
/**
* Create an endpoint for a specified host and port.
* This should not be used by external classes to create endpoints
* for servers in this VM; use getLocalEndpoint instead.
*/
public TCPEndpoint(String host, int port) {
this(host, port, null, null);
}
/**
* Create a custom socket factory endpoint for a specified host and port.
* This should not be used by external classes to create endpoints
* for servers in this VM; use getLocalEndpoint instead.
*/
public TCPEndpoint(String host, int port, RMIClientSocketFactory csf,
RMIServerSocketFactory ssf)
{
if (host == null)
host = "";
this.host = host;
this.port = port;
this.csf = csf;
this.ssf = ssf;
}
/**
* Get an endpoint for the local address space on specified port.
* If port number is 0, it returns shared default endpoint object
* whose host name and port may or may not have been determined.
*/
public static TCPEndpoint getLocalEndpoint(int port) {
return getLocalEndpoint(port, null, null);
}
public static TCPEndpoint getLocalEndpoint(int port,
RMIClientSocketFactory csf,
RMIServerSocketFactory ssf)
{
/*
* Find mapping for an endpoint key to the list of local unique
* endpoints for this client/server socket factory pair (perhaps
* null) for the specific port.
*/
TCPEndpoint ep = null;
synchronized (localEndpoints) {
TCPEndpoint endpointKey = new TCPEndpoint(null, port, csf, ssf);
LinkedList<TCPEndpoint> epList = localEndpoints.get(endpointKey);
String localHost = resampleLocalHost();
if (epList == null) {
/*
* Create new endpoint list.
*/
ep = new TCPEndpoint(localHost, port, csf, ssf);
epList = new LinkedList<TCPEndpoint>();
epList.add(ep);
ep.listenPort = port;
ep.transport = new TCPTransport(epList);
localEndpoints.put(endpointKey, epList);
if (TCPTransport.tcpLog.isLoggable(Log.BRIEF)) {
TCPTransport.tcpLog.log(Log.BRIEF,
"created local endpoint for socket factory " + ssf +
" on port " + port);
}
} else {
synchronized (epList) {
ep = epList.getLast();
String lastHost = ep.host;
int lastPort = ep.port;
TCPTransport lastTransport = ep.transport;
// assert (localHost == null ^ lastHost != null)
if (localHost != null && !localHost.equals(lastHost)) {
/*
* Hostname has been updated; add updated endpoint
* to list.
*/
if (lastPort != 0) {
/*
* Remove outdated endpoints only if the
* port has already been set on those endpoints.
*/
epList.clear();
}
ep = new TCPEndpoint(localHost, lastPort, csf, ssf);
ep.listenPort = port;
ep.transport = lastTransport;
epList.add(ep);
}
}
}
}
return ep;
}
/**
* Resamples the local hostname and returns the possibly-updated
* local hostname.
*/
private static String resampleLocalHost() {
String hostnameProperty = getHostnameProperty();
synchronized (localEndpoints) {
// assert(localHostKnown ^ (localHost == null))
if (hostnameProperty != null) {
if (!localHostKnown) {
/*
* If the local hostname is unknown, update ALL
* existing endpoints with the new hostname.
*/
setLocalHost(hostnameProperty);
} else if (!hostnameProperty.equals(localHost)) {
/*
* Only update the localHost field for reference
* in future endpoint creation.
*/
localHost = hostnameProperty;
if (TCPTransport.tcpLog.isLoggable(Log.BRIEF)) {
TCPTransport.tcpLog.log(Log.BRIEF,
"updated local hostname to: " + localHost);
}
}
}
return localHost;
}
}
/**
* Set the local host name, if currently unknown.
*/
static void setLocalHost(String host) {
// assert (host != null)
synchronized (localEndpoints) {
/*
* If host is not known, change the host field of ALL
* the local endpoints.
*/
if (!localHostKnown) {
localHost = host;
localHostKnown = true;
if (TCPTransport.tcpLog.isLoggable(Log.BRIEF)) {
TCPTransport.tcpLog.log(Log.BRIEF,
"local host set to " + host);
}
for (LinkedList<TCPEndpoint> epList : localEndpoints.values())
{
synchronized (epList) {
for (TCPEndpoint ep : epList) {
ep.host = host;
}
}
}
}
}
}
/**
* Set the port of the (shared) default endpoint object.
* When first created, it contains port 0 because the transport
* hasn't tried to listen to get assigned a port, or if listening
* failed, a port hasn't been assigned from the server.
*/
static void setDefaultPort(int port, RMIClientSocketFactory csf,
RMIServerSocketFactory ssf)
{
TCPEndpoint endpointKey = new TCPEndpoint(null, 0, csf, ssf);
synchronized (localEndpoints) {
LinkedList<TCPEndpoint> epList = localEndpoints.get(endpointKey);
synchronized (epList) {
int size = epList.size();
TCPEndpoint lastEp = epList.getLast();
for (TCPEndpoint ep : epList) {
ep.port = port;
}
if (size > 1) {
/*
* Remove all but the last element of the list
* (which contains the most recent hostname).
*/
epList.clear();
epList.add(lastEp);
}
}
/*
* Allow future exports to use the actual bound port
* explicitly (see 6269166).
*/
TCPEndpoint newEndpointKey = new TCPEndpoint(null, port, csf, ssf);
localEndpoints.put(newEndpointKey, epList);
if (TCPTransport.tcpLog.isLoggable(Log.BRIEF)) {
TCPTransport.tcpLog.log(Log.BRIEF,
"default port for server socket factory " + ssf +
" and client socket factory " + csf +
" set to " + port);
}
}
}
/**
* Returns transport for making connections to remote endpoints;
* (here, the default transport at port 0 is used).
*/
public Transport getOutboundTransport() {
TCPEndpoint localEndpoint = getLocalEndpoint(0, null, null);
return localEndpoint.transport;
}
/**
* Returns the current list of known transports.
* The returned list is an unshared collection of Transports,
* including all transports which may have channels to remote
* endpoints.
*/
private static Collection<TCPTransport> allKnownTransports() {
// Loop through local endpoints, getting the transport of each one.
Set<TCPTransport> s;
synchronized (localEndpoints) {
// presize s to number of localEndpoints
s = new HashSet<TCPTransport>(localEndpoints.size());
for (LinkedList<TCPEndpoint> epList : localEndpoints.values()) {
/*
* Each local endpoint has its transport added to s.
* Note: the transport is the same for all endpoints
* in the list, so it is okay to pick any one of them.
*/
TCPEndpoint ep = epList.getFirst();
s.add(ep.transport);
}
}
return s;
}
/**
* Release idle outbound connections to reduce demand on I/O resources.
* All transports are asked to release excess connections.
*/
public static void shedConnectionCaches() {
for (TCPTransport transport : allKnownTransports()) {
transport.shedConnectionCaches();
}
}
/**
* Export the object to accept incoming calls.
*/
public void exportObject(Target target) throws RemoteException {
transport.exportObject(target);
}
/**
* Returns a channel for this (remote) endpoint.
*/
public Channel getChannel() {
return getOutboundTransport().getChannel(this);
}
/**
* Returns address for endpoint
*/
public String getHost() {
return host;
}
/**
* Returns the port for this endpoint. If this endpoint was
* created as a server endpoint (using getLocalEndpoint) for a
* default/anonymous port and its inbound transport has started
* listening, this method returns (instead of zero) the actual
* bound port suitable for passing to clients.
**/
public int getPort() {
return port;
}
/**
* Returns the port that this endpoint's inbound transport listens
* on, if this endpoint was created as a server endpoint (using
* getLocalEndpoint). If this endpoint was created for the
* default/anonymous port, then this method returns zero even if
* the transport has started listening.
**/
public int getListenPort() {
return listenPort;
}
/**
* Returns the transport for incoming connections to this
* endpoint, if this endpoint was created as a server endpoint
* (using getLocalEndpoint).
**/
public Transport getInboundTransport() {
return transport;
}
/**
* Get the client socket factory associated with this endpoint.
*/
public RMIClientSocketFactory getClientSocketFactory() {
return csf;
}
/**
* Get the server socket factory associated with this endpoint.
*/
public RMIServerSocketFactory getServerSocketFactory() {
return ssf;
}
/**
* Return string representation for endpoint.
*/
public String toString() {
return "[" + host + ":" + port +
(ssf != null ? "," + ssf : "") +
(csf != null ? "," + csf : "") +
"]";
}
public int hashCode() {
return port;
}
public boolean equals(Object obj) {
if ((obj != null) && (obj instanceof TCPEndpoint)) {
TCPEndpoint ep = (TCPEndpoint) obj;
if (port != ep.port || !host.equals(ep.host))
return false;
if (((csf == null) ^ (ep.csf == null)) ||
((ssf == null) ^ (ep.ssf == null)))
return false;
/*
* Fix for 4254510: perform socket factory *class* equality check
* before socket factory equality check to avoid passing
* a potentially naughty socket factory to this endpoint's
* {client,server} socket factory equals method.
*/
if ((csf != null) &&
!(csf.getClass() == ep.csf.getClass() && csf.equals(ep.csf)))
return false;
if ((ssf != null) &&
!(ssf.getClass() == ep.ssf.getClass() && ssf.equals(ep.ssf)))
return false;
return true;
} else {
return false;
}
}
/* codes for the self-describing formats of wire representation */
private static final int FORMAT_HOST_PORT = 0;
private static final int FORMAT_HOST_PORT_FACTORY = 1;
/**
* Write endpoint to output stream.
*/
public void write(ObjectOutput out) throws IOException {
if (csf == null) {
out.writeByte(FORMAT_HOST_PORT);
out.writeUTF(host);
out.writeInt(port);
} else {
out.writeByte(FORMAT_HOST_PORT_FACTORY);
out.writeUTF(host);
out.writeInt(port);
out.writeObject(csf);
}
}
/**
* Get the endpoint from the input stream.
* @param in the input stream
* @exception IOException If id could not be read (due to stream failure)
*/
public static TCPEndpoint read(ObjectInput in)
throws IOException, ClassNotFoundException
{
String host;
int port;
RMIClientSocketFactory csf = null;
byte format = in.readByte();
switch (format) {
case FORMAT_HOST_PORT:
host = in.readUTF();
port = in.readInt();
break;
case FORMAT_HOST_PORT_FACTORY:
host = in.readUTF();
port = in.readInt();
csf = (RMIClientSocketFactory) in.readObject();
if (csf != null && Proxy.isProxyClass(csf.getClass())) {
throw new IOException("Invalid SocketFactory");
}
break;
default:
throw new IOException("invalid endpoint format");
}
return new TCPEndpoint(host, port, csf, null);
}
/**
* Write endpoint to output stream in older format used by
* UnicastRef for JDK1.1 compatibility.
*/
public void writeHostPortFormat(DataOutput out) throws IOException {
if (csf != null) {
throw new InternalError("TCPEndpoint.writeHostPortFormat: " +
"called for endpoint with non-null socket factory");
}
out.writeUTF(host);
out.writeInt(port);
}
/**
* Create a new endpoint from input stream data.
* @param in the input stream
*/
public static TCPEndpoint readHostPortFormat(DataInput in)
throws IOException
{
String host = in.readUTF();
int port = in.readInt();
return new TCPEndpoint(host, port);
}
private static RMISocketFactory chooseFactory() {
RMISocketFactory sf = RMISocketFactory.getSocketFactory();
if (sf == null) {
sf = TCPTransport.defaultSocketFactory;
}
return sf;
}
/**
* Open and return new client socket connection to endpoint.
*/
Socket newSocket() throws RemoteException {
if (TCPTransport.tcpLog.isLoggable(Log.VERBOSE)) {
TCPTransport.tcpLog.log(Log.VERBOSE,
"opening socket to " + this);
}
Socket socket;
try {
RMIClientSocketFactory clientFactory = csf;
if (clientFactory == null) {
clientFactory = chooseFactory();
}
socket = clientFactory.createSocket(host, port);
} catch (java.net.UnknownHostException e) {
throw new java.rmi.UnknownHostException(
"Unknown host: " + host, e);
} catch (java.net.ConnectException e) {
throw new java.rmi.ConnectException(
"Connection refused to host: " + host, e);
} catch (IOException e) {
// We might have simply run out of file descriptors
try {
TCPEndpoint.shedConnectionCaches();
// REMIND: should we retry createSocket?
} catch (OutOfMemoryError | Exception mem) {
// don't quit if out of memory
// or shed fails non-catastrophically
}
throw new ConnectIOException("Exception creating connection to: " +
host, e);
}
// set socket to disable Nagle's algorithm (always send immediately)
// TBD: should this be left up to socket factory instead?
try {
socket.setTcpNoDelay(true);
} catch (Exception e) {
// if we fail to set this, ignore and proceed anyway
}
// fix 4187495: explicitly set SO_KEEPALIVE to prevent client hangs
try {
socket.setKeepAlive(true);
} catch (Exception e) {
// ignore and proceed
}
return socket;
}
/**
* Return new server socket to listen for connections on this endpoint.
*/
ServerSocket newServerSocket() throws IOException {
if (TCPTransport.tcpLog.isLoggable(Log.VERBOSE)) {
TCPTransport.tcpLog.log(Log.VERBOSE,
"creating server socket on " + this);
}
RMIServerSocketFactory serverFactory = ssf;
if (serverFactory == null) {
serverFactory = chooseFactory();
}
ServerSocket server = serverFactory.createServerSocket(listenPort);
// if we listened on an anonymous port, set the default port
// (for this socket factory)
if (listenPort == 0)
setDefaultPort(server.getLocalPort(), csf, ssf);
return server;
}
/**
* The class FQDN encapsulates a routine that makes a best effort
* attempt to retrieve the fully qualified domain name of the local
* host.
*
* @author Laird Dornin
*/
private static class FQDN implements Runnable {
/**
* strings in which we can store discovered fqdn
*/
private String reverseLookup;
private String hostAddress;
private FQDN(String hostAddress) {
this.hostAddress = hostAddress;
}
/**
* Do our best to obtain a fully qualified hostname for the local
* host. Perform the following steps to get a localhostname:
*
* 1. InetAddress.getLocalHost().getHostName() - if contains
* '.' use as FQDN
* 2. if no '.' query name service for FQDN in a thread
* Note: We query the name service for an FQDN by creating
* an InetAddress via a stringified copy of the local ip
* address; this creates an InetAddress with a null hostname.
* Asking for the hostname of this InetAddress causes a name
* service lookup.
*
* 3. if name service takes too long to return, use ip address
* 4. if name service returns but response contains no '.'
* default to ipaddress.
*/
static String attemptFQDN(InetAddress localAddr)
throws java.net.UnknownHostException
{
String hostName = localAddr.getHostName();
if (hostName.indexOf('.') < 0 ) {
String hostAddress = localAddr.getHostAddress();
FQDN f = new FQDN(hostAddress);
int nameServiceTimeOut =
TCPEndpoint.getInt("sun.rmi.transport.tcp.localHostNameTimeOut",
10000);
try {
synchronized(f) {
f.getFQDN();
/* wait to obtain an FQDN */
f.wait(nameServiceTimeOut);
}
} catch (InterruptedException e) {
/* propagate the exception to the caller */
Thread.currentThread().interrupt();
}
hostName = f.getHost();
if ((hostName == null) || (hostName.equals(""))
|| (hostName.indexOf('.') < 0 )) {
hostName = hostAddress;
}
}
return hostName;
}
/**
* Method that that will start a thread to wait to retrieve a
* fully qualified domain name from a name service. The spawned
* thread may never return but we have marked it as a daemon so the vm
* will terminate appropriately.
*/
private void getFQDN() {
/* FQDN finder will run in RMI threadgroup. */
Thread t = AccessController.doPrivileged(
new NewThreadAction(FQDN.this, "FQDN Finder", true));
t.start();
}
private synchronized String getHost() {
return reverseLookup;
}
/**
* thread to query a name service for the fqdn of this host.
*/
public void run() {
String name = null;
try {
name = InetAddress.getByName(hostAddress).getHostName();
} catch (java.net.UnknownHostException e) {
} finally {
synchronized(this) {
reverseLookup = name;
this.notify();
}
}
}
}
}

View File

@@ -0,0 +1,893 @@
/*
* Copyright (c) 1996, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.rmi.transport.tcp;
import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.UndeclaredThrowableException;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.rmi.RemoteException;
import java.rmi.server.ExportException;
import java.rmi.server.LogStream;
import java.rmi.server.RMIFailureHandler;
import java.rmi.server.RMISocketFactory;
import java.rmi.server.RemoteCall;
import java.rmi.server.ServerNotActiveException;
import java.rmi.server.UID;
import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.Permissions;
import java.security.PrivilegedAction;
import java.security.ProtectionDomain;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.logging.Level;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import sun.rmi.runtime.Log;
import sun.rmi.runtime.NewThreadAction;
import sun.rmi.transport.Channel;
import sun.rmi.transport.Connection;
import sun.rmi.transport.DGCAckHandler;
import sun.rmi.transport.Endpoint;
import sun.rmi.transport.StreamRemoteCall;
import sun.rmi.transport.Target;
import sun.rmi.transport.Transport;
import sun.rmi.transport.TransportConstants;
import sun.rmi.transport.proxy.HttpReceiveSocket;
import sun.security.action.GetIntegerAction;
import sun.security.action.GetLongAction;
import sun.security.action.GetPropertyAction;
/**
* TCPTransport is the socket-based implementation of the RMI Transport
* abstraction.
*
* @author Ann Wollrath
* @author Peter Jones
*/
@SuppressWarnings("deprecation")
public class TCPTransport extends Transport {
/* tcp package log */
static final Log tcpLog = Log.getLog("sun.rmi.transport.tcp", "tcp",
LogStream.parseLevel(AccessController.doPrivileged(
new GetPropertyAction("sun.rmi.transport.tcp.logLevel"))));
/** maximum number of connection handler threads */
private static final int maxConnectionThreads = // default no limit
AccessController.doPrivileged(
new GetIntegerAction("sun.rmi.transport.tcp.maxConnectionThreads",
Integer.MAX_VALUE));
/** keep alive time for idle connection handler threads */
private static final long threadKeepAliveTime = // default 1 minute
AccessController.doPrivileged(
new GetLongAction("sun.rmi.transport.tcp.threadKeepAliveTime",
60000));
/** thread pool for connection handlers */
private static final ExecutorService connectionThreadPool =
new ThreadPoolExecutor(0, maxConnectionThreads,
threadKeepAliveTime, TimeUnit.MILLISECONDS,
new SynchronousQueue<Runnable>(),
new ThreadFactory() {
public Thread newThread(Runnable runnable) {
return AccessController.doPrivileged(new NewThreadAction(
runnable, "TCP Connection(idle)", true, true));
}
});
private static final boolean disableIncomingHttp =
java.security.AccessController.doPrivileged(
new GetPropertyAction("sun.rmi.server.disableIncomingHttp", "true"))
.equalsIgnoreCase("true");
/** total connections handled */
private static final AtomicInteger connectionCount = new AtomicInteger(0);
/** client host for the current thread's connection */
private static final ThreadLocal<ConnectionHandler>
threadConnectionHandler = new ThreadLocal<>();
/** an AccessControlContext with no permissions */
private static final AccessControlContext NOPERMS_ACC;
static {
Permissions perms = new Permissions();
ProtectionDomain[] pd = { new ProtectionDomain(null, perms) };
NOPERMS_ACC = new AccessControlContext(pd);
}
/** endpoints for this transport */
private final LinkedList<TCPEndpoint> epList;
/** number of objects exported on this transport */
private int exportCount = 0;
/** server socket for this transport */
private ServerSocket server = null;
/** table mapping endpoints to channels */
private final Map<TCPEndpoint,Reference<TCPChannel>> channelTable =
new WeakHashMap<>();
static final RMISocketFactory defaultSocketFactory =
RMISocketFactory.getDefaultSocketFactory();
/** number of milliseconds in accepted-connection timeout.
* Warning: this should be greater than 15 seconds (the client-side
* timeout), and defaults to 2 hours.
* The maximum representable value is slightly more than 24 days
* and 20 hours.
*/
private static final int connectionReadTimeout = // default 2 hours
AccessController.doPrivileged(
new GetIntegerAction("sun.rmi.transport.tcp.readTimeout",
2 * 3600 * 1000));
/**
* Constructs a TCPTransport.
*/
TCPTransport(LinkedList<TCPEndpoint> epList) {
// assert ((epList.size() != null) && (epList.size() >= 1))
this.epList = epList;
if (tcpLog.isLoggable(Log.BRIEF)) {
tcpLog.log(Log.BRIEF, "Version = " +
TransportConstants.Version + ", ep = " + getEndpoint());
}
}
/**
* Closes all cached connections in every channel subordinated to this
* transport. Currently, this only closes outgoing connections.
*/
public void shedConnectionCaches() {
List<TCPChannel> channels;
synchronized (channelTable) {
channels = new ArrayList<TCPChannel>(channelTable.values().size());
for (Reference<TCPChannel> ref : channelTable.values()) {
TCPChannel ch = ref.get();
if (ch != null) {
channels.add(ch);
}
}
}
for (TCPChannel channel : channels) {
channel.shedCache();
}
}
/**
* Returns a <I>Channel</I> that generates connections to the
* endpoint <I>ep</I>. A Channel is an object that creates and
* manages connections of a particular type to some particular
* address space.
* @param ep the endpoint to which connections will be generated.
* @return the channel or null if the transport cannot
* generate connections to this endpoint
*/
public TCPChannel getChannel(Endpoint ep) {
TCPChannel ch = null;
if (ep instanceof TCPEndpoint) {
synchronized (channelTable) {
Reference<TCPChannel> ref = channelTable.get(ep);
if (ref != null) {
ch = ref.get();
}
if (ch == null) {
TCPEndpoint tcpEndpoint = (TCPEndpoint) ep;
ch = new TCPChannel(this, tcpEndpoint);
channelTable.put(tcpEndpoint,
new WeakReference<TCPChannel>(ch));
}
}
}
return ch;
}
/**
* Removes the <I>Channel</I> that generates connections to the
* endpoint <I>ep</I>.
*/
public void free(Endpoint ep) {
if (ep instanceof TCPEndpoint) {
synchronized (channelTable) {
Reference<TCPChannel> ref = channelTable.remove(ep);
if (ref != null) {
TCPChannel channel = ref.get();
if (channel != null) {
channel.shedCache();
}
}
}
}
}
/**
* Export the object so that it can accept incoming calls.
*/
public void exportObject(Target target) throws RemoteException {
/*
* Ensure that a server socket is listening, and count this
* export while synchronized to prevent the server socket from
* being closed due to concurrent unexports.
*/
synchronized (this) {
listen();
exportCount++;
}
/*
* Try to add the Target to the exported object table; keep
* counting this export (to keep server socket open) only if
* that succeeds.
*/
boolean ok = false;
try {
super.exportObject(target);
ok = true;
} finally {
if (!ok) {
synchronized (this) {
decrementExportCount();
}
}
}
}
protected synchronized void targetUnexported() {
decrementExportCount();
}
/**
* Decrements the count of exported objects, closing the current
* server socket if the count reaches zero.
**/
private void decrementExportCount() {
assert Thread.holdsLock(this);
exportCount--;
if (exportCount == 0 && getEndpoint().getListenPort() != 0) {
ServerSocket ss = server;
server = null;
try {
ss.close();
} catch (IOException e) {
}
}
}
/**
* Verify that the current access control context has permission to
* accept the connection being dispatched by the current thread.
*/
protected void checkAcceptPermission(AccessControlContext acc) {
SecurityManager sm = System.getSecurityManager();
if (sm == null) {
return;
}
ConnectionHandler h = threadConnectionHandler.get();
if (h == null) {
throw new Error(
"checkAcceptPermission not in ConnectionHandler thread");
}
h.checkAcceptPermission(sm, acc);
}
private TCPEndpoint getEndpoint() {
synchronized (epList) {
return epList.getLast();
}
}
/**
* Listen on transport's endpoint.
*/
private void listen() throws RemoteException {
assert Thread.holdsLock(this);
TCPEndpoint ep = getEndpoint();
int port = ep.getPort();
if (server == null) {
if (tcpLog.isLoggable(Log.BRIEF)) {
tcpLog.log(Log.BRIEF,
"(port " + port + ") create server socket");
}
try {
server = ep.newServerSocket();
/*
* Don't retry ServerSocket if creation fails since
* "port in use" will cause export to hang if an
* RMIFailureHandler is not installed.
*/
Thread t = AccessController.doPrivileged(
new NewThreadAction(new AcceptLoop(server),
"TCP Accept-" + port, true));
t.start();
} catch (java.net.BindException e) {
throw new ExportException("Port already in use: " + port, e);
} catch (IOException e) {
throw new ExportException("Listen failed on port: " + port, e);
}
} else {
// otherwise verify security access to existing server socket
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkListen(port);
}
}
}
/**
* Worker for accepting connections from a server socket.
**/
private class AcceptLoop implements Runnable {
private final ServerSocket serverSocket;
// state for throttling loop on exceptions (local to accept thread)
private long lastExceptionTime = 0L;
private int recentExceptionCount;
AcceptLoop(ServerSocket serverSocket) {
this.serverSocket = serverSocket;
}
public void run() {
try {
executeAcceptLoop();
} finally {
try {
/*
* Only one accept loop is started per server
* socket, so after no more connections will be
* accepted, ensure that the server socket is no
* longer listening.
*/
serverSocket.close();
} catch (IOException e) {
}
}
}
/**
* Accepts connections from the server socket and executes
* handlers for them in the thread pool.
**/
private void executeAcceptLoop() {
if (tcpLog.isLoggable(Log.BRIEF)) {
tcpLog.log(Log.BRIEF, "listening on port " +
getEndpoint().getPort());
}
while (true) {
Socket socket = null;
try {
socket = serverSocket.accept();
/*
* Find client host name (or "0.0.0.0" if unknown)
*/
InetAddress clientAddr = socket.getInetAddress();
String clientHost = (clientAddr != null
? clientAddr.getHostAddress()
: "0.0.0.0");
/*
* Execute connection handler in the thread pool,
* which uses non-system threads.
*/
try {
connectionThreadPool.execute(
new ConnectionHandler(socket, clientHost));
} catch (RejectedExecutionException e) {
closeSocket(socket);
tcpLog.log(Log.BRIEF,
"rejected connection from " + clientHost);
}
} catch (Throwable t) {
try {
/*
* If the server socket has been closed, such
* as because there are no more exported
* objects, then we expect accept to throw an
* exception, so just terminate normally.
*/
if (serverSocket.isClosed()) {
break;
}
try {
if (tcpLog.isLoggable(Level.WARNING)) {
tcpLog.log(Level.WARNING,
"accept loop for " + serverSocket +
" throws", t);
}
} catch (Throwable tt) {
}
} finally {
/*
* Always close the accepted socket (if any)
* if an exception occurs, but only after
* logging an unexpected exception.
*/
if (socket != null) {
closeSocket(socket);
}
}
/*
* In case we're running out of file descriptors,
* release resources held in caches.
*/
if (!(t instanceof SecurityException)) {
try {
TCPEndpoint.shedConnectionCaches();
} catch (Throwable tt) {
}
}
/*
* A NoClassDefFoundError can occur if no file
* descriptors are available, in which case this
* loop should not terminate.
*/
if (t instanceof Exception ||
t instanceof OutOfMemoryError ||
t instanceof NoClassDefFoundError)
{
if (!continueAfterAcceptFailure(t)) {
return;
}
// continue loop
} else if (t instanceof Error) {
throw (Error) t;
} else {
throw new UndeclaredThrowableException(t);
}
}
}
}
/**
* Returns true if the accept loop should continue after the
* specified exception has been caught, or false if the accept
* loop should terminate (closing the server socket). If
* there is an RMIFailureHandler, this method returns the
* result of passing the specified exception to it; otherwise,
* this method always returns true, after sleeping to throttle
* the accept loop if necessary.
**/
private boolean continueAfterAcceptFailure(Throwable t) {
RMIFailureHandler fh = RMISocketFactory.getFailureHandler();
if (fh != null) {
return fh.failure(t instanceof Exception ? (Exception) t :
new InvocationTargetException(t));
} else {
throttleLoopOnException();
return true;
}
}
/**
* Throttles the accept loop after an exception has been
* caught: if a burst of 10 exceptions in 5 seconds occurs,
* then wait for 10 seconds to curb busy CPU usage.
**/
private void throttleLoopOnException() {
long now = System.currentTimeMillis();
if (lastExceptionTime == 0L || (now - lastExceptionTime) > 5000) {
// last exception was long ago (or this is the first)
lastExceptionTime = now;
recentExceptionCount = 0;
} else {
// exception burst window was started recently
if (++recentExceptionCount >= 10) {
try {
Thread.sleep(10000);
} catch (InterruptedException ignore) {
}
}
}
}
}
/** close socket and eat exception */
private static void closeSocket(Socket sock) {
try {
sock.close();
} catch (IOException ex) {
// eat exception
}
}
/**
* handleMessages decodes transport operations and handles messages
* appropriately. If an exception occurs during message handling,
* the socket is closed.
*/
void handleMessages(Connection conn, boolean persistent) {
int port = getEndpoint().getPort();
try {
DataInputStream in = new DataInputStream(conn.getInputStream());
do {
int op = in.read(); // transport op
if (op == -1) {
if (tcpLog.isLoggable(Log.BRIEF)) {
tcpLog.log(Log.BRIEF, "(port " +
port + ") connection closed");
}
break;
}
if (tcpLog.isLoggable(Log.BRIEF)) {
tcpLog.log(Log.BRIEF, "(port " + port +
") op = " + op);
}
switch (op) {
case TransportConstants.Call:
// service incoming RMI call
RemoteCall call = new StreamRemoteCall(conn);
if (serviceCall(call) == false)
return;
break;
case TransportConstants.Ping:
// send ack for ping
DataOutputStream out =
new DataOutputStream(conn.getOutputStream());
out.writeByte(TransportConstants.PingAck);
conn.releaseOutputStream();
break;
case TransportConstants.DGCAck:
DGCAckHandler.received(UID.read(in));
break;
default:
throw new IOException("unknown transport op " + op);
}
} while (persistent);
} catch (IOException e) {
// exception during processing causes connection to close (below)
if (tcpLog.isLoggable(Log.BRIEF)) {
tcpLog.log(Log.BRIEF, "(port " + port +
") exception: ", e);
}
} finally {
try {
conn.close();
} catch (IOException ex) {
// eat exception
}
}
}
/**
* Returns the client host for the current thread's connection. Throws
* ServerNotActiveException if no connection is active for this thread.
*/
public static String getClientHost() throws ServerNotActiveException {
ConnectionHandler h = threadConnectionHandler.get();
if (h != null) {
return h.getClientHost();
} else {
throw new ServerNotActiveException("not in a remote call");
}
}
/**
* Services messages on accepted connection
*/
private class ConnectionHandler implements Runnable {
/** int value of "POST" in ASCII (Java's specified data formats
* make this once-reviled tactic again socially acceptable) */
private static final int POST = 0x504f5354;
/** most recently accept-authorized AccessControlContext */
private AccessControlContext okContext;
/** cache of accept-authorized AccessControlContexts */
private Map<AccessControlContext,
Reference<AccessControlContext>> authCache;
/** security manager which authorized contexts in authCache */
private SecurityManager cacheSecurityManager = null;
private Socket socket;
private String remoteHost;
ConnectionHandler(Socket socket, String remoteHost) {
this.socket = socket;
this.remoteHost = remoteHost;
}
String getClientHost() {
return remoteHost;
}
/**
* Verify that the given AccessControlContext has permission to
* accept this connection.
*/
void checkAcceptPermission(SecurityManager sm,
AccessControlContext acc)
{
/*
* Note: no need to synchronize on cache-related fields, since this
* method only gets called from the ConnectionHandler's thread.
*/
if (sm != cacheSecurityManager) {
okContext = null;
authCache = new WeakHashMap<AccessControlContext,
Reference<AccessControlContext>>();
cacheSecurityManager = sm;
}
if (acc.equals(okContext) || authCache.containsKey(acc)) {
return;
}
InetAddress addr = socket.getInetAddress();
String host = (addr != null) ? addr.getHostAddress() : "*";
sm.checkAccept(host, socket.getPort());
authCache.put(acc, new SoftReference<AccessControlContext>(acc));
okContext = acc;
}
public void run() {
Thread t = Thread.currentThread();
String name = t.getName();
try {
t.setName("RMI TCP Connection(" +
connectionCount.incrementAndGet() +
")-" + remoteHost);
AccessController.doPrivileged((PrivilegedAction<Void>)() -> {
run0();
return null;
}, NOPERMS_ACC);
} finally {
t.setName(name);
}
}
private void run0() {
TCPEndpoint endpoint = getEndpoint();
int port = endpoint.getPort();
threadConnectionHandler.set(this);
// set socket to disable Nagle's algorithm (always send
// immediately)
// TBD: should this be left up to socket factory instead?
try {
socket.setTcpNoDelay(true);
} catch (Exception e) {
// if we fail to set this, ignore and proceed anyway
}
// set socket to timeout after excessive idle time
try {
if (connectionReadTimeout > 0)
socket.setSoTimeout(connectionReadTimeout);
} catch (Exception e) {
// too bad, continue anyway
}
try {
InputStream sockIn = socket.getInputStream();
InputStream bufIn = sockIn.markSupported()
? sockIn
: new BufferedInputStream(sockIn);
// Read magic (or HTTP wrapper)
bufIn.mark(4);
DataInputStream in = new DataInputStream(bufIn);
int magic = in.readInt();
if (magic == POST) {
if (disableIncomingHttp) {
throw new RemoteException("RMI over HTTP is disabled");
}
tcpLog.log(Log.BRIEF, "decoding HTTP-wrapped call");
// It's really a HTTP-wrapped request. Repackage
// the socket in a HttpReceiveSocket, reinitialize
// sockIn and in, and reread magic.
bufIn.reset(); // unread "POST"
try {
socket = new HttpReceiveSocket(socket, bufIn, null);
remoteHost = "0.0.0.0";
sockIn = socket.getInputStream();
bufIn = new BufferedInputStream(sockIn);
in = new DataInputStream(bufIn);
magic = in.readInt();
} catch (IOException e) {
throw new RemoteException("Error HTTP-unwrapping call",
e);
}
}
// bufIn's mark will invalidate itself when it overflows
// so it doesn't have to be turned off
// read and verify transport header
short version = in.readShort();
if (magic != TransportConstants.Magic ||
version != TransportConstants.Version) {
// protocol mismatch detected...
// just close socket: this would recurse if we marshal an
// exception to the client and the protocol at other end
// doesn't match.
closeSocket(socket);
return;
}
OutputStream sockOut = socket.getOutputStream();
BufferedOutputStream bufOut =
new BufferedOutputStream(sockOut);
DataOutputStream out = new DataOutputStream(bufOut);
int remotePort = socket.getPort();
if (tcpLog.isLoggable(Log.BRIEF)) {
tcpLog.log(Log.BRIEF, "accepted socket from [" +
remoteHost + ":" + remotePort + "]");
}
TCPEndpoint ep;
TCPChannel ch;
TCPConnection conn;
// send ack (or nack) for protocol
byte protocol = in.readByte();
switch (protocol) {
case TransportConstants.SingleOpProtocol:
// no ack for protocol
// create dummy channel for receiving messages
ep = new TCPEndpoint(remoteHost, socket.getLocalPort(),
endpoint.getClientSocketFactory(),
endpoint.getServerSocketFactory());
ch = new TCPChannel(TCPTransport.this, ep);
conn = new TCPConnection(ch, socket, bufIn, bufOut);
// read input messages
handleMessages(conn, false);
break;
case TransportConstants.StreamProtocol:
// send ack
out.writeByte(TransportConstants.ProtocolAck);
// suggest endpoint (in case client doesn't know host name)
if (tcpLog.isLoggable(Log.VERBOSE)) {
tcpLog.log(Log.VERBOSE, "(port " + port +
") " + "suggesting " + remoteHost + ":" +
remotePort);
}
out.writeUTF(remoteHost);
out.writeInt(remotePort);
out.flush();
// read and discard (possibly bogus) endpoint
// REMIND: would be faster to read 2 bytes then skip N+4
String clientHost = in.readUTF();
int clientPort = in.readInt();
if (tcpLog.isLoggable(Log.VERBOSE)) {
tcpLog.log(Log.VERBOSE, "(port " + port +
") client using " + clientHost + ":" + clientPort);
}
// create dummy channel for receiving messages
// (why not use clientHost and clientPort?)
ep = new TCPEndpoint(remoteHost, socket.getLocalPort(),
endpoint.getClientSocketFactory(),
endpoint.getServerSocketFactory());
ch = new TCPChannel(TCPTransport.this, ep);
conn = new TCPConnection(ch, socket, bufIn, bufOut);
// read input messages
handleMessages(conn, true);
break;
case TransportConstants.MultiplexProtocol:
if (tcpLog.isLoggable(Log.VERBOSE)) {
tcpLog.log(Log.VERBOSE, "(port " + port +
") accepting multiplex protocol");
}
// send ack
out.writeByte(TransportConstants.ProtocolAck);
// suggest endpoint (in case client doesn't already have one)
if (tcpLog.isLoggable(Log.VERBOSE)) {
tcpLog.log(Log.VERBOSE, "(port " + port +
") suggesting " + remoteHost + ":" + remotePort);
}
out.writeUTF(remoteHost);
out.writeInt(remotePort);
out.flush();
// read endpoint client has decided to use
ep = new TCPEndpoint(in.readUTF(), in.readInt(),
endpoint.getClientSocketFactory(),
endpoint.getServerSocketFactory());
if (tcpLog.isLoggable(Log.VERBOSE)) {
tcpLog.log(Log.VERBOSE, "(port " +
port + ") client using " +
ep.getHost() + ":" + ep.getPort());
}
ConnectionMultiplexer multiplexer;
synchronized (channelTable) {
// create or find channel for this endpoint
ch = getChannel(ep);
multiplexer =
new ConnectionMultiplexer(ch, bufIn, sockOut,
false);
ch.useMultiplexer(multiplexer);
}
multiplexer.run();
break;
default:
// protocol not understood, send nack and close socket
out.writeByte(TransportConstants.ProtocolNack);
out.flush();
break;
}
} catch (IOException e) {
// socket in unknown state: destroy socket
tcpLog.log(Log.BRIEF, "terminated with exception:", e);
} finally {
closeSocket(socket);
}
}
}
}