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 729843 2008-12-29 09:06:57Z byron $
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 * Resource type (RESOURCE_TEMPLATE or RESOURCE_CONTENT)
89 */
90 protected int type;
91
92 /**
93 * Default constructor
94 */
95 public Resource()
96 {
97 }
98
99 /**
100 * @param rs
101 */
102 public void setRuntimeServices( RuntimeServices rs )
103 {
104 rsvc = rs;
105 }
106
107 /**
108 * Perform any subsequent processing that might need
109 * to be done by a resource. In the case of a template
110 * the actual parsing of the input stream needs to be
111 * performed.
112 *
113 * @return Whether the resource could be processed successfully.
114 * For a {@link org.apache.velocity.Template} or {@link
115 * org.apache.velocity.runtime.resource.ContentResource}, this
116 * indicates whether the resource could be read.
117 * @exception ResourceNotFoundException Similar in semantics as
118 * returning <code>false</code>.
119 * @throws ParseErrorException
120 */
121 public abstract boolean process()
122 throws ResourceNotFoundException, ParseErrorException;
123
124 /**
125 * @return True if source has been modified.
126 */
127 public boolean isSourceModified()
128 {
129 return resourceLoader.isSourceModified(this);
130 }
131
132 /**
133 * Set the modification check interval.
134 * @param modificationCheckInterval The interval (in seconds).
135 */
136 public void setModificationCheckInterval(long modificationCheckInterval)
137 {
138 this.modificationCheckInterval = modificationCheckInterval;
139 }
140
141 /**
142 * Is it time to check to see if the resource
143 * source has been updated?
144 * @return True if resource must be checked.
145 */
146 public boolean requiresChecking()
147 {
148 /*
149 * short circuit this if modificationCheckInterval == 0
150 * as this means "don't check"
151 */
152
153 if (modificationCheckInterval <= 0 )
154 {
155 return false;
156 }
157
158 /*
159 * see if we need to check now
160 */
161
162 return ( System.currentTimeMillis() >= nextCheck );
163 }
164
165 /**
166 * 'Touch' this template and thereby resetting
167 * the nextCheck field.
168 */
169 public void touch()
170 {
171 nextCheck = System.currentTimeMillis() + ( MILLIS_PER_SECOND * modificationCheckInterval);
172 }
173
174 /**
175 * Set the name of this resource, for example
176 * test.vm.
177 * @param name
178 */
179 public void setName(String name)
180 {
181 this.name = name;
182 }
183
184 /**
185 * Get the name of this template.
186 * @return The name of this template.
187 */
188 public String getName()
189 {
190 return name;
191 }
192
193 /**
194 * set the encoding of this resource
195 * for example, "ISO-8859-1"
196 * @param encoding
197 */
198 public void setEncoding( String encoding )
199 {
200 this.encoding = encoding;
201 }
202
203 /**
204 * get the encoding of this resource
205 * for example, "ISO-8859-1"
206 * @return The encoding of this resource.
207 */
208 public String getEncoding()
209 {
210 return encoding;
211 }
212
213
214 /**
215 * Return the lastModifed time of this
216 * resource.
217 * @return The lastModifed time of this resource.
218 */
219 public long getLastModified()
220 {
221 return lastModified;
222 }
223
224 /**
225 * Set the last modified time for this
226 * resource.
227 * @param lastModified
228 */
229 public void setLastModified(long lastModified)
230 {
231 this.lastModified = lastModified;
232 }
233
234 /**
235 * Return the template loader that pulled
236 * in the template stream
237 * @return The resource loader for this resource.
238 */
239 public ResourceLoader getResourceLoader()
240 {
241 return resourceLoader;
242 }
243
244 /**
245 * Set the template loader for this template. Set
246 * when the Runtime determines where this template
247 * came from the list of possible sources.
248 * @param resourceLoader
249 */
250 public void setResourceLoader(ResourceLoader resourceLoader)
251 {
252 this.resourceLoader = resourceLoader;
253 }
254
255 /**
256 * Set arbitrary data object that might be used
257 * by the resource.
258 * @param data
259 */
260 public void setData(Object data)
261 {
262 this.data = data;
263 }
264
265 /**
266 * Get arbitrary data object that might be used
267 * by the resource.
268 * @return The data object for this resource.
269 */
270 public Object getData()
271 {
272 return data;
273 }
274
275 /**
276 * Sets the type of this Resource (RESOURCE_TEMPLATE or RESOURCE_CONTENT)
277 * @since 1.6
278 */
279 public void setType(int type)
280 {
281 this.type = type;
282 }
283
284 /**
285 * @return type code of the Resource
286 * @since 1.6
287 */
288 public int getType()
289 {
290 return type;
291 }
292 }