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.cas.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.cas.CasAuthenticationToken;
023: import org.acegisecurity.providers.cas.StatelessTicketCache;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027:
028: import org.springframework.beans.factory.InitializingBean;
029:
030: import org.springframework.dao.DataRetrievalFailureException;
031:
032: import org.springframework.util.Assert;
033:
034: /**
035: * Caches tickets using a Spring IoC defined <A HREF="http://ehcache.sourceforge.net">EHCACHE</a>.
036: *
037: * @author Ben Alex
038: * @version $Id: EhCacheBasedTicketCache.java 1965 2007-08-27 23:41:59Z luke_t $
039: */
040: public class EhCacheBasedTicketCache implements StatelessTicketCache,
041: InitializingBean {
042: //~ Static fields/initializers =====================================================================================
043:
044: private static final Log logger = LogFactory
045: .getLog(EhCacheBasedTicketCache.class);
046:
047: //~ Instance fields ================================================================================================
048:
049: private Ehcache cache;
050:
051: //~ Methods ========================================================================================================
052:
053: public void afterPropertiesSet() throws Exception {
054: Assert.notNull(cache, "cache mandatory");
055: }
056:
057: public CasAuthenticationToken getByTicketId(String serviceTicket) {
058: Element element = null;
059:
060: try {
061: element = cache.get(serviceTicket);
062: } catch (CacheException cacheException) {
063: throw new DataRetrievalFailureException("Cache failure: "
064: + cacheException.getMessage());
065: }
066:
067: if (logger.isDebugEnabled()) {
068: logger.debug("Cache hit: " + (element != null)
069: + "; service ticket: " + serviceTicket);
070: }
071:
072: if (element == null) {
073: return null;
074: } else {
075: return (CasAuthenticationToken) element.getValue();
076: }
077: }
078:
079: public Ehcache getCache() {
080: return cache;
081: }
082:
083: public void putTicketInCache(CasAuthenticationToken token) {
084: Element element = new Element(
085: token.getCredentials().toString(), token);
086:
087: if (logger.isDebugEnabled()) {
088: logger.debug("Cache put: " + element.getKey());
089: }
090:
091: cache.put(element);
092: }
093:
094: public void removeTicketFromCache(CasAuthenticationToken token) {
095: if (logger.isDebugEnabled()) {
096: logger.debug("Cache remove: "
097: + token.getCredentials().toString());
098: }
099:
100: this .removeTicketFromCache(token.getCredentials().toString());
101: }
102:
103: public void removeTicketFromCache(String serviceTicket) {
104: cache.remove(serviceTicket);
105: }
106:
107: public void setCache(Ehcache cache) {
108: this.cache = cache;
109: }
110: }
|