1
2
3
4
5
6
7
8
9
10
11
12
13
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
48
49 private static final Log logger = LogFactory.getLog(EhCacheBasedX509UserCache.class);
50
51
52
53 private Cache cache;
54
55
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 }