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/15 13:14:41 UTC

svn commit: r825464 - /httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java

Author: olegk
Date: Thu Oct 15 11:14:40 2009
New Revision: 825464

URL: http://svn.apache.org/viewvc?rev=825464&view=rev
Log:
HTTPCLIENT-824: Use only one immutable copy of HTTP protocol processor instead creating a new copy for every request execution 

Modified:
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java?rev=825464&r1=825463&r2=825464&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/AbstractHttpClient.java Thu Oct 15 11:14:40 2009
@@ -66,6 +66,7 @@
 import org.apache.http.protocol.HttpContext;
 import org.apache.http.protocol.HttpProcessor;
 import org.apache.http.protocol.HttpRequestExecutor;
+import org.apache.http.protocol.ImmutableHttpProcessor;
 
 /**
  * Base class for {@link HttpClient} implementations. This class acts as 
@@ -186,10 +187,13 @@
     @GuardedBy("this")
     private AuthSchemeRegistry supportedAuthSchemes;
     
-    /** The HTTP processor. */
+    /** The HTTP protocol processor and its immutable copy. */
     @GuardedBy("this")
-    private BasicHttpProcessor httpProcessor;
+    private BasicHttpProcessor mutableProcessor;
 
+    @GuardedBy("this")
+    private ImmutableHttpProcessor protocolProcessor;
+    
     /** The request retry handler. */
     @GuardedBy("this")
     private HttpRequestRetryHandler retryHandler;
@@ -482,20 +486,36 @@
     
     
     protected synchronized final BasicHttpProcessor getHttpProcessor() {
-        if (httpProcessor == null) {
-            httpProcessor = createHttpProcessor();
+        if (mutableProcessor == null) {
+            mutableProcessor = createHttpProcessor();
         }
-        return httpProcessor;
+        return mutableProcessor;
     }
 
 
-    public synchronized void addResponseInterceptor(final HttpResponseInterceptor itcp) {
-        getHttpProcessor().addInterceptor(itcp);
+    private synchronized final HttpProcessor getProtocolProcessor() {
+        if (protocolProcessor == null) {
+            // Get mutable HTTP processor
+            BasicHttpProcessor proc = getHttpProcessor();
+            // and create an immutable copy of it
+            int reqc = proc.getRequestInterceptorCount();
+            HttpRequestInterceptor[] reqinterceptors = new HttpRequestInterceptor[reqc];
+            for (int i = 0; i < reqc; i++) {
+                reqinterceptors[i] = proc.getRequestInterceptor(i);
+            }
+            int resc = proc.getResponseInterceptorCount();
+            HttpResponseInterceptor[] resinterceptors = new HttpResponseInterceptor[resc];
+            for (int i = 0; i < resc; i++) {
+                resinterceptors[i] = proc.getResponseInterceptor(i);
+            }
+            protocolProcessor = new ImmutableHttpProcessor(reqinterceptors, resinterceptors);
+        }
+        return protocolProcessor;
     }
 
 
-    public synchronized void addResponseInterceptor(final HttpResponseInterceptor itcp, int index) {
-        getHttpProcessor().addInterceptor(itcp, index);
+    public synchronized int getResponseInterceptorCount() {
+        return getHttpProcessor().getResponseInterceptorCount();
     }
 
 
@@ -504,48 +524,61 @@
     }
 
 
-    public synchronized int getResponseInterceptorCount() {
-        return getHttpProcessor().getResponseInterceptorCount();
+    public synchronized HttpRequestInterceptor getRequestInterceptor(int index) {
+        return getHttpProcessor().getRequestInterceptor(index);
+    }
+
+
+    public synchronized int getRequestInterceptorCount() {
+        return getHttpProcessor().getRequestInterceptorCount();
+    }
+
+
+    public synchronized void addResponseInterceptor(final HttpResponseInterceptor itcp) {
+        getHttpProcessor().addInterceptor(itcp);
+        protocolProcessor = null;
+    }
+
+
+    public synchronized void addResponseInterceptor(final HttpResponseInterceptor itcp, int index) {
+        getHttpProcessor().addInterceptor(itcp, index);
+        protocolProcessor = null;
     }
 
 
     public synchronized void clearResponseInterceptors() {
         getHttpProcessor().clearResponseInterceptors();
+        protocolProcessor = null;
     }
 
 
     public synchronized void removeResponseInterceptorByClass(Class<? extends HttpResponseInterceptor> clazz) {
         getHttpProcessor().removeResponseInterceptorByClass(clazz);
+        protocolProcessor = null;
     }
 
     
     public synchronized void addRequestInterceptor(final HttpRequestInterceptor itcp) {
         getHttpProcessor().addInterceptor(itcp);
+        protocolProcessor = null;
     }
 
 
     public synchronized void addRequestInterceptor(final HttpRequestInterceptor itcp, int index) {
         getHttpProcessor().addInterceptor(itcp, index);
-    }
-
-
-    public synchronized HttpRequestInterceptor getRequestInterceptor(int index) {
-        return getHttpProcessor().getRequestInterceptor(index);
-    }
-
-
-    public synchronized int getRequestInterceptorCount() {
-        return getHttpProcessor().getRequestInterceptorCount();
+        protocolProcessor = null;
     }
 
 
     public synchronized void clearRequestInterceptors() {
         getHttpProcessor().clearRequestInterceptors();
+        protocolProcessor = null;
     }
 
 
     public synchronized void removeRequestInterceptorByClass(Class<? extends HttpRequestInterceptor> clazz) {
         getHttpProcessor().removeRequestInterceptorByClass(clazz);
+        protocolProcessor = null;
     }
 
     public final HttpResponse execute(HttpUriRequest request)
@@ -628,7 +661,7 @@
                     getConnectionReuseStrategy(),
                     getConnectionKeepAliveStrategy(),
                     getRoutePlanner(),
-                    getHttpProcessor().copy(),
+                    getProtocolProcessor(),
                     getHttpRequestRetryHandler(),
                     getRedirectHandler(),
                     getTargetAuthenticationHandler(),