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