hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r980358 - in /httpcomponents/httpclient/trunk/httpclient-cache/src: main/java/org/apache/http/impl/client/cache/ test/java/org/apache/http/impl/client/cache/
Date Thu, 29 Jul 2010 08:30:43 GMT
Author: olegk
Date: Thu Jul 29 08:30:43 2010
New Revision: 980358

URL: http://svn.apache.org/viewvc?rev=980358&view=rev
Log:
HTTPCLIENT-972: Cache config 
Contributed by Jonathan Moore <jonathan_moore at comcast.com>

Added:
    httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheConfig.java
  (with props)
Modified:
    httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClient.java
    httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/DoNotTestProtocolRequirements.java
    httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java
    httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestProtocolDeviations.java
    httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestProtocolRequirements.java

Added: httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheConfig.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheConfig.java?rev=980358&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheConfig.java
(added)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheConfig.java
Thu Jul 29 08:30:43 2010
@@ -0,0 +1,51 @@
+package org.apache.http.impl.client.cache;
+
+/**
+ * Java Beans-style configuration for a
+ * {@link org.apache.http.impl.client.cache.CachingHttpClient}.
+ */
+public class CacheConfig {
+
+    /** Default setting for the maximum object size that will be
+     * cached, in bytes.
+     */
+    public final static int DEFAULT_MAX_OBJECT_SIZE_BYTES = 8192;
+
+    private int maxObjectSizeBytes = DEFAULT_MAX_OBJECT_SIZE_BYTES;
+    private boolean isSharedCache = true;
+
+    /**
+     * Returns the current maximum object size that will be cached.
+     * @return size in bytes
+     */
+    public int getMaxObjectSizeBytes() {
+        return maxObjectSizeBytes;
+    }
+
+    /**
+     * Specifies the maximum object size that will be eligible for caching.
+     * @param maxObjectSizeBytes size in bytes
+     */
+    public void setMaxObjectSizeBytes(int maxObjectSizeBytes) {
+        this.maxObjectSizeBytes = maxObjectSizeBytes;
+    }
+
+    /**
+     * Returns whether the cache will behave as a shared cache or not.
+     * @return true for a shared cache, false for a non-shared (private)
+     * cache
+     */
+    public boolean isSharedCache() {
+        return isSharedCache;
+    }
+
+    /**
+     * Sets whether the cache should behave as a shared cache or not.
+     * @param isSharedCache true to behave as a shared cache, false to
+     * behave as a non-shared (private) cache.
+     */
+    public void setSharedCache(boolean isSharedCache) {
+        this.isSharedCache = isSharedCache;
+    }
+
+}

Propchange: httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheConfig.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheConfig.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheConfig.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClient.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClient.java?rev=980358&r1=980357&r2=980358&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClient.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClient.java
Thu Jul 29 08:30:43 2010
@@ -67,8 +67,6 @@ import org.apache.http.protocol.HttpCont
 public class CachingHttpClient implements HttpClient {
 
     private final static int MAX_CACHE_ENTRIES = 1000;
-    private final static int DEFAULT_MAX_OBJECT_SIZE_BYTES = 8192;
-
     private final static boolean SUPPORTS_RANGE_AND_CONTENT_RANGE_HEADERS = false;
 
     private final AtomicLong cacheHits = new AtomicLong();
@@ -87,16 +85,30 @@ public class CachingHttpClient implement
     private final CachedResponseSuitabilityChecker suitabilityChecker;
 
     private final ConditionalRequestBuilder conditionalRequestBuilder;
-    private final int maxObjectSizeBytes;
+
     private final CacheEntryUpdater cacheEntryUpdater;
 
+    private final int maxObjectSizeBytes;
+    private final boolean sharedCache;
+
     private final ResponseProtocolCompliance responseCompliance;
     private final RequestProtocolCompliance requestCompliance;
 
     private final Log log = LogFactory.getLog(getClass());
 
-    public CachingHttpClient(HttpClient client, HttpCache cache, int maxObjectSizeBytes)
{
+    public CachingHttpClient(HttpClient client, HttpCache cache, CacheConfig config) {
         super();
+        if (client == null) {
+            throw new IllegalArgumentException("HttpClient may not be null");
+        }
+        if (cache == null) {
+            throw new IllegalArgumentException("HttpCache may not be null");
+        }
+        if (config == null) {
+            throw new IllegalArgumentException("CacheConfig may not be null");
+        }
+        this.maxObjectSizeBytes = config.getMaxObjectSizeBytes();
+        this.sharedCache = config.isSharedCache();
         this.backend = client;
         this.responseCache = cache;
         this.validityPolicy = new CacheValidityPolicy();
@@ -109,17 +121,37 @@ public class CachingHttpClient implement
         this.suitabilityChecker = new CachedResponseSuitabilityChecker(this.validityPolicy);
         this.conditionalRequestBuilder = new ConditionalRequestBuilder();
         this.cacheEntryUpdater = new CacheEntryUpdater();
-        this.maxObjectSizeBytes = maxObjectSizeBytes;
+
         this.responseCompliance = new ResponseProtocolCompliance();
         this.requestCompliance = new RequestProtocolCompliance();
     }
 
     public CachingHttpClient() {
-        this(new DefaultHttpClient(), new BasicHttpCache(MAX_CACHE_ENTRIES), DEFAULT_MAX_OBJECT_SIZE_BYTES);
+        this(new DefaultHttpClient(), new BasicHttpCache(MAX_CACHE_ENTRIES), new CacheConfig());
+    }
+
+    public CachingHttpClient(CacheConfig config) {
+        this(new DefaultHttpClient(), new BasicHttpCache(MAX_CACHE_ENTRIES), config);
+    }
+
+    public CachingHttpClient(HttpClient client) {
+        this(client, new BasicHttpCache(MAX_CACHE_ENTRIES), new CacheConfig());
+    }
+
+    public CachingHttpClient(HttpClient client, CacheConfig config) {
+        this(client, new BasicHttpCache(MAX_CACHE_ENTRIES), config);
     }
 
-    public CachingHttpClient(HttpCache cache, int maxObjectSizeBytes) {
-        this(new DefaultHttpClient(), cache, maxObjectSizeBytes);
+    public CachingHttpClient(HttpCache cache) {
+        this(new DefaultHttpClient(), cache, new CacheConfig());
+    }
+
+    public CachingHttpClient(HttpCache cache, CacheConfig config) {
+        this(new DefaultHttpClient(), cache, config);
+    }
+
+    public CachingHttpClient(HttpClient client, HttpCache cache) {
+        this(client, cache, new CacheConfig());
     }
 
     CachingHttpClient(HttpClient backend, CacheValidityPolicy validityPolicy, ResponseCachingPolicy
responseCachingPolicy,
@@ -130,7 +162,9 @@ public class CachingHttpClient implement
                              ConditionalRequestBuilder conditionalRequestBuilder, CacheEntryUpdater
entryUpdater,
                              ResponseProtocolCompliance responseCompliance,
                              RequestProtocolCompliance requestCompliance) {
-        this.maxObjectSizeBytes = DEFAULT_MAX_OBJECT_SIZE_BYTES;
+        CacheConfig config = new CacheConfig();
+        this.maxObjectSizeBytes = config.getMaxObjectSizeBytes();
+        this.sharedCache = config.isSharedCache();
         this.backend = backend;
         this.validityPolicy = validityPolicy;
         this.responseCachingPolicy = responseCachingPolicy;
@@ -383,7 +417,7 @@ public class CachingHttpClient implement
     }
 
     public boolean isSharedCache() {
-        return true;
+        return sharedCache;
     }
 
     Date getCurrentDate() {

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/DoNotTestProtocolRequirements.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/DoNotTestProtocolRequirements.java?rev=980358&r1=980357&r2=980358&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/DoNotTestProtocolRequirements.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/DoNotTestProtocolRequirements.java
Thu Jul 29 08:30:43 2010
@@ -80,7 +80,9 @@ public class DoNotTestProtocolRequiremen
         mockBackend = EasyMock.createMock(HttpClient.class);
         mockEntity = EasyMock.createMock(HttpEntity.class);
         mockCache = EasyMock.createMock(HttpCache.class);
-        impl = new CachingHttpClient(mockBackend, cache, MAX_BYTES);
+        CacheConfig params = new CacheConfig();
+        params.setMaxObjectSizeBytes(MAX_BYTES);
+        impl = new CachingHttpClient(mockBackend, cache, params);
     }
 
     private HttpResponse make200Response() {

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java?rev=980358&r1=980357&r2=980358&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java
Thu Jul 29 08:30:43 2010
@@ -884,7 +884,7 @@ public class TestCachingHttpClient {
 
         HttpCache cacheImpl = new BasicHttpCache(100);
 
-        CachingHttpClient cachingClient = new CachingHttpClient(httpClient, cacheImpl, 8192);
+        CachingHttpClient cachingClient = new CachingHttpClient(httpClient, cacheImpl);
 
         HttpUriRequest request = new HttpGet("http://www.fancast.com/static-28262/styles/base.css");
 

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestProtocolDeviations.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestProtocolDeviations.java?rev=980358&r1=980357&r2=980358&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestProtocolDeviations.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestProtocolDeviations.java
Thu Jul 29 08:30:43 2010
@@ -97,7 +97,10 @@ public class TestProtocolDeviations {
         mockBackend = EasyMock.createMock(HttpClient.class);
         mockEntity = EasyMock.createMock(HttpEntity.class);
         mockCache = EasyMock.createMock(HttpCache.class);
-        impl = new CachingHttpClient(mockBackend, cache, MAX_BYTES);
+
+        CacheConfig params = new CacheConfig();
+        params.setMaxObjectSizeBytes(MAX_BYTES);
+        impl = new CachingHttpClient(mockBackend, cache, params);
     }
 
     private HttpResponse make200Response() {

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestProtocolRequirements.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestProtocolRequirements.java?rev=980358&r1=980357&r2=980358&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestProtocolRequirements.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestProtocolRequirements.java
Thu Jul 29 08:30:43 2010
@@ -84,6 +84,7 @@ public class TestProtocolRequirements {
     private HttpCache mockCache;
     private HttpRequest request;
     private HttpResponse originResponse;
+    private CacheConfig params;
 
     private CachingHttpClient impl;
 
@@ -101,7 +102,9 @@ public class TestProtocolRequirements {
         mockBackend = EasyMock.createMock(HttpClient.class);
         mockEntity = EasyMock.createMock(HttpEntity.class);
         mockCache = EasyMock.createMock(HttpCache.class);
-        impl = new CachingHttpClient(mockBackend, cache, MAX_BYTES);
+        params = new CacheConfig();
+        params.setMaxObjectSizeBytes(MAX_BYTES);
+        impl = new CachingHttpClient(mockBackend, cache, params);
     }
 
     private void replayMocks() {
@@ -140,7 +143,7 @@ public class TestProtocolRequirements {
         mockCache = EasyMock.createMock(HttpCache.class);
         mockEntity = EasyMock.createMock(HttpEntity.class);
 
-        impl = new CachingHttpClient(mockBackend, mockCache, MAX_BYTES);
+        impl = new CachingHttpClient(mockBackend, mockCache, params);
 
         EasyMock.expect(mockCache.getEntry((String) EasyMock.anyObject())).andReturn(null)
                 .anyTimes();
@@ -2307,7 +2310,7 @@ public class TestProtocolRequirements {
 
         mockCache.putEntry(EasyMock.eq("http://foo.example.com/thing"), EasyMock.isA(HttpCacheEntry.class));
 
-        impl = new CachingHttpClient(mockBackend, mockCache, MAX_BYTES);
+        impl = new CachingHttpClient(mockBackend, mockCache, params);
 
         HttpRequest validate = new BasicHttpRequest("GET", "/thing", HttpVersion.HTTP_1_1);
         validate.setHeader("If-None-Match", "\"etag\"");
@@ -2337,7 +2340,7 @@ public class TestProtocolRequirements {
         Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
         Date nineSecondsAgo = new Date(now.getTime() - 9 * 1000L);
         Date eightSecondsAgo = new Date(now.getTime() - 8 * 1000L);
-        
+
         Header[] hdrs = new Header[] {
                 new BasicHeader("Date", DateUtils.formatDate(nineSecondsAgo)),
                 new BasicHeader("Cache-Control", "max-age=3600"),
@@ -2349,7 +2352,7 @@ public class TestProtocolRequirements {
 
         CacheEntry entry = new CacheEntry(tenSecondsAgo, eightSecondsAgo, hdrs, bytes);
 
-        impl = new CachingHttpClient(mockBackend, mockCache, MAX_BYTES);
+        impl = new CachingHttpClient(mockBackend, mockCache, params);
 
         EasyMock.expect(mockCache.getEntry("http://foo.example.com/thing")).andReturn(entry);
 
@@ -2390,7 +2393,7 @@ public class TestProtocolRequirements {
 
         CacheEntry entry = new CacheEntry(tenSecondsAgo, eightSecondsAgo, hdrs, bytes);
 
-        impl = new CachingHttpClient(mockBackend, mockCache, MAX_BYTES);
+        impl = new CachingHttpClient(mockBackend, mockCache, params);
 
         EasyMock.expect(mockCache.getEntry("http://foo.example.com/thing")).andReturn(entry);
         EasyMock.expect(
@@ -2591,7 +2594,7 @@ public class TestProtocolRequirements {
 
         CacheEntry entry = new CacheEntry(tenSecondsAgo, eightSecondsAgo, hdrs, bytes);
 
-        impl = new CachingHttpClient(mockBackend, mockCache, MAX_BYTES);
+        impl = new CachingHttpClient(mockBackend, mockCache, params);
 
         EasyMock.expect(mockCache.getEntry("http://foo.example.com/thing")).andReturn(entry);
 
@@ -2635,7 +2638,7 @@ public class TestProtocolRequirements {
 
         CacheEntry entry = new CacheEntry(requestTime, responseTime, hdrs, bytes);
 
-        impl = new CachingHttpClient(mockBackend, mockCache, MAX_BYTES);
+        impl = new CachingHttpClient(mockBackend, mockCache, params);
 
         HttpResponse validated = make200Response();
         validated.setHeader("Cache-Control", "public");



Mime
View raw message