001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.acl.basic.cache;
017:
018: import net.sf.ehcache.CacheException;
019: import net.sf.ehcache.Element;
020: import net.sf.ehcache.Ehcache;
021:
022: import org.acegisecurity.acl.basic.AclObjectIdentity;
023: import org.acegisecurity.acl.basic.BasicAclEntry;
024: import org.acegisecurity.acl.basic.BasicAclEntryCache;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028:
029: import org.springframework.beans.factory.InitializingBean;
030:
031: import org.springframework.dao.DataRetrievalFailureException;
032:
033: import org.springframework.util.Assert;
034:
035: /**
036: * Caches <code>BasicAclEntry</code>s using a Spring IoC defined <A
037: * HREF="http://ehcache.sourceforge.net">EHCACHE</a>.
038: *
039: * @author Ben Alex
040: * @version $Id: EhCacheBasedAclEntryCache.java 1965 2007-08-27 23:41:59Z luke_t $
041: */
042: public class EhCacheBasedAclEntryCache implements BasicAclEntryCache,
043: InitializingBean {
044: //~ Static fields/initializers =====================================================================================
045:
046: private static final Log logger = LogFactory
047: .getLog(EhCacheBasedAclEntryCache.class);
048:
049: //~ Instance fields ================================================================================================
050:
051: private Ehcache cache;
052:
053: //~ Methods ========================================================================================================
054:
055: public void afterPropertiesSet() throws Exception {
056: Assert.notNull(cache, "cache mandatory");
057: }
058:
059: public BasicAclEntry[] getEntriesFromCache(
060: AclObjectIdentity aclObjectIdentity) {
061: Element element = null;
062:
063: try {
064: element = cache.get(aclObjectIdentity);
065: } catch (CacheException cacheException) {
066: throw new DataRetrievalFailureException("Cache failure: "
067: + cacheException.getMessage());
068: }
069:
070: // Return null if cache element has expired or not found
071: if (element == null) {
072: if (logger.isDebugEnabled()) {
073: logger.debug("Cache miss: " + aclObjectIdentity);
074: }
075:
076: return null;
077: }
078:
079: if (logger.isDebugEnabled()) {
080: logger.debug("Cache hit: " + (element != null)
081: + "; object: " + aclObjectIdentity);
082: }
083:
084: BasicAclEntryHolder holder = (BasicAclEntryHolder) element
085: .getValue();
086:
087: return holder.getBasicAclEntries();
088: }
089:
090: public void putEntriesInCache(BasicAclEntry[] basicAclEntry) {
091: BasicAclEntryHolder holder = new BasicAclEntryHolder(
092: basicAclEntry);
093: Element element = new Element(basicAclEntry[0]
094: .getAclObjectIdentity(), holder);
095:
096: if (logger.isDebugEnabled()) {
097: logger.debug("Cache put: " + element.getKey());
098: }
099:
100: cache.put(element);
101: }
102:
103: public void removeEntriesFromCache(
104: AclObjectIdentity aclObjectIdentity) {
105: cache.remove(aclObjectIdentity);
106: }
107:
108: public Ehcache getCache() {
109: return cache;
110: }
111:
112: public void setCache(Ehcache cache) {
113: this.cache = cache;
114: }
115: }
|