View Javadoc

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  public class EhCacheBasedX509UserCache implements X509UserCache, InitializingBean {
47      //~ Static fields/initializers =====================================================================================
48  
49      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          Assert.notNull(cache, "cache is mandatory");
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: " + cacheException.getMessage());
68          }
69  
70          if (logger.isDebugEnabled()) {
71              String subjectDN = "unknown";
72  
73              if ((userCert != null) && (userCert.getSubjectDN() != null)) {
74                  subjectDN = userCert.getSubjectDN().toString();
75              }
76  
77              logger.debug("X.509 Cache hit. SubjectDN: " + subjectDN);
78          }
79  
80          if (element == null) {
81              return null;
82          } else {
83              return (UserDetails) element.getValue();
84          }
85      }
86  
87      public void putUserInCache(X509Certificate userCert, UserDetails user) {
88          Element element = new Element(userCert, user);
89  
90          if (logger.isDebugEnabled()) {
91              logger.debug("Cache put: " + userCert.getSubjectDN());
92          }
93  
94          cache.put(element);
95      }
96  
97      public void removeUserFromCache(X509Certificate userCert) {
98          if (logger.isDebugEnabled()) {
99              logger.debug("Cache remove: " + userCert.getSubjectDN());
100         }
101 
102         cache.remove(userCert);
103     }
104 
105     public void setCache(Ehcache cache) {
106         this.cache = cache;
107     }
108 }