01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.forms;
18:
19: import java.util.Map;
20:
21: import org.apache.avalon.framework.activity.Disposable;
22: import org.apache.avalon.framework.component.Component;
23: import org.apache.avalon.framework.logger.AbstractLogEnabled;
24: import org.apache.avalon.framework.thread.ThreadSafe;
25: import org.apache.excalibur.source.Source;
26: import org.apache.excalibur.source.SourceValidity;
27:
28: import org.apache.commons.collections.FastHashMap;
29:
30: /**
31: * Component implementing the {@link CacheManager} role.
32: *
33: * @version $Id: DefaultCacheManager.java 449149 2006-09-23 03:58:05Z crossley $
34: */
35: public class DefaultCacheManager extends AbstractLogEnabled implements
36: CacheManager, Disposable, ThreadSafe, Component {
37: // NOTE: Component is there to allow this block to also run in the 2.1 branch
38:
39: // FIXME Unbounded map - the road to OOME
40: protected Map cache;
41:
42: public DefaultCacheManager() {
43: this .cache = new FastHashMap();
44: }
45:
46: public void dispose() {
47: this .cache = null;
48: }
49:
50: public Object get(Source source, String prefix) {
51: // Create a cache key
52: final String key = prefix + source.getURI();
53:
54: // If object is not in the cache then return null
55: Object[] objectAndValidity = (Object[]) this .cache.get(key);
56: if (objectAndValidity == null) {
57: return null;
58: }
59:
60: // If object is in the cache, check stored object validity
61: final SourceValidity validity = (SourceValidity) objectAndValidity[1];
62: int valid = validity.isValid();
63: if (valid == SourceValidity.UNKNOWN) {
64: // Compare against current source validity
65: valid = validity.isValid(source.getValidity());
66: }
67:
68: // If stored object is not valid then remove object from cache and return null
69: if (valid != SourceValidity.VALID) {
70: this .cache.remove(key);
71: return null;
72: }
73:
74: // If valid then return cached object
75: return objectAndValidity[0];
76: }
77:
78: public void set(Object object, Source source, String prefix) {
79: final SourceValidity validity = source.getValidity();
80: if (validity != null) {
81: final String key = prefix + source.getURI();
82: this .cache.put(key, new Object[] { object, validity });
83: }
84: }
85:
86: public void remove(Source source, String prefix) {
87: final String key = prefix + source.getURI();
88: this.cache.remove(key);
89: }
90: }
|