HTTPCLIENT-881: More code cleanups

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@826690 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2009-10-19 14:35:03 +00:00
parent 3d991231f0
commit eb1205a223
4 changed files with 78 additions and 71 deletions

View File

@ -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;

View File

@ -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();

View File

@ -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 <code>null</code> 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);
}
}

View File

@ -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 <code>null</code> 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();
}
}