hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r963849 - in /httpcomponents/httpclient/trunk/httpclient-cache/src: main/java/org/apache/http/client/cache/ main/java/org/apache/http/impl/client/cache/ test/java/org/apache/http/impl/client/cache/
Date Tue, 13 Jul 2010 20:14:00 GMT
Author: olegk
Date: Tue Jul 13 20:14:00 2010
New Revision: 963849

URL: http://svn.apache.org/viewvc?rev=963849&view=rev
Log:
* Disambiguated the contract of HttpCache#updatEntry / HttpCacheUpdateCallback 
  Contributed by Jonathan Moore <jonathan_moore at comcast.com>

* Fixed method synchronization in BasicHttpCache 

Modified:
    httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCache.java
    httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheUpdateCallback.java
    httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java
    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/TestCachingHttpClient.java
    httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestResponseCache.java

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCache.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCache.java?rev=963849&r1=963848&r2=963849&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCache.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCache.java
Tue Jul 13 20:14:00 2010
@@ -37,7 +37,7 @@ public interface HttpCache<E> {
 
     void removeEntry(String url) throws HttpCacheOperationException;
 
-    void updateCacheEntry(
+    void updateEntry(
             String url, HttpCacheUpdateCallback<E> callback) throws HttpCacheOperationException;
 
 }

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheUpdateCallback.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheUpdateCallback.java?rev=963849&r1=963848&r2=963849&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheUpdateCallback.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheUpdateCallback.java
Tue Jul 13 20:14:00 2010
@@ -41,6 +41,6 @@ public interface HttpCacheUpdateCallback
      *
      * @since 4.1
      */
-    E getUpdatedEntry(E existing) throws HttpCacheOperationException;
+    E update(E existing) throws HttpCacheOperationException;
 
 }
\ No newline at end of file

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java?rev=963849&r1=963848&r2=963849&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java
Tue Jul 13 20:14:00 2010
@@ -26,7 +26,6 @@
  */
 package org.apache.http.impl.client.cache;
 
-import java.util.Collections;
 import java.util.LinkedHashMap;
 import java.util.Map;
 
@@ -43,8 +42,8 @@ import org.apache.http.client.cache.Http
 @ThreadSafe
 public class BasicHttpCache implements HttpCache<CacheEntry> {
 
-    private final LinkedHashMap<String, CacheEntry> baseMap = new LinkedHashMap<String,
CacheEntry>(20,
-            0.75f, true) {
+    private final LinkedHashMap<String, CacheEntry> baseMap = new LinkedHashMap<String,
CacheEntry>(
+            20, 0.75f, true) {
 
         private static final long serialVersionUID = -7750025207539768511L;
 
@@ -52,15 +51,13 @@ public class BasicHttpCache implements H
         protected boolean removeEldestEntry(Map.Entry<String, CacheEntry> eldest) {
             return size() > maxEntries;
         }
-    };
 
-    private final Map<String, CacheEntry> syncMap;
+    };
 
     private final int maxEntries;
 
     public BasicHttpCache(int maxEntries) {
         this.maxEntries = maxEntries;
-        syncMap = Collections.synchronizedMap(baseMap);
     }
 
     /**
@@ -71,8 +68,8 @@ public class BasicHttpCache implements H
      * @param entry
      *            CacheEntry to place in the cache
      */
-    public void putEntry(String url, CacheEntry entry) {
-        syncMap.put(url, entry);
+    public synchronized void putEntry(String url, CacheEntry entry) {
+        baseMap.put(url, entry);
     }
 
     /**
@@ -82,8 +79,8 @@ public class BasicHttpCache implements H
      *            Url that is the cache key
      * @return CacheEntry if one exists, or null for cache miss
      */
-    public CacheEntry getEntry(String url) {
-        return syncMap.get(url);
+    public synchronized CacheEntry getEntry(String url) {
+        return baseMap.get(url);
     }
 
     /**
@@ -92,15 +89,15 @@ public class BasicHttpCache implements H
      * @param url
      *            Url that is the cache key
      */
-    public void removeEntry(String url) {
-        syncMap.remove(url);
+    public synchronized void removeEntry(String url) {
+        baseMap.remove(url);
     }
 
-    public synchronized void updateCacheEntry(
+    public synchronized void updateEntry(
             String url,
             HttpCacheUpdateCallback<CacheEntry> callback) throws HttpCacheOperationException
{
-        CacheEntry existingEntry = syncMap.get(url);
-        CacheEntry updated = callback.getUpdatedEntry(existingEntry);
-        syncMap.put(url, updated);
+        CacheEntry existingEntry = baseMap.get(url);
+        baseMap.put(url, callback.update(existingEntry));
     }
+
 }

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=963849&r1=963848&r2=963849&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
Tue Jul 13 20:14:00 2010
@@ -488,15 +488,7 @@ public class CachingHttpClient implement
 
     protected void storeInCache(HttpHost target, HttpRequest request, CacheEntry entry) {
         if (entry.hasVariants()) {
-            try {
-                String uri = uriExtractor.getURI(target, request);
-                HttpCacheUpdateCallback<CacheEntry> callback = storeVariantEntry(
-                        target, request, entry);
-                responseCache.updateCacheEntry(uri, callback);
-            } catch (HttpCacheOperationException ex) {
-                log.debug("Was unable to PUT/UPDATE an entry into the cache based on the
uri provided",
-                        ex);
-            }
+            storeVariantEntry(target, request, entry);
         } else {
             storeNonVariantEntry(target, request, entry);
         }
@@ -511,28 +503,35 @@ public class CachingHttpClient implement
         }
     }
 
-    protected HttpCacheUpdateCallback<CacheEntry> storeVariantEntry(
+    protected void storeVariantEntry(
             final HttpHost target,
             final HttpRequest req,
             final CacheEntry entry) {
+        final String variantURI = uriExtractor.getVariantURI(target, req, entry);
+        try {
+            responseCache.putEntry(variantURI, entry);
+        } catch (HttpCacheOperationException e) {
+            log.debug("Was unable to PUT a variant entry into the cache based on the uri
provided", e);
+        }
 
-        return new HttpCacheUpdateCallback<CacheEntry>() {
-            public CacheEntry getUpdatedEntry(CacheEntry existing) throws HttpCacheOperationException
{
+        HttpCacheUpdateCallback<CacheEntry> callback = new HttpCacheUpdateCallback<CacheEntry>()
{
 
-                return doGetUpdatedParentEntry(existing, target, req, entry);
+            public CacheEntry update(CacheEntry existing) throws HttpCacheOperationException
{
+                return doGetUpdatedParentEntry(existing, entry, variantURI);
             }
+
         };
+        String parentURI = uriExtractor.getURI(target, req);
+        try {
+            responseCache.updateEntry(parentURI, callback);
+        } catch (HttpCacheOperationException e) {
+            log.debug("Was unable to UPDATE a parent entry for a variant", e);
+        }
     }
 
     protected CacheEntry doGetUpdatedParentEntry(
             CacheEntry existing,
-            HttpHost target,
-            HttpRequest req,
-            CacheEntry entry) throws HttpCacheOperationException {
-
-        String variantURI = uriExtractor.getVariantURI(target, req, entry);
-        responseCache.putEntry(variantURI, entry);
-
+            CacheEntry entry, String variantURI) throws HttpCacheOperationException {
         if (existing != null) {
             return existing.addVariantURI(variantURI);
         } else {

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=963849&r1=963848&r2=963849&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
Tue Jul 13 20:14:00 2010
@@ -323,12 +323,9 @@ public class TestCachingHttpClient {
         final CacheEntry entry = new CacheEntry(new Date(), new Date(), HTTP_1_1,
                 new Header[] {}, new ByteArrayEntity(new byte[] {}), 200, "OK");
 
-        extractVariantURI(variantURI, entry);
-        putInCache(variantURI, entry);
-
         replayMocks();
 
-        CacheEntry updatedEntry = impl.doGetUpdatedParentEntry(null, host, mockRequest, entry);
+        CacheEntry updatedEntry = impl.doGetUpdatedParentEntry(null, entry, variantURI);
 
         verifyMocks();
 

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestResponseCache.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestResponseCache.java?rev=963849&r1=963848&r2=963849&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestResponseCache.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestResponseCache.java
Tue Jul 13 20:14:00 2010
@@ -147,9 +147,9 @@ public class TestResponseCache {
         cache.putEntry("foo", entry);
         cache.putEntry("bar", entry2);
 
-        cache.updateCacheEntry("foo", new HttpCacheUpdateCallback<CacheEntry>() {
+        cache.updateEntry("foo", new HttpCacheUpdateCallback<CacheEntry>() {
 
-            public CacheEntry getUpdatedEntry(CacheEntry existing) {
+            public CacheEntry update(CacheEntry existing) {
                 CacheEntry updated = new CacheEntry(
                         existing.getRequestDate(),
                         existing.getRequestDate(),



Mime
View raw message