001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.providers.dao.cache;
017:
018: import net.sf.ehcache.CacheException;
019: import net.sf.ehcache.Element;
020: import net.sf.ehcache.Ehcache;
021:
022: import org.acegisecurity.providers.dao.UserCache;
023:
024: import org.acegisecurity.userdetails.UserDetails;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028:
029: import org.springframework.beans.factory.InitializingBean;
030:
031: import org.springframework.dao.DataRetrievalFailureException;
032:
033: import org.springframework.util.Assert;
034:
035: /**
036: * Caches <code>User</code> objects using a Spring IoC defined <A
037: * HREF="http://ehcache.sourceforge.net">EHCACHE</a>.
038: *
039: * @author Ben Alex
040: * @version $Id: EhCacheBasedUserCache.java 1965 2007-08-27 23:41:59Z luke_t $
041: */
042: public class EhCacheBasedUserCache implements UserCache,
043: InitializingBean {
044: //~ Static fields/initializers =====================================================================================
045:
046: private static final Log logger = LogFactory
047: .getLog(EhCacheBasedUserCache.class);
048:
049: //~ Instance fields ================================================================================================
050:
051: private Ehcache cache;
052:
053: //~ Methods ========================================================================================================
054:
055: public void afterPropertiesSet() throws Exception {
056: Assert.notNull(cache, "cache mandatory");
057: }
058:
059: public Ehcache getCache() {
060: return cache;
061: }
062:
063: public UserDetails getUserFromCache(String username) {
064: Element element = null;
065:
066: try {
067: element = cache.get(username);
068: } catch (CacheException cacheException) {
069: throw new DataRetrievalFailureException("Cache failure: "
070: + cacheException.getMessage());
071: }
072:
073: if (logger.isDebugEnabled()) {
074: logger.debug("Cache hit: " + (element != null)
075: + "; username: " + username);
076: }
077:
078: if (element == null) {
079: return null;
080: } else {
081: return (UserDetails) element.getValue();
082: }
083: }
084:
085: public void putUserInCache(UserDetails user) {
086: Element element = new Element(user.getUsername(), user);
087:
088: if (logger.isDebugEnabled()) {
089: logger.debug("Cache put: " + element.getKey());
090: }
091:
092: cache.put(element);
093: }
094:
095: public void removeUserFromCache(UserDetails user) {
096: if (logger.isDebugEnabled()) {
097: logger.debug("Cache remove: " + user.getUsername());
098: }
099:
100: this .removeUserFromCache(user.getUsername());
101: }
102:
103: public void removeUserFromCache(String username) {
104: cache.remove(username);
105: }
106:
107: public void setCache(Ehcache cache) {
108: this.cache = cache;
109: }
110: }
|