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.x509.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.x509.X509UserCache;
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: import java.security.cert.X509Certificate;
036:
037: /**
038: * Caches <code>User</code> objects using a Spring IoC defined <a
039: * href="http://ehcache.sourceforge.net">EHCACHE</a>.
040: *
041: * @author Luke Taylor
042: * @author Ben Alex
043: * @version $Id: EhCacheBasedX509UserCache.java 1965 2007-08-27 23:41:59Z luke_t $
044: */
045: public class EhCacheBasedX509UserCache implements X509UserCache,
046: InitializingBean {
047: //~ Static fields/initializers =====================================================================================
048:
049: private static final Log logger = LogFactory
050: .getLog(EhCacheBasedX509UserCache.class);
051:
052: //~ Instance fields ================================================================================================
053:
054: private Ehcache cache;
055:
056: //~ Methods ========================================================================================================
057:
058: public void afterPropertiesSet() throws Exception {
059: Assert.notNull(cache, "cache is mandatory");
060: }
061:
062: public UserDetails getUserFromCache(X509Certificate userCert) {
063: Element element = null;
064:
065: try {
066: element = cache.get(userCert);
067: } catch (CacheException cacheException) {
068: throw new DataRetrievalFailureException("Cache failure: "
069: + cacheException.getMessage());
070: }
071:
072: if (logger.isDebugEnabled()) {
073: String subjectDN = "unknown";
074:
075: if ((userCert != null) && (userCert.getSubjectDN() != null)) {
076: subjectDN = userCert.getSubjectDN().toString();
077: }
078:
079: logger.debug("X.509 Cache hit. SubjectDN: " + subjectDN);
080: }
081:
082: if (element == null) {
083: return null;
084: } else {
085: return (UserDetails) element.getValue();
086: }
087: }
088:
089: public void putUserInCache(X509Certificate userCert,
090: UserDetails user) {
091: Element element = new Element(userCert, user);
092:
093: if (logger.isDebugEnabled()) {
094: logger.debug("Cache put: " + userCert.getSubjectDN());
095: }
096:
097: cache.put(element);
098: }
099:
100: public void removeUserFromCache(X509Certificate userCert) {
101: if (logger.isDebugEnabled()) {
102: logger.debug("Cache remove: " + userCert.getSubjectDN());
103: }
104:
105: cache.remove(userCert);
106: }
107:
108: public void setCache(Ehcache cache) {
109: this.cache = cache;
110: }
111: }
|