KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > DefaultCacheManager


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.forms;
17
18 import org.apache.avalon.framework.activity.Disposable;
19 import org.apache.avalon.framework.component.Component;
20 import org.apache.avalon.framework.configuration.Configurable;
21 import org.apache.avalon.framework.configuration.Configuration;
22 import org.apache.avalon.framework.configuration.ConfigurationException;
23 import org.apache.avalon.framework.logger.AbstractLogEnabled;
24 import org.apache.avalon.framework.service.ServiceException;
25 import org.apache.avalon.framework.service.ServiceManager;
26 import org.apache.avalon.framework.service.Serviceable;
27 import org.apache.avalon.framework.thread.ThreadSafe;
28
29 import org.apache.commons.collections.FastHashMap;
30 import org.apache.excalibur.source.Source;
31 import org.apache.excalibur.source.SourceValidity;
32
33 import java.io.IOException JavaDoc;
34 import java.util.Map JavaDoc;
35
36 /**
37  * Component implementing the {@link CacheManager} role.
38  *
39  * @version $Id: DefaultCacheManager.java 289538 2005-09-16 13:46:22Z sylvain $
40  */

41 public class DefaultCacheManager
42         extends AbstractLogEnabled
43         implements CacheManager, ThreadSafe, Serviceable, Disposable,
44                    Configurable, Component {
45 // FIXME: Component is there to allow this block to also run in the 2.1 branch
46

47     protected ServiceManager manager;
48     protected Configuration configuration;
49     protected Map cache;
50
51     public DefaultCacheManager() {
52         this.cache = new FastHashMap();
53     }
54
55     public void service(ServiceManager serviceManager) throws ServiceException {
56         this.manager = serviceManager;
57     }
58
59     /**
60      * Configurable
61      */

62     public void configure(Configuration configuration) throws ConfigurationException {
63         this.configuration = configuration;
64     }
65
66     public Object JavaDoc get(Source source, String JavaDoc prefix) {
67         // Create a cache key
68
final String JavaDoc key = prefix + source.getURI();
69
70         // If object is not in the cache then return null
71
Object JavaDoc[] objectAndValidity = (Object JavaDoc[]) this.cache.get(key);
72         if (objectAndValidity == null) {
73             return null;
74         }
75
76         // If object is in the cache, check stored object validity
77
final SourceValidity validity = (SourceValidity) objectAndValidity[1];
78         int valid = validity.isValid();
79         if (valid == SourceValidity.UNKNOWN) {
80             // Compare against current source validity
81
valid = validity.isValid(source.getValidity());
82         }
83
84         // If stored object is not valid then remove object from cache and return null
85
if (valid != SourceValidity.VALID) {
86             this.cache.remove(key);
87             return null;
88         }
89
90         // If valid then return cached object
91
return objectAndValidity[0];
92     }
93
94     public void set(Object JavaDoc object, Source source, String JavaDoc prefix) throws IOException JavaDoc {
95         final String JavaDoc key = prefix + source.getURI();
96         final SourceValidity validity = source.getValidity();
97         if (validity != null) {
98             Object JavaDoc[] objectAndValidity = {object, validity};
99             this.cache.put(key, objectAndValidity);
100         }
101     }
102
103     /**
104      * Disposable
105      */

106     public void dispose() {
107         this.manager = null;
108         this.cache = null;
109     }
110 }
111
Popular Tags