01: /*
02: * Copyright 2004 Outerthought bvba and Schaubroeck nv
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
23:
24: public class CacheHelper {
25: public static Object getFromCache(Store store, Source source,
26: String prefix) {
27: String key = prefix + source.getURI();
28: SourceValidity newValidity = source.getValidity();
29:
30: // If source is not valid then remove object from cache and return null
31: if (newValidity == null) {
32: store.remove(key);
33: return null;
34: }
35:
36: // If object is not in cache then return null
37: Object[] objectAndValidity = (Object[]) store.get(key);
38: if (objectAndValidity == null) {
39: return null;
40: }
41:
42: // Check stored validity against current source validity
43: SourceValidity storedValidity = (SourceValidity) objectAndValidity[1];
44: int valid = storedValidity.isValid();
45: boolean isValid;
46: if (valid == SourceValidity.UNKNOWN) {
47: valid = storedValidity.isValid(newValidity);
48: isValid = (valid == SourceValidity.VALID);
49: } else {
50: isValid = (valid == SourceValidity.VALID);
51: }
52:
53: // If stored object is not valid then remove object from cache and return null
54: if (!isValid) {
55: store.remove(key);
56: return null;
57: }
58:
59: // If valid then return cached object
60: return objectAndValidity[0];
61: }
62:
63: public static void setInCache(Store store, Object object,
64: Source source, String prefix) throws IOException {
65: String key = prefix + source.getURI();
66: SourceValidity validity = source.getValidity();
67: if (validity != null) {
68: Object[] objectAndValidity = { object, validity };
69: store.store(key, objectAndValidity);
70: }
71: }
72: }
|