View Javadoc

1   /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
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   * Caches tickets using a Spring IoC defined <A HREF="http://ehcache.sourceforge.net">EHCACHE</a>.
37   *
38   * @author Ben Alex
39   * @version $Id: EhCacheBasedTicketCache.java 1965 2007-08-27 23:41:59Z luke_t $
40   */
41  public class EhCacheBasedTicketCache implements StatelessTicketCache, InitializingBean {
42      //~ Static fields/initializers =====================================================================================
43  
44      private static final Log logger = LogFactory.getLog(EhCacheBasedTicketCache.class);
45  
46      //~ Instance fields ================================================================================================
47  
48      private Ehcache cache;
49  
50      //~ Methods ========================================================================================================
51  
52      public void afterPropertiesSet() throws Exception {
53          Assert.notNull(cache, "cache mandatory");
54      }
55  
56      public CasAuthenticationToken getByTicketId(String serviceTicket) {
57          Element element = null;
58  
59          try {
60              element = cache.get(serviceTicket);
61          } catch (CacheException cacheException) {
62              throw new DataRetrievalFailureException("Cache failure: " + cacheException.getMessage());
63          }
64  
65          if (logger.isDebugEnabled()) {
66              logger.debug("Cache hit: " + (element != null) + "; service ticket: " + serviceTicket);
67          }
68  
69          if (element == null) {
70              return null;
71          } else {
72              return (CasAuthenticationToken) element.getValue();
73          }
74      }
75  
76      public Ehcache getCache() {
77          return cache;
78      }
79  
80      public void putTicketInCache(CasAuthenticationToken token) {
81          Element element = new Element(token.getCredentials().toString(), token);
82  
83          if (logger.isDebugEnabled()) {
84              logger.debug("Cache put: " + element.getKey());
85          }
86  
87          cache.put(element);
88      }
89  
90      public void removeTicketFromCache(CasAuthenticationToken token) {
91          if (logger.isDebugEnabled()) {
92              logger.debug("Cache remove: " + token.getCredentials().toString());
93          }
94  
95          this.removeTicketFromCache(token.getCredentials().toString());
96      }
97  
98      public void removeTicketFromCache(String serviceTicket) {
99          cache.remove(serviceTicket);
100     }
101 
102     public void setCache(Ehcache cache) {
103         this.cache = cache;
104     }
105 }