Coverage Report - org.acegisecurity.providers.cas.cache.EhCacheBasedTicketCache
 
Classes in this File Line Coverage Branch Coverage Complexity
EhCacheBasedTicketCache
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.cas.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.cas.CasAuthenticationToken;
 23  
 import org.acegisecurity.providers.cas.StatelessTicketCache;
 24  
 
 25  
 import org.apache.commons.logging.Log;
 26  
 import org.apache.commons.logging.LogFactory;
 27  
 
 28  
 import org.springframework.beans.factory.InitializingBean;
 29  
 
 30  
 import org.springframework.dao.DataRetrievalFailureException;
 31  
 
 32  
 import org.springframework.util.Assert;
 33  
 
 34  
 
 35  
 /**
 36  
  * Caches tickets using a Spring IoC defined <A HREF="http://ehcache.sourceforge.net">EHCACHE</a>.
 37  
  *
 38  
  * @author Ben Alex
 39  
  * @version $Id: EhCacheBasedTicketCache.java 1965 2007-08-27 23:41:59Z luke_t $
 40  
  */
 41  2
 public class EhCacheBasedTicketCache implements StatelessTicketCache, InitializingBean {
 42  
     //~ Static fields/initializers =====================================================================================
 43  
 
 44  2
     private static final Log logger = LogFactory.getLog(EhCacheBasedTicketCache.class);
 45  
 
 46  
     //~ Instance fields ================================================================================================
 47  
 
 48  
     private Ehcache cache;
 49  
 
 50  
     //~ Methods ========================================================================================================
 51  
 
 52  
     public void afterPropertiesSet() throws Exception {
 53  2
         Assert.notNull(cache, "cache mandatory");
 54  1
     }
 55  
 
 56  
     public CasAuthenticationToken getByTicketId(String serviceTicket) {
 57  4
         Element element = null;
 58  
 
 59  
         try {
 60  4
             element = cache.get(serviceTicket);
 61  0
         } catch (CacheException cacheException) {
 62  0
             throw new DataRetrievalFailureException("Cache failure: " + cacheException.getMessage());
 63  4
         }
 64  
 
 65  4
         if (logger.isDebugEnabled()) {
 66  0
             logger.debug("Cache hit: " + (element != null) + "; service ticket: " + serviceTicket);
 67  
         }
 68  
 
 69  4
         if (element == null) {
 70  3
             return null;
 71  
         } else {
 72  1
             return (CasAuthenticationToken) element.getValue();
 73  
         }
 74  
     }
 75  
 
 76  
     public Ehcache getCache() {
 77  1
         return cache;
 78  
     }
 79  
 
 80  
     public void putTicketInCache(CasAuthenticationToken token) {
 81  1
         Element element = new Element(token.getCredentials().toString(), token);
 82  
 
 83  1
         if (logger.isDebugEnabled()) {
 84  0
             logger.debug("Cache put: " + element.getKey());
 85  
         }
 86  
 
 87  1
         cache.put(element);
 88  1
     }
 89  
 
 90  
     public void removeTicketFromCache(CasAuthenticationToken token) {
 91  1
         if (logger.isDebugEnabled()) {
 92  0
             logger.debug("Cache remove: " + token.getCredentials().toString());
 93  
         }
 94  
 
 95  1
         this.removeTicketFromCache(token.getCredentials().toString());
 96  1
     }
 97  
 
 98  
     public void removeTicketFromCache(String serviceTicket) {
 99  1
         cache.remove(serviceTicket);
 100  1
     }
 101  
 
 102  
     public void setCache(Ehcache cache) {
 103  2
         this.cache = cache;
 104  2
     }
 105  
 }