hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1567935 - in /httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache: CachingHttpClientBuilder.java ManagedHttpCacheStorage.java
Date Thu, 13 Feb 2014 14:11:18 GMT
Author: olegk
Date: Thu Feb 13 14:11:18 2014
New Revision: 1567935

URL: http://svn.apache.org/r1567935
Log:
HTTPCLIENT-1460: ManagedHttpCacheStorage to keep valid cache entries upon close

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

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=1567935&r1=1567934&r2=1567935&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
Thu Feb 13 14:11:18 2014
@@ -26,7 +26,9 @@
  */
 package org.apache.http.impl.client.cache;
 
+import java.io.Closeable;
 import java.io.File;
+import java.io.IOException;
 
 import org.apache.http.client.cache.HttpCacheInvalidator;
 import org.apache.http.client.cache.HttpCacheStorage;
@@ -48,6 +50,7 @@ public class CachingHttpClientBuilder ex
     private CacheConfig cacheConfig;
     private SchedulingStrategy schedulingStrategy;
     private HttpCacheInvalidator httpCacheInvalidator;
+    private boolean deleteCache;
 
     public static CachingHttpClientBuilder create() {
         return new CachingHttpClientBuilder();
@@ -55,6 +58,7 @@ public class CachingHttpClientBuilder ex
 
     protected CachingHttpClientBuilder() {
         super();
+        this.deleteCache = true;
     }
 
     public final CachingHttpClientBuilder setResourceFactory(
@@ -93,6 +97,11 @@ public class CachingHttpClientBuilder ex
         return this;
     }
 
+    public CachingHttpClientBuilder setDeleteCache(final boolean deleteCache) {
+        this.deleteCache = deleteCache;
+        return this;
+    }
+
     @Override
     protected ClientExecChain decorateMainExec(final ClientExecChain mainExec) {
         final CacheConfig config = this.cacheConfig != null ? this.cacheConfig : CacheConfig.DEFAULT;
@@ -110,7 +119,18 @@ public class CachingHttpClientBuilder ex
                 storage = new BasicHttpCacheStorage(config);
             } else {
                 final ManagedHttpCacheStorage managedStorage = new ManagedHttpCacheStorage(config);
-                addCloseable(managedStorage);
+                if (this.deleteCache) {
+                    addCloseable(new Closeable() {
+
+                        @Override
+                        public void close() throws IOException {
+                            managedStorage.shutdown();
+                        }
+
+                    });
+                } else {
+                    addCloseable(managedStorage);
+                }
                 storage = managedStorage;
             }
         }

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=1567935&r1=1567934&r2=1567935&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
Thu Feb 13 14:11:18 2014
@@ -49,9 +49,17 @@ import org.apache.http.util.Args;
  * call {@link #cleanResources()} method to trigger resource deallocation. The cache can
be
  * permanently shut down using {@link #shutdown()} method. All resources associated with
  * the entries used by the cache will be deallocated.
- *
+ * <p/>
  * This {@link HttpCacheStorage} implementation is intended for use with {@link FileResource}
  * and similar.
+ * <p/>
+ * Compatibility note. Prior to version 4.4 this storage implementation used to dispose of
+ * all resource entries upon {@link #close()}. As of version 4.4 the {@link #close()} method
+ * disposes only of those resources that have been explicitly removed from the cache with
+ * {@link #removeEntry(String)} method.
+ * <p/>
+ * The {@link #shutdown()} ()} method can still be used to shut down the storage and dispose
of
+ * all resources currently managed by it.
  *
  * @since 4.1
  */
@@ -162,7 +170,15 @@ public class ManagedHttpCacheStorage imp
 
     @Override
     public void close() {
-        shutdown();
+        if (this.active.compareAndSet(true, false)) {
+            synchronized (this) {
+                ResourceReference ref;
+                while ((ref = (ResourceReference) this.morque.poll()) != null) {
+                    this.resources.remove(ref);
+                    ref.getResource().dispose();
+                }
+            }
+        }
     }
 
 }



Mime
View raw message