001: package org.apache.velocity.runtime.resource;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import org.apache.velocity.runtime.RuntimeServices;
023: import org.apache.velocity.runtime.RuntimeConstants;
024:
025: import org.apache.velocity.runtime.resource.loader.ResourceLoader;
026:
027: import org.apache.velocity.exception.ResourceNotFoundException;
028: import org.apache.velocity.exception.ParseErrorException;
029:
030: /**
031: * This class represent a general text resource that
032: * may have been retrieved from any number of possible
033: * sources.
034: *
035: * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
036: * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
037: * @version $Id: Resource.java 463298 2006-10-12 16:10:32Z henning $
038: */
039: public abstract class Resource {
040: protected RuntimeServices rsvc = null;
041:
042: /**
043: * The template loader that initially loaded the input
044: * stream for this template, and knows how to check the
045: * source of the input stream for modification.
046: */
047: protected ResourceLoader resourceLoader;
048:
049: /**
050: * The number of milliseconds in a minute, used to calculate the
051: * check interval.
052: */
053: protected static final long MILLIS_PER_SECOND = 1000;
054:
055: /**
056: * How often the file modification time is checked (in seconds).
057: */
058: protected long modificationCheckInterval = 0;
059:
060: /**
061: * The file modification time (in milliseconds) for the cached template.
062: */
063: protected long lastModified = 0;
064:
065: /**
066: * The next time the file modification time will be checked (in
067: * milliseconds).
068: */
069: protected long nextCheck = 0;
070:
071: /**
072: * Name of the resource
073: */
074: protected String name;
075:
076: /**
077: * Character encoding of this resource
078: */
079: protected String encoding = RuntimeConstants.ENCODING_DEFAULT;
080:
081: /**
082: * Resource might require ancillary storage of some kind
083: */
084: protected Object data = null;
085:
086: /**
087: * Default constructor
088: */
089: public Resource() {
090: }
091:
092: /**
093: * @param rs
094: */
095: public void setRuntimeServices(RuntimeServices rs) {
096: rsvc = rs;
097: }
098:
099: /**
100: * Perform any subsequent processing that might need
101: * to be done by a resource. In the case of a template
102: * the actual parsing of the input stream needs to be
103: * performed.
104: *
105: * @return Whether the resource could be processed successfully.
106: * For a {@link org.apache.velocity.Template} or {@link
107: * org.apache.velocity.runtime.resource.ContentResource}, this
108: * indicates whether the resource could be read.
109: * @exception ResourceNotFoundException Similar in semantics as
110: * returning <code>false</code>.
111: * @throws ParseErrorException
112: * @throws Exception
113: */
114: public abstract boolean process() throws ResourceNotFoundException,
115: ParseErrorException, Exception;
116:
117: /**
118: * @return True if source has been modified.
119: */
120: public boolean isSourceModified() {
121: return resourceLoader.isSourceModified(this );
122: }
123:
124: /**
125: * Set the modification check interval.
126: * @param modificationCheckInterval The interval (in seconds).
127: */
128: public void setModificationCheckInterval(
129: long modificationCheckInterval) {
130: this .modificationCheckInterval = modificationCheckInterval;
131: }
132:
133: /**
134: * Is it time to check to see if the resource
135: * source has been updated?
136: * @return True if resource must be checked.
137: */
138: public boolean requiresChecking() {
139: /*
140: * short circuit this if modificationCheckInterval == 0
141: * as this means "don't check"
142: */
143:
144: if (modificationCheckInterval <= 0) {
145: return false;
146: }
147:
148: /*
149: * see if we need to check now
150: */
151:
152: return (System.currentTimeMillis() >= nextCheck);
153: }
154:
155: /**
156: * 'Touch' this template and thereby resetting
157: * the nextCheck field.
158: */
159: public void touch() {
160: nextCheck = System.currentTimeMillis()
161: + (MILLIS_PER_SECOND * modificationCheckInterval);
162: }
163:
164: /**
165: * Set the name of this resource, for example
166: * test.vm.
167: * @param name
168: */
169: public void setName(String name) {
170: this .name = name;
171: }
172:
173: /**
174: * Get the name of this template.
175: * @return The name of this template.
176: */
177: public String getName() {
178: return name;
179: }
180:
181: /**
182: * set the encoding of this resource
183: * for example, "ISO-8859-1"
184: * @param encoding
185: */
186: public void setEncoding(String encoding) {
187: this .encoding = encoding;
188: }
189:
190: /**
191: * get the encoding of this resource
192: * for example, "ISO-8859-1"
193: * @return The encoding of this resource.
194: */
195: public String getEncoding() {
196: return encoding;
197: }
198:
199: /**
200: * Return the lastModifed time of this
201: * resource.
202: * @return The lastModifed time of this resource.
203: */
204: public long getLastModified() {
205: return lastModified;
206: }
207:
208: /**
209: * Set the last modified time for this
210: * resource.
211: * @param lastModified
212: */
213: public void setLastModified(long lastModified) {
214: this .lastModified = lastModified;
215: }
216:
217: /**
218: * Return the template loader that pulled
219: * in the template stream
220: * @return The resource loader for this resource.
221: */
222: public ResourceLoader getResourceLoader() {
223: return resourceLoader;
224: }
225:
226: /**
227: * Set the template loader for this template. Set
228: * when the Runtime determines where this template
229: * came from the list of possible sources.
230: * @param resourceLoader
231: */
232: public void setResourceLoader(ResourceLoader resourceLoader) {
233: this .resourceLoader = resourceLoader;
234: }
235:
236: /**
237: * Set arbitrary data object that might be used
238: * by the resource.
239: * @param data
240: */
241: public void setData(Object data) {
242: this .data = data;
243: }
244:
245: /**
246: * Get arbitrary data object that might be used
247: * by the resource.
248: * @return The data object for this resource.
249: */
250: public Object getData() {
251: return data;
252: }
253: }
|