KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > runtime > resource > Resource


1 package org.apache.velocity.runtime.resource;
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 org.apache.velocity.runtime.RuntimeServices;
20 import org.apache.velocity.runtime.RuntimeConstants;
21
22 import org.apache.velocity.runtime.resource.loader.ResourceLoader;
23
24 import org.apache.velocity.exception.ResourceNotFoundException;
25 import org.apache.velocity.exception.ParseErrorException;
26
27 /**
28  * This class represent a general text resource that
29  * may have been retrieved from any number of possible
30  * sources.
31  *
32  * @author <a HREF="mailto:jvanzyl@apache.org">Jason van Zyl</a>
33  * @author <a HREF="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
34  * @version $Id: Resource.java,v 1.12.4.1 2004/03/03 23:23:01 geirm Exp $
35  */

36 public abstract class Resource
37 {
38     protected RuntimeServices rsvc = null;
39
40     /**
41      * The template loader that initially loaded the input
42      * stream for this template, and knows how to check the
43      * source of the input stream for modification.
44      */

45     protected ResourceLoader resourceLoader;
46
47     /**
48      * The number of milliseconds in a minute, used to calculate the
49      * check interval.
50      */

51     protected static final long MILLIS_PER_SECOND = 1000;
52
53     /**
54      * How often the file modification time is checked (in seconds).
55      */

56     protected long modificationCheckInterval = 0;
57
58     /**
59      * The file modification time (in milliseconds) for the cached template.
60      */

61     protected long lastModified = 0;
62
63     /**
64      * The next time the file modification time will be checked (in
65      * milliseconds).
66      */

67     protected long nextCheck = 0;
68
69     /**
70      * Name of the resource
71      */

72     protected String JavaDoc name;
73
74     /**
75      * Character encoding of this resource
76      */

77     protected String JavaDoc encoding = RuntimeConstants.ENCODING_DEFAULT;
78
79     /**
80      * Resource might require ancillary storage of some kind
81      */

82     protected Object JavaDoc data = null;
83
84     /**
85      * Default constructor
86      */

87     public Resource()
88     {
89     }
90
91     public void setRuntimeServices( RuntimeServices rs )
92     {
93         rsvc = rs;
94     }
95
96     /**
97      * Perform any subsequent processing that might need
98      * to be done by a resource. In the case of a template
99      * the actual parsing of the input stream needs to be
100      * performed.
101      *
102      * @return Whether the resource could be processed successfully.
103      * For a {@link org.apache.velocity.Template} or {@link
104      * org.apache.velocity.runtime.resource.ContentResource}, this
105      * indicates whether the resource could be read.
106      * @exception ResourceNotFoundException Similar in semantics as
107      * returning <code>false</code>.
108      */

109     public abstract boolean process()
110         throws ResourceNotFoundException, ParseErrorException, Exception JavaDoc;
111
112     public boolean isSourceModified()
113     {
114         return resourceLoader.isSourceModified(this);
115     }
116
117     /**
118      * Set the modification check interval.
119      * @param interval The interval (in seconds).
120      */

121     public void setModificationCheckInterval(long modificationCheckInterval)
122     {
123         this.modificationCheckInterval = modificationCheckInterval;
124     }
125     
126     /**
127      * Is it time to check to see if the resource
128      * source has been updated?
129      */

130     public boolean requiresChecking()
131     {
132         /*
133          * short circuit this if modificationCheckInterval == 0
134          * as this means "don't check"
135          */

136         
137         if (modificationCheckInterval <= 0 )
138         {
139            return false;
140         }
141
142         /*
143          * see if we need to check now
144          */

145
146         return ( System.currentTimeMillis() >= nextCheck );
147     }
148
149     /**
150      * 'Touch' this template and thereby resetting
151      * the nextCheck field.
152      */

153     public void touch()
154     {
155         nextCheck = System.currentTimeMillis() + ( MILLIS_PER_SECOND * modificationCheckInterval);
156     }
157     
158     /**
159      * Set the name of this resource, for example
160      * test.vm.
161      */

162     public void setName(String JavaDoc name)
163     {
164         this.name = name;
165     }
166
167     /**
168      * Get the name of this template.
169      */

170     public String JavaDoc getName()
171     {
172         return name;
173     }
174
175     /**
176      * set the encoding of this resource
177      * for example, "ISO-8859-1"
178      */

179     public void setEncoding( String JavaDoc encoding )
180     {
181         this.encoding = encoding;
182     }
183
184     /**
185      * get the encoding of this resource
186      * for example, "ISO-8859-1"
187      */

188     public String JavaDoc getEncoding()
189     {
190         return encoding;
191     }
192
193
194     /**
195      * Return the lastModifed time of this
196      * template.
197      */

198     public long getLastModified()
199     {
200         return lastModified;
201     }
202     
203     /**
204      * Set the last modified time for this
205      * template.
206      */

207     public void setLastModified(long lastModified)
208     {
209         this.lastModified = lastModified;
210     }
211
212     /**
213      * Return the template loader that pulled
214      * in the template stream
215      */

216     public ResourceLoader getResourceLoader()
217     {
218         return resourceLoader;
219     }
220     
221     /**
222      * Set the template loader for this template. Set
223      * when the Runtime determines where this template
224      * came from the list of possible sources.
225      */

226     public void setResourceLoader(ResourceLoader resourceLoader)
227     {
228         this.resourceLoader = resourceLoader;
229     }
230
231     /**
232      * Set arbitrary data object that might be used
233      * by the resource.
234      */

235     public void setData(Object JavaDoc data)
236     {
237         this.data = data;
238     }
239     
240     /**
241      * Get arbitrary data object that might be used
242      * by the resource.
243      */

244     public Object JavaDoc getData()
245     {
246         return data;
247     }
248 }
249
Popular Tags