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