1 package org.apache.velocity.runtime.resource;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import org.apache.velocity.runtime.RuntimeServices;
23 import org.apache.velocity.runtime.RuntimeConstants;
24
25 import org.apache.velocity.runtime.resource.loader.ResourceLoader;
26
27 import org.apache.velocity.exception.ResourceNotFoundException;
28 import org.apache.velocity.exception.ParseErrorException;
29
30 /**
31 * This class represent a general text resource that
32 * may have been retrieved from any number of possible
33 * sources.
34 *
35 * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
36 * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
37 * @version $Id: Resource.java 463298 2006-10-12 16:10:32Z henning $
38 */
39 public abstract class Resource
40 {
41 protected RuntimeServices rsvc = null;
42
43 /**
44 * The template loader that initially loaded the input
45 * stream for this template, and knows how to check the
46 * source of the input stream for modification.
47 */
48 protected ResourceLoader resourceLoader;
49
50 /**
51 * The number of milliseconds in a minute, used to calculate the
52 * check interval.
53 */
54 protected static final long MILLIS_PER_SECOND = 1000;
55
56 /**
57 * How often the file modification time is checked (in seconds).
58 */
59 protected long modificationCheckInterval = 0;
60
61 /**
62 * The file modification time (in milliseconds) for the cached template.
63 */
64 protected long lastModified = 0;
65
66 /**
67 * The next time the file modification time will be checked (in
68 * milliseconds).
69 */
70 protected long nextCheck = 0;
71
72 /**
73 * Name of the resource
74 */
75 protected String name;
76
77 /**
78 * Character encoding of this resource
79 */
80 protected String encoding = RuntimeConstants.ENCODING_DEFAULT;
81
82 /**
83 * Resource might require ancillary storage of some kind
84 */
85 protected Object data = null;
86
87 /**
88 * Default constructor
89 */
90 public Resource()
91 {
92 }
93
94 /**
95 * @param rs
96 */
97 public void setRuntimeServices( RuntimeServices rs )
98 {
99 rsvc = rs;
100 }
101
102 /**
103 * Perform any subsequent processing that might need
104 * to be done by a resource. In the case of a template
105 * the actual parsing of the input stream needs to be
106 * performed.
107 *
108 * @return Whether the resource could be processed successfully.
109 * For a {@link org.apache.velocity.Template} or {@link
110 * org.apache.velocity.runtime.resource.ContentResource}, this
111 * indicates whether the resource could be read.
112 * @exception ResourceNotFoundException Similar in semantics as
113 * returning <code>false</code>.
114 * @throws ParseErrorException
115 * @throws Exception
116 */
117 public abstract boolean process()
118 throws ResourceNotFoundException, ParseErrorException, Exception;
119
120 /**
121 * @return True if source has been modified.
122 */
123 public boolean isSourceModified()
124 {
125 return resourceLoader.isSourceModified(this);
126 }
127
128 /**
129 * Set the modification check interval.
130 * @param modificationCheckInterval The interval (in seconds).
131 */
132 public void setModificationCheckInterval(long modificationCheckInterval)
133 {
134 this.modificationCheckInterval = modificationCheckInterval;
135 }
136
137 /**
138 * Is it time to check to see if the resource
139 * source has been updated?
140 * @return True if resource must be checked.
141 */
142 public boolean requiresChecking()
143 {
144 /*
145 * short circuit this if modificationCheckInterval == 0
146 * as this means "don't check"
147 */
148
149 if (modificationCheckInterval <= 0 )
150 {
151 return false;
152 }
153
154 /*
155 * see if we need to check now
156 */
157
158 return ( System.currentTimeMillis() >= nextCheck );
159 }
160
161 /**
162 * 'Touch' this template and thereby resetting
163 * the nextCheck field.
164 */
165 public void touch()
166 {
167 nextCheck = System.currentTimeMillis() + ( MILLIS_PER_SECOND * modificationCheckInterval);
168 }
169
170 /**
171 * Set the name of this resource, for example
172 * test.vm.
173 * @param name
174 */
175 public void setName(String name)
176 {
177 this.name = name;
178 }
179
180 /**
181 * Get the name of this template.
182 * @return The name of this template.
183 */
184 public String getName()
185 {
186 return name;
187 }
188
189 /**
190 * set the encoding of this resource
191 * for example, "ISO-8859-1"
192 * @param encoding
193 */
194 public void setEncoding( String encoding )
195 {
196 this.encoding = encoding;
197 }
198
199 /**
200 * get the encoding of this resource
201 * for example, "ISO-8859-1"
202 * @return The encoding of this resource.
203 */
204 public String getEncoding()
205 {
206 return encoding;
207 }
208
209
210 /**
211 * Return the lastModifed time of this
212 * resource.
213 * @return The lastModifed time of this resource.
214 */
215 public long getLastModified()
216 {
217 return lastModified;
218 }
219
220 /**
221 * Set the last modified time for this
222 * resource.
223 * @param lastModified
224 */
225 public void setLastModified(long lastModified)
226 {
227 this.lastModified = lastModified;
228 }
229
230 /**
231 * Return the template loader that pulled
232 * in the template stream
233 * @return The resource loader for this resource.
234 */
235 public ResourceLoader getResourceLoader()
236 {
237 return resourceLoader;
238 }
239
240 /**
241 * Set the template loader for this template. Set
242 * when the Runtime determines where this template
243 * came from the list of possible sources.
244 * @param resourceLoader
245 */
246 public void setResourceLoader(ResourceLoader resourceLoader)
247 {
248 this.resourceLoader = resourceLoader;
249 }
250
251 /**
252 * Set arbitrary data object that might be used
253 * by the resource.
254 * @param data
255 */
256 public void setData(Object data)
257 {
258 this.data = data;
259 }
260
261 /**
262 * Get arbitrary data object that might be used
263 * by the resource.
264 * @return The data object for this resource.
265 */
266 public Object getData()
267 {
268 return data;
269 }
270 }