diff --git a/core/src/main/java/org/acegisecurity/acl/basic/cache/EhCacheBasedAclEntryCache.java b/core/src/main/java/org/acegisecurity/acl/basic/cache/EhCacheBasedAclEntryCache.java
index 3b21bc2ac8..4fd8d96b9f 100644
--- a/core/src/main/java/org/acegisecurity/acl/basic/cache/EhCacheBasedAclEntryCache.java
+++ b/core/src/main/java/org/acegisecurity/acl/basic/cache/EhCacheBasedAclEntryCache.java
@@ -21,40 +21,43 @@ import net.sf.acegisecurity.acl.basic.BasicAclEntryCache;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheException;
-import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.dao.DataRetrievalFailureException;
/**
- * Caches BasicAclEntry
s using BasicAclEntrys using a Spring IoC defined EHCACHE.
*
* @author Ben Alex
* @version $Id$
*/
public class EhCacheBasedAclEntryCache implements BasicAclEntryCache,
- InitializingBean, DisposableBean {
+ InitializingBean {
//~ Static fields/initializers =============================================
private static final Log logger = LogFactory.getLog(EhCacheBasedAclEntryCache.class);
- private static final String CACHE_NAME = "ehCacheBasedAclEntryCache";
//~ Instance fields ========================================================
private Cache cache;
- private CacheManager manager;
- private int minutesToIdle = 5;
//~ Methods ================================================================
+ public void setCache(Cache cache) {
+ this.cache = cache;
+ }
+
+ public Cache getCache() {
+ return cache;
+ }
+
public BasicAclEntry[] getEntriesFromCache(
AclObjectIdentity aclObjectIdentity) {
Element element = null;
@@ -85,43 +88,12 @@ public class EhCacheBasedAclEntryCache implements BasicAclEntryCache,
return holder.getBasicAclEntries();
}
- public void setMinutesToIdle(int minutesToIdle) {
- this.minutesToIdle = minutesToIdle;
- }
-
- /**
- * Specifies how many minutes an entry will remain in the cache from when
- * it was last accessed.
- *
- *
- * Defaults to 5 minutes. - *
- * - * @return Returns the minutes an element remains in the cache - */ - public int getMinutesToIdle() { - return minutesToIdle; - } - public void afterPropertiesSet() throws Exception { - if (CacheManager.getInstance().cacheExists(CACHE_NAME)) { - // don’t remove the cache - cache = CacheManager.getInstance().getCache(CACHE_NAME); - } else { - manager = CacheManager.create(); - - // Cache name, max memory, overflowToDisk, eternal, timeToLive, timeToIdle - cache = new Cache(CACHE_NAME, Integer.MAX_VALUE, false, false, - minutesToIdle * 60, minutesToIdle * 60); - - manager.addCache(cache); + if (cache == null) { + throw new IllegalArgumentException("cache mandatory"); } } - public void destroy() throws Exception { - manager.removeCache(CACHE_NAME); - } - public void putEntriesInCache(BasicAclEntry[] basicAclEntry) { BasicAclEntryHolder holder = new BasicAclEntryHolder(basicAclEntry); Element element = new Element(basicAclEntry[0].getAclObjectIdentity(), diff --git a/core/src/main/java/org/acegisecurity/providers/cas/cache/EhCacheBasedTicketCache.java b/core/src/main/java/org/acegisecurity/providers/cas/cache/EhCacheBasedTicketCache.java index 8f49acb298..c68d9e0fb4 100644 --- a/core/src/main/java/org/acegisecurity/providers/cas/cache/EhCacheBasedTicketCache.java +++ b/core/src/main/java/org/acegisecurity/providers/cas/cache/EhCacheBasedTicketCache.java @@ -20,36 +20,32 @@ import net.sf.acegisecurity.providers.cas.StatelessTicketCache; import net.sf.ehcache.Cache; import net.sf.ehcache.CacheException; -import net.sf.ehcache.CacheManager; import net.sf.ehcache.Element; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.dao.DataRetrievalFailureException; /** - * Caches tickets using EHCACHE. + * Caches tickets using a Spring IoC defined EHCACHE. * * @author Ben Alex * @version $Id$ */ public class EhCacheBasedTicketCache implements StatelessTicketCache, - InitializingBean, DisposableBean { + InitializingBean { //~ Static fields/initializers ============================================= private static final Log logger = LogFactory.getLog(EhCacheBasedTicketCache.class); - private static final String CACHE_NAME = "ehCacheBasedTicketCache"; //~ Instance fields ======================================================== private Cache cache; - private CacheManager manager; - private int minutesToIdle = 20; //~ Methods ================================================================ @@ -75,43 +71,20 @@ public class EhCacheBasedTicketCache implements StatelessTicketCache, } } - public void setMinutesToIdle(int minutesToIdle) { - this.minutesToIdle = minutesToIdle; + public void setCache(Cache cache) { + this.cache = cache; } - /** - * Specifies how many minutes an entry will remain in the cache from when - * it was last accessed. This is effectively the session duration. - * - *- * Defaults to 20 minutes. - *
- * - * @return Returns the minutes an element remains in the cache - */ - public int getMinutesToIdle() { - return minutesToIdle; + public Cache getCache() { + return cache; } public void afterPropertiesSet() throws Exception { - if (CacheManager.getInstance().cacheExists(CACHE_NAME)) { - // don’t remove the cache - cache = CacheManager.getInstance().getCache(CACHE_NAME); - } else { - manager = CacheManager.create(); - - // Cache name, max memory, overflowToDisk, eternal, timeToLive, timeToIdle - cache = new Cache(CACHE_NAME, Integer.MAX_VALUE, false, false, - minutesToIdle * 60, minutesToIdle * 60); - - manager.addCache(cache); + if (cache == null) { + throw new IllegalArgumentException("cache mandatory"); } } - public void destroy() throws Exception { - manager.removeCache(CACHE_NAME); - } - public void putTicketInCache(CasAuthenticationToken token) { Element element = new Element(token.getCredentials().toString(), token); diff --git a/core/src/main/java/org/acegisecurity/providers/dao/cache/EhCacheBasedUserCache.java b/core/src/main/java/org/acegisecurity/providers/dao/cache/EhCacheBasedUserCache.java index a74c2bc78c..a59726b7d2 100644 --- a/core/src/main/java/org/acegisecurity/providers/dao/cache/EhCacheBasedUserCache.java +++ b/core/src/main/java/org/acegisecurity/providers/dao/cache/EhCacheBasedUserCache.java @@ -20,56 +20,40 @@ import net.sf.acegisecurity.providers.dao.UserCache; import net.sf.ehcache.Cache; import net.sf.ehcache.CacheException; -import net.sf.ehcache.CacheManager; import net.sf.ehcache.Element; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.dao.DataRetrievalFailureException; /** - * CachesUser
objects using User objects using a Spring IoC defined EHCACHE.
*
* @author Ben Alex
* @version $Id$
*/
-public class EhCacheBasedUserCache implements UserCache, InitializingBean,
- DisposableBean {
+public class EhCacheBasedUserCache implements UserCache, InitializingBean {
//~ Static fields/initializers =============================================
private static final Log logger = LogFactory.getLog(EhCacheBasedUserCache.class);
- private static final String CACHE_NAME = "ehCacheBasedUserCache";
//~ Instance fields ========================================================
private Cache cache;
- private CacheManager manager;
- private int minutesToIdle = 5;
//~ Methods ================================================================
- public void setMinutesToIdle(int minutesToIdle) {
- this.minutesToIdle = minutesToIdle;
+ public void setCache(Cache cache) {
+ this.cache = cache;
}
- /**
- * Specifies how many minutes an entry will remain in the cache from when
- * it was last accessed. This is effectively the session duration.
- *
- * - * Defaults to 5 minutes. - *
- * - * @return Returns the minutes an element remains in the cache - */ - public int getMinutesToIdle() { - return minutesToIdle; + public Cache getCache() { + return cache; } public UserDetails getUserFromCache(String username) { @@ -95,24 +79,11 @@ public class EhCacheBasedUserCache implements UserCache, InitializingBean, } public void afterPropertiesSet() throws Exception { - if (CacheManager.getInstance().cacheExists(CACHE_NAME)) { - // don’t remove the cache - cache = CacheManager.getInstance().getCache(CACHE_NAME); - } else { - manager = CacheManager.create(); - - // Cache name, max memory, overflowToDisk, eternal, timeToLive, timeToIdle - cache = new Cache(CACHE_NAME, Integer.MAX_VALUE, false, false, - minutesToIdle * 60, minutesToIdle * 60); - - manager.addCache(cache); + if (cache == null) { + throw new IllegalArgumentException("cache mandatory"); } } - public void destroy() throws Exception { - manager.removeCache(CACHE_NAME); - } - public void putUserInCache(UserDetails user) { Element element = new Element(user.getUsername(), user); diff --git a/core/src/test/java/org/acegisecurity/acl/basic/cache/EhCacheBasedAclEntryCacheTests.java b/core/src/test/java/org/acegisecurity/acl/basic/cache/EhCacheBasedAclEntryCacheTests.java index b0834549af..3e8cf0d809 100644 --- a/core/src/test/java/org/acegisecurity/acl/basic/cache/EhCacheBasedAclEntryCacheTests.java +++ b/core/src/test/java/org/acegisecurity/acl/basic/cache/EhCacheBasedAclEntryCacheTests.java @@ -17,11 +17,16 @@ package net.sf.acegisecurity.acl.basic.cache; import junit.framework.TestCase; +import net.sf.acegisecurity.MockApplicationContext; import net.sf.acegisecurity.acl.basic.AclObjectIdentity; import net.sf.acegisecurity.acl.basic.BasicAclEntry; import net.sf.acegisecurity.acl.basic.NamedEntityObjectIdentity; import net.sf.acegisecurity.acl.basic.SimpleAclEntry; +import net.sf.ehcache.Cache; + +import org.springframework.context.ApplicationContext; + /** * Tests {@link EhCacheBasedAclEntryCache}. @@ -65,9 +70,7 @@ public class EhCacheBasedAclEntryCacheTests extends TestCase { public void testCacheOperation() throws Exception { EhCacheBasedAclEntryCache cache = new EhCacheBasedAclEntryCache(); - cache.afterPropertiesSet(); - - // execute a second time to test detection of existing instance + cache.setCache(getCache()); cache.afterPropertiesSet(); cache.putEntriesInCache(new BasicAclEntry[] {OBJECT_100_SCOTT, OBJECT_100_MARISSA}); @@ -83,13 +86,28 @@ public class EhCacheBasedAclEntryCacheTests extends TestCase { assertEquals(OBJECT_200_PETER, cache.getEntriesFromCache( new NamedEntityObjectIdentity("OBJECT", "200"))[0]); - - cache.destroy(); + assertNull(cache.getEntriesFromCache( + new NamedEntityObjectIdentity("OBJECT", "NOT_IN_CACHE"))); } - public void testGettersSetters() { + public void testStartupDetectsMissingCache() throws Exception { EhCacheBasedAclEntryCache cache = new EhCacheBasedAclEntryCache(); - cache.setMinutesToIdle(15); - assertEquals(15, cache.getMinutesToIdle()); + + try { + cache.afterPropertiesSet(); + fail("Should have thrown IllegalArgumentException"); + } catch (IllegalArgumentException expected) { + assertTrue(true); + } + + Cache myCache = getCache(); + cache.setCache(myCache); + assertEquals(myCache, cache.getCache()); + } + + private Cache getCache() { + ApplicationContext ctx = MockApplicationContext.getContext(); + + return (Cache) ctx.getBean("eHCacheBackend"); } } diff --git a/core/src/test/java/org/acegisecurity/providers/cas/cache/EhCacheBasedTicketCacheTests.java b/core/src/test/java/org/acegisecurity/providers/cas/cache/EhCacheBasedTicketCacheTests.java index 4da1ff33b9..660ba7931a 100644 --- a/core/src/test/java/org/acegisecurity/providers/cas/cache/EhCacheBasedTicketCacheTests.java +++ b/core/src/test/java/org/acegisecurity/providers/cas/cache/EhCacheBasedTicketCacheTests.java @@ -19,9 +19,14 @@ import junit.framework.TestCase; import net.sf.acegisecurity.GrantedAuthority; import net.sf.acegisecurity.GrantedAuthorityImpl; +import net.sf.acegisecurity.MockApplicationContext; import net.sf.acegisecurity.providers.cas.CasAuthenticationToken; import net.sf.acegisecurity.providers.dao.User; +import net.sf.ehcache.Cache; + +import org.springframework.context.ApplicationContext; + import java.util.List; import java.util.Vector; @@ -55,8 +60,8 @@ public class EhCacheBasedTicketCacheTests extends TestCase { public void testCacheOperation() throws Exception { EhCacheBasedTicketCache cache = new EhCacheBasedTicketCache(); + cache.setCache(getCache()); cache.afterPropertiesSet(); - cache.afterPropertiesSet(); // second run for test coverage // Check it gets stored in the cache cache.putTicketInCache(getToken()); @@ -70,14 +75,27 @@ public class EhCacheBasedTicketCacheTests extends TestCase { // Check it doesn't return values for null or unknown service tickets assertNull(cache.getByTicketId(null)); assertNull(cache.getByTicketId("UNKNOWN_SERVICE_TICKET")); - - cache.destroy(); } - public void testGettersSetters() { + public void testStartupDetectsMissingCache() throws Exception { EhCacheBasedTicketCache cache = new EhCacheBasedTicketCache(); - cache.setMinutesToIdle(5); - assertEquals(5, cache.getMinutesToIdle()); + + try { + cache.afterPropertiesSet(); + fail("Should have thrown IllegalArgumentException"); + } catch (IllegalArgumentException expected) { + assertTrue(true); + } + + Cache myCache = getCache(); + cache.setCache(myCache); + assertEquals(myCache, cache.getCache()); + } + + private Cache getCache() { + ApplicationContext ctx = MockApplicationContext.getContext(); + + return (Cache) ctx.getBean("eHCacheBackend"); } private CasAuthenticationToken getToken() { diff --git a/core/src/test/java/org/acegisecurity/providers/dao/cache/EhCacheBasedUserCacheTests.java b/core/src/test/java/org/acegisecurity/providers/dao/cache/EhCacheBasedUserCacheTests.java index 4c99535dd1..80ffe11137 100644 --- a/core/src/test/java/org/acegisecurity/providers/dao/cache/EhCacheBasedUserCacheTests.java +++ b/core/src/test/java/org/acegisecurity/providers/dao/cache/EhCacheBasedUserCacheTests.java @@ -19,8 +19,13 @@ import junit.framework.TestCase; import net.sf.acegisecurity.GrantedAuthority; import net.sf.acegisecurity.GrantedAuthorityImpl; +import net.sf.acegisecurity.MockApplicationContext; import net.sf.acegisecurity.providers.dao.User; +import net.sf.ehcache.Cache; + +import org.springframework.context.ApplicationContext; + /** * Tests {@link EhCacheBasedUserCache}. @@ -51,6 +56,7 @@ public class EhCacheBasedUserCacheTests extends TestCase { public void testCacheOperation() throws Exception { EhCacheBasedUserCache cache = new EhCacheBasedUserCache(); + cache.setCache(getCache()); cache.afterPropertiesSet(); // Check it gets stored in the cache @@ -65,14 +71,27 @@ public class EhCacheBasedUserCacheTests extends TestCase { // Check it doesn't return values for null or unknown users assertNull(cache.getUserFromCache(null)); assertNull(cache.getUserFromCache("UNKNOWN_USER")); - - cache.destroy(); } - public void testGettersSetters() { + public void testStartupDetectsMissingCache() throws Exception { EhCacheBasedUserCache cache = new EhCacheBasedUserCache(); - cache.setMinutesToIdle(15); - assertEquals(15, cache.getMinutesToIdle()); + + try { + cache.afterPropertiesSet(); + fail("Should have thrown IllegalArgumentException"); + } catch (IllegalArgumentException expected) { + assertTrue(true); + } + + Cache myCache = getCache(); + cache.setCache(myCache); + assertEquals(myCache, cache.getCache()); + } + + private Cache getCache() { + ApplicationContext ctx = MockApplicationContext.getContext(); + + return (Cache) ctx.getBean("eHCacheBackend"); } private User getUser() { diff --git a/core/src/test/resources/org/acegisecurity/applicationContext.xml b/core/src/test/resources/org/acegisecurity/applicationContext.xml index 86d1f55a75..a59ee01240 100644 --- a/core/src/test/resources/org/acegisecurity/applicationContext.xml +++ b/core/src/test/resources/org/acegisecurity/applicationContext.xml @@ -26,4 +26,18 @@