diff -ur commons-httpclient-3.0.1.orig/src/java/org/apache/commons/httpclient/protocol/ControllerThreadSocketFactory.java commons-httpclient-3.0.1/src/java/org/apache/commons/httpclient/protocol/ControllerThreadSocketFactory.java
--- commons-httpclient-3.0.1.orig/src/java/org/apache/commons/httpclient/protocol/ControllerThreadSocketFactory.java 2008-01-24 13:35:59.000000000 -0600
+++ commons-httpclient-3.0.1/src/java/org/apache/commons/httpclient/protocol/ControllerThreadSocketFactory.java 2008-01-24 15:32:32.232625799 -0600
@@ -70,6 +70,7 @@
* @return a connected Socket
*
* @throws IOException if an I/O error occurs while creating the socket
+ * @throws IOException if the thread invoking this method is interrupted
* @throws UnknownHostException if the IP address of the host cannot be
* determined
* @throws ConnectTimeoutException if socket cannot be connected within the
@@ -96,23 +97,9 @@
throw new ConnectTimeoutException(
"The host did not accept the connection within timeout of "
+ timeout + " ms");
- }
- Socket socket = task.getSocket();
- if (task.exception != null) {
- throw task.exception;
- }
- return socket;
- }
-
- public static Socket createSocket(final SocketTask task, int timeout)
- throws IOException, UnknownHostException, ConnectTimeoutException
- {
- try {
- TimeoutController.execute(task, timeout);
- } catch (TimeoutController.TimeoutException e) {
- throw new ConnectTimeoutException(
- "The host did not accept the connection within timeout of "
- + timeout + " ms");
+ } catch (InterruptedException e) {
+ task.interrupt();
+ throw (IOException)new IOException("thread interrupted").initCause(e);
}
Socket socket = task.getSocket();
if (task.exception != null) {
@@ -124,12 +111,17 @@
/**
* Helper class for wrapping socket based tasks.
*/
- public static abstract class SocketTask implements Runnable {
+ public static abstract class SocketTask extends Thread {
/** The socket */
private Socket socket;
/** The exception */
private IOException exception;
+ protected SocketTask() {
+ super("Timeout guard");
+ setDaemon(true);
+ }
+
/**
* Set the socket.
* @param newSocket The new socket.
diff -ur commons-httpclient-3.0.1.orig/src/java/org/apache/commons/httpclient/util/TimeoutController.java commons-httpclient-3.0.1/src/java/org/apache/commons/httpclient/util/TimeoutController.java
--- commons-httpclient-3.0.1.orig/src/java/org/apache/commons/httpclient/util/TimeoutController.java 2008-01-24 13:35:59.000000000 -0600
+++ commons-httpclient-3.0.1/src/java/org/apache/commons/httpclient/util/TimeoutController.java 2008-01-24 15:31:20.576452214 -0600
@@ -49,20 +49,18 @@
/**
* Executes task. Waits for timeout
* milliseconds for the task to end and returns. If the task does not return
- * in time, the thread is interrupted and an Exception is thrown.
+ * in time, the thread is interrupted and a TimeoutException is thrown.
* The caller should override the Thread.interrupt() method to something that
* quickly makes the thread die or use Thread.isInterrupted().
* @param task The thread to execute
* @param timeout The timeout in milliseconds. 0 means to wait forever.
* @throws TimeoutException if the timeout passes and the thread does not return.
+ * @throws InterruptedException if thread invoking this method gets interrupted
*/
- public static void execute(Thread task, long timeout) throws TimeoutException {
+ public static void execute(Thread task, long timeout) throws TimeoutException,
+ InterruptedException {
task.start();
- try {
- task.join(timeout);
- } catch (InterruptedException e) {
- /* if somebody interrupts us he knows what he is doing */
- }
+ task.join(timeout);
if (task.isAlive()) {
task.interrupt();
throw new TimeoutException();
@@ -70,18 +68,6 @@
}
/**
- * Executes task in a new deamon Thread and waits for the timeout.
- * @param task The task to execute
- * @param timeout The timeout in milliseconds. 0 means to wait forever.
- * @throws TimeoutException if the timeout passes and the thread does not return.
- */
- public static void execute(Runnable task, long timeout) throws TimeoutException {
- Thread t = new Thread(task, "Timeout guard");
- t.setDaemon(true);
- execute(t, timeout);
- }
-
- /**
* Signals that the task timed out.
*/
public static class TimeoutException extends Exception {