hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1423960 - in /httpcomponents/httpclient/trunk: httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ httpclient/src/main/java/org/apache/http/impl/client/ httpclient/src/main/java/org/apache/http/impl/conn/ httpclient/src/test/...
Date Wed, 19 Dec 2012 17:12:04 GMT
Author: olegk
Date: Wed Dec 19 17:12:03 2012
New Revision: 1423960

URL: http://svn.apache.org/viewvc?rev=1423960&view=rev
Log:
Authmatically shut down file system based cache storage instantiated by CachingHttpClientBuilder

Modified:
    httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClientBuilder.java
    httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ManagedHttpCacheStorage.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpClientBuilder.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/InternalHttpClient.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/BasicHttpClientConnectionManager.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/PoolingHttpClientConnectionManager.java
    httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestAbortHandling.java

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClientBuilder.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClientBuilder.java?rev=1423960&r1=1423959&r2=1423960&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClientBuilder.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClientBuilder.java
Wed Dec 19 17:12:03 2012
@@ -88,6 +88,13 @@ public class CachingHttpClientBuilder ex
         }
         HttpCacheStorage storage = this.storage;
         if (storage == null) {
+            if (this.cacheDir == null) {
+                storage = new BasicHttpCacheStorage(cacheConfig);
+            } else {
+                ManagedHttpCacheStorage managedStorage = new ManagedHttpCacheStorage(cacheConfig);
+                addCloseable(managedStorage);
+                storage = managedStorage;
+            }
             storage = new BasicHttpCacheStorage(cacheConfig);
         }
         return new CachingExec(mainExec,

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ManagedHttpCacheStorage.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ManagedHttpCacheStorage.java?rev=1423960&r1=1423959&r2=1423960&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ManagedHttpCacheStorage.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ManagedHttpCacheStorage.java
Wed Dec 19 17:12:03 2012
@@ -26,6 +26,7 @@
  */
 package org.apache.http.impl.client.cache;
 
+import java.io.Closeable;
 import java.io.IOException;
 import java.lang.ref.PhantomReference;
 import java.lang.ref.ReferenceQueue;
@@ -54,7 +55,7 @@ import org.apache.http.util.Args;
  * @since 4.1
  */
 @ThreadSafe
-public class ManagedHttpCacheStorage implements HttpCacheStorage {
+public class ManagedHttpCacheStorage implements HttpCacheStorage, Closeable {
 
     private final CacheMap entries;
     private final ReferenceQueue<HttpCacheEntry> morque;
@@ -157,4 +158,8 @@ public class ManagedHttpCacheStorage imp
         }
     }
 
+    public void close() {
+        shutdown();
+    }
+
 }

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpClientBuilder.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpClientBuilder.java?rev=1423960&r1=1423959&r2=1423960&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpClientBuilder.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/HttpClientBuilder.java
Wed Dec 19 17:12:03 2012
@@ -27,10 +27,13 @@
 
 package org.apache.http.impl.client;
 
+import java.io.Closeable;
 import java.net.ProxySelector;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.LinkedList;
+import java.util.List;
 import java.util.Map;
 
 import org.apache.http.ConnectionReuseStrategy;
@@ -181,6 +184,8 @@ public class HttpClientBuilder {
     private int maxConnTotal = 0;
     private int maxConnPerRoute = 0;
 
+    private List<Closeable> closeables;
+
     public static HttpClientBuilder create() {
         return new HttpClientBuilder();
     }
@@ -431,6 +436,17 @@ public class HttpClientBuilder {
         return protocolExec;
     }
 
+    protected void addCloseable(final Closeable closeable) {
+        if (closeable == null) {
+            return;
+        }
+        if (closeables == null) {
+            closeables = new ArrayList<Closeable>();
+        }
+        closeables.add(closeable);
+    }
+
+
     public CloseableHttpClient build() {
         // Create main request executor
         HttpRequestExecutor requestExec = this.requestExec;
@@ -679,7 +695,8 @@ public class HttpClientBuilder {
                 authSchemeRegistry,
                 defaultCookieStore,
                 defaultCredentialsProvider,
-                defaultRequestConfig != null ? defaultRequestConfig : RequestConfig.DEFAULT);
+                defaultRequestConfig != null ? defaultRequestConfig : RequestConfig.DEFAULT,
+                closeables != null ? new ArrayList<Closeable>(closeables) : null);
     }
 
 }

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/InternalHttpClient.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/InternalHttpClient.java?rev=1423960&r1=1423959&r2=1423960&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/InternalHttpClient.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/InternalHttpClient.java
Wed Dec 19 17:12:03 2012
@@ -27,9 +27,13 @@
 
 package org.apache.http.impl.client;
 
+import java.io.Closeable;
 import java.io.IOException;
+import java.util.List;
 import java.util.concurrent.TimeUnit;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.apache.http.HttpException;
 import org.apache.http.HttpHost;
 import org.apache.http.HttpRequest;
@@ -72,6 +76,8 @@ import org.apache.http.util.Asserts;
 @SuppressWarnings("deprecation")
 class InternalHttpClient extends CloseableHttpClient {
 
+    private final Log log = LogFactory.getLog(getClass());
+
     private final ClientExecChain execChain;
     private final HttpClientConnectionManager connManager;
     private final HttpRoutePlanner routePlanner;
@@ -80,6 +86,7 @@ class InternalHttpClient extends Closeab
     private final CookieStore cookieStore;
     private final CredentialsProvider credentialsProvider;
     private final RequestConfig defaultConfig;
+    private final List<Closeable> closeables;
     private final HttpParams params;
 
     public InternalHttpClient(
@@ -90,7 +97,8 @@ class InternalHttpClient extends Closeab
             final Lookup<AuthSchemeProvider> authSchemeRegistry,
             final CookieStore cookieStore,
             final CredentialsProvider credentialsProvider,
-            final RequestConfig defaultConfig) {
+            final RequestConfig defaultConfig,
+            final List<Closeable> closeables) {
         super();
         Args.notNull(execChain, "HTTP client exec chain");
         Args.notNull(connManager, "HTTP connection manager");
@@ -103,6 +111,7 @@ class InternalHttpClient extends Closeab
         this.cookieStore = cookieStore;
         this.credentialsProvider = credentialsProvider;
         this.defaultConfig = defaultConfig;
+        this.closeables = closeables;
         this.params = new BasicHttpParams();
     }
 
@@ -179,6 +188,15 @@ class InternalHttpClient extends Closeab
 
     public void close() {
         this.connManager.shutdown();
+        if (this.closeables != null) {
+            for (Closeable closeable: this.closeables) {
+                try {
+                    closeable.close();
+                } catch (IOException ex) {
+                    this.log.error(ex.getMessage(), ex);
+                }
+            }
+        }
     }
 
     public ClientConnectionManager getConnectionManager() {

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/BasicHttpClientConnectionManager.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/BasicHttpClientConnectionManager.java?rev=1423960&r1=1423959&r2=1423960&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/BasicHttpClientConnectionManager.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/BasicHttpClientConnectionManager.java
Wed Dec 19 17:12:03 2012
@@ -27,6 +27,7 @@
 
 package org.apache.http.impl.conn;
 
+import java.io.Closeable;
 import java.io.IOException;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
@@ -76,7 +77,7 @@ import org.apache.http.util.LangUtils;
  * @since 4.3
  */
 @ThreadSafe
-public class BasicHttpClientConnectionManager implements HttpClientConnectionManager {
+public class BasicHttpClientConnectionManager implements HttpClientConnectionManager, Closeable
{
 
     private final Log log = LogFactory.getLog(getClass());
 
@@ -155,6 +156,10 @@ public class BasicHttpClientConnectionMa
         }
     }
 
+    public void close() {
+        shutdown();
+    }
+
     HttpRoute getRoute() {
         return route;
     }

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/PoolingHttpClientConnectionManager.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/PoolingHttpClientConnectionManager.java?rev=1423960&r1=1423959&r2=1423960&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/PoolingHttpClientConnectionManager.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/conn/PoolingHttpClientConnectionManager.java
Wed Dec 19 17:12:03 2012
@@ -26,6 +26,7 @@
 
 package org.apache.http.impl.conn;
 
+import java.io.Closeable;
 import java.io.IOException;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
@@ -83,7 +84,7 @@ import org.apache.http.util.Asserts;
  * @since 4.3
  */
 @ThreadSafe
-public class PoolingHttpClientConnectionManager implements HttpClientConnectionManager {
+public class PoolingHttpClientConnectionManager implements HttpClientConnectionManager, Closeable
{
 
     private final Log log = LogFactory.getLog(getClass());
 
@@ -159,6 +160,10 @@ public class PoolingHttpClientConnection
         }
     }
 
+    public void close() {
+        shutdown();
+    }
+
     private String format(final HttpRoute route, final Object state) {
         StringBuilder buf = new StringBuilder();
         buf.append("[route: ").append(route).append("]");

Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestAbortHandling.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestAbortHandling.java?rev=1423960&r1=1423959&r2=1423960&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestAbortHandling.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestAbortHandling.java
Wed Dec 19 17:12:03 2012
@@ -447,6 +447,9 @@ public class TestAbortHandling extends I
         public void shutdown() {
         }
 
+        public void close() {
+        }
+
         public void releaseConnection(
                 final HttpClientConnection conn,
                 final Object newState,



Mime
View raw message