KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > source > impl > 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.impl;
17
18 import org.apache.avalon.excalibur.pool.Recyclable;
19 import org.apache.excalibur.source.Source;
20 import org.apache.excalibur.source.SourceException;
21 import org.apache.excalibur.source.SourceValidity;
22
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 /**
28  * A wrapper around a <code>Source</code> that reduces the number of calls to
29  * <code>Source.getLastModified()</code> which can be a costly operation.
30  *
31  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
32  * @version CVS $Id: DelayedRefreshSourceWrapper.java 30932 2004-07-29 17:35:38Z vgritsenko $
33  */

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

53     public DelayedRefreshSourceWrapper(Source source, long delay) {
54         this.source = source;
55         this.delay = delay;
56     }
57     
58     /**
59      * Get the real source
60      */

61     public Source getSource() {
62         return this.source;
63     }
64     
65     public final InputStream JavaDoc getInputStream()
66     throws SourceException, IOException JavaDoc {
67         return this.source.getInputStream();
68     }
69
70     public final String JavaDoc getURI() {
71         return this.source.getURI();
72     }
73
74     /**
75      * Get the Validity object. This can either wrap the last modification
76      * date or the expires information or...
77      * If it is currently not possible to calculate such an information
78      * <code>null</code> is returned.
79      */

80     public SourceValidity getValidity() {
81         return this.source.getValidity();
82     }
83
84     /**
85      * Return the protocol identifier.
86      */

87     public String JavaDoc getScheme() {
88         return this.source.getScheme();
89     }
90
91     /**
92      *
93      * @see org.apache.excalibur.source.Source#exists()
94      */

95     public boolean exists() {
96         return this.source.exists();
97     }
98     
99     /**
100      * Get the last modification time for the wrapped <code>Source</code>. The
101      * age of the returned information is guaranteed to be lower than or equal to
102      * the delay specified in the constructor.
103      * <p>
104      * This method is also thread-safe, even if the underlying Source is not.
105      *
106      * @return the last modification time.
107      */

108     public final long getLastModified() {
109
110         // Do we have to refresh the source ?
111
if (System.currentTimeMillis() >= nextCheckTime) {
112             // Yes
113
this.refresh();
114         }
115         return this.lastModified;
116     }
117
118     /**
119      * Force the refresh of the wrapped <code>Source</code>, even if the refresh period
120      * isn't over, and starts a new period.
121      * <p>
122      * This method is thread-safe, even if the underlying Source is not.
123      */

124     public synchronized final void refresh() {
125
126         this.nextCheckTime = System.currentTimeMillis() + this.delay;
127         // Refresh modifiable sources
128
this.source.refresh();
129
130         // Keep the last modified date
131
this.lastModified = source.getLastModified();
132     }
133
134     public final long getContentLength() {
135         return this.source.getContentLength();
136     }
137
138     /**
139      * The mime-type of the content described by this object.
140      * If the source is not able to determine the mime-type by itself
141      * this can be <code>null</code>.
142      */

143     public String JavaDoc getMimeType() {
144         return this.source.getMimeType();
145     }
146
147     public final void recycle() {
148         if (this.source instanceof Recyclable) {
149             ((Recyclable)this.source).recycle();
150         }
151     }
152
153     /**
154      * Get the value of a parameter.
155      * Using this it is possible to get custom information provided by the
156      * source implementation, like an expires date, HTTP headers etc.
157      */

158     public String JavaDoc getParameter(String JavaDoc name) {
159         return null;
160     }
161
162     /**
163      * Get the value of a parameter.
164      * Using this it is possible to get custom information provided by the
165      * source implementation, like an expires date, HTTP headers etc.
166      */

167     public long getParameterAsLong(String JavaDoc name) {
168         return 0;
169     }
170
171     /**
172      * Get parameter names
173      * Using this it is possible to get custom information provided by the
174      * source implementation, like an expires date, HTTP headers etc.
175      */

176     public Iterator JavaDoc getParameterNames() {
177         return java.util.Collections.EMPTY_LIST.iterator();
178
179     }
180
181
182 }
183
Popular Tags