diff --git a/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java b/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java index 2d9578256..6d42f5ac5 100644 --- a/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java +++ b/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java @@ -561,7 +561,10 @@ public class DefaultRequestDirector implements RequestDirector { return response; } catch (ConnectionShutdownException ex) { - throw new InterruptedIOException("Connection has been shut down"); + InterruptedIOException ioex = new InterruptedIOException( + "Connection has been shut down"); + ioex.initCause(ex); + throw ioex; } catch (HttpException ex) { abortConnection(); throw ex; diff --git a/httpclient/src/main/java/org/apache/http/impl/conn/AbstractClientConnAdapter.java b/httpclient/src/main/java/org/apache/http/impl/conn/AbstractClientConnAdapter.java index 9c23a6949..46d435420 100644 --- a/httpclient/src/main/java/org/apache/http/impl/conn/AbstractClientConnAdapter.java +++ b/httpclient/src/main/java/org/apache/http/impl/conn/AbstractClientConnAdapter.java @@ -81,7 +81,7 @@ public abstract class AbstractClientConnAdapter implements ManagedClientConnecti private volatile boolean markedReusable; /** True if the connection has been shut down or released. */ - private volatile boolean shutdown; + private volatile boolean released; /** The duration this is valid for while idle (in ms). */ private volatile long duration; @@ -100,7 +100,7 @@ public abstract class AbstractClientConnAdapter implements ManagedClientConnecti connManager = mgr; wrappedConnection = conn; markedReusable = false; - shutdown = false; + released = false; duration = Long.MAX_VALUE; } @@ -123,25 +123,32 @@ public abstract class AbstractClientConnAdapter implements ManagedClientConnecti } /** - * Asserts that the connection has not been aborted. - * - * @throws InterruptedIOException if the connection has been aborted + * @deprecated use {@link #assertValid(OperatedClientConnection)} */ + @Deprecated protected final void assertNotAborted() throws InterruptedIOException { - if (shutdown) { + if (isReleased()) { throw new InterruptedIOException("Connection has been shut down"); } } /** - * Asserts that there is a wrapped connection to delegate to. + * @since 4.1 + * @return value of released flag + */ + protected boolean isReleased() { + return released; + } + + /** + * Asserts that there is a valid wrapped connection to delegate to. * - * @throws IllegalStateException if there is no wrapped connection + * @throws ConnectionShutdownException if there is no wrapped connection * or connection has been aborted */ protected final void assertValid( - final OperatedClientConnection wrappedConn) { - if (wrappedConn == null) { + final OperatedClientConnection wrappedConn) throws ConnectionShutdownException { + if (isReleased() || wrappedConn == null) { throw new ConnectionShutdownException(); } } @@ -155,7 +162,7 @@ public abstract class AbstractClientConnAdapter implements ManagedClientConnecti } public boolean isStale() { - if (shutdown) + if (isReleased()) return true; OperatedClientConnection conn = getWrappedConnection(); if (conn == null) @@ -182,66 +189,46 @@ public abstract class AbstractClientConnAdapter implements ManagedClientConnecti return conn.getMetrics(); } - public void flush() - throws IOException { - - assertNotAborted(); + public void flush() throws IOException { OperatedClientConnection conn = getWrappedConnection(); assertValid(conn); - conn.flush(); } - public boolean isResponseAvailable(int timeout) - throws IOException { - - assertNotAborted(); + public boolean isResponseAvailable(int timeout) throws IOException { OperatedClientConnection conn = getWrappedConnection(); assertValid(conn); - return conn.isResponseAvailable(timeout); } public void receiveResponseEntity(HttpResponse response) throws HttpException, IOException { - - assertNotAborted(); OperatedClientConnection conn = getWrappedConnection(); assertValid(conn); - unmarkReusable(); conn.receiveResponseEntity(response); } public HttpResponse receiveResponseHeader() throws HttpException, IOException { - - assertNotAborted(); OperatedClientConnection conn = getWrappedConnection(); assertValid(conn); - unmarkReusable(); return conn.receiveResponseHeader(); } public void sendRequestEntity(HttpEntityEnclosingRequest request) throws HttpException, IOException { - - assertNotAborted(); OperatedClientConnection conn = getWrappedConnection(); assertValid(conn); - unmarkReusable(); conn.sendRequestEntity(request); } public void sendRequestHeader(HttpRequest request) throws HttpException, IOException { - - assertNotAborted(); OperatedClientConnection conn = getWrappedConnection(); assertValid(conn); - unmarkReusable(); conn.sendRequestHeader(request); } @@ -311,20 +298,20 @@ public abstract class AbstractClientConnAdapter implements ManagedClientConnecti } public synchronized void releaseConnection() { - if (shutdown) { + if (released) { return; } - shutdown = true; + released = true; if (connManager != null) { connManager.releaseConnection(this, duration, TimeUnit.MILLISECONDS); } } public synchronized void abortConnection() { - if (shutdown) { + if (released) { return; } - shutdown = true; + released = true; unmarkReusable(); try { shutdown(); diff --git a/httpclient/src/main/java/org/apache/http/impl/conn/AbstractPooledConnAdapter.java b/httpclient/src/main/java/org/apache/http/impl/conn/AbstractPooledConnAdapter.java index eb723268e..438e9ac9f 100644 --- a/httpclient/src/main/java/org/apache/http/impl/conn/AbstractPooledConnAdapter.java +++ b/httpclient/src/main/java/org/apache/http/impl/conn/AbstractPooledConnAdapter.java @@ -63,12 +63,32 @@ public abstract class AbstractPooledConnAdapter extends AbstractClientConnAdapte super(manager, entry.connection); this.poolEntry = entry; } + + /** + * Obtains the pool entry. + * + * @return the pool entry, or null if detached + */ + protected AbstractPoolEntry getPoolEntry() { + return this.poolEntry; + } /** - * Asserts that this adapter is still attached. + * Asserts that there is a valid pool entry. * - * @throws IllegalStateException - * if it is {@link #detach detach}ed + * @throws ConnectionShutdownException if there is no pool entry + * or connection has been aborted + * + * @see #assertValid(OperatedClientConnection) + */ + protected void assertValid(final AbstractPoolEntry entry) { + if (isReleased() || entry == null) { + throw new ConnectionShutdownException(); + } + } + + /** + * @deprecated use {@link #assertValid(AbstractPoolEntry)} */ protected final void assertAttached() { if (poolEntry == null) { @@ -87,42 +107,42 @@ public abstract class AbstractPooledConnAdapter extends AbstractClientConnAdapte } public HttpRoute getRoute() { - assertAttached(); - return (poolEntry.tracker == null) ? - null : poolEntry.tracker.toRoute(); + AbstractPoolEntry entry = getPoolEntry(); + assertValid(entry); + return (entry.tracker == null) ? null : entry.tracker.toRoute(); } public void open(HttpRoute route, HttpContext context, HttpParams params) throws IOException { - assertNotAborted(); - assertAttached(); - poolEntry.open(route, context, params); + AbstractPoolEntry entry = getPoolEntry(); + assertValid(entry); + entry.open(route, context, params); } public void tunnelTarget(boolean secure, HttpParams params) throws IOException { - assertNotAborted(); - assertAttached(); - poolEntry.tunnelTarget(secure, params); + AbstractPoolEntry entry = getPoolEntry(); + assertValid(entry); + entry.tunnelTarget(secure, params); } public void tunnelProxy(HttpHost next, boolean secure, HttpParams params) throws IOException { - assertNotAborted(); - assertAttached(); - poolEntry.tunnelProxy(next, secure, params); + AbstractPoolEntry entry = getPoolEntry(); + assertValid(entry); + entry.tunnelProxy(next, secure, params); } public void layerProtocol(HttpContext context, HttpParams params) throws IOException { - assertNotAborted(); - assertAttached(); - poolEntry.layerProtocol(context, params); + AbstractPoolEntry entry = getPoolEntry(); + assertValid(entry); + entry.layerProtocol(context, params); } public void close() throws IOException { - AbstractPoolEntry entry = poolEntry; + AbstractPoolEntry entry = getPoolEntry(); if (entry != null) entry.shutdownEntry(); @@ -133,7 +153,7 @@ public abstract class AbstractPooledConnAdapter extends AbstractClientConnAdapte } public void shutdown() throws IOException { - AbstractPoolEntry entry = poolEntry; + AbstractPoolEntry entry = getPoolEntry(); if (entry != null) entry.shutdownEntry(); @@ -144,13 +164,15 @@ public abstract class AbstractPooledConnAdapter extends AbstractClientConnAdapte } public Object getState() { - assertAttached(); - return poolEntry.getState(); + AbstractPoolEntry entry = getPoolEntry(); + assertValid(entry); + return entry.getState(); } public void setState(final Object state) { - assertAttached(); - poolEntry.setState(state); + AbstractPoolEntry entry = getPoolEntry(); + assertValid(entry); + entry.setState(state); } } diff --git a/httpclient/src/main/java/org/apache/http/impl/conn/tsccm/BasicPooledConnAdapter.java b/httpclient/src/main/java/org/apache/http/impl/conn/tsccm/BasicPooledConnAdapter.java index e304f6a61..607fde7b2 100644 --- a/httpclient/src/main/java/org/apache/http/impl/conn/tsccm/BasicPooledConnAdapter.java +++ b/httpclient/src/main/java/org/apache/http/impl/conn/tsccm/BasicPooledConnAdapter.java @@ -51,27 +51,22 @@ public class BasicPooledConnAdapter extends AbstractPooledConnAdapter { markReusable(); } - @Override protected ClientConnectionManager getManager() { // override needed only to make method visible in this package return super.getManager(); } - - /** - * Obtains the pool entry. - * - * @return the pool entry, or null if detached - */ + @Override protected AbstractPoolEntry getPoolEntry() { - return super.poolEntry; + // override needed only to make method visible in this package + return super.getPoolEntry(); } - @Override protected void detach() { // override needed only to make method visible in this package super.detach(); } + }