hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1368428 - in /httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src: main/java/org/apache/http/client/methods/ main/java/org/apache/http/impl/client/ main/java/org/apache/http/impl/client/exec/ test/java/org/apache/http/i...
Date Thu, 02 Aug 2012 11:50:58 GMT
Author: olegk
Date: Thu Aug  2 11:50:58 2012
New Revision: 1368428

URL: http://svn.apache.org/viewvc?rev=1368428&view=rev
Log:
Made HttpExecutionAware use generic Cancellable interface for ongoing operations

Modified:
    httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/client/methods/HttpExecutionAware.java
    httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/client/methods/HttpRequestBase.java
    httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/InternalHttpClient.java
    httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/exec/MainClientExec.java
    httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/test/java/org/apache/http/impl/client/integration/TestAbortHandling.java

Modified: httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/client/methods/HttpExecutionAware.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/client/methods/HttpExecutionAware.java?rev=1368428&r1=1368427&r2=1368428&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/client/methods/HttpExecutionAware.java
(original)
+++ httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/client/methods/HttpExecutionAware.java
Thu Aug  2 11:50:58 2012
@@ -27,9 +27,7 @@
 
 package org.apache.http.client.methods;
 
-import org.apache.http.conn.ClientConnectionRequest;
-import org.apache.http.conn.ConnectionReleaseTrigger;
-import org.apache.http.conn.ManagedClientConnection;
+import org.apache.http.concurrent.Cancellable;
 
 /**
  * Interface to be implemented by any object that wishes to be notified of request execution
@@ -42,17 +40,9 @@ public interface HttpExecutionAware {
     boolean isAborted();
 
     /**
-     * Sets the {@link ClientConnectionRequest} callback that can be
-     * used to abort a long-lived request for a connection.
+     * Sets {@link Cancellable} for the ongoing operation.
      */
-    void setConnectionRequest(ClientConnectionRequest connRequest);
-
-    /**
-     * Sets the {@link ConnectionReleaseTrigger} callback that can
-     * be used to abort an active connection.
-     * Typically, this will be the {@link ManagedClientConnection} itself.
-     */
-    void setReleaseTrigger(ConnectionReleaseTrigger releaseTrigger);
+    void setCancellable(Cancellable cancellable);
 
 }
 

Modified: httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/client/methods/HttpRequestBase.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/client/methods/HttpRequestBase.java?rev=1368428&r1=1368427&r2=1368428&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/client/methods/HttpRequestBase.java
(original)
+++ httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/client/methods/HttpRequestBase.java
Thu Aug  2 11:50:58 2012
@@ -37,6 +37,7 @@ import org.apache.http.annotation.NotThr
 import org.apache.http.ProtocolVersion;
 import org.apache.http.RequestLine;
 import org.apache.http.client.utils.CloneUtils;
+import org.apache.http.concurrent.Cancellable;
 import org.apache.http.conn.ClientConnectionRequest;
 import org.apache.http.conn.ConnectionReleaseTrigger;
 import org.apache.http.message.AbstractHttpMessage;
@@ -60,8 +61,7 @@ public abstract class HttpRequestBase ex
     private volatile boolean aborted;
 
     private URI uri;
-    private ClientConnectionRequest connRequest;
-    private ConnectionReleaseTrigger releaseTrigger;
+    private Cancellable cancellable;
 
     public HttpRequestBase() {
         super();
@@ -102,41 +102,54 @@ public abstract class HttpRequestBase ex
         this.uri = uri;
     }
 
+    @Deprecated
     public void setConnectionRequest(final ClientConnectionRequest connRequest) {
         if (this.aborted) {
             return;
         }
         this.abortLock.lock();
         try {
-            this.connRequest = connRequest;
+            this.cancellable = new Cancellable() {
+
+                public boolean cancel() {
+                    connRequest.abortRequest();
+                    return true;
+                }
+
+            };
         } finally {
             this.abortLock.unlock();
         }
     }
 
+    @Deprecated
     public void setReleaseTrigger(final ConnectionReleaseTrigger releaseTrigger) {
         if (this.aborted) {
             return;
         }
         this.abortLock.lock();
         try {
-            this.releaseTrigger = releaseTrigger;
+            this.cancellable = new Cancellable() {
+
+                public boolean cancel() {
+                    try {
+                        releaseTrigger.abortConnection();
+                        return true;
+                    } catch (IOException ex) {
+                        return false;
+                    }
+                }
+
+            };
         } finally {
             this.abortLock.unlock();
         }
     }
 
-    private void cleanup() {
-        if (this.connRequest != null) {
-            this.connRequest.abortRequest();
-            this.connRequest = null;
-        }
-        if (this.releaseTrigger != null) {
-            try {
-                this.releaseTrigger.abortConnection();
-            } catch (IOException ex) {
-            }
-            this.releaseTrigger = null;
+    private void cancelExecution() {
+        if (this.cancellable != null) {
+            this.cancellable.cancel();
+            this.cancellable = null;
         }
     }
 
@@ -147,7 +160,7 @@ public abstract class HttpRequestBase ex
         this.abortLock.lock();
         try {
             this.aborted = true;
-            cleanup();
+            cancelExecution();
         } finally {
             this.abortLock.unlock();
         }
@@ -158,6 +171,39 @@ public abstract class HttpRequestBase ex
     }
 
     /**
+     * @since 4.2
+     */
+    public void started() {
+    }
+
+    /**
+     * @since 4.2
+     */
+    public void setCancellable(final Cancellable cancellable) {
+        if (this.aborted) {
+            return;
+        }
+        this.abortLock.lock();
+        try {
+            this.cancellable = cancellable;
+        } finally {
+            this.abortLock.unlock();
+        }
+    }
+
+    /**
+     * @since 4.2
+     */
+    public void completed() {
+        this.abortLock.lock();
+        try {
+            this.cancellable = null;
+        } finally {
+            this.abortLock.unlock();
+        }
+    }
+
+    /**
      * Resets internal state of the request making it reusable.
      *
      * @since 4.2
@@ -165,7 +211,7 @@ public abstract class HttpRequestBase ex
     public void reset() {
         this.abortLock.lock();
         try {
-            cleanup();
+            cancelExecution();
             this.aborted = false;
         } finally {
             this.abortLock.unlock();
@@ -187,8 +233,7 @@ public abstract class HttpRequestBase ex
         HttpRequestBase clone = (HttpRequestBase) super.clone();
         clone.abortLock = new ReentrantLock();
         clone.aborted = false;
-        clone.releaseTrigger = null;
-        clone.connRequest = null;
+        clone.cancellable = null;
         clone.headergroup = (HeaderGroup) CloneUtils.clone(this.headergroup);
         clone.params = (HttpParams) CloneUtils.clone(this.params);
         return clone;

Modified: httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/InternalHttpClient.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/InternalHttpClient.java?rev=1368428&r1=1368427&r2=1368428&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/InternalHttpClient.java
(original)
+++ httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/InternalHttpClient.java
Thu Aug  2 11:50:58 2012
@@ -145,6 +145,10 @@ class InternalHttpClient extends Abstrac
         if (request == null) {
             throw new IllegalArgumentException("Request must not be null.");
         }
+        HttpExecutionAware execListner = null;
+        if (request instanceof HttpExecutionAware) {
+            execListner = (HttpExecutionAware) request;
+        }
         try {
             HttpParams params = new DefaultedHttpParams(request.getParams(), getParams());
             HttpHost virtualHost = (HttpHost) params.getParameter(ClientPNames.VIRTUAL_HOST);
@@ -152,13 +156,6 @@ class InternalHttpClient extends Abstrac
             HttpRequestWrapper wrapper = HttpRequestWrapper.wrap(request);
             wrapper.setParams(params);
             wrapper.setVirtualHost(virtualHost);
-            HttpExecutionAware execListner = null;
-            if (request instanceof HttpExecutionAware) {
-                execListner = (HttpExecutionAware) request;
-                if (execListner.isAborted()) {
-                    throw new RequestAbortedException("Request aborted");
-                }
-            }
             HttpRoute route = determineRoute(target, wrapper, context);
             return this.execChain.execute(route, wrapper, setupContext(context), execListner);
         } catch (HttpException httpException) {

Modified: httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/exec/MainClientExec.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/exec/MainClientExec.java?rev=1368428&r1=1368427&r2=1368428&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/exec/MainClientExec.java
(original)
+++ httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/main/java/org/apache/http/impl/client/exec/MainClientExec.java
Thu Aug  2 11:50:58 2012
@@ -52,9 +52,11 @@ import org.apache.http.client.methods.Ht
 import org.apache.http.client.params.HttpClientParams;
 import org.apache.http.client.protocol.ClientContext;
 import org.apache.http.client.protocol.RequestClientConnControl;
+import org.apache.http.concurrent.Cancellable;
 import org.apache.http.conn.ClientConnectionManager;
 import org.apache.http.conn.ClientConnectionRequest;
 import org.apache.http.conn.ConnectionKeepAliveStrategy;
+import org.apache.http.conn.ConnectionReleaseTrigger;
 import org.apache.http.conn.ManagedClientConnection;
 import org.apache.http.conn.routing.BasicRouteDirector;
 import org.apache.http.conn.routing.HttpRoute;
@@ -193,13 +195,20 @@ public class MainClientExec implements C
 
         Object userToken = context.getAttribute(ClientContext.USER_TOKEN);
 
-        ClientConnectionRequest connRequest = connManager.requestConnection(route, userToken);
+        final ClientConnectionRequest connRequest = connManager.requestConnection(route,
userToken);
         if (execAware != null) {
             if (execAware.isAborted()) {
                 connRequest.abortRequest();
                 throw new RequestAbortedException("Request aborted");
             } else {
-                execAware.setConnectionRequest(connRequest);
+                execAware.setCancellable(new Cancellable() {
+
+                    public boolean cancel() {
+                        connRequest.abortRequest();
+                        return true;
+                    }
+
+                });
             }
         }
 
@@ -224,6 +233,27 @@ public class MainClientExec implements C
             }
         }
 
+        if (execAware != null) {
+            if (execAware.isAborted()) {
+                managedConn.releaseConnection();
+                throw new RequestAbortedException("Request aborted");
+            } else {
+                final ConnectionReleaseTrigger trigger = managedConn;
+                execAware.setCancellable(new Cancellable() {
+
+                    public boolean cancel() {
+                        try {
+                            trigger.abortConnection();
+                        } catch (IOException ex) {
+                            log.debug("I/O error aborting connection", ex);
+                        }
+                        return true;
+                    }
+
+                });
+            }
+        }
+
         try {
             HttpResponse response = null;
             for (int execCount = 1;; execCount++) {
@@ -233,13 +263,8 @@ public class MainClientExec implements C
                             "with a non-repeatable request entity.");
                 }
 
-                if (execAware != null) {
-                    if (execAware.isAborted()) {
-                        managedConn.releaseConnection();
-                        throw new RequestAbortedException("Request aborted");
-                    } else {
-                        execAware.setReleaseTrigger(managedConn);
-                    }
+                if (execAware != null && execAware.isAborted()) {
+                    throw new RequestAbortedException("Request aborted");
                 }
 
                 if (!managedConn.isOpen()) {

Modified: httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/test/java/org/apache/http/impl/client/integration/TestAbortHandling.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/test/java/org/apache/http/impl/client/integration/TestAbortHandling.java?rev=1368428&r1=1368427&r2=1368428&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/test/java/org/apache/http/impl/client/integration/TestAbortHandling.java
(original)
+++ httpcomponents/httpclient/branches/decorator-refactoring/httpclient/src/test/java/org/apache/http/impl/client/integration/TestAbortHandling.java
Thu Aug  2 11:50:58 2012
@@ -40,10 +40,10 @@ import org.apache.http.HttpStatus;
 import org.apache.http.ProtocolVersion;
 import org.apache.http.client.HttpClient;
 import org.apache.http.client.methods.HttpGet;
+import org.apache.http.concurrent.Cancellable;
 import org.apache.http.conn.ClientConnectionManager;
 import org.apache.http.conn.ClientConnectionRequest;
 import org.apache.http.conn.ConnectionPoolTimeoutException;
-import org.apache.http.conn.ConnectionReleaseTrigger;
 import org.apache.http.conn.ManagedClientConnection;
 import org.apache.http.conn.routing.HttpRoute;
 import org.apache.http.conn.scheme.Scheme;
@@ -481,7 +481,7 @@ public class TestAbortHandling extends I
         }
 
         @Override
-        public void setReleaseTrigger(ConnectionReleaseTrigger releaseTrigger) {
+        public void setCancellable(Cancellable cancellable) {
             try {
                 if(!releaseTriggerLatch.await(1, TimeUnit.SECONDS))
                     throw new RuntimeException("Waited too long...");
@@ -489,7 +489,7 @@ public class TestAbortHandling extends I
                 throw new RuntimeException(ie);
             }
 
-            super.setReleaseTrigger(releaseTrigger);
+            super.setCancellable(cancellable);
         }
 
     }



Mime
View raw message