Coverage Report - org.acegisecurity.providers.dao.cache.EhCacheBasedUserCache
 
Classes in this File Line Coverage Branch Coverage Complexity
EhCacheBasedUserCache
82% 
80% 
2
 
 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.providers.dao.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.providers.dao.UserCache;
 23  
 
 24  
 import org.acegisecurity.userdetails.UserDetails;
 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>User</code> objects using a Spring IoC defined <A
 38  
  * HREF="http://ehcache.sourceforge.net">EHCACHE</a>.
 39  
  *
 40  
  * @author Ben Alex
 41  
  * @version $Id: EhCacheBasedUserCache.java 1965 2007-08-27 23:41:59Z luke_t $
 42  
  */
 43  4
 public class EhCacheBasedUserCache implements UserCache, InitializingBean {
 44  
     //~ Static fields/initializers =====================================================================================
 45  
 
 46  2
     private static final Log logger = LogFactory.getLog(EhCacheBasedUserCache.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 Ehcache getCache() {
 59  1
         return cache;
 60  
     }
 61  
 
 62  
     public UserDetails getUserFromCache(String username) {
 63  4
         Element element = null;
 64  
 
 65  
         try {
 66  4
             element = cache.get(username);
 67  0
         } catch (CacheException cacheException) {
 68  0
             throw new DataRetrievalFailureException("Cache failure: " + cacheException.getMessage());
 69  4
         }
 70  
 
 71  4
         if (logger.isDebugEnabled()) {
 72  0
             logger.debug("Cache hit: " + (element != null) + "; username: " + username);
 73  
         }
 74  
 
 75  4
         if (element == null) {
 76  3
             return null;
 77  
         } else {
 78  1
             return (UserDetails) element.getValue();
 79  
         }
 80  
     }
 81  
 
 82  
     public void putUserInCache(UserDetails user) {
 83  1
         Element element = new Element(user.getUsername(), user);
 84  
 
 85  1
         if (logger.isDebugEnabled()) {
 86  0
             logger.debug("Cache put: " + element.getKey());
 87  
         }
 88  
 
 89  1
         cache.put(element);
 90  1
     }
 91  
 
 92  
     public void removeUserFromCache(UserDetails user) {
 93  1
         if (logger.isDebugEnabled()) {
 94  0
             logger.debug("Cache remove: " + user.getUsername());
 95  
         }
 96  
 
 97  1
         this.removeUserFromCache(user.getUsername());
 98  1
     }
 99  
 
 100  
     public void removeUserFromCache(String username) {
 101  1
         cache.remove(username);
 102  1
     }
 103  
 
 104  
     public void setCache(Ehcache cache) {
 105  2
         this.cache = cache;
 106  2
     }
 107  
 }