Outer wrap write timeout socket

pull/214/head
M66B 1 year ago
parent 40cfe6c2eb
commit cfc1f665b0

@ -320,13 +320,6 @@ public class SocketFetcher {
logger.finest("set socket read timeout " + to); logger.finest("set socket read timeout " + to);
socket.setSoTimeout(to); socket.setSoTimeout(to);
} }
int writeTimeout = PropUtil.getIntProperty(props,
prefix + ".writetimeout", -1);
if (writeTimeout != -1) { // wrap original
if (logger.isLoggable(Level.FINEST))
logger.finest("set socket write timeout " + writeTimeout);
socket = new WriteTimeoutSocket(socket, writeTimeout);
}
if (localaddr != null) if (localaddr != null)
socket.bind(new InetSocketAddress(localaddr, localport)); socket.bind(new InetSocketAddress(localaddr, localport));
try { try {
@ -383,7 +376,7 @@ public class SocketFetcher {
ssf = (SSLSocketFactory)sf; ssf = (SSLSocketFactory)sf;
else else
ssf = (SSLSocketFactory)SSLSocketFactory.getDefault(); ssf = (SSLSocketFactory)SSLSocketFactory.getDefault();
socket = ssf.createSocket(socket, host, port, true); socket = ssf.createSocket(WriteTimeoutSocket.unwrap(socket), host, port, true);
sf = ssf; sf = ssf;
} }
@ -393,7 +386,7 @@ public class SocketFetcher {
*/ */
configureSSLSocket(socket, host, props, prefix, sf); configureSSLSocket(socket, host, props, prefix, sf);
return socket; return WriteTimeoutSocket.wrap(socket, props, prefix, logger);
} }
/** /**
@ -543,7 +536,7 @@ public class SocketFetcher {
} }
} }
socket = ssf.createSocket(socket, host, port, true); socket = ssf.createSocket(WriteTimeoutSocket.unwrap(socket), host, port, true);
configureSSLSocket(socket, host, props, prefix, ssf); configureSSLSocket(socket, host, props, prefix, ssf);
} catch (Exception ex) { } catch (Exception ex) {
if (ex instanceof InvocationTargetException) { if (ex instanceof InvocationTargetException) {
@ -563,7 +556,7 @@ public class SocketFetcher {
ioex.initCause(ex); ioex.initCause(ex);
throw ioex; throw ioex;
} }
return socket; return WriteTimeoutSocket.wrap(socket, props, prefix, logger);
} }
/** /**

@ -18,11 +18,13 @@ package com.sun.mail.util;
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
import java.util.Properties;
import java.util.concurrent.*; import java.util.concurrent.*;
import java.util.Collections; import java.util.Collections;
import java.util.Set; import java.util.Set;
import java.nio.channels.SocketChannel; import java.nio.channels.SocketChannel;
import java.lang.reflect.*; import java.lang.reflect.*;
import java.util.logging.Level;
/** /**
* A special Socket that uses a ScheduledExecutorService to * A special Socket that uses a ScheduledExecutorService to
@ -337,6 +339,28 @@ public class WriteTimeoutSocket extends Socket {
} }
return null; return null;
} }
static Socket wrap(Socket socket, Properties props, String prefix, MailLogger logger) throws IOException {
if (socket instanceof WriteTimeoutSocket)
return socket;
int writeTimeout = PropUtil.getIntProperty(props,
prefix + ".writetimeout", -1);
if (writeTimeout == -1)
return socket;
if (logger.isLoggable(Level.FINEST))
logger.finest("set socket write timeout " + writeTimeout);
return new WriteTimeoutSocket(socket, writeTimeout);
}
static Socket unwrap(Socket socket) {
if (socket instanceof WriteTimeoutSocket)
socket = ((WriteTimeoutSocket) socket).socket;
return socket;
}
} }

Loading…
Cancel
Save