1 package org.apache.velocity.runtime.resource.loader;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 import org.apache.velocity.exception.ResourceNotFoundException;
29 import org.apache.velocity.exception.VelocityException;
30 import org.apache.commons.collections.ExtendedProperties;
31
32
33
34
35
36
37
38
39
40 public abstract class ResourceLoader
41 {
42
43
44
45
46 protected boolean isCachingOn = false;
47
48
49
50
51
52 protected long modificationCheckInterval = 2;
53
54
55
56
57
58 protected String className = null;
59
60 protected RuntimeServices rsvc = null;
61 protected Log log = null;
62
63
64
65
66
67
68
69
70 public void commonInit( RuntimeServices rs, ExtendedProperties configuration)
71 {
72 this.rsvc = rs;
73 this.log = rsvc.getLog();
74
75
76
77
78
79
80
81
82
83 try
84 {
85 isCachingOn = configuration.getBoolean("cache", false);
86 }
87 catch (Exception e)
88 {
89 isCachingOn = false;
90 String msg = "Exception parsing cache setting: "+configuration.getString("cache");
91 log.error(msg, e);
92 throw new VelocityException(msg, e);
93 }
94 try
95 {
96 modificationCheckInterval = configuration.getLong("modificationCheckInterval", 0);
97 }
98 catch (Exception e)
99 {
100 modificationCheckInterval = 0;
101 String msg = "Exception parsing modificationCheckInterval setting: "+configuration.getString("modificationCheckInterval");
102 log.error(msg, e);
103 throw new VelocityException(msg, e);
104 }
105
106
107
108
109 className = ResourceCacheImpl.class.getName();
110 try
111 {
112 className = configuration.getString("class", className);
113 }
114 catch (Exception e)
115 {
116 String msg = "Exception retrieving resource cache class name";
117 log.error(msg, e);
118 throw new VelocityException(msg, e);
119 }
120 }
121
122
123
124
125
126
127 public abstract void init( ExtendedProperties configuration);
128
129
130
131
132
133
134
135
136 public abstract InputStream getResourceStream( String source )
137 throws ResourceNotFoundException;
138
139
140
141
142
143
144
145 public abstract boolean isSourceModified(Resource resource);
146
147
148
149
150
151
152
153
154
155 public abstract long getLastModified(Resource resource);
156
157
158
159
160
161 public String getClassName()
162 {
163 return className;
164 }
165
166
167
168
169
170
171
172
173 public void setCachingOn(boolean value)
174 {
175 isCachingOn = value;
176 }
177
178
179
180
181
182
183
184
185 public boolean isCachingOn()
186 {
187 return isCachingOn;
188 }
189
190
191
192
193
194
195 public void setModificationCheckInterval(long modificationCheckInterval)
196 {
197 this.modificationCheckInterval = modificationCheckInterval;
198 }
199
200
201
202
203
204
205 public long getModificationCheckInterval()
206 {
207 return modificationCheckInterval;
208 }
209
210
211
212
213
214
215
216
217
218
219 public boolean resourceExists(final String resourceName)
220 {
221 InputStream is = null;
222 try
223 {
224 is = getResourceStream(resourceName);
225 }
226 catch (ResourceNotFoundException e)
227 {
228 if (log.isDebugEnabled())
229 {
230 log.debug("Could not load resource '" + resourceName
231 + "' from ResourceLoader " + this.getClass().getName()
232 + ": ", e);
233 }
234 }
235 finally
236 {
237 try
238 {
239 if (is != null)
240 {
241 is.close();
242 }
243 }
244 catch (Exception e)
245 {
246 if (log.isErrorEnabled())
247 {
248 String msg = "While closing InputStream for resource '" + resourceName
249 + "' from ResourceLoader "+this.getClass().getName();
250 log.error(msg, e);
251 throw new VelocityException(msg, e);
252 }
253 }
254 }
255 return (is != null);
256 }
257 }