From 64c481f4bf2da157b54e962e69715d86dea20151 Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Fri, 16 Oct 2009 19:00:17 +0000 Subject: [PATCH] HTTPCLIENT-881: An I/O operation on an aborted or released connection will result in an InterruptedIOException git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@826037 13f79535-47bb-0310-9956-ffa450edef68 --- .../impl/client/DefaultRequestDirector.java | 3 ++ .../impl/conn/AbstractClientConnAdapter.java | 4 +- .../impl/conn/AbstractPooledConnAdapter.java | 21 ++++---- .../conn/ConnectionShutdownException.java | 50 +++++++++++++++++++ 4 files changed, 66 insertions(+), 12 deletions(-) create mode 100644 httpclient/src/main/java/org/apache/http/impl/conn/ConnectionShutdownException.java 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 31e7c3b9e..2d9578256 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 @@ -80,6 +80,7 @@ import org.apache.http.conn.routing.HttpRouteDirector; import org.apache.http.conn.routing.HttpRoutePlanner; import org.apache.http.conn.scheme.Scheme; import org.apache.http.entity.BufferedHttpEntity; +import org.apache.http.impl.conn.ConnectionShutdownException; import org.apache.http.message.BasicHttpRequest; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; @@ -559,6 +560,8 @@ public class DefaultRequestDirector implements RequestDirector { return response; + } catch (ConnectionShutdownException ex) { + throw new InterruptedIOException("Connection has been shut down"); } 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 55a73dd24..9c23a6949 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 @@ -129,7 +129,7 @@ public abstract class AbstractClientConnAdapter implements ManagedClientConnecti */ protected final void assertNotAborted() throws InterruptedIOException { if (shutdown) { - throw new InterruptedIOException("Connection has been shut down."); + throw new InterruptedIOException("Connection has been shut down"); } } @@ -142,7 +142,7 @@ public abstract class AbstractClientConnAdapter implements ManagedClientConnecti protected final void assertValid( final OperatedClientConnection wrappedConn) { if (wrappedConn == null) { - throw new IllegalStateException("No wrapped connection."); + throw new ConnectionShutdownException(); } } 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 2dbf9807e..eb723268e 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 @@ -72,7 +72,7 @@ public abstract class AbstractPooledConnAdapter extends AbstractClientConnAdapte */ protected final void assertAttached() { if (poolEntry == null) { - throw new IllegalStateException("Adapter is detached."); + throw new ConnectionShutdownException(); } } @@ -87,7 +87,6 @@ public abstract class AbstractPooledConnAdapter extends AbstractClientConnAdapte } public HttpRoute getRoute() { - assertAttached(); return (poolEntry.tracker == null) ? null : poolEntry.tracker.toRoute(); @@ -96,35 +95,36 @@ public abstract class AbstractPooledConnAdapter extends AbstractClientConnAdapte public void open(HttpRoute route, HttpContext context, HttpParams params) throws IOException { - + assertNotAborted(); assertAttached(); poolEntry.open(route, context, params); } public void tunnelTarget(boolean secure, HttpParams params) throws IOException { - + assertNotAborted(); assertAttached(); poolEntry.tunnelTarget(secure, params); } public void tunnelProxy(HttpHost next, boolean secure, HttpParams params) throws IOException { - + assertNotAborted(); assertAttached(); poolEntry.tunnelProxy(next, secure, params); } public void layerProtocol(HttpContext context, HttpParams params) throws IOException { - + assertNotAborted(); assertAttached(); poolEntry.layerProtocol(context, params); } public void close() throws IOException { - if (poolEntry != null) - poolEntry.shutdownEntry(); + AbstractPoolEntry entry = poolEntry; + if (entry != null) + entry.shutdownEntry(); OperatedClientConnection conn = getWrappedConnection(); if (conn != null) { @@ -133,8 +133,9 @@ public abstract class AbstractPooledConnAdapter extends AbstractClientConnAdapte } public void shutdown() throws IOException { - if (poolEntry != null) - poolEntry.shutdownEntry(); + AbstractPoolEntry entry = poolEntry; + if (entry != null) + entry.shutdownEntry(); OperatedClientConnection conn = getWrappedConnection(); if (conn != null) { diff --git a/httpclient/src/main/java/org/apache/http/impl/conn/ConnectionShutdownException.java b/httpclient/src/main/java/org/apache/http/impl/conn/ConnectionShutdownException.java new file mode 100644 index 000000000..c11c342fc --- /dev/null +++ b/httpclient/src/main/java/org/apache/http/impl/conn/ConnectionShutdownException.java @@ -0,0 +1,50 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ + +package org.apache.http.impl.conn; + +import org.apache.http.annotation.Immutable; + +/** + * Signals that the connection has been shut down or released back to the + * the connection pool + * + * @since 4.1 + */ +@Immutable +public class ConnectionShutdownException extends IllegalStateException { + + private static final long serialVersionUID = 5868657401162844497L; + + /** + * Creates a new ConnectionShutdownException with a null detail message. + */ + public ConnectionShutdownException() { + super(); + } + +}