|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| package org.acegisecurity.providers.dao.cache; |
|
17 |
| |
|
18 |
| import org.acegisecurity.providers.dao.UserCache; |
|
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 |
| import org.springframework.util.Assert; |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| public class EhCacheBasedUserCache implements UserCache, InitializingBean { |
|
42 |
| |
|
43 |
| |
|
44 |
| private static final Log logger = LogFactory.getLog(EhCacheBasedUserCache.class); |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| private Cache cache; |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
2
| public void setCache(Cache cache) {
|
|
53 |
2
| this.cache = cache;
|
|
54 |
| } |
|
55 |
| |
|
56 |
1
| public Cache getCache() {
|
|
57 |
1
| return cache;
|
|
58 |
| } |
|
59 |
| |
|
60 |
4
| public UserDetails getUserFromCache(String username) {
|
|
61 |
4
| Element element = null;
|
|
62 |
| |
|
63 |
4
| try {
|
|
64 |
4
| element = cache.get(username);
|
|
65 |
| } catch (CacheException cacheException) { |
|
66 |
0
| throw new DataRetrievalFailureException("Cache failure: "
|
|
67 |
| + cacheException.getMessage()); |
|
68 |
| } |
|
69 |
| |
|
70 |
4
| if (logger.isDebugEnabled()) {
|
|
71 |
0
| logger.debug("Cache hit: " + (element != null) + "; username: "
|
|
72 |
| + username); |
|
73 |
| } |
|
74 |
| |
|
75 |
4
| if (element == null) {
|
|
76 |
3
| return null;
|
|
77 |
| } else { |
|
78 |
1
| return (UserDetails) element.getValue();
|
|
79 |
| } |
|
80 |
| } |
|
81 |
| |
|
82 |
2
| public void afterPropertiesSet() throws Exception {
|
|
83 |
2
| Assert.notNull(cache, "cache mandatory");
|
|
84 |
| } |
|
85 |
| |
|
86 |
1
| public void putUserInCache(UserDetails user) {
|
|
87 |
1
| Element element = new Element(user.getUsername(), user);
|
|
88 |
| |
|
89 |
1
| if (logger.isDebugEnabled()) {
|
|
90 |
0
| logger.debug("Cache put: " + element.getKey());
|
|
91 |
| } |
|
92 |
| |
|
93 |
1
| cache.put(element);
|
|
94 |
| } |
|
95 |
| |
|
96 |
1
| public void removeUserFromCache(UserDetails user) {
|
|
97 |
1
| if (logger.isDebugEnabled()) {
|
|
98 |
0
| logger.debug("Cache remove: " + user.getUsername());
|
|
99 |
| } |
|
100 |
| |
|
101 |
1
| this.removeUserFromCache(user.getUsername());
|
|
102 |
| } |
|
103 |
| |
|
104 |
1
| public void removeUserFromCache(String username) {
|
|
105 |
1
| cache.remove(username);
|
|
106 |
| } |
|
107 |
| } |