hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r617817 - in /httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn: AbstractClientConnAdapter.java AbstractPooledConnAdapter.java SingleClientConnManager.java tsccm/BasicPooledConnAdapter.java
Date Sat, 02 Feb 2008 15:47:08 GMT
Author: olegk
Date: Sat Feb  2 07:47:07 2008
New Revision: 617817

URL: http://svn.apache.org/viewvc?rev=617817&view=rev
Log:
HTTPCLIENT-741: Ensure thread safety for AbstractClientConnAdapter and its subclasses 

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

Modified: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/AbstractClientConnAdapter.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/AbstractClientConnAdapter.java?rev=617817&r1=617816&r2=617817&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/AbstractClientConnAdapter.java
(original)
+++ httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/AbstractClientConnAdapter.java
Sat Feb  2 07:47:07 2008
@@ -32,6 +32,7 @@
 
 
 import java.io.IOException;
+import java.io.InterruptedIOException;
 import java.net.InetAddress;
 import java.net.Socket;
 import javax.net.ssl.SSLSocket;
@@ -82,14 +83,15 @@
      * This attribute MUST NOT be final, so the adapter can be detached
      * from the connection manager without keeping a hard reference there.
      */
-    protected ClientConnectionManager connManager;
+    private volatile ClientConnectionManager connManager;
 
     /** The wrapped connection. */
-    protected OperatedClientConnection wrappedConnection;
+    private volatile OperatedClientConnection wrappedConnection;
 
     /** The reusability marker. */
-    protected boolean markedReusable;
+    private volatile boolean markedReusable;
 
+    private volatile boolean aborted;
 
     /**
      * Creates a new connection adapter.
@@ -105,58 +107,101 @@
         connManager = mgr;
         wrappedConnection = conn;
         markedReusable = false;
+        aborted = false;
 
     } // <constructor>
 
 
     /**
+     * Detaches this adapter from the wrapped connection.
+     * This adapter becomes useless.
+     */
+    protected void detach() {
+        wrappedConnection = null;
+        connManager = null; // base class attribute
+    }
+
+    protected OperatedClientConnection getWrappedConnection() {
+        return wrappedConnection;
+    }
+    
+    protected ClientConnectionManager getManager() {
+        return connManager;
+    }
+    
+    /**
      * Asserts that there is a wrapped connection to delegate to.
      *
      * @throws IllegalStateException    if there is no wrapped connection
+     * @throws InterruptedIOException   if the connection has been aborted
      */
-    protected final void assertWrappedConn() {
-        if (wrappedConnection == null) {
+    protected final void assertIOState(
+            final OperatedClientConnection wrappedConn) throws InterruptedIOException {
+        if (aborted) {
+            throw new InterruptedIOException("Connection has been shut down");
+        }
+        if (wrappedConn == null) {
             throw new IllegalStateException("No wrapped connection.");
         }
     }
 
+    /**
+     * Asserts that there is a wrapped connection to delegate to.
+     *
+     * @throws IllegalStateException    if there is no wrapped connection
+     *                                  or connection has been aborted
+     */
+    protected final void assertState(
+            final OperatedClientConnection wrappedConn) {
+        if (aborted) {
+            throw new IllegalStateException("Connection has been shut down");
+        }
+        if (wrappedConn == null) {
+            throw new IllegalStateException("No wrapped connection.");
+        }
+    }
 
     // non-javadoc, see interface HttpConnection
     public boolean isOpen() {
-        if (wrappedConnection == null)
+        OperatedClientConnection conn = getWrappedConnection();
+        if (conn == null)
             return false;
 
-        return wrappedConnection.isOpen();
+        return conn.isOpen();
     }
 
 
     // non-javadoc, see interface HttpConnection
     public boolean isStale() {
-        if (wrappedConnection == null)
+        OperatedClientConnection conn = getWrappedConnection();
+        if (conn == null)
             return true;
 
-        return wrappedConnection.isStale();
+        return conn.isStale();
     }
 
 
     // non-javadoc, see interface HttpConnection
     public void setSocketTimeout(int timeout) {
-        assertWrappedConn();
-        wrappedConnection.setSocketTimeout(timeout);
+        OperatedClientConnection conn = getWrappedConnection();
+        assertState(conn);
+        conn.setSocketTimeout(timeout);
     }
 
 
     // non-javadoc, see interface HttpConnection
     public int getSocketTimeout() {
-        assertWrappedConn();
-        return wrappedConnection.getSocketTimeout();
+        OperatedClientConnection conn = getWrappedConnection();
+        assertState(conn);
+        return conn.getSocketTimeout();
     }
 
 
     // non-javadoc, see interface HttpConnection
     public HttpConnectionMetrics getMetrics() {
-        assertWrappedConn();
-        return wrappedConnection.getMetrics();
+        OperatedClientConnection conn = getWrappedConnection();
+        assertState(conn);
+        return conn.getMetrics();
     }
 
 
@@ -164,8 +209,9 @@
     public void flush()
         throws IOException {
 
-        assertWrappedConn();
-        wrappedConnection.flush();
+        OperatedClientConnection conn = getWrappedConnection();
+        assertIOState(conn);
+        conn.flush();
     }
 
 
@@ -173,8 +219,9 @@
     public boolean isResponseAvailable(int timeout)
         throws IOException {
 
-        assertWrappedConn();
-        return wrappedConnection.isResponseAvailable(timeout);
+        OperatedClientConnection conn = getWrappedConnection();
+        assertIOState(conn);
+        return conn.isResponseAvailable(timeout);
     }
 
 
@@ -182,9 +229,10 @@
     public void receiveResponseEntity(HttpResponse response)
         throws HttpException, IOException {
 
-        assertWrappedConn();
-        markedReusable = false;
-        wrappedConnection.receiveResponseEntity(response);
+        OperatedClientConnection conn = getWrappedConnection();
+        assertIOState(conn);
+        unmarkReusable();
+        conn.receiveResponseEntity(response);
     }
 
 
@@ -192,9 +240,10 @@
     public HttpResponse receiveResponseHeader()
         throws HttpException, IOException {
 
-        assertWrappedConn();
-        markedReusable = false;
-        return wrappedConnection.receiveResponseHeader();
+        OperatedClientConnection conn = getWrappedConnection();
+        assertIOState(conn);
+        unmarkReusable();
+        return conn.receiveResponseHeader();
     }
 
 
@@ -202,9 +251,10 @@
     public void sendRequestEntity(HttpEntityEnclosingRequest request)
         throws HttpException, IOException {
 
-        assertWrappedConn();
-        markedReusable = false;
-        wrappedConnection.sendRequestEntity(request);
+        OperatedClientConnection conn = getWrappedConnection();
+        assertIOState(conn);
+        unmarkReusable();
+        conn.sendRequestEntity(request);
     }
 
 
@@ -212,50 +262,58 @@
     public void sendRequestHeader(HttpRequest request)
         throws HttpException, IOException {
 
-        assertWrappedConn();
-        markedReusable = false;
-        wrappedConnection.sendRequestHeader(request);
+        OperatedClientConnection conn = getWrappedConnection();
+        assertIOState(conn);
+        unmarkReusable();
+        conn.sendRequestHeader(request);
     }
 
 
     // non-javadoc, see interface HttpInetConnection
     public InetAddress getLocalAddress() {
-        assertWrappedConn();
-        return wrappedConnection.getLocalAddress();
+        OperatedClientConnection conn = getWrappedConnection();
+        assertState(conn);
+        return conn.getLocalAddress();
     }
 
     // non-javadoc, see interface HttpInetConnection
     public int getLocalPort() {
-        assertWrappedConn();
-        return wrappedConnection.getLocalPort();
+        OperatedClientConnection conn = getWrappedConnection();
+        assertState(conn);
+        return conn.getLocalPort();
     }
 
 
     // non-javadoc, see interface HttpInetConnection
     public InetAddress getRemoteAddress() {
-        assertWrappedConn();
-        return wrappedConnection.getRemoteAddress();
+        OperatedClientConnection conn = getWrappedConnection();
+        assertState(conn);
+        return conn.getRemoteAddress();
     }
 
     // non-javadoc, see interface HttpInetConnection
     public int getRemotePort() {
-        assertWrappedConn();
-        return wrappedConnection.getRemotePort();
+        OperatedClientConnection conn = getWrappedConnection();
+        assertState(conn);
+        return conn.getRemotePort();
     }
 
     // non-javadoc, see interface ManagedClientConnection
     public boolean isSecure() {
-        assertWrappedConn();
-        return wrappedConnection.isSecure();
+        OperatedClientConnection conn = getWrappedConnection();
+        assertState(conn);
+        return conn.isSecure();
     }
 
     // non-javadoc, see interface ManagedClientConnection
     public SSLSession getSSLSession() {
+        OperatedClientConnection conn = getWrappedConnection();
+        assertState(conn);
         if (!isOpen())
             return null;
 
         SSLSession result = null;
-        Socket    sock    = wrappedConnection.getSocket();
+        Socket    sock    = conn.getSocket();
         if (sock instanceof SSLSocket) {
             result = ((SSLSocket)sock).getSession();
         }
@@ -285,7 +343,7 @@
 
     // non-javadoc, see interface ConnectionReleaseTrigger
     public void abortConnection() {
-
+        aborted = true;
         unmarkReusable();
 
         if (connManager != null)

Modified: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/AbstractPooledConnAdapter.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/AbstractPooledConnAdapter.java?rev=617817&r1=617816&r2=617817&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/AbstractPooledConnAdapter.java
(original)
+++ httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/AbstractPooledConnAdapter.java
Sat Feb  2 07:47:07 2008
@@ -37,8 +37,8 @@
 import org.apache.http.params.HttpParams;
 import org.apache.http.protocol.HttpContext;
 import org.apache.http.conn.routing.HttpRoute;
-import org.apache.http.conn.ManagedClientConnection;
 import org.apache.http.conn.ClientConnectionManager;
+import org.apache.http.conn.OperatedClientConnection;
 
 
 
@@ -60,9 +60,7 @@
  *
  * @since 4.0
  */
-public abstract class AbstractPooledConnAdapter
-    extends AbstractClientConnAdapter
-    implements ManagedClientConnection {
+public abstract class AbstractPooledConnAdapter extends AbstractClientConnAdapter {
 
     /** The wrapped pool entry. */
     protected AbstractPoolEntry poolEntry;
@@ -97,10 +95,10 @@
      * Detaches this adapter from the wrapped connection.
      * This adapter becomes useless.
      */
+    @Override
     protected void detach() {
-        wrappedConnection = null;
+        super.detach();
         poolEntry = null;
-        connManager = null; // base class attribute
     }
 
 
@@ -155,8 +153,9 @@
         if (poolEntry != null)
             poolEntry.closing();
 
-        if (wrappedConnection != null) {
-            wrappedConnection.close();
+        OperatedClientConnection conn = getWrappedConnection();
+        if (conn != null) {
+            conn.close();
         }
     }
 
@@ -165,8 +164,9 @@
         if (poolEntry != null)
             poolEntry.closing();
 
-        if (wrappedConnection != null) {
-            wrappedConnection.shutdown();
+        OperatedClientConnection conn = getWrappedConnection();
+        if (conn != null) {
+            conn.shutdown();
         }
     }
 

Modified: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/SingleClientConnManager.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/SingleClientConnManager.java?rev=617817&r1=617816&r2=617817&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/SingleClientConnManager.java
(original)
+++ httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/SingleClientConnManager.java
Sat Feb  2 07:47:07 2008
@@ -253,7 +253,7 @@
                  "connection not obtained from this manager.");
         }
         ConnAdapter sca = (ConnAdapter) conn;
-        if (sca.connManager != this) {
+        if (sca.getManager() != this) {
             throw new IllegalArgumentException
                 ("Connection not obtained from this manager.");
         }
@@ -420,7 +420,7 @@
          */
         protected ConnAdapter(PoolEntry entry, HttpRoute plan) {
             super(SingleClientConnManager.this, entry);
-            super.markedReusable = true;
+            markReusable();
             entry.plannedRoute = plan;
         }
     }

Modified: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/BasicPooledConnAdapter.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/BasicPooledConnAdapter.java?rev=617817&r1=617816&r2=617817&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/BasicPooledConnAdapter.java
(original)
+++ httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/BasicPooledConnAdapter.java
Sat Feb  2 07:47:07 2008
@@ -53,17 +53,13 @@
     protected BasicPooledConnAdapter(ThreadSafeClientConnManager tsccm,
                                AbstractPoolEntry entry) {
         super(tsccm, entry);
-        super.markedReusable = true;
+        markReusable();
     }
 
 
-    /**
-     * Obtains the connection manager.
-     *
-     * @return  the connection manager, or <code>null</code> if detached
-     */
     protected ClientConnectionManager getManager() {
-        return super.connManager;
+        // override needed only to make method visible in this package
+        return super.getManager();
     }
 
 



Mime
View raw message