KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 1999-2004 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.woody;
17
18 import java.io.IOException JavaDoc;
19
20 import org.apache.avalon.framework.activity.Disposable;
21 import org.apache.avalon.framework.component.Component;
22 import org.apache.avalon.framework.configuration.Configurable;
23 import org.apache.avalon.framework.configuration.Configuration;
24 import org.apache.avalon.framework.configuration.ConfigurationException;
25 import org.apache.avalon.framework.logger.AbstractLogEnabled;
26 import org.apache.avalon.framework.service.ServiceException;
27 import org.apache.avalon.framework.service.ServiceManager;
28 import org.apache.avalon.framework.service.Serviceable;
29 import org.apache.avalon.framework.thread.ThreadSafe;
30 import org.apache.commons.collections.FastHashMap;
31 import org.apache.excalibur.source.Source;
32 import org.apache.excalibur.source.SourceValidity;
33
34 /**
35  * Component implementing the {@link CacheManager} role.
36  *
37  * @version $Id: DefaultCacheManager.java 30932 2004-07-29 17:35:38Z vgritsenko $
38  */

39 public class DefaultCacheManager
40   extends AbstractLogEnabled
41   implements CacheManager, ThreadSafe, Serviceable, Disposable, Configurable, Component {
42       
43     protected ServiceManager manager;
44     protected Configuration configuration;
45     protected FastHashMap cache = new FastHashMap();
46
47     public void service(ServiceManager serviceManager) throws ServiceException {
48         this.manager = serviceManager;
49     }
50
51     /**
52      * Configurable
53      */

54     public void configure(Configuration configuration) throws ConfigurationException {
55         this.configuration = configuration;
56     }
57
58     public Object JavaDoc get(Source source, String JavaDoc prefix) {
59         String JavaDoc key = prefix + source.getURI();
60         SourceValidity newValidity = source.getValidity();
61
62         if (newValidity == null) {
63             cache.remove(key);
64             return null;
65         }
66
67         Object JavaDoc[] objectAndValidity = (Object JavaDoc[])cache.get(key);
68         if (objectAndValidity == null)
69             return null;
70
71         SourceValidity storedValidity = (SourceValidity)objectAndValidity[1];
72         int valid = storedValidity.isValid();
73         boolean isValid;
74         if (valid == 0) {
75             valid = storedValidity.isValid(newValidity);
76             isValid = (valid == 1);
77         } else {
78             isValid = (valid == 1);
79         }
80
81         if (!isValid) {
82             cache.remove(key);
83             return null;
84         }
85
86         return objectAndValidity[0];
87     }
88
89     public void set(Object JavaDoc object, Source source, String JavaDoc prefix) throws IOException JavaDoc {
90         String JavaDoc key = prefix + source.getURI();
91         SourceValidity validity = source.getValidity();
92         if (validity != null) {
93             Object JavaDoc[] objectAndValidity = {object, validity};
94             cache.put(key, objectAndValidity);
95         }
96     }
97
98     /**
99      * Disposable
100      */

101     public void dispose() {
102         this.manager = null;
103         this.cache = null;
104     }
105 }
106
Popular Tags