KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > source > DelayedRefreshSourceWrapper


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.components.source;
17
18 import org.apache.cocoon.ProcessingException;
19 import org.apache.cocoon.environment.ModifiableSource;
20 import org.apache.cocoon.environment.Source;
21 import org.apache.cocoon.xml.XMLizable;
22 import org.xml.sax.ContentHandler JavaDoc;
23 import org.xml.sax.InputSource JavaDoc;
24 import org.xml.sax.SAXException JavaDoc;
25
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28
29 /**
30  * A wrapper around a <code>Source</code> that reduces the number of calls to
31  * <code>Source.getLastModified()</code> which can be a costly operation.
32  *
33  * @deprecated by the Avalon Exalibur Source Resolving
34  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
35  * @version CVS $Id: DelayedRefreshSourceWrapper.java 30932 2004-07-29 17:35:38Z vgritsenko $
36  */

37 public final class DelayedRefreshSourceWrapper implements Source, ModifiableSource, XMLizable {
38
39     private Source source;
40
41     private long delay;
42
43     private long nextCheckTime = 0;
44
45     private long lastModified = 0;
46
47     private boolean isModifiableSource;
48
49     /**
50      * Creates a wrapper for a <code>Source</code> which ensures that
51      * <code>Source.getLastModified()</code> won't be called more than once per
52      * <code>delay</code> milliseconds period.
53      *
54      * @param source the wrapped <code>Source</code>
55      * @param delay the last-modified refresh delay, in milliseconds
56      */

57     public DelayedRefreshSourceWrapper(Source source, long delay) {
58         this.source = source;
59         this.delay = delay;
60         this.isModifiableSource = source instanceof ModifiableSource;
61     }
62
63     /**
64      * Get the last modification time for the wrapped <code>Source</code>. The
65      * age of the returned information is guaranteed to be lower than or equal to
66      * the delay specified in the constructor.
67      * <p>
68      * This method is also thread-safe, even if the underlying Source is not.
69      *
70      * @return the last modification time.
71      */

72     public final long getLastModified() {
73
74         // Do we have to refresh the source ?
75
if (System.currentTimeMillis() >= nextCheckTime) {
76             // Yes
77
this.refresh();
78         }
79
80         return this.lastModified;
81     }
82
83     /**
84      * Force the refresh of the wrapped <code>Source</code>, even if the refresh period
85      * isn't over, and starts a new period.
86      * <p>
87      * This method is thread-safe, even if the underlying Source is not.
88      */

89     public synchronized final void refresh() {
90
91         this.nextCheckTime = System.currentTimeMillis() + this.delay;
92         // Refresh modifiable sources
93
if (this.isModifiableSource) {
94             ((ModifiableSource)this.source).refresh();
95         }
96
97         // Keep the last modified date
98
this.lastModified = source.getLastModified();
99     }
100
101     public final long getContentLength() {
102         return this.source.getContentLength();
103     }
104
105     public final InputStream JavaDoc getInputStream() throws ProcessingException, IOException JavaDoc {
106         return this.source.getInputStream();
107     }
108
109     public final InputSource JavaDoc getInputSource() throws ProcessingException, IOException JavaDoc {
110         return this.source.getInputSource();
111     }
112
113     public final String JavaDoc getSystemId() {
114         return this.source.getSystemId();
115     }
116
117     public final void recycle() {
118         this.source.recycle();
119     }
120
121     public final void toSAX(ContentHandler JavaDoc handler) throws SAXException JavaDoc {
122         this.source.toSAX(handler);
123     }
124 }
125
Popular Tags