1
0
mirror of synced 2026-08-05 17:57:15 +00:00

Refactor EH-CACHE integration classes to work with Spring IoC provided Cache rather than manage our own cache internally.

This commit is contained in:
Ben Alex
2004-12-05 04:37:05 +00:00
parent 01165ea0e1
commit 76c82db196
12 changed files with 187 additions and 158 deletions
@@ -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 <code>BasicAclEntry</code>s using <A
* Caches <code>BasicAclEntry</code>s using a Spring IoC defined <A
* HREF="http://ehcache.sourceforge.net">EHCACHE</a>.
*
* @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.
*
* <P>
* Defaults to 5 minutes.
* </p>
*
* @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)) {
// dont 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(),
@@ -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 <A HREF="http://ehcache.sourceforge.net">EHCACHE</a>.
* Caches tickets using a Spring IoC defined <A
* HREF="http://ehcache.sourceforge.net">EHCACHE</a>.
*
* @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.
*
* <P>
* Defaults to 20 minutes.
* </p>
*
* @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)) {
// dont 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);
@@ -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;
/**
* Caches <code>User</code> objects using <A
* Caches <code>User</code> objects using a Spring IoC defined <A
* HREF="http://ehcache.sourceforge.net">EHCACHE</a>.
*
* @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.
*
* <P>
* Defaults to 5 minutes.
* </p>
*
* @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)) {
// dont 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);
@@ -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");
}
}
@@ -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() {
@@ -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() {
@@ -26,4 +26,18 @@
<!-- Automatically receives AuthenticationEvent messages from AbstractSecurityInterceptor -->
<bean id="secureObjectLoggerListener" class="net.sf.acegisecurity.intercept.event.LoggerListener"/>
<!-- Setup a cache we can use in tests of the caching layer -->
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
</bean>
<bean id="eHCacheBackend" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager">
<ref local="cacheManager"/>
</property>
<property name="cacheName">
<value>testingCache</value>
</property>
</bean>
</beans>