Coverage Report - org.acegisecurity.providers.x509.cache.EhCacheBasedX509UserCache
 
Classes in this File Line Coverage Branch Coverage Complexity
EhCacheBasedX509UserCache
71% 
80% 
2.6
 
 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.x509.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.x509.X509UserCache;
 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  
 import java.security.cert.X509Certificate;
 36  
 
 37  
 
 38  
 /**
 39  
  * Caches <code>User</code> objects using a Spring IoC defined <a
 40  
  * href="http://ehcache.sourceforge.net">EHCACHE</a>.
 41  
  *
 42  
  * @author Luke Taylor
 43  
  * @author Ben Alex
 44  
  * @version $Id: EhCacheBasedX509UserCache.java 1965 2007-08-27 23:41:59Z luke_t $
 45  
  */
 46  1
 public class EhCacheBasedX509UserCache implements X509UserCache, InitializingBean {
 47  
     //~ Static fields/initializers =====================================================================================
 48  
 
 49  2
     private static final Log logger = LogFactory.getLog(EhCacheBasedX509UserCache.class);
 50  
 
 51  
     //~ Instance fields ================================================================================================
 52  
 
 53  
     private Ehcache cache;
 54  
 
 55  
     //~ Methods ========================================================================================================
 56  
 
 57  
     public void afterPropertiesSet() throws Exception {
 58  1
         Assert.notNull(cache, "cache is mandatory");
 59  1
     }
 60  
 
 61  
     public UserDetails getUserFromCache(X509Certificate userCert) {
 62  3
         Element element = null;
 63  
 
 64  
         try {
 65  3
             element = cache.get(userCert);
 66  0
         } catch (CacheException cacheException) {
 67  0
             throw new DataRetrievalFailureException("Cache failure: " + cacheException.getMessage());
 68  3
         }
 69  
 
 70  3
         if (logger.isDebugEnabled()) {
 71  0
             String subjectDN = "unknown";
 72  
 
 73  0
             if ((userCert != null) && (userCert.getSubjectDN() != null)) {
 74  0
                 subjectDN = userCert.getSubjectDN().toString();
 75  
             }
 76  
 
 77  0
             logger.debug("X.509 Cache hit. SubjectDN: " + subjectDN);
 78  
         }
 79  
 
 80  3
         if (element == null) {
 81  2
             return null;
 82  
         } else {
 83  1
             return (UserDetails) element.getValue();
 84  
         }
 85  
     }
 86  
 
 87  
     public void putUserInCache(X509Certificate userCert, UserDetails user) {
 88  1
         Element element = new Element(userCert, user);
 89  
 
 90  1
         if (logger.isDebugEnabled()) {
 91  0
             logger.debug("Cache put: " + userCert.getSubjectDN());
 92  
         }
 93  
 
 94  1
         cache.put(element);
 95  1
     }
 96  
 
 97  
     public void removeUserFromCache(X509Certificate userCert) {
 98  1
         if (logger.isDebugEnabled()) {
 99  0
             logger.debug("Cache remove: " + userCert.getSubjectDN());
 100  
         }
 101  
 
 102  1
         cache.remove(userCert);
 103  1
     }
 104  
 
 105  
     public void setCache(Ehcache cache) {
 106  1
         this.cache = cache;
 107  1
     }
 108  
 }