KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > frontend > util > CacheHelper


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
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.outerj.daisy.frontend.util;
17
18 import org.apache.excalibur.store.Store;
19 import org.apache.excalibur.source.Source;
20 import org.apache.excalibur.source.SourceValidity;
21
22 import java.io.IOException JavaDoc;
23
24 public class CacheHelper {
25     public static Object JavaDoc getFromCache(Store store, Source source, String JavaDoc prefix) {
26         String JavaDoc key = prefix + source.getURI();
27         SourceValidity newValidity = source.getValidity();
28
29         // If source is not valid then remove object from cache and return null
30
if (newValidity == null) {
31             store.remove(key);
32             return null;
33         }
34
35         // If object is not in cache then return null
36
Object JavaDoc[] objectAndValidity = (Object JavaDoc[]) store.get(key);
37         if (objectAndValidity == null) {
38             return null;
39         }
40
41         // Check stored validity against current source validity
42
SourceValidity storedValidity = (SourceValidity) objectAndValidity[1];
43         int valid = storedValidity.isValid();
44         boolean isValid;
45         if (valid == SourceValidity.UNKNOWN) {
46             valid = storedValidity.isValid(newValidity);
47             isValid = (valid == SourceValidity.VALID);
48         } else {
49             isValid = (valid == SourceValidity.VALID);
50         }
51
52         // If stored object is not valid then remove object from cache and return null
53
if (!isValid) {
54             store.remove(key);
55             return null;
56         }
57
58         // If valid then return cached object
59
return objectAndValidity[0];
60     }
61
62     public static void setInCache(Store store, Object JavaDoc object, Source source, String JavaDoc prefix) throws IOException JavaDoc {
63         String JavaDoc key = prefix + source.getURI();
64         SourceValidity validity = source.getValidity();
65         if (validity != null) {
66             Object JavaDoc[] objectAndValidity = {object, validity};
67             store.store(key, objectAndValidity);
68         }
69     }
70 }
71
Popular Tags