001: package org.apache.velocity.runtime.resource.loader;
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 java.io.InputStream;
023:
024: import org.apache.velocity.runtime.RuntimeServices;
025: import org.apache.velocity.runtime.log.Log;
026: import org.apache.velocity.runtime.resource.Resource;
027: import org.apache.velocity.runtime.resource.ResourceCacheImpl;
028:
029: import org.apache.velocity.exception.ResourceNotFoundException;
030:
031: import org.apache.commons.collections.ExtendedProperties;
032:
033: /**
034: * This is abstract class the all text resource loaders should
035: * extend.
036: *
037: * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
038: * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
039: * @version $Id: ResourceLoader.java 463298 2006-10-12 16:10:32Z henning $
040: */
041: public abstract class ResourceLoader {
042: /**
043: * Does this loader want templates produced with it
044: * cached in the Runtime.
045: */
046: protected boolean isCachingOn = false;
047:
048: /**
049: * This property will be passed on to the templates
050: * that are created with this loader.
051: */
052: protected long modificationCheckInterval = 2;
053:
054: /**
055: * Class name for this loader, for logging/debuggin
056: * purposes.
057: */
058: protected String className = null;
059:
060: protected RuntimeServices rsvc = null;
061: protected Log log = null;
062:
063: /**
064: * This initialization is used by all resource
065: * loaders and must be called to set up common
066: * properties shared by all resource loaders
067: * @param rs
068: * @param configuration
069: */
070: public void commonInit(RuntimeServices rs,
071: ExtendedProperties configuration) {
072: this .rsvc = rs;
073: this .log = rsvc.getLog();
074:
075: /*
076: * these two properties are not required for all loaders.
077: * For example, for ClasspathLoader, what would cache mean?
078: * so adding default values which I think are the safest
079: *
080: * don't cache, and modCheckInterval irrelevant...
081: */
082:
083: try {
084: isCachingOn = configuration.getBoolean("cache", false);
085: } catch (Exception e) {
086: isCachingOn = false;
087: log.error("Exception using default of '" + isCachingOn
088: + '\'', e);
089: }
090: try {
091: modificationCheckInterval = configuration.getLong(
092: "modificationCheckInterval", 0);
093: } catch (Exception e) {
094: modificationCheckInterval = 0;
095: log.error("Exception using default of '"
096: + modificationCheckInterval + '\'', e);
097: }
098:
099: /*
100: * this is a must!
101: */
102: className = ResourceCacheImpl.class.getName();
103: try {
104: className = configuration.getString("class", className);
105: } catch (Exception e) {
106: log.error(
107: "Exception using default of '" + className + '\'',
108: e);
109: }
110: }
111:
112: /**
113: * Initialize the template loader with a
114: * a resources class.
115: * @param configuration
116: */
117: public abstract void init(ExtendedProperties configuration);
118:
119: /**
120: * Get the InputStream that the Runtime will parse
121: * to create a template.
122: * @param source
123: * @return The input stream for the requested resource.
124: * @throws ResourceNotFoundException
125: */
126: public abstract InputStream getResourceStream(String source)
127: throws ResourceNotFoundException;
128:
129: /**
130: * Given a template, check to see if the source of InputStream
131: * has been modified.
132: * @param resource
133: * @return True if the resource has been modified.
134: */
135: public abstract boolean isSourceModified(Resource resource);
136:
137: /**
138: * Get the last modified time of the InputStream source
139: * that was used to create the template. We need the template
140: * here because we have to extract the name of the template
141: * in order to locate the InputStream source.
142: * @param resource
143: * @return Time in millis when the resource has been modified.
144: */
145: public abstract long getLastModified(Resource resource);
146:
147: /**
148: * Return the class name of this resource Loader
149: * @return Class name of the resource loader.
150: */
151: public String getClassName() {
152: return className;
153: }
154:
155: /**
156: * Set the caching state. If true, then this loader
157: * would like the Runtime to cache templates that
158: * have been created with InputStreams provided
159: * by this loader.
160: * @param value
161: */
162: public void setCachingOn(boolean value) {
163: isCachingOn = value;
164: }
165:
166: /**
167: * The Runtime uses this to find out whether this
168: * template loader wants the Runtime to cache
169: * templates created with InputStreams provided
170: * by this loader.
171: * @return True if this resource loader caches.
172: */
173: public boolean isCachingOn() {
174: return isCachingOn;
175: }
176:
177: /**
178: * Set the interval at which the InputStream source
179: * should be checked for modifications.
180: * @param modificationCheckInterval
181: */
182: public void setModificationCheckInterval(
183: long modificationCheckInterval) {
184: this .modificationCheckInterval = modificationCheckInterval;
185: }
186:
187: /**
188: * Get the interval at which the InputStream source
189: * should be checked for modifications.
190: * @return The modification check interval.
191: */
192: public long getModificationCheckInterval() {
193: return modificationCheckInterval;
194: }
195: }
|