KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > ac > cache > SourceCacheImpl


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  */

17
18 package org.apache.lenya.ac.cache;
19
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.net.MalformedURLException JavaDoc;
23
24 import org.apache.avalon.framework.activity.Disposable;
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.excalibur.source.Source;
30 import org.apache.excalibur.source.SourceNotFoundException;
31 import org.apache.excalibur.source.SourceResolver;
32 import org.apache.excalibur.source.SourceValidity;
33 import org.apache.lenya.util.CacheMap;
34
35 /**
36  * Basic implementation of a source cache.
37  * @version $Id: SourceCacheImpl.java 43238 2004-08-16 16:06:14Z andreas $
38  */

39 public class SourceCacheImpl
40     extends AbstractLogEnabled
41     implements SourceCache, Serviceable, Disposable {
42
43     /**
44      * Returns the service manager.
45      * @return A service manager.
46      */

47     public ServiceManager getManager() {
48         return manager;
49     }
50
51     /**
52      * Returns the source resolver.
53      * @return A source resolver.
54      */

55     public SourceResolver getResolver() {
56         return resolver;
57     }
58
59     /**
60      * Ctor.
61      */

62     public SourceCacheImpl() {
63         cache = new CacheMap(CAPACITY);
64     }
65
66     public static final int CAPACITY = 1000;
67     private CacheMap cache;
68
69     /**
70      * Returns the cache.
71      * @return A cache object.
72      */

73     protected CacheMap getCache() {
74         return cache;
75     }
76
77     /**
78      * @see org.apache.lenya.ac.cache.SourceCache#get(java.lang.String, org.apache.lenya.ac.cache.InputStreamBuilder)
79      */

80     public Object JavaDoc get(String JavaDoc sourceUri, InputStreamBuilder builder) throws CachingException {
81
82         String JavaDoc key = sourceUri;
83         Object JavaDoc value = null;
84
85         CachedObject cachedObject = (CachedObject) getCache().get(key);
86         boolean usedCache = false;
87         SourceValidity sourceValidity = null;
88
89         try {
90
91             if (cachedObject != null) {
92                 if (getLogger().isDebugEnabled()){
93                     getLogger().debug("Found cached object [" + cachedObject + "]");
94                 }
95                 SourceValidity cachedValidity = cachedObject.getValidityObject();
96
97                 int result = cachedValidity.isValid();
98                 boolean valid = false;
99                 if (result == 0) {
100
101                     // get source validity and compare
102

103                     sourceValidity = getSourceValidity(sourceUri);
104
105                     if (sourceValidity != null) {
106                         result = cachedValidity.isValid(sourceValidity);
107                         if (result == 0) {
108                             sourceValidity = null;
109                         } else {
110                             valid = (result == 1);
111                         }
112                     }
113                 } else {
114                     valid = (result > 0);
115                 }
116
117                 if (valid) {
118                     if (this.getLogger().isDebugEnabled()) {
119                         this.getLogger().debug(
120                             "Using valid cached source for '" + sourceUri + "'.");
121                     }
122                     usedCache = true;
123                     value = cachedObject.getValue();
124                 } else {
125                     if (this.getLogger().isDebugEnabled()) {
126                         this.getLogger().debug(
127                             "Cached content is invalid for '" + sourceUri + "'.");
128                     }
129                     // remove invalid cached object
130
getCache().remove(key);
131                 }
132
133             } else {
134                 getLogger().debug("Did not find cached object.");
135             }
136
137             if (!usedCache) {
138                 getLogger().debug("Did not use cache.");
139                 if (key != null) {
140                     if (sourceValidity == null) {
141                         sourceValidity = getSourceValidity(sourceUri);
142                     }
143                     if (sourceValidity != null) {
144                         if (getLogger().isDebugEnabled()) {
145                             getLogger().debug("Source validity is not null.");
146                         }
147                     } else {
148                         if (getLogger().isDebugEnabled()) {
149                             getLogger().debug("Source validity is null - not caching.");
150                         }
151                         key = null;
152                     }
153                 }
154
155                 value = buildObject(sourceUri, builder);
156
157                 // store the response
158
if (key != null) {
159                     if (this.getLogger().isDebugEnabled()) {
160                         this.getLogger().debug(
161                             "Caching object ["
162                                 + value
163                                 + "] for further requests of ["
164                                 + sourceUri
165                                 + "].");
166                     }
167                     getCache().put(key, new CachedObject(sourceValidity, value));
168                 }
169             }
170
171         } catch (Exception JavaDoc e) {
172             throw new CachingException(e);
173         }
174
175         return value;
176     }
177
178     /**
179      * Returns the input stream to read a source from.
180      * @param sourceUri The URI of the source.
181      * @param builder The input stream builder that should be used.
182      * @return An object.
183      * @throws MalformedURLException when an error occurs.
184      * @throws IOException when an error occurs.
185      * @throws SourceNotFoundException when an error occurs.
186      * @throws BuildException if an error occurs.
187      */

188     protected Object JavaDoc buildObject(String JavaDoc sourceUri, InputStreamBuilder builder)
189         throws MalformedURLException JavaDoc, IOException JavaDoc, SourceNotFoundException, BuildException {
190         Object JavaDoc value = null;
191         Source source = null;
192         try {
193             source = getResolver().resolveURI(sourceUri);
194             if (source.exists()) {
195                 InputStream JavaDoc stream = source.getInputStream();
196                 value = builder.build(stream);
197             }
198         } finally {
199             if (source != null) {
200                 getResolver().release(source);
201             }
202         }
203         return value;
204     }
205
206     /**
207      * Returns the validity of a source.
208      * @param sourceUri The URI of the source.
209      * @return A source validity object.
210      * @throws MalformedURLException when an error occurs.
211      * @throws IOException when an error occurs.
212      */

213     protected SourceValidity getSourceValidity(String JavaDoc sourceUri)
214         throws MalformedURLException JavaDoc, IOException JavaDoc {
215         SourceValidity sourceValidity;
216         Source source = null;
217         try {
218             source = getResolver().resolveURI(sourceUri);
219             sourceValidity = source.getValidity();
220         } finally {
221             if (source != null) {
222                 getResolver().release(source);
223             }
224         }
225         return sourceValidity;
226     }
227
228     private ServiceManager manager;
229     private SourceResolver resolver;
230
231     /**
232      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
233      */

234     public void service(ServiceManager manager) throws ServiceException {
235         this.manager = manager;
236         this.resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
237     }
238
239     /**
240      * @see org.apache.avalon.framework.activity.Disposable#dispose()
241      */

242     public void dispose() {
243         if (getResolver() != null) {
244             getManager().release(getResolver());
245         }
246     }
247
248 }
249
Popular Tags