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