hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r985733 - in /httpcomponents/httpclient/trunk: ./ httpclient-cache/ httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ehcache/ httpclient-cache/src/test/java/org/apache/http/impl/client/cache/ehcache/
Date Sun, 15 Aug 2010 18:51:32 GMT
Author: olegk
Date: Sun Aug 15 18:51:32 2010
New Revision: 985733

URL: http://svn.apache.org/viewvc?rev=985733&view=rev
Log:
HTTPCLIENT-978: ehcache based HTTP cache implementation
Contributed by Michajlo Matijkiw <michajlo_matijkiw at comcast.com>

Added:
    httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ehcache/
    httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ehcache/EhcacheHttpCache.java
  (with props)
    httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/ehcache/
    httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/ehcache/TestEhcacheHttpCache.java
  (with props)
    httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/ehcache/TestEhcacheProtcolRequirements.java
  (with props)
Modified:
    httpcomponents/httpclient/trunk/httpclient-cache/pom.xml
    httpcomponents/httpclient/trunk/pom.xml

Modified: httpcomponents/httpclient/trunk/httpclient-cache/pom.xml
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/pom.xml?rev=985733&r1=985732&r2=985733&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/pom.xml (original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/pom.xml Sun Aug 15 18:51:32 2010
@@ -38,7 +38,7 @@
    HttpComponents HttpClient - Cache
   </description>
   <url>http://hc.apache.org/httpcomponents-client</url>
-  <packaging>jar</packaging>  
+  <packaging>jar</packaging>
 
   <licenses>
     <license>
@@ -79,6 +79,20 @@
       <version>${easymock.version}</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>net.sf.ehcache</groupId>
+      <artifactId>ehcache-core</artifactId>
+      <version>${ehcache.version}</version>
+      <scope>compile</scope>
+      <optional>true</optional>
+    </dependency>
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>slf4j-jcl</artifactId>
+      <version>${slf4j.version}</version>
+      <scope>compile</scope>
+      <optional>true</optional>
+    </dependency>
   </dependencies>
 
   <properties>

Added: httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ehcache/EhcacheHttpCache.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ehcache/EhcacheHttpCache.java?rev=985733&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ehcache/EhcacheHttpCache.java
(added)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ehcache/EhcacheHttpCache.java
Sun Aug 15 18:51:32 2010
@@ -0,0 +1,76 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.http.impl.client.cache.ehcache;
+
+import java.io.IOException;
+
+import net.sf.ehcache.Ehcache;
+import net.sf.ehcache.Element;
+
+import org.apache.http.client.cache.HttpCache;
+import org.apache.http.client.cache.HttpCacheEntry;
+import org.apache.http.client.cache.HttpCacheUpdateCallback;
+
+public class EhcacheHttpCache implements HttpCache {
+
+    private Ehcache cache;
+
+    public EhcacheHttpCache(Ehcache cache) {
+        this.cache = cache;
+    }
+
+    public synchronized void putEntry(String key, HttpCacheEntry entry) throws IOException
{
+        cache.put(new Element(key, entry));
+    }
+
+    public synchronized HttpCacheEntry getEntry(String url) {
+        Element e = cache.get(url);
+        return (e != null) ? (HttpCacheEntry)e.getValue() : null;
+    }
+
+    public synchronized void removeEntry(String url) {
+        cache.remove(url);
+    }
+
+    public synchronized void updateEntry(String key, HttpCacheUpdateCallback callback)
+            throws IOException {
+        Element e = cache.get(key);
+        HttpCacheEntry existingEntry = (e != null) ? (HttpCacheEntry)e.getValue() : null;
+        HttpCacheEntry updatedEntry = callback.update(existingEntry);
+
+        if (e == null) {
+            putEntry(key, updatedEntry);
+        } else {
+            // Attempt to do a CAS replace, if we fail throw an IOException for now
+            // While this operation should work fine within this instance, multiple instances
+            //  could trample each others' data
+            if (!cache.replace(e, new Element(key, updatedEntry))) {
+                throw new IOException();
+            }
+        }
+    }
+}
\ No newline at end of file

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

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

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

Added: httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/ehcache/TestEhcacheHttpCache.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/ehcache/TestEhcacheHttpCache.java?rev=985733&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/ehcache/TestEhcacheHttpCache.java
(added)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/ehcache/TestEhcacheHttpCache.java
Sun Aug 15 18:51:32 2010
@@ -0,0 +1,91 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.http.impl.client.cache.ehcache;
+
+import java.io.IOException;
+
+import junit.framework.TestCase;
+import net.sf.ehcache.Ehcache;
+import net.sf.ehcache.Element;
+
+import org.apache.http.client.cache.HttpCacheEntry;
+import org.apache.http.impl.client.cache.CacheEntry;
+import org.easymock.EasyMock;
+import org.junit.Test;
+
+public class TestEhcacheHttpCache extends TestCase {
+
+    private Ehcache mockCache;
+    private EhcacheHttpCache impl;
+
+    public void setUp() {
+        mockCache = EasyMock.createMock(Ehcache.class);
+        impl = new EhcacheHttpCache(mockCache);
+    }
+
+    @Test
+    public void testCachePut() throws IOException {
+        final String key = "foo";
+        final HttpCacheEntry value = new CacheEntry();
+
+        Element e = new Element(key, value);
+
+        mockCache.put(e);
+
+        EasyMock.replay(mockCache);
+        impl.putEntry(key, value);
+        EasyMock.verify(mockCache);
+    }
+
+    @Test
+    public void testCacheGet() {
+        final String key = "foo";
+        final HttpCacheEntry cachedValue = new CacheEntry();
+        Element element = new Element(key, cachedValue);
+
+        EasyMock.expect(mockCache.get(key))
+                .andReturn(element);
+
+        EasyMock.replay(mockCache);
+        HttpCacheEntry resultingEntry = impl.getEntry(key);
+        EasyMock.verify(mockCache);
+
+        assertSame(cachedValue, resultingEntry);
+    }
+
+    @Test
+    public void testCacheRemove() {
+        final String key = "foo";
+
+        EasyMock.expect(mockCache.remove(key)).andReturn(true);
+
+        EasyMock.replay(mockCache);
+        impl.removeEntry(key);
+        EasyMock.verify(mockCache);
+    }
+
+}

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

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

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

Added: httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/ehcache/TestEhcacheProtcolRequirements.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/ehcache/TestEhcacheProtcolRequirements.java?rev=985733&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/ehcache/TestEhcacheProtcolRequirements.java
(added)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/ehcache/TestEhcacheProtcolRequirements.java
Sun Aug 15 18:51:32 2010
@@ -0,0 +1,99 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.http.impl.client.cache.ehcache;
+
+import net.sf.ehcache.CacheManager;
+import net.sf.ehcache.config.CacheConfiguration;
+import net.sf.ehcache.config.Configuration;
+import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
+
+import org.apache.http.HttpHost;
+import org.apache.http.HttpVersion;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.cache.HttpCache;
+import org.apache.http.impl.client.cache.CacheConfig;
+import org.apache.http.impl.client.cache.CachingHttpClient;
+import org.apache.http.impl.client.cache.HeapResourceFactory;
+import org.apache.http.impl.client.cache.HttpTestUtils;
+import org.apache.http.impl.client.cache.TestProtocolRequirements;
+import org.apache.http.message.BasicHttpRequest;
+import org.easymock.classextension.EasyMock;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+
+public class TestEhcacheProtcolRequirements extends TestProtocolRequirements{
+
+    private final String TEST_EHCACHE_NAME = "TestEhcacheProtocolRequirements-cache";
+
+    private static CacheManager CACHE_MANAGER;
+
+    @BeforeClass
+    public static void setUpGlobal() {
+        Configuration config = new Configuration();
+        config.addDefaultCache(
+                new CacheConfiguration("default", Integer.MAX_VALUE)
+                    .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU)
+                    .overflowToDisk(false));
+        CACHE_MANAGER = CacheManager.create(config);
+    }
+
+    @Override
+    @Before
+    public void setUp() {
+        host = new HttpHost("foo.example.com");
+
+        body = HttpTestUtils.makeBody(entityLength);
+
+        request = new BasicHttpRequest("GET", "/foo", HttpVersion.HTTP_1_1);
+
+        originResponse = make200Response();
+
+        if (CACHE_MANAGER.cacheExists(TEST_EHCACHE_NAME)){
+            CACHE_MANAGER.removeCache(TEST_EHCACHE_NAME);
+        }
+        CACHE_MANAGER.addCache(TEST_EHCACHE_NAME);
+        cache = new EhcacheHttpCache(CACHE_MANAGER.getCache(TEST_EHCACHE_NAME));
+        mockBackend = EasyMock.createMock(HttpClient.class);
+        mockCache = EasyMock.createMock(HttpCache.class);
+        params = new CacheConfig();
+        params.setMaxObjectSizeBytes(MAX_BYTES);
+        impl = new CachingHttpClient(mockBackend, cache, new HeapResourceFactory(), params);
+    }
+
+    @After
+    public void tearDown(){
+        CACHE_MANAGER.removeCache(TEST_EHCACHE_NAME);
+    }
+
+    @AfterClass
+    public static void tearDownGlobal(){
+        CACHE_MANAGER.shutdown();
+    }
+
+}

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

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

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

Modified: httpcomponents/httpclient/trunk/pom.xml
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/pom.xml?rev=985733&r1=985732&r2=985733&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/pom.xml (original)
+++ httpcomponents/httpclient/trunk/pom.xml Sun Aug 15 18:51:32 2010
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<!-- 
+<!--
    ====================================================================
    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
@@ -71,6 +71,8 @@
     <httpcore.version>4.1-beta1</httpcore.version>
     <commons-logging.version>1.1.1</commons-logging.version>
     <commons-codec.version>1.4</commons-codec.version>
+    <ehcache.version>2.2.0</ehcache.version>
+    <slf4j.version>1.5.11</slf4j.version>
     <junit.version>4.8.1</junit.version>
     <easymock.version>2.5.2</easymock.version>
   </properties>



Mime
View raw message