View Javadoc

    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      public void setCache(Cache cache) {
    58          this.cache = cache;
    59      }
    60
    61      public UserDetails getUserFromCache(X509Certificate userCert) {
    62          Element element = null;
    63
    64          try {
    65              element = cache.get(userCert);
    66          } catch (CacheException cacheException) {
    67              throw new DataRetrievalFailureException("Cache failure: "
    68                  + cacheException.getMessage());
    69          }
    70
    71          if (logger.isDebugEnabled()) {
    72              String subjectDN = "unknown";
    73
    74              if ((userCert != null) && (userCert.getSubjectDN() != null)) {
    75                  subjectDN = userCert.getSubjectDN().toString();
    76              }
    77
    78              logger.debug("X.509 Cache hit. SubjectDN: " + subjectDN);
    79          }
    80
    81          if (element == null) {
    82              return null;
    83          } else {
    84              return (UserDetails) element.getValue();
    85          }
    86      }
    87
    88      public void afterPropertiesSet() throws Exception {
    89          Assert.notNull(cache, "cache is mandatory");
    90      }
    91
    92      public void putUserInCache(X509Certificate userCert, UserDetails user) {
    93          Element element = new Element(userCert, user);
    94
    95          if (logger.isDebugEnabled()) {
    96              logger.debug("Cache put: " + userCert.getSubjectDN());
    97          }
    98
    99          cache.put(element);
    100     }
    101
    102     public void removeUserFromCache(X509Certificate userCert) {
    103         if (logger.isDebugEnabled()) {
    104             logger.debug("Cache remove: " + userCert.getSubjectDN());
    105         }
    106
    107         cache.remove(userCert);
    108     }
    109 }