| 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 |
2 |
public class EhCacheBasedTicketCache implements StatelessTicketCache, InitializingBean { |
| 42 |
|
|
| 43 |
|
|
| 44 |
2 |
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 |
2 |
Assert.notNull(cache, "cache mandatory"); |
| 54 |
1 |
} |
| 55 |
|
|
| 56 |
|
public CasAuthenticationToken getByTicketId(String serviceTicket) { |
| 57 |
4 |
Element element = null; |
| 58 |
|
|
| 59 |
|
try { |
| 60 |
4 |
element = cache.get(serviceTicket); |
| 61 |
0 |
} catch (CacheException cacheException) { |
| 62 |
0 |
throw new DataRetrievalFailureException("Cache failure: " + cacheException.getMessage()); |
| 63 |
4 |
} |
| 64 |
|
|
| 65 |
4 |
if (logger.isDebugEnabled()) { |
| 66 |
0 |
logger.debug("Cache hit: " + (element != null) + "; service ticket: " + serviceTicket); |
| 67 |
|
} |
| 68 |
|
|
| 69 |
4 |
if (element == null) { |
| 70 |
3 |
return null; |
| 71 |
|
} else { |
| 72 |
1 |
return (CasAuthenticationToken) element.getValue(); |
| 73 |
|
} |
| 74 |
|
} |
| 75 |
|
|
| 76 |
|
public Ehcache getCache() { |
| 77 |
1 |
return cache; |
| 78 |
|
} |
| 79 |
|
|
| 80 |
|
public void putTicketInCache(CasAuthenticationToken token) { |
| 81 |
1 |
Element element = new Element(token.getCredentials().toString(), token); |
| 82 |
|
|
| 83 |
1 |
if (logger.isDebugEnabled()) { |
| 84 |
0 |
logger.debug("Cache put: " + element.getKey()); |
| 85 |
|
} |
| 86 |
|
|
| 87 |
1 |
cache.put(element); |
| 88 |
1 |
} |
| 89 |
|
|
| 90 |
|
public void removeTicketFromCache(CasAuthenticationToken token) { |
| 91 |
1 |
if (logger.isDebugEnabled()) { |
| 92 |
0 |
logger.debug("Cache remove: " + token.getCredentials().toString()); |
| 93 |
|
} |
| 94 |
|
|
| 95 |
1 |
this.removeTicketFromCache(token.getCredentials().toString()); |
| 96 |
1 |
} |
| 97 |
|
|
| 98 |
|
public void removeTicketFromCache(String serviceTicket) { |
| 99 |
1 |
cache.remove(serviceTicket); |
| 100 |
1 |
} |
| 101 |
|
|
| 102 |
|
public void setCache(Ehcache cache) { |
| 103 |
2 |
this.cache = cache; |
| 104 |
2 |
} |
| 105 |
|
} |