You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2009/10/19 16:35:03 UTC

svn commit: r826690 - in /httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl: client/DefaultRequestDirector.java conn/AbstractClientConnAdapter.java conn/AbstractPooledConnAdapter.java conn/tsccm/BasicPooledConnAdapter.java

Author: olegk
Date: Mon Oct 19 14:35:03 2009
New Revision: 826690

URL: http://svn.apache.org/viewvc?rev=826690&view=rev
Log:
HTTPCLIENT-881: More code cleanups

Modified:
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/AbstractClientConnAdapter.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/AbstractPooledConnAdapter.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/tsccm/BasicPooledConnAdapter.java

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java?rev=826690&r1=826689&r2=826690&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java Mon Oct 19 14:35:03 2009
@@ -561,7 +561,10 @@
             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;

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/AbstractClientConnAdapter.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/AbstractClientConnAdapter.java?rev=826690&r1=826689&r2=826690&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/AbstractClientConnAdapter.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/AbstractClientConnAdapter.java Mon Oct 19 14:35:03 2009
@@ -81,7 +81,7 @@
     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 @@
         connManager = mgr;
         wrappedConnection = conn;
         markedReusable = false;
-        shutdown = false;
+        released = false;
         duration = Long.MAX_VALUE;
     }
 
@@ -123,25 +123,32 @@
     }
     
     /**
-     * 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 boolean isStale() {
-        if (shutdown)
+        if (isReleased())
             return true;
         OperatedClientConnection conn = getWrappedConnection();
         if (conn == null)
@@ -182,66 +189,46 @@
         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 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();

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/AbstractPooledConnAdapter.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/AbstractPooledConnAdapter.java?rev=826690&r1=826689&r2=826690&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/AbstractPooledConnAdapter.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/AbstractPooledConnAdapter.java Mon Oct 19 14:35:03 2009
@@ -63,12 +63,32 @@
         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 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 void shutdown() throws IOException {
-        AbstractPoolEntry entry = poolEntry;
+        AbstractPoolEntry entry = getPoolEntry();
         if (entry != null)
             entry.shutdownEntry();
 
@@ -144,13 +164,15 @@
     }
 
     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);
     }
 
 }

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/tsccm/BasicPooledConnAdapter.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/tsccm/BasicPooledConnAdapter.java?rev=826690&r1=826689&r2=826690&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/tsccm/BasicPooledConnAdapter.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/tsccm/BasicPooledConnAdapter.java Mon Oct 19 14:35:03 2009
@@ -51,27 +51,22 @@
         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();
     }
+
 }