Source code: org/acegisecurity/acl/basic/cache/EhCacheBasedAclEntryCache.java
1 /* Copyright 2004 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.acl.basic.cache;
17
18 import org.acegisecurity.acl.basic.AclObjectIdentity;
19 import org.acegisecurity.acl.basic.BasicAclEntry;
20 import org.acegisecurity.acl.basic.BasicAclEntryCache;
21
22 import net.sf.ehcache.Cache;
23 import net.sf.ehcache.CacheException;
24 import net.sf.ehcache.Element;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import org.springframework.beans.factory.InitializingBean;
30
31 import org.springframework.dao.DataRetrievalFailureException;
32 import org.springframework.util.Assert;
33
34
35 /**
36 * Caches <code>BasicAclEntry</code>s using a Spring IoC defined <A
37 * HREF="http://ehcache.sourceforge.net">EHCACHE</a>.
38 *
39 * @author Ben Alex
40 * @version $Id: EhCacheBasedAclEntryCache.java,v 1.5 2005/11/17 00:56:47 benalex Exp $
41 */
42 public class EhCacheBasedAclEntryCache implements BasicAclEntryCache,
43 InitializingBean {
44 //~ Static fields/initializers =============================================
45
46 private static final Log logger = LogFactory.getLog(EhCacheBasedAclEntryCache.class);
47
48 //~ Instance fields ========================================================
49
50 private Cache cache;
51
52 //~ Methods ================================================================
53
54 public void setCache(Cache cache) {
55 this.cache = cache;
56 }
57
58 public Cache getCache() {
59 return cache;
60 }
61
62 public BasicAclEntry[] getEntriesFromCache(
63 AclObjectIdentity aclObjectIdentity) {
64 Element element = null;
65
66 try {
67 element = cache.get(aclObjectIdentity);
68 } catch (CacheException cacheException) {
69 throw new DataRetrievalFailureException("Cache failure: "
70 + cacheException.getMessage());
71 }
72
73 // Return null if cache element has expired or not found
74 if (element == null) {
75 if (logger.isDebugEnabled()) {
76 logger.debug("Cache miss: " + aclObjectIdentity);
77 }
78
79 return null;
80 }
81
82 if (logger.isDebugEnabled()) {
83 logger.debug("Cache hit: " + (element != null) + "; object: "
84 + aclObjectIdentity);
85 }
86
87 BasicAclEntryHolder holder = (BasicAclEntryHolder) element.getValue();
88
89 return holder.getBasicAclEntries();
90 }
91
92 public void afterPropertiesSet() throws Exception {
93 Assert.notNull(cache, "cache mandatory");
94 }
95
96 public void putEntriesInCache(BasicAclEntry[] basicAclEntry) {
97 BasicAclEntryHolder holder = new BasicAclEntryHolder(basicAclEntry);
98 Element element = new Element(basicAclEntry[0].getAclObjectIdentity(),
99 holder);
100
101 if (logger.isDebugEnabled()) {
102 logger.debug("Cache put: " + element.getKey());
103 }
104
105 cache.put(element);
106 }
107 }