Outer wrap write timeout socket

This commit is contained in:
M66B 2023-12-07 16:19:52 +01:00
parent 40cfe6c2eb
commit cfc1f665b0
2 changed files with 28 additions and 11 deletions

View File

@ -320,13 +320,6 @@ public class SocketFetcher {
logger.finest("set socket read timeout " + 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)
socket.bind(new InetSocketAddress(localaddr, localport));
try {
@ -383,7 +376,7 @@ public class SocketFetcher {
ssf = (SSLSocketFactory)sf;
else
ssf = (SSLSocketFactory)SSLSocketFactory.getDefault();
socket = ssf.createSocket(socket, host, port, true);
socket = ssf.createSocket(WriteTimeoutSocket.unwrap(socket), host, port, true);
sf = ssf;
}
@ -393,7 +386,7 @@ public class SocketFetcher {
*/
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);
} catch (Exception ex) {
if (ex instanceof InvocationTargetException) {
@ -563,7 +556,7 @@ public class SocketFetcher {
ioex.initCause(ex);
throw ioex;
}
return socket;
return WriteTimeoutSocket.wrap(socket, props, prefix, logger);
}
/**

View File

@ -18,11 +18,13 @@ package com.sun.mail.util;
import java.io.*;
import java.net.*;
import java.util.Properties;
import java.util.concurrent.*;
import java.util.Collections;
import java.util.Set;
import java.nio.channels.SocketChannel;
import java.lang.reflect.*;
import java.util.logging.Level;
/**
* A special Socket that uses a ScheduledExecutorService to
@ -337,6 +339,28 @@ public class WriteTimeoutSocket extends Socket {
}
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;
}
}