Clover coverage report - Acegi Security System for Spring - 1.0.0-RC1
Coverage timestamp: Mon Dec 5 2005 09:05:15 EST
file stats: LOC: 109   Methods: 5
NCLOC: 57   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
EhCacheBasedX509UserCache.java 50% 66.7% 100% 66.7%
coverage coverage
 1    /* Copyright 2004, 2005 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 org.acegisecurity.providers.x509.X509UserCache;
 19    import org.acegisecurity.userdetails.UserDetails;
 20   
 21    import net.sf.ehcache.Cache;
 22    import net.sf.ehcache.CacheException;
 23    import net.sf.ehcache.Element;
 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    import java.security.cert.X509Certificate;
 35   
 36   
 37    /**
 38    * Caches <code>User</code> objects using a Spring IoC defined <a
 39    * HREF="http://ehcache.sourceforge.net">EHCACHE</a>.
 40    *
 41    * @author Luke Taylor
 42    * @author Ben Alex
 43    * @version $Id: EhCacheBasedX509UserCache.java,v 1.7 2005/11/29 13:10:08 benalex Exp $
 44    */
 45    public class EhCacheBasedX509UserCache implements X509UserCache,
 46    InitializingBean {
 47    //~ Static fields/initializers =============================================
 48   
 49    private static final Log logger = LogFactory.getLog(EhCacheBasedX509UserCache.class);
 50   
 51    //~ Instance fields ========================================================
 52   
 53    private Cache cache;
 54   
 55    //~ Methods ================================================================
 56   
 57  1 public void setCache(Cache cache) {
 58  1 this.cache = cache;
 59    }
 60   
 61  3 public UserDetails getUserFromCache(X509Certificate userCert) {
 62  3 Element element = null;
 63   
 64  3 try {
 65  3 element = cache.get(userCert);
 66    } catch (CacheException cacheException) {
 67  0 throw new DataRetrievalFailureException("Cache failure: "
 68    + cacheException.getMessage());
 69    }
 70   
 71  3 if (logger.isDebugEnabled()) {
 72  0 String subjectDN = "unknown";
 73   
 74  0 if ((userCert != null) && (userCert.getSubjectDN() != null)) {
 75  0 subjectDN = userCert.getSubjectDN().toString();
 76    }
 77   
 78  0 logger.debug("X.509 Cache hit. SubjectDN: " + subjectDN);
 79    }
 80   
 81  3 if (element == null) {
 82  2 return null;
 83    } else {
 84  1 return (UserDetails) element.getValue();
 85    }
 86    }
 87   
 88  1 public void afterPropertiesSet() throws Exception {
 89  1 Assert.notNull(cache, "cache is mandatory");
 90    }
 91   
 92  1 public void putUserInCache(X509Certificate userCert, UserDetails user) {
 93  1 Element element = new Element(userCert, user);
 94   
 95  1 if (logger.isDebugEnabled()) {
 96  0 logger.debug("Cache put: " + userCert.getSubjectDN());
 97    }
 98   
 99  1 cache.put(element);
 100    }
 101   
 102  1 public void removeUserFromCache(X509Certificate userCert) {
 103  1 if (logger.isDebugEnabled()) {
 104  0 logger.debug("Cache remove: " + userCert.getSubjectDN());
 105    }
 106   
 107  1 cache.remove(userCert);
 108    }
 109    }