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: package org.acegisecurity.acls.jdbc;
016:
017: import net.sf.ehcache.CacheException;
018: import net.sf.ehcache.Element;
019: import net.sf.ehcache.Ehcache;
020:
021: import org.acegisecurity.acls.MutableAcl;
022: import org.acegisecurity.acls.objectidentity.ObjectIdentity;
023:
024: import org.springframework.util.Assert;
025:
026: import java.io.Serializable;
027:
028: /**
029: * Simple implementation of {@link AclCache} that delegates to EH-CACHE.
030: *
031: * @author Ben Alex
032: * @version $Id: EhCacheBasedAclCache.java 1965 2007-08-27 23:41:59Z luke_t $
033: */
034: public class EhCacheBasedAclCache implements AclCache {
035: //~ Instance fields ================================================================================================
036:
037: private Ehcache cache;
038:
039: //~ Constructors ===================================================================================================
040:
041: public EhCacheBasedAclCache(Ehcache cache) {
042: Assert.notNull(cache, "Cache required");
043: this .cache = cache;
044: }
045:
046: //~ Methods ========================================================================================================
047:
048: public void evictFromCache(Serializable pk) {
049: Assert.notNull(pk, "Primary key (identifier) required");
050:
051: MutableAcl acl = getFromCache(pk);
052:
053: if (acl != null) {
054: cache.remove(acl.getId());
055: cache.remove(acl.getObjectIdentity());
056: }
057: }
058:
059: public void evictFromCache(ObjectIdentity objectIdentity) {
060: Assert.notNull(objectIdentity, "ObjectIdentity required");
061:
062: MutableAcl acl = getFromCache(objectIdentity);
063:
064: if (acl != null) {
065: cache.remove(acl.getId());
066: cache.remove(acl.getObjectIdentity());
067: }
068: }
069:
070: public MutableAcl getFromCache(ObjectIdentity objectIdentity) {
071: Assert.notNull(objectIdentity, "ObjectIdentity required");
072:
073: Element element = null;
074:
075: try {
076: element = cache.get(objectIdentity);
077: } catch (CacheException ignored) {
078: }
079:
080: if (element == null) {
081: return null;
082: }
083:
084: return (MutableAcl) element.getValue();
085: }
086:
087: public MutableAcl getFromCache(Serializable pk) {
088: Assert.notNull(pk, "Primary key (identifier) required");
089:
090: Element element = null;
091:
092: try {
093: element = cache.get(pk);
094: } catch (CacheException ignored) {
095: }
096:
097: if (element == null) {
098: return null;
099: }
100:
101: return (MutableAcl) element.getValue();
102: }
103:
104: public void putInCache(MutableAcl acl) {
105: Assert.notNull(acl, "Acl required");
106: Assert.notNull(acl.getObjectIdentity(),
107: "ObjectIdentity required");
108: Assert.notNull(acl.getId(), "ID required");
109:
110: if ((acl.getParentAcl() != null)
111: && (acl.getParentAcl() instanceof MutableAcl)) {
112: putInCache((MutableAcl) acl.getParentAcl());
113: }
114:
115: cache.put(new Element(acl.getObjectIdentity(), acl));
116: cache.put(new Element(acl.getId(), acl));
117: }
118: }
|