KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > runtime > resource > loader > ResourceLoader


1 package org.apache.velocity.runtime.resource.loader;
2
3 /*
4  * Copyright 2001,2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.InputStream JavaDoc;
20
21 import org.apache.velocity.runtime.RuntimeServices;
22
23 import org.apache.velocity.runtime.resource.Resource;
24
25 import org.apache.velocity.exception.ResourceNotFoundException;
26
27 import org.apache.commons.collections.ExtendedProperties;
28
29 /**
30  * This is abstract class the all text resource loaders should
31  * extend.
32  *
33  * @author <a HREF="mailto:jvanzyl@apache.org">Jason van Zyl</a>
34  * @author <a HREF="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
35  * @version $Id: ResourceLoader.java,v 1.14.4.1 2004/03/03 23:23:02 geirm Exp $
36  */

37 public abstract class ResourceLoader
38 {
39     /**
40      * Does this loader want templates produced with it
41      * cached in the Runtime.
42      */

43      protected boolean isCachingOn = false;
44     
45     /**
46      * This property will be passed on to the templates
47      * that are created with this loader.
48      */

49     protected long modificationCheckInterval = 2;
50    
51     /**
52      * Class name for this loader, for logging/debuggin
53      * purposes.
54      */

55     protected String JavaDoc className = null;
56
57     protected RuntimeServices rsvc = null;
58
59     /**
60      * This initialization is used by all resource
61      * loaders and must be called to set up common
62      * properties shared by all resource loaders
63      */

64     public void commonInit( RuntimeServices rs, ExtendedProperties configuration)
65     {
66         this.rsvc = rs;
67
68         /*
69          * these two properties are not required for all loaders.
70          * For example, for ClasspathLoader, what would cache mean?
71          * so adding default values which I think are the safest
72          *
73          * don't cache, and modCheckInterval irrelevant...
74          */

75
76         isCachingOn = configuration.getBoolean("cache", false);
77         modificationCheckInterval = configuration.getLong("modificationCheckInterval", 0);
78         
79         /*
80          * this is a must!
81          */

82
83         className = configuration.getString("class");
84     }
85
86     /**
87      * Initialize the template loader with a
88      * a resources class.
89      */

90     public abstract void init( ExtendedProperties configuration);
91
92     /**
93      * Get the InputStream that the Runtime will parse
94      * to create a template.
95      */

96     public abstract InputStream JavaDoc getResourceStream( String JavaDoc source )
97         throws ResourceNotFoundException;
98
99     /**
100      * Given a template, check to see if the source of InputStream
101      * has been modified.
102      */

103     public abstract boolean isSourceModified(Resource resource);
104     
105     /**
106      * Get the last modified time of the InputStream source
107      * that was used to create the template. We need the template
108      * here because we have to extract the name of the template
109      * in order to locate the InputStream source.
110      */

111     public abstract long getLastModified(Resource resource);
112
113     /**
114      * Return the class name of this resource Loader
115      */

116     public String JavaDoc getClassName()
117     {
118         return className;
119     }
120
121     /**
122      * Set the caching state. If true, then this loader
123      * would like the Runtime to cache templates that
124      * have been created with InputStreams provided
125      * by this loader.
126      */

127     public void setCachingOn(boolean value)
128     {
129         isCachingOn = value;
130     }
131
132     /**
133      * The Runtime uses this to find out whether this
134      * template loader wants the Runtime to cache
135      * templates created with InputStreams provided
136      * by this loader.
137      */

138     public boolean isCachingOn()
139     {
140         return isCachingOn;
141     }
142
143     /**
144      * Set the interval at which the InputStream source
145      * should be checked for modifications.
146      */

147     public void setModificationCheckInterval(long modificationCheckInterval)
148     {
149         this.modificationCheckInterval = modificationCheckInterval;
150     }
151     
152     /**
153      * Get the interval at which the InputStream source
154      * should be checked for modifications.
155      */

156     public long getModificationCheckInterval()
157     {
158         return modificationCheckInterval;
159     }
160 }
161
Popular Tags