32 import java.io.DataInputStream; 33 import java.io.DataOutputStream; 34 import java.io.IOException; 35 import java.io.InputStream; 36 import java.io.OutputStream; 37 import java.io.BufferedInputStream; 38 import java.io.BufferedOutputStream; 39 import java.net.InetAddress; 40 import java.net.ServerSocket; 41 import java.net.Socket; 42 import java.rmi.RemoteException; 43 import java.rmi.server.ExportException; 44 import java.rmi.server.LogStream; 45 import java.rmi.server.RMIFailureHandler; 46 import java.rmi.server.RMISocketFactory; 47 import java.rmi.server.RemoteCall; 48 import java.rmi.server.ServerNotActiveException; 49 import java.rmi.server.UID; 50 import java.security.AccessControlContext; 51 import java.security.AccessController; 52 import java.util.ArrayList; 53 import java.util.LinkedList; 54 import java.util.List; 55 import java.util.Map; 56 import java.util.WeakHashMap; 57 import java.util.logging.Level; 58 import java.util.concurrent.ExecutorService; 59 import java.util.concurrent.RejectedExecutionException; 60 import java.util.concurrent.SynchronousQueue; 61 import java.util.concurrent.ThreadFactory; 62 import java.util.concurrent.ThreadPoolExecutor; 63 import java.util.concurrent.TimeUnit; 64 import java.util.concurrent.atomic.AtomicInteger; 65 import sun.rmi.runtime.Log; 66 import sun.rmi.runtime.NewThreadAction; 67 import sun.rmi.transport.Channel; 68 import sun.rmi.transport.Connection; 69 import sun.rmi.transport.DGCAckHandler; 70 import sun.rmi.transport.Endpoint; 71 import sun.rmi.transport.StreamRemoteCall; 72 import sun.rmi.transport.Target; 73 import sun.rmi.transport.Transport; 74 import sun.rmi.transport.TransportConstants; 75 import sun.rmi.transport.proxy.HttpReceiveSocket; 76 import sun.security.action.GetIntegerAction; 77 import sun.security.action.GetLongAction; 78 import sun.security.action.GetPropertyAction; 79 80 /** 81 * TCPTransport is the socket-based implementation of the RMI Transport 82 * abstraction. 83 * 84 * @author Ann Wollrath 85 * @author Peter Jones 86 */ 87 @SuppressWarnings("deprecation") 88 public class TCPTransport extends Transport { 89 90 /* tcp package log */ 91 static final Log tcpLog = Log.getLog("sun.rmi.transport.tcp", "tcp", 92 LogStream.parseLevel(AccessController.doPrivileged( 93 new GetPropertyAction("sun.rmi.transport.tcp.logLevel")))); 94 95 /** maximum number of connection handler threads */ 96 private static final int maxConnectionThreads = // default no limit 97 AccessController.doPrivileged( 98 new GetIntegerAction("sun.rmi.transport.tcp.maxConnectionThreads", 99 Integer.MAX_VALUE)); 100 101 /** keep alive time for idle connection handler threads */ 102 private static final long threadKeepAliveTime = // default 1 minute 103 AccessController.doPrivileged( 104 new GetLongAction("sun.rmi.transport.tcp.threadKeepAliveTime", 105 60000)); 106 107 /** thread pool for connection handlers */ 108 private static final ExecutorService connectionThreadPool = 109 new ThreadPoolExecutor(0, maxConnectionThreads, 110 threadKeepAliveTime, TimeUnit.MILLISECONDS, 111 new SynchronousQueue<Runnable>(), 112 new ThreadFactory() { 113 public Thread newThread(Runnable runnable) { 114 return AccessController.doPrivileged(new NewThreadAction( 115 runnable, "TCP Connection(idle)", true, true)); 116 } 117 }); 118 119 /** total connections handled */ 120 private static final AtomicInteger connectionCount = new AtomicInteger(0); 121 122 /** client host for the current thread's connection */ 123 private static final ThreadLocal<ConnectionHandler> 124 threadConnectionHandler = new ThreadLocal<>(); 125 126 /** endpoints for this transport */ 127 private final LinkedList<TCPEndpoint> epList; 128 /** number of objects exported on this transport */ 129 private int exportCount = 0; 130 /** server socket for this transport */ 131 private ServerSocket server = null; 132 /** table mapping endpoints to channels */ 133 private final Map<TCPEndpoint,Reference<TCPChannel>> channelTable = 134 new WeakHashMap<>(); 135 136 static final RMISocketFactory defaultSocketFactory = 137 RMISocketFactory.getDefaultSocketFactory(); 138 139 /** number of milliseconds in accepted-connection timeout. 140 * Warning: this should be greater than 15 seconds (the client-side 141 * timeout), and defaults to 2 hours. 142 * The maximum representable value is slightly more than 24 days 143 * and 20 hours. 144 */ 145 private static final int connectionReadTimeout = // default 2 hours 146 AccessController.doPrivileged( 147 new GetIntegerAction("sun.rmi.transport.tcp.readTimeout", 148 2 * 3600 * 1000)); 149 150 /** 151 * Constructs a TCPTransport. 152 */ 153 TCPTransport(LinkedList<TCPEndpoint> epList) { 154 // assert ((epList.size() != null) && (epList.size() >= 1)) 155 this.epList = epList; 156 if (tcpLog.isLoggable(Log.BRIEF)) { 157 tcpLog.log(Log.BRIEF, "Version = " + 158 TransportConstants.Version + ", ep = " + getEndpoint()); 159 } 160 } 161 162 /** 163 * Closes all cached connections in every channel subordinated to this 164 * transport. Currently, this only closes outgoing connections. 165 */ 166 public void shedConnectionCaches() { 167 List<TCPChannel> channels; 168 synchronized (channelTable) { | 32 import java.io.DataInputStream; 33 import java.io.DataOutputStream; 34 import java.io.IOException; 35 import java.io.InputStream; 36 import java.io.OutputStream; 37 import java.io.BufferedInputStream; 38 import java.io.BufferedOutputStream; 39 import java.net.InetAddress; 40 import java.net.ServerSocket; 41 import java.net.Socket; 42 import java.rmi.RemoteException; 43 import java.rmi.server.ExportException; 44 import java.rmi.server.LogStream; 45 import java.rmi.server.RMIFailureHandler; 46 import java.rmi.server.RMISocketFactory; 47 import java.rmi.server.RemoteCall; 48 import java.rmi.server.ServerNotActiveException; 49 import java.rmi.server.UID; 50 import java.security.AccessControlContext; 51 import java.security.AccessController; 52 import java.security.PrivilegedAction; 53 import java.util.ArrayList; 54 import java.util.LinkedList; 55 import java.util.List; 56 import java.util.Map; 57 import java.util.WeakHashMap; 58 import java.util.logging.Level; 59 import java.util.concurrent.ExecutorService; 60 import java.util.concurrent.RejectedExecutionException; 61 import java.util.concurrent.SynchronousQueue; 62 import java.util.concurrent.ThreadFactory; 63 import java.util.concurrent.ThreadPoolExecutor; 64 import java.util.concurrent.TimeUnit; 65 import java.util.concurrent.atomic.AtomicInteger; 66 import sun.rmi.runtime.Log; 67 import sun.rmi.runtime.NewThreadAction; 68 import sun.rmi.transport.Channel; 69 import sun.rmi.transport.Connection; 70 import sun.rmi.transport.DGCAckHandler; 71 import sun.rmi.transport.Endpoint; 72 import sun.rmi.transport.StreamRemoteCall; 73 import sun.rmi.transport.Target; 74 import sun.rmi.transport.Transport; 75 import sun.rmi.transport.TransportConstants; 76 import sun.rmi.transport.proxy.HttpReceiveSocket; 77 78 /** 79 * TCPTransport is the socket-based implementation of the RMI Transport 80 * abstraction. 81 * 82 * @author Ann Wollrath 83 * @author Peter Jones 84 */ 85 @SuppressWarnings("deprecation") 86 public class TCPTransport extends Transport { 87 88 /* tcp package log */ 89 static final Log tcpLog = Log.getLog("sun.rmi.transport.tcp", "tcp", 90 LogStream.parseLevel(AccessController.doPrivileged( 91 (PrivilegedAction<String>) () -> System.getProperty("sun.rmi.transport.tcp.logLevel")))); 92 93 /** maximum number of connection handler threads */ 94 private static final int maxConnectionThreads = // default no limit 95 AccessController.doPrivileged((PrivilegedAction<Integer>) () -> 96 Integer.getInteger("sun.rmi.transport.tcp.maxConnectionThreads", 97 Integer.MAX_VALUE)); 98 99 /** keep alive time for idle connection handler threads */ 100 private static final long threadKeepAliveTime = // default 1 minute 101 AccessController.doPrivileged((PrivilegedAction<Long>) () -> 102 Long.getLong("sun.rmi.transport.tcp.threadKeepAliveTime", 60000)); 103 104 /** thread pool for connection handlers */ 105 private static final ExecutorService connectionThreadPool = 106 new ThreadPoolExecutor(0, maxConnectionThreads, 107 threadKeepAliveTime, TimeUnit.MILLISECONDS, 108 new SynchronousQueue<Runnable>(), 109 new ThreadFactory() { 110 public Thread newThread(Runnable runnable) { 111 return AccessController.doPrivileged(new NewThreadAction( 112 runnable, "TCP Connection(idle)", true, true)); 113 } 114 }); 115 116 /** total connections handled */ 117 private static final AtomicInteger connectionCount = new AtomicInteger(0); 118 119 /** client host for the current thread's connection */ 120 private static final ThreadLocal<ConnectionHandler> 121 threadConnectionHandler = new ThreadLocal<>(); 122 123 /** endpoints for this transport */ 124 private final LinkedList<TCPEndpoint> epList; 125 /** number of objects exported on this transport */ 126 private int exportCount = 0; 127 /** server socket for this transport */ 128 private ServerSocket server = null; 129 /** table mapping endpoints to channels */ 130 private final Map<TCPEndpoint,Reference<TCPChannel>> channelTable = 131 new WeakHashMap<>(); 132 133 static final RMISocketFactory defaultSocketFactory = 134 RMISocketFactory.getDefaultSocketFactory(); 135 136 /** number of milliseconds in accepted-connection timeout. 137 * Warning: this should be greater than 15 seconds (the client-side 138 * timeout), and defaults to 2 hours. 139 * The maximum representable value is slightly more than 24 days 140 * and 20 hours. 141 */ 142 private static final int connectionReadTimeout = // default 2 hours 143 AccessController.doPrivileged((PrivilegedAction<Integer>) () -> 144 Integer.getInteger("sun.rmi.transport.tcp.readTimeout", 2 * 3600 * 1000)); 145 146 /** 147 * Constructs a TCPTransport. 148 */ 149 TCPTransport(LinkedList<TCPEndpoint> epList) { 150 // assert ((epList.size() != null) && (epList.size() >= 1)) 151 this.epList = epList; 152 if (tcpLog.isLoggable(Log.BRIEF)) { 153 tcpLog.log(Log.BRIEF, "Version = " + 154 TransportConstants.Version + ", ep = " + getEndpoint()); 155 } 156 } 157 158 /** 159 * Closes all cached connections in every channel subordinated to this 160 * transport. Currently, this only closes outgoing connections. 161 */ 162 public void shedConnectionCaches() { 163 List<TCPChannel> channels; 164 synchronized (channelTable) { |