Coverage Report - org.acegisecurity.acl.basic.cache.EhCacheBasedAclEntryCache
 
Classes in this File Line Coverage Branch Coverage Complexity
EhCacheBasedAclEntryCache
82% 
80% 
2.167
 
 1  
 /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
 2  
  *
 3  
  * Licensed under the Apache License, Version 2.0 (the "License");
 4  
  * you may not use this file except in compliance with the License.
 5  
  * You may obtain a copy of the License at
 6  
  *
 7  
  *     http://www.apache.org/licenses/LICENSE-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing, software
 10  
  * distributed under the License is distributed on an "AS IS" BASIS,
 11  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
  * See the License for the specific language governing permissions and
 13  
  * limitations under the License.
 14  
  */
 15  
 
 16  
 package org.acegisecurity.acl.basic.cache;
 17  
 
 18  
 import net.sf.ehcache.CacheException;
 19  
 import net.sf.ehcache.Element;
 20  
 import net.sf.ehcache.Ehcache;
 21  
 
 22  
 import org.acegisecurity.acl.basic.AclObjectIdentity;
 23  
 import org.acegisecurity.acl.basic.BasicAclEntry;
 24  
 import org.acegisecurity.acl.basic.BasicAclEntryCache;
 25  
 
 26  
 import org.apache.commons.logging.Log;
 27  
 import org.apache.commons.logging.LogFactory;
 28  
 
 29  
 import org.springframework.beans.factory.InitializingBean;
 30  
 
 31  
 import org.springframework.dao.DataRetrievalFailureException;
 32  
 
 33  
 import org.springframework.util.Assert;
 34  
 
 35  
 
 36  
 /**
 37  
  * Caches <code>BasicAclEntry</code>s using a Spring IoC defined <A
 38  
  * HREF="http://ehcache.sourceforge.net">EHCACHE</a>.
 39  
  *
 40  
  * @author Ben Alex
 41  
  * @version $Id: EhCacheBasedAclEntryCache.java 1965 2007-08-27 23:41:59Z luke_t $
 42  
  */
 43  2
 public class EhCacheBasedAclEntryCache implements BasicAclEntryCache, InitializingBean {
 44  
     //~ Static fields/initializers =====================================================================================
 45  
 
 46  2
     private static final Log logger = LogFactory.getLog(EhCacheBasedAclEntryCache.class);
 47  
 
 48  
     //~ Instance fields ================================================================================================
 49  
 
 50  
     private Ehcache cache;
 51  
 
 52  
     //~ Methods ========================================================================================================
 53  
 
 54  
     public void afterPropertiesSet() throws Exception {
 55  2
         Assert.notNull(cache, "cache mandatory");
 56  1
     }
 57  
 
 58  
     public BasicAclEntry[] getEntriesFromCache(AclObjectIdentity aclObjectIdentity) {
 59  5
         Element element = null;
 60  
 
 61  
         try {
 62  5
             element = cache.get(aclObjectIdentity);
 63  0
         } catch (CacheException cacheException) {
 64  0
             throw new DataRetrievalFailureException("Cache failure: " + cacheException.getMessage());
 65  5
         }
 66  
 
 67  
         // Return null if cache element has expired or not found
 68  5
         if (element == null) {
 69  2
             if (logger.isDebugEnabled()) {
 70  0
                 logger.debug("Cache miss: " + aclObjectIdentity);
 71  
             }
 72  
 
 73  2
             return null;
 74  
         }
 75  
 
 76  3
         if (logger.isDebugEnabled()) {
 77  0
             logger.debug("Cache hit: " + (element != null) + "; object: " + aclObjectIdentity);
 78  
         }
 79  
 
 80  3
         BasicAclEntryHolder holder = (BasicAclEntryHolder) element.getValue();
 81  
 
 82  3
         return holder.getBasicAclEntries();
 83  
     }
 84  
 
 85  
     public void putEntriesInCache(BasicAclEntry[] basicAclEntry) {
 86  2
         BasicAclEntryHolder holder = new BasicAclEntryHolder(basicAclEntry);
 87  2
         Element element = new Element(basicAclEntry[0].getAclObjectIdentity(), holder);
 88  
 
 89  2
         if (logger.isDebugEnabled()) {
 90  0
             logger.debug("Cache put: " + element.getKey());
 91  
         }
 92  
 
 93  2
         cache.put(element);
 94  2
     }
 95  
 
 96  
     public void removeEntriesFromCache(AclObjectIdentity aclObjectIdentity) {
 97  1
         cache.remove(aclObjectIdentity);
 98  1
     }
 99  
 
 100  
     public Ehcache getCache() {
 101  1
         return cache;
 102  
     }
 103  
 
 104  
     public void setCache(Ehcache cache) {
 105  2
         this.cache = cache;
 106  2
     }
 107  
 }