KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > caching > IncludeCacheValidity


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.caching;
17
18 import org.apache.excalibur.source.Source;
19 import org.apache.cocoon.environment.SourceResolver;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25
26 /**
27  * A validation object used in CachingCIncludeTransformer
28  *
29  * @deprecated Use the Avalon Excalibur SourceValidity implementations instead
30  * @author <a HREF="mailto:maciejka@tiger.com.pl">Maciek Kaminski</a>
31  * @version CVS $Id: IncludeCacheValidity.java 30932 2004-07-29 17:35:38Z vgritsenko $
32  */

33 public final class IncludeCacheValidity implements CacheValidity {
34
35     private List JavaDoc sources;
36     private List JavaDoc timeStamps;
37
38     private boolean isNew;
39
40     private SourceResolver resolver;
41
42     public IncludeCacheValidity(SourceResolver resolver) {
43         this.resolver = resolver;
44         sources = new ArrayList JavaDoc();
45         timeStamps = new ArrayList JavaDoc();
46         isNew = true;
47     }
48
49     public boolean isNew() {
50         return isNew;
51     }
52
53     public void setIsNew2False() {
54         isNew = false;
55         resolver = null;
56     }
57
58     public void add(String JavaDoc source, long timeStamp) {
59         this.sources.add(source);
60         this.timeStamps.add(new Long JavaDoc(timeStamp));
61     }
62
63     public boolean isValid(CacheValidity validity) {
64         if (validity instanceof IncludeCacheValidity) {
65             SourceResolver otherResolver = ((IncludeCacheValidity) validity).resolver;
66
67             for(Iterator JavaDoc i = sources.iterator(), j = timeStamps.iterator(); i.hasNext();) {
68                 String JavaDoc src = ((String JavaDoc)i.next());
69                 long timeStamp = ((Long JavaDoc)j.next()).longValue();
70                 Source otherSource = null;
71                 try {
72                     otherSource = otherResolver.resolveURI(src);
73                     if(otherSource.getLastModified() != timeStamp ||
74                         timeStamp == 0)
75                         return false;
76                 } catch (Exception JavaDoc e) {
77                     return false;
78                 } finally {
79                     otherResolver.release(otherSource);
80                 }
81             }
82             return true;
83         }
84         return false;
85     }
86
87     public String JavaDoc toString() {
88         StringBuffer JavaDoc b = new StringBuffer JavaDoc("Include Validity[");
89         for(Iterator JavaDoc i = sources.iterator(), j = timeStamps.iterator(); i.hasNext();) {
90             b.append('{');
91             b.append(i.next());
92             b.append(':');
93             b.append(j.next());
94             b.append('}');
95             if(i.hasNext()) b.append(':');
96         }
97         b.append(']');
98         return b.toString();
99     }
100 }
101
Popular Tags