View Javadoc

1   package org.apache.velocity.runtime.resource.loader;
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 java.io.InputStream;
23  
24  import org.apache.velocity.runtime.RuntimeServices;
25  import org.apache.velocity.runtime.log.Log;
26  import org.apache.velocity.runtime.resource.Resource;
27  import org.apache.velocity.runtime.resource.ResourceCacheImpl;
28  
29  import org.apache.velocity.exception.ResourceNotFoundException;
30  
31  import org.apache.commons.collections.ExtendedProperties;
32  
33  /**
34   * This is abstract class the all text resource loaders should
35   * extend.
36   *
37   * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
38   * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
39   * @version $Id: ResourceLoader.java 463298 2006-10-12 16:10:32Z henning $
40   */
41  public abstract class ResourceLoader
42  {
43      /**
44       * Does this loader want templates produced with it
45       * cached in the Runtime.
46       */
47       protected boolean isCachingOn = false;
48  
49      /**
50       * This property will be passed on to the templates
51       * that are created with this loader.
52       */
53      protected long modificationCheckInterval = 2;
54  
55      /**
56       * Class name for this loader, for logging/debuggin
57       * purposes.
58       */
59      protected String className = null;
60  
61      protected RuntimeServices rsvc = null;
62      protected Log log = null;
63  
64      /**
65       * This initialization is used by all resource
66       * loaders and must be called to set up common
67       * properties shared by all resource loaders
68       * @param rs
69       * @param configuration
70       */
71      public void commonInit( RuntimeServices rs, ExtendedProperties configuration)
72      {
73          this.rsvc = rs;
74          this.log = rsvc.getLog();
75  
76          /*
77           *  these two properties are not required for all loaders.
78           *  For example, for ClasspathLoader, what would cache mean?
79           *  so adding default values which I think are the safest
80           *
81           *  don't cache, and modCheckInterval irrelevant...
82           */
83  
84          try
85          {
86              isCachingOn = configuration.getBoolean("cache", false);
87          }
88          catch (Exception e)
89          {
90              isCachingOn = false;
91              log.error("Exception using default of '" + isCachingOn + '\'', e);
92          }
93          try
94          {
95              modificationCheckInterval = configuration.getLong("modificationCheckInterval", 0);
96          }
97          catch (Exception e)
98          {
99              modificationCheckInterval = 0;
100             log.error("Exception using default of '" +
101                       modificationCheckInterval + '\'', e);
102         }
103 
104         /*
105          * this is a must!
106          */
107         className = ResourceCacheImpl.class.getName();
108         try
109         {
110             className = configuration.getString("class", className);
111         }
112         catch (Exception e)
113         {
114             log.error("Exception using default of '" + className + '\'', e);
115         }
116     }
117 
118     /**
119      * Initialize the template loader with a
120      * a resources class.
121      * @param configuration
122      */
123     public abstract void init( ExtendedProperties configuration);
124 
125     /**
126      * Get the InputStream that the Runtime will parse
127      * to create a template.
128      * @param source
129      * @return The input stream for the requested resource.
130      * @throws ResourceNotFoundException
131      */
132     public abstract InputStream getResourceStream( String source )
133         throws ResourceNotFoundException;
134 
135     /**
136      * Given a template, check to see if the source of InputStream
137      * has been modified.
138      * @param resource
139      * @return True if the resource has been modified.
140      */
141     public abstract boolean isSourceModified(Resource resource);
142 
143     /**
144      * Get the last modified time of the InputStream source
145      * that was used to create the template. We need the template
146      * here because we have to extract the name of the template
147      * in order to locate the InputStream source.
148      * @param resource
149      * @return Time in millis when the resource has been modified.
150      */
151     public abstract long getLastModified(Resource resource);
152 
153     /**
154      * Return the class name of this resource Loader
155      * @return Class name of the resource loader.
156      */
157     public String getClassName()
158     {
159         return className;
160     }
161 
162     /**
163      * Set the caching state. If true, then this loader
164      * would like the Runtime to cache templates that
165      * have been created with InputStreams provided
166      * by this loader.
167      * @param value
168      */
169     public void setCachingOn(boolean value)
170     {
171         isCachingOn = value;
172     }
173 
174     /**
175      * The Runtime uses this to find out whether this
176      * template loader wants the Runtime to cache
177      * templates created with InputStreams provided
178      * by this loader.
179      * @return True if this resource loader caches.
180      */
181     public boolean isCachingOn()
182     {
183         return isCachingOn;
184     }
185 
186     /**
187      * Set the interval at which the InputStream source
188      * should be checked for modifications.
189      * @param modificationCheckInterval
190      */
191     public void setModificationCheckInterval(long modificationCheckInterval)
192     {
193         this.modificationCheckInterval = modificationCheckInterval;
194     }
195 
196     /**
197      * Get the interval at which the InputStream source
198      * should be checked for modifications.
199      * @return The modification check interval.
200      */
201     public long getModificationCheckInterval()
202     {
203         return modificationCheckInterval;
204     }
205 }