From: <leb...@us...> - 2011-03-24 13:44:38
|
Revision: 604 http://webassembletool.svn.sourceforge.net/webassembletool/?rev=604&view=rev Author: lebedinskii Date: 2011-03-24 13:44:30 +0000 (Thu, 24 Mar 2011) Log Message: ----------- 43: Should be created own CacheStorage implementation (without external dependencies ) that will be used by default 1)With help of LinkedHashMap to implement LRU CacheStorage (http://fr.w3support.net/index.php?db=so&id=224868) 2)Move OSCache implementation to separate module (webassembletool-oscache) Modified Paths: -------------- trunk/pom.xml trunk/src/site/xdoc/cache.xml trunk/webassembletool-core/pom.xml trunk/webassembletool-core/src/main/java/net/webassembletool/DriverConfiguration.java trunk/webassembletool-core/src/main/java/net/webassembletool/cache/DefaultCacheStorage.java trunk/webassembletool-core/src/test/java/net/webassembletool/cache/CacheStorageTest.java Added Paths: ----------- trunk/webassembletool-core/src/main/java/net/webassembletool/cache/LRUMap.java trunk/webassembletool-core/src/test/java/net/webassembletool/cache/LRUMapTest.java trunk/webassembletool-oscache/ trunk/webassembletool-oscache/pom.xml trunk/webassembletool-oscache/src/ trunk/webassembletool-oscache/src/main/ trunk/webassembletool-oscache/src/main/java/ trunk/webassembletool-oscache/src/main/java/net/ trunk/webassembletool-oscache/src/main/java/net/webassembletool/ trunk/webassembletool-oscache/src/main/java/net/webassembletool/oscache/ trunk/webassembletool-oscache/src/main/java/net/webassembletool/oscache/OSCacheStorage.java trunk/webassembletool-oscache/src/main/resources/ trunk/webassembletool-oscache/src/main/resources/oscache.properties trunk/webassembletool-oscache/src/site/ trunk/webassembletool-oscache/src/site/site.xml trunk/webassembletool-oscache/src/site/xdoc/ trunk/webassembletool-oscache/src/site/xdoc/index.xml trunk/webassembletool-oscache/src/test/ trunk/webassembletool-oscache/src/test/java/ trunk/webassembletool-oscache/src/test/java/net/ trunk/webassembletool-oscache/src/test/java/net/webassembletool/ trunk/webassembletool-oscache/src/test/java/net/webassembletool/cache/ trunk/webassembletool-oscache/src/test/java/net/webassembletool/cache/OSCacheStorageTest.java trunk/webassembletool-oscache/src/test/resources/ Removed Paths: ------------- trunk/webassembletool-core/src/main/resources/oscache.properties Modified: trunk/pom.xml =================================================================== --- trunk/pom.xml 2011-03-24 13:15:35 UTC (rev 603) +++ trunk/pom.xml 2011-03-24 13:44:30 UTC (rev 604) @@ -298,6 +298,84 @@ </plugins> </pluginManagement> </build> + <dependencyManagement> + <dependencies> + <dependency> + <groupId>javax.servlet</groupId> + <artifactId>servlet-api</artifactId> + <version>2.4</version> + </dependency> + <dependency> + <groupId>org.apache.httpcomponents</groupId> + <artifactId>httpclient</artifactId> + <version>4.1</version> + <type>jar</type> + </dependency> + <dependency> + <groupId>opensymphony</groupId> + <artifactId>oscache</artifactId> + <version>2.4.1</version> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.8.2</version> + </dependency> + <dependency> + <groupId>org.easymock</groupId> + <artifactId>easymock</artifactId> + <version>3.0</version> + </dependency> + <dependency> + <groupId>org.eclipse.jetty</groupId> + <artifactId>jetty-server</artifactId> + <version>7.2.0.v20101020</version> + </dependency> + <dependency> + <groupId>commons-io</groupId> + <artifactId>commons-io</artifactId> + <version>2.0.1</version> + </dependency> + <dependency> + <groupId>nu.validator.htmlparser</groupId> + <artifactId>htmlparser</artifactId> + <version>1.2.1</version> + <type>jar</type> + </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-mock</artifactId> + <version>2.0.8</version> + <type>jar</type> + </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-core</artifactId> + <version>3.0.1.RELEASE</version> + </dependency> + <dependency> + <groupId>commons-lang</groupId> + <artifactId>commons-lang</artifactId> + <version>2.6</version> + <type>jar</type> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + <version>1.6.1</version> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>jcl-over-slf4j</artifactId> + <version>1.6.1</version> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-nop</artifactId> + <version>1.6.1</version> + </dependency> + </dependencies> + </dependencyManagement> <profiles> <profile> <id>release-sign-artifacts</id> @@ -329,6 +407,7 @@ <modules> <module>webassembletool-core</module> + <module>webassembletool-oscache</module> <module>webassembletool-taglib</module> <module>webassembletool-taglib-helper</module> <module>webassembletool-jsf</module> Modified: trunk/src/site/xdoc/cache.xml =================================================================== --- trunk/src/site/xdoc/cache.xml 2011-03-24 13:15:35 UTC (rev 603) +++ trunk/src/site/xdoc/cache.xml 2011-03-24 13:44:30 UTC (rev 604) @@ -10,12 +10,15 @@ <p> ESIGate includes a full <a href="http://www.w3.org/Protocols/rfc2616/rfc2616.html">RFC 2616</a> - compliant HTTP cache. The cache uses - <a href="http://www.opensymphony.com/oscache/">OsCache</a> - as a store. - The default cache settings defined in the + compliant HTTP cache. The cache uses in-memory LRUMap as a store. + Also it is possible to use + <a href="http://www.opensymphony.com/oscache/">OsCache</a>. + The OsCache as store can be enabled for a provider by setting the cacheStorageClassName + parameter to "net.webassembletool.oscache.OSCacheStorage" and adding webassembletool-oscache jar to the /WEB-INF/lib + directory of your application. + The default cache settings for OsCache defined in the oscache.properties file - included in the core jar can be overridden + included in the webassembletool-oscache jar can be overridden simply by creating a oscache.properties file in the /WEB-INF/classes directory of your Modified: trunk/webassembletool-core/pom.xml =================================================================== --- trunk/webassembletool-core/pom.xml 2011-03-24 13:15:35 UTC (rev 603) +++ trunk/webassembletool-core/pom.xml 2011-03-24 13:44:30 UTC (rev 604) @@ -15,13 +15,11 @@ <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> - <version>2.4</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> - <version>4.1</version> <type>jar</type> <scope>compile</scope> <exclusions> @@ -32,89 +30,59 @@ </exclusions> </dependency> <dependency> - <groupId>opensymphony</groupId> - <artifactId>oscache</artifactId> - <version>2.4.1</version> - <exclusions> - <exclusion> - <artifactId>jms</artifactId> - <groupId>javax.jms</groupId> - </exclusion> - <exclusion> - <artifactId>servlet-api</artifactId> - <groupId>javax.servlet</groupId> - </exclusion> - <exclusion> - <groupId>commons-logging</groupId> - <artifactId>commons-logging</artifactId> - </exclusion> - </exclusions> - </dependency> - <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> - <version>4.8.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.easymock</groupId> <artifactId>easymock</artifactId> - <version>3.0</version> <scope>test</scope> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-server</artifactId> - <version>7.2.0.v20101020</version> <scope>test</scope> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> - <version>2.0.1</version> <scope>compile</scope> </dependency> <dependency> <groupId>nu.validator.htmlparser</groupId> <artifactId>htmlparser</artifactId> - <version>1.2.1</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-mock</artifactId> - <version>2.0.8</version> <type>jar</type> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> - <version>3.0.1.RELEASE</version> <scope>test</scope> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> - <version>2.6</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> - <version>1.6.1</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> - <version>1.6.1</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-nop</artifactId> - <version>1.6.1</version> <scope>test</scope> </dependency> </dependencies> Modified: trunk/webassembletool-core/src/main/java/net/webassembletool/DriverConfiguration.java =================================================================== --- trunk/webassembletool-core/src/main/java/net/webassembletool/DriverConfiguration.java 2011-03-24 13:15:35 UTC (rev 603) +++ trunk/webassembletool-core/src/main/java/net/webassembletool/DriverConfiguration.java 2011-03-24 13:44:30 UTC (rev 604) @@ -90,7 +90,7 @@ .getProperty("cacheMaxFileSize")); } if (props.getProperty("cacheStorageClassName") != null) { - String cacheStorageClassName = props.getProperty("cacheStorageClass"); + String cacheStorageClassName = props.getProperty("cacheStorageClassName"); try { Class<? extends CacheStorage> cacheStorageClass = (Class<? extends CacheStorage>) this.getClass().getClassLoader().loadClass(cacheStorageClassName); if(cacheStorageClass != null) Modified: trunk/webassembletool-core/src/main/java/net/webassembletool/cache/DefaultCacheStorage.java =================================================================== --- trunk/webassembletool-core/src/main/java/net/webassembletool/cache/DefaultCacheStorage.java 2011-03-24 13:15:35 UTC (rev 603) +++ trunk/webassembletool-core/src/main/java/net/webassembletool/cache/DefaultCacheStorage.java 2011-03-24 13:44:30 UTC (rev 604) @@ -1,87 +1,137 @@ package net.webassembletool.cache; +import java.util.Collections; +import java.util.Comparator; +import java.util.Map; +import java.util.PriorityQueue; import java.util.Properties; -import org.apache.commons.lang.StringUtils; +public class DefaultCacheStorage implements CacheStorage { -import com.opensymphony.oscache.base.CacheEntry; -import com.opensymphony.oscache.base.EntryRefreshPolicy; -import com.opensymphony.oscache.base.NeedsRefreshException; -import com.opensymphony.oscache.general.GeneralCacheAdministrator; + private final Map<String, CacheEntry> cache = Collections + .synchronizedMap(new LRUMap<String, CacheEntry>()); + private final PriorityQueue<CacheEntry> ttlQueue = new PriorityQueue<DefaultCacheStorage.CacheEntry>( + 100, new CacheEntryComparator()); -public class DefaultCacheStorage implements CacheStorage{ - private final GeneralCacheAdministrator cache; + public void put(String key, Object value) { + removeExpiredEntries(); + cache.put(key, new CacheEntry(value)); + } - public DefaultCacheStorage() { - cache = new GeneralCacheAdministrator(); + public void put(String key, Object value, long ttl) { + removeExpiredEntries(); + CacheEntry cacheEntry = new CacheEntry(value, key, + System.currentTimeMillis() + ttl); + ttlQueue.add(cacheEntry); + cache.put(key, cacheEntry); } - public void put(String key, Object value) { - key = prepairKey(key); - cache.putInCache(key, value); + public Object get(String key) { + removeExpiredEntries(); + CacheEntry cacheEntry = cache.get(key); + return null != cacheEntry ? cacheEntry.getValue() : null; } - private String prepairKey(String key) { - // OsCache does not support empty String keys - if (StringUtils.isEmpty(key)) { - key = "/"; - } - return key; + public <T> T get(String key, Class<T> clazz) { + return clazz.cast(this.get(key)); } - public Object get(String key) { - key = prepairKey(key); - Object result = null; - try { - result = cache.getFromCache(key); - } catch (NeedsRefreshException e1) { - // If the cache is configured to be blocking, we have to do this to - // remove the lock. - cache.cancelUpdate(key); - } - return result; + public void touch(String key) { + this.get(key); } - public void put(String key, Object value, long ttl) { - key = prepairKey(key); - cache.putInCache(key, value, new StorageEntryRefreshPolicy(ttl)); - + public void init(Properties properties) { + } - public <T> T get(String key, Class<T> clazz) { - Object ret = get(key); - if(null == ret){ - return null; + private void removeExpiredEntries() { + if (ttlQueue.size() > 0) { + synchronized (cache) { + boolean needToProcess = true; + while (needToProcess) { + CacheEntry cacheEntry = ttlQueue.peek(); + if (cacheEntry != null + && cacheEntry.getTtl() < System.currentTimeMillis()) { + ttlQueue.remove(cacheEntry); + if (cache.containsValue(cacheEntry)) { + cache.remove(cacheEntry.getKey()); + } + } else { + needToProcess = false; + } + } + } + } - return clazz.cast(ret); } - public void touch(String key) { - key = prepairKey(key); - get(key); - } - - private static class StorageEntryRefreshPolicy implements EntryRefreshPolicy{ + private static class CacheEntry { + private final Object value; + private long ttl = -1; + private String key; - private static final long serialVersionUID = -4570614485500011699L; - long expireTime; + public CacheEntry(Object value, String key, long ttl) { - public StorageEntryRefreshPolicy(long ttl) { - super(); - this.expireTime = System.currentTimeMillis()+ ttl; + this.value = value; + this.key = key; + this.ttl = ttl; } - public boolean needsRefresh(CacheEntry arg0) { - if(expireTime <= System.currentTimeMillis()){ + public CacheEntry(Object value) { + this.value = value; + } + + public long getTtl() { + return ttl; + } + + public Object getValue() { + return value; + } + + public String getKey() { + return key; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { return true; } - return false; + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + CacheEntry other = (CacheEntry) obj; + if (key == null) { + if (other.key != null) { + return false; + } + } else if (!key.equals(other.key)) { + return false; + } + if (ttl != other.ttl) { + return false; + } + if (value == null) { + if (other.value != null) { + return false; + } + } else if (!value.equals(other.value)) { + return false; + } + return true; } + } - public void init(Properties properties) { - + private static class CacheEntryComparator implements Comparator<CacheEntry> { + + public int compare(CacheEntry o1, CacheEntry o2) { + return (int) (o1.getTtl() - o2.getTtl()); + } } - } Added: trunk/webassembletool-core/src/main/java/net/webassembletool/cache/LRUMap.java =================================================================== --- trunk/webassembletool-core/src/main/java/net/webassembletool/cache/LRUMap.java (rev 0) +++ trunk/webassembletool-core/src/main/java/net/webassembletool/cache/LRUMap.java 2011-03-24 13:44:30 UTC (rev 604) @@ -0,0 +1,24 @@ +package net.webassembletool.cache; + +import java.util.LinkedHashMap; + +public class LRUMap<K, V> extends LinkedHashMap<K, V>{ + private static final long serialVersionUID = 7760625028168125797L; + private int maximumSize = 0; + + + + public LRUMap(int maximumSize) { + super(maximumSize, 0.75f, true); + this.maximumSize = maximumSize; + } + + public LRUMap() { + this(1000); + } + + @Override + protected boolean removeEldestEntry(java.util.Map.Entry<K, V> eldest) { + return size() > maximumSize; + } +} Deleted: trunk/webassembletool-core/src/main/resources/oscache.properties =================================================================== --- trunk/webassembletool-core/src/main/resources/oscache.properties 2011-03-24 13:15:35 UTC (rev 603) +++ trunk/webassembletool-core/src/main/resources/oscache.properties 2011-03-24 13:44:30 UTC (rev 604) @@ -1,5 +0,0 @@ -# Default cache configuration. -cache.memory=true -cache.capacity=1000 -cache.algorithm=com.opensymphony.oscache.base.algorithm.LRUCache -cache.blocking=false \ No newline at end of file Modified: trunk/webassembletool-core/src/test/java/net/webassembletool/cache/CacheStorageTest.java =================================================================== --- trunk/webassembletool-core/src/test/java/net/webassembletool/cache/CacheStorageTest.java 2011-03-24 13:15:35 UTC (rev 603) +++ trunk/webassembletool-core/src/test/java/net/webassembletool/cache/CacheStorageTest.java 2011-03-24 13:44:30 UTC (rev 604) @@ -40,6 +40,7 @@ assertNotNull(cache); String key = UUID.randomUUID().toString(); cache.put(key, "test", 300); + cache.put(key+"_bla-bla-bla", "test2", 30000); assertEquals("test", cache.get(key)); assertEquals("test", cache.get(key, String.class)); assertNull(cache.get(key+"_123")); Added: trunk/webassembletool-core/src/test/java/net/webassembletool/cache/LRUMapTest.java =================================================================== --- trunk/webassembletool-core/src/test/java/net/webassembletool/cache/LRUMapTest.java (rev 0) +++ trunk/webassembletool-core/src/test/java/net/webassembletool/cache/LRUMapTest.java 2011-03-24 13:44:30 UTC (rev 604) @@ -0,0 +1,46 @@ +package net.webassembletool.cache; + +import junit.framework.TestCase; + +public class LRUMapTest extends TestCase{ + public void testRemoveValueFromMap() { + LRUMap<String, String> cache = new LRUMap<String, String>(3); + cache.put("key1", "val1"); + cache.put("key2", "val2"); + cache.put("key3", "val3"); + cache.put("key4", "val4"); + assertEquals(3, cache.size()); + + assertFalse(cache.containsKey("key1")); + assertTrue(cache.containsKey("key2")); + assertTrue(cache.containsKey("key3")); + assertTrue(cache.containsKey("key4")); + + assertEquals(null, cache.get("key1")); + assertEquals("val2", cache.get("key2")); + assertEquals("val3", cache.get("key3")); + assertEquals("val4", cache.get("key4")); + } + + public void testRemoveLRUValueFromMap() { + LRUMap<String, String> cache = new LRUMap<String, String>(3); + cache.put("key1", "val1"); + cache.put("key2", "val2"); + cache.put("key3", "val3"); + assertNotNull(cache.get("key1")); + assertNotNull(cache.get("key2")); + assertTrue(cache.containsValue("val3")); + cache.put("key4", "val4"); + assertEquals(3, cache.size()); + + assertFalse("", cache.containsKey("key3")); + assertTrue(cache.containsKey("key1")); + assertTrue(cache.containsKey("key2")); + assertTrue(cache.containsKey("key4")); + + assertEquals(null, cache.get("key3")); + assertEquals("val1", cache.get("key1")); + assertEquals("val2", cache.get("key2")); + assertEquals("val4", cache.get("key4")); + } +} Added: trunk/webassembletool-oscache/pom.xml =================================================================== --- trunk/webassembletool-oscache/pom.xml (rev 0) +++ trunk/webassembletool-oscache/pom.xml 2011-03-24 13:44:30 UTC (rev 604) @@ -0,0 +1,75 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + <groupId>net.sourceforge.webassembletool</groupId> + <artifactId>webassembletool-oscache</artifactId> + <name>webassembletool-oscache</name> + <version>2.18-SNAPSHOT</version> + <parent> + <artifactId>webassembletool</artifactId> + <groupId>net.sourceforge.webassembletool</groupId> + <version>2.18-SNAPSHOT</version> + </parent> + <build> + </build> + <dependencies> + <dependency> + <groupId>opensymphony</groupId> + <artifactId>oscache</artifactId> + <exclusions> + <exclusion> + <artifactId>jms</artifactId> + <groupId>javax.jms</groupId> + </exclusion> + <exclusion> + <artifactId>servlet-api</artifactId> + <groupId>javax.servlet</groupId> + </exclusion> + <exclusion> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>javax.servlet</groupId> + <artifactId>servlet-api</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.easymock</groupId> + <artifactId>easymock</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>net.sourceforge.webassembletool</groupId> + <artifactId>webassembletool-core</artifactId> + <version>${project.version}</version> + <scope>provided</scope> + </dependency> + </dependencies> + <reporting> + <plugins> + <plugin> + <groupId>org.codehaus.mojo</groupId> + <artifactId>findbugs-maven-plugin</artifactId> + <version>2.3.1</version> + <configuration> + <locales>en</locales> + <threshold>Low</threshold> + <effort>Max</effort> + <debug>false</debug> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-jxr-plugin</artifactId> + <version>2.2</version> + </plugin> + </plugins> + </reporting> +</project> \ No newline at end of file Added: trunk/webassembletool-oscache/src/main/java/net/webassembletool/oscache/OSCacheStorage.java =================================================================== --- trunk/webassembletool-oscache/src/main/java/net/webassembletool/oscache/OSCacheStorage.java (rev 0) +++ trunk/webassembletool-oscache/src/main/java/net/webassembletool/oscache/OSCacheStorage.java 2011-03-24 13:44:30 UTC (rev 604) @@ -0,0 +1,89 @@ +package net.webassembletool.oscache; + +import java.util.Properties; + +import org.apache.commons.lang.StringUtils; + +import com.opensymphony.oscache.base.CacheEntry; +import com.opensymphony.oscache.base.EntryRefreshPolicy; +import com.opensymphony.oscache.base.NeedsRefreshException; +import com.opensymphony.oscache.general.GeneralCacheAdministrator; + +import net.webassembletool.cache.CacheStorage; + +public class OSCacheStorage implements CacheStorage { + private final GeneralCacheAdministrator cache; + + public OSCacheStorage() { + cache = new GeneralCacheAdministrator(); + + } + + public void put(String key, Object value) { + key = prepairKey(key); + cache.putInCache(key, value); + } + + private String prepairKey(String key) { + // OsCache does not support empty String keys + if (StringUtils.isEmpty(key)) { + key = "/"; + } + return key; + } + + public Object get(String key) { + key = prepairKey(key); + Object result = null; + try { + result = cache.getFromCache(key); + } catch (NeedsRefreshException e1) { + // If the cache is configured to be blocking, we have to do this to + // remove the lock. + cache.cancelUpdate(key); + } + return result; + } + + public void put(String key, Object value, long ttl) { + key = prepairKey(key); + cache.putInCache(key, value, new StorageEntryRefreshPolicy(ttl)); + + } + + public <T> T get(String key, Class<T> clazz) { + Object ret = get(key); + if(null == ret){ + return null; + } + return clazz.cast(ret); + } + + public void touch(String key) { + key = prepairKey(key); + get(key); + } + + private static class StorageEntryRefreshPolicy implements EntryRefreshPolicy{ + + private static final long serialVersionUID = -4570614485500011699L; + long expireTime; + + public StorageEntryRefreshPolicy(long ttl) { + super(); + this.expireTime = System.currentTimeMillis()+ ttl; + } + + public boolean needsRefresh(CacheEntry arg0) { + if(expireTime <= System.currentTimeMillis()){ + return true; + } + return false; + } + } + + public void init(Properties properties) { + + } + +} Added: trunk/webassembletool-oscache/src/main/resources/oscache.properties =================================================================== --- trunk/webassembletool-oscache/src/main/resources/oscache.properties (rev 0) +++ trunk/webassembletool-oscache/src/main/resources/oscache.properties 2011-03-24 13:44:30 UTC (rev 604) @@ -0,0 +1,5 @@ +# Default cache configuration. +cache.memory=true +cache.capacity=1000 +cache.algorithm=com.opensymphony.oscache.base.algorithm.LRUCache +cache.blocking=false \ No newline at end of file Added: trunk/webassembletool-oscache/src/site/site.xml =================================================================== --- trunk/webassembletool-oscache/src/site/site.xml (rev 0) +++ trunk/webassembletool-oscache/src/site/site.xml 2011-03-24 13:44:30 UTC (rev 604) @@ -0,0 +1,7 @@ +<?xml version="1.0" encoding="ISO-8859-1"?> +<project> + <body> + <menu ref="parent" /> + <menu ref="reports" /> + </body> +</project> \ No newline at end of file Added: trunk/webassembletool-oscache/src/site/xdoc/index.xml =================================================================== --- trunk/webassembletool-oscache/src/site/xdoc/index.xml (rev 0) +++ trunk/webassembletool-oscache/src/site/xdoc/index.xml 2011-03-24 13:44:30 UTC (rev 604) @@ -0,0 +1,10 @@ +<?xml version="1.0" encoding="UTF-8"?> +<document> + <body> + <section name="About"> + <p> + This module contains the OSCache cache storage implementation. + </p> + </section> + </body> +</document> \ No newline at end of file Added: trunk/webassembletool-oscache/src/test/java/net/webassembletool/cache/OSCacheStorageTest.java =================================================================== --- trunk/webassembletool-oscache/src/test/java/net/webassembletool/cache/OSCacheStorageTest.java (rev 0) +++ trunk/webassembletool-oscache/src/test/java/net/webassembletool/cache/OSCacheStorageTest.java 2011-03-24 13:44:30 UTC (rev 604) @@ -0,0 +1,201 @@ +package net.webassembletool.cache; + +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; +import java.util.UUID; +import java.util.Vector; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import junit.framework.TestCase; + +import org.easymock.EasyMock; +import org.easymock.IAnswer; + +import net.webassembletool.Driver; +import net.webassembletool.DriverConfiguration; +import net.webassembletool.HttpErrorPage; +import net.webassembletool.ResourceContext; +import net.webassembletool.cache.CacheEntry; +import net.webassembletool.cache.CacheStorage; +import net.webassembletool.cache.CachedResponse; +import net.webassembletool.oscache.OSCacheStorage; +import net.webassembletool.output.StringOutput; + +public class OSCacheStorageTest extends TestCase{ + + protected CacheStorage getCache() { + return new OSCacheStorage(); + } + + public void testPutGet(){ + CacheStorage cache = getCache(); + assertNotNull(cache); + String key = UUID.randomUUID().toString(); + cache.put(key, "test"); + assertEquals("test", cache.get(key)); + assertEquals("test", cache.get(key, String.class)); + assertNull(cache.get(key+"_123")); + assertNull(cache.get(key+"_123", String.class)); + } + + public void testPutWithTtl() throws Exception{ + CacheStorage cache = getCache(); + assertNotNull(cache); + String key = UUID.randomUUID().toString(); + cache.put(key, "test", 300); + assertEquals("test", cache.get(key)); + assertEquals("test", cache.get(key, String.class)); + assertNull(cache.get(key+"_123")); + assertNull(cache.get(key+"_123", String.class)); + Thread.sleep(350); + assertNull(cache.get(key)); + assertNull(cache.get(key)); + } + + public void testCachingCachedResponse() throws Exception{ + CacheStorage cache = getCache(); + + CachedResponse cachedResponse = new CachedResponse(); + String key = UUID.randomUUID().toString(); + cache.put(key, cachedResponse); + CachedResponse newCachedResponse = cache.get(key, CachedResponse.class); + + assertEquals(cachedResponse, newCachedResponse); + + byte[] byteArray = "test".getBytes("utf-8"); + Map<String, Object> headers = new HashMap<String, Object>(); + headers.put("Date", "Wed, 02 Mar 2011 12:30:03 GMT"); + headers.put("Server", "Apache/2.2.12 (Ubuntu)"); + headers.put("Set-Cookie", "sdnsessionhash=5b55e10d5184470e607e0f0001781f77; path=/; HttpOnly"); + headers.put("Cache-Control", "private"); + headers.put("Pragma", "private"); + headers.put("x-ua-compatible", "IE=7"); + headers.put("Keep-Alive", "timeout=15, max=100"); + headers.put("Connection", "Keep-Alive"); + headers.put("Transfer-Encoding", "chunked"); + headers.put("Content-Type", "text/html; charset=windows-1251"); + + int statusCode = 200; + String statusMessage = "OK"; + + cachedResponse = new CachedResponse(byteArray, "utf-8", headers, statusCode, statusMessage); + key = UUID.randomUUID().toString(); + cache.put(key, cachedResponse); + newCachedResponse = cache.get(key, CachedResponse.class); + + assertEquals(cachedResponse, newCachedResponse); + } + + public void testCachingCacheEntry() throws Exception{ + CacheStorage cache = getCache(); + String url = "http://google.com"; + CacheEntry cacheEntry = new CacheEntry(url, cache); + String key = UUID.randomUUID().toString(); + + byte[] byteArray = "test".getBytes("utf-8"); + Map<String, Object> headers = new HashMap<String, Object>(); + headers.put("Date", "Wed, 02 Mar 2011 12:30:03 GMT"); + headers.put("Server", "Apache/2.2.12 (Ubuntu)"); + headers.put("Set-Cookie", "sdnsessionhash=5b55e10d5184470e607e0f0001781f77; path=/; HttpOnly"); + headers.put("Cache-Control", "private"); + headers.put("Pragma", "private"); + headers.put("x-ua-compatible", "IE=7"); + headers.put("Keep-Alive", "timeout=15, max=100"); + headers.put("Connection", "Keep-Alive"); + headers.put("Transfer-Encoding", "chunked"); + headers.put("Content-Type", "text/html; charset=windows-1251"); + + int statusCode = 200; + String statusMessage = "OK"; + + CachedResponse cachedResponse = new CachedResponse(byteArray, "utf-8", headers, statusCode, statusMessage); + + HttpServletRequest originalRequest = EasyMock.createMock(HttpServletRequest.class); + HttpServletResponse originalResponse = EasyMock.createMock(HttpServletResponse.class); + + final Map<String, String> requestHeaders = new HashMap<String, String>(); + requestHeaders.put("Host", "google.com"); + requestHeaders.put("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13"); + requestHeaders.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); + requestHeaders.put("Accept-Language", "ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3"); + requestHeaders.put("Accept-Encoding", "gzip,deflate"); + requestHeaders.put("Accept-Charset", "windows-1251,utf-8;q=0.7,*;q=0.7"); + requestHeaders.put("Keep-Alive", "115"); + requestHeaders.put("Connection", "keep-alive"); + requestHeaders.put("Cache-Control", "max-age=0"); + + EasyMock.expect(originalRequest.getHeaderNames()).andReturn(new Vector(requestHeaders.keySet()).elements()).anyTimes(); + EasyMock.expect(originalRequest.getHeader((String)EasyMock.anyObject())).andAnswer(new IAnswer<String>() { + + public String answer() throws Throwable { + return requestHeaders.get(EasyMock.getCurrentArguments()[0]); + } + }).anyTimes(); + EasyMock.expect(originalRequest.getMethod()).andReturn("GET").anyTimes(); + + EasyMock.replay(originalRequest, originalResponse); + + MockDriver mockDriver = new MockDriver("test"); + ResourceContext resourceContext = new ResourceContext(mockDriver, "/test", null, originalRequest, originalResponse); + + cacheEntry.put(resourceContext, cachedResponse); + cache.put(key, cacheEntry); + CacheEntry newCacheEntry = cache.get(key, CacheEntry.class); + assertEquals(cacheEntry, newCacheEntry); + assertEquals(cachedResponse, newCacheEntry.get(resourceContext)); + + } + + public void testConfigaurationWithOsCache(){ + Properties props = new Properties(); + props.setProperty("remoteUrlBase", "http://localhost:8080"); + props.setProperty("cacheRefreshDelay", "1"); + props.setProperty("cacheStorageClassName", OSCacheStorage.class.getName()+"_abra-cadabra"); + try{ + new DriverConfiguration("test2", props); + fail(); + }catch (Exception e) { + + } + + props.setProperty("cacheStorageClassName", OSCacheStorage.class.getName()); + assertEquals(OSCacheStorage.class, new DriverConfiguration("test2", props).getCacheStorageClass()); + } + + private static class MockDriver extends Driver { + private final HashMap<String, StringOutput> resources = new HashMap<String, StringOutput>(); + + public MockDriver(String name) { + this(name, new Properties()); + } + + public MockDriver(String name, Properties props) { + super(name, props); + } + + public void addResource(String relUrl, String content) { + StringOutput stringOutput = new StringOutput(); + stringOutput.setStatusCode(200); + stringOutput.setStatusMessage("OK"); + stringOutput.setCharsetName("ISO-8859-1"); + stringOutput.open(); + stringOutput.write(content); + resources.put(relUrl, stringOutput); + } + + @Override + protected StringOutput getResourceAsString(ResourceContext target) + throws HttpErrorPage { + StringOutput result = resources.get(target.getRelUrl()); + if (result == null) + throw new HttpErrorPage(404, "Not found", "The page: " + + target.getRelUrl() + " does not exist"); + return result; + } + + } + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |