diff --git a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClientBuilder.java b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClientBuilder.java index 04377dec3..31503bb20 100644 --- a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClientBuilder.java +++ b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClientBuilder.java @@ -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 extends HttpClientBuilder { private CacheConfig cacheConfig; private SchedulingStrategy schedulingStrategy; private HttpCacheInvalidator httpCacheInvalidator; + private boolean deleteCache; public static CachingHttpClientBuilder create() { return new CachingHttpClientBuilder(); @@ -55,6 +58,7 @@ public static CachingHttpClientBuilder create() { protected CachingHttpClientBuilder() { super(); + this.deleteCache = true; } public final CachingHttpClientBuilder setResourceFactory( @@ -93,6 +97,11 @@ public final CachingHttpClientBuilder setHttpCacheInvalidator( 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 @@ protected ClientExecChain decorateMainExec(final ClientExecChain mainExec) { 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; } } diff --git a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ManagedHttpCacheStorage.java b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ManagedHttpCacheStorage.java index 1d2372050..0f74a8c3b 100644 --- a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ManagedHttpCacheStorage.java +++ b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ManagedHttpCacheStorage.java @@ -49,9 +49,17 @@ * 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. - * + *

* This {@link HttpCacheStorage} implementation is intended for use with {@link FileResource} * and similar. + *

+ * 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. + *

+ * 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 void shutdown() { @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(); + } + } + } } }