1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.abstracthorizon.danube.velocity;
18
19 import java.io.BufferedInputStream;
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.FileNotFoundException;
23 import java.io.InputStream;
24 import java.util.Hashtable;
25 import java.util.Vector;
26
27 import org.apache.commons.collections.ExtendedProperties;
28 import org.apache.velocity.exception.ResourceNotFoundException;
29 import org.apache.velocity.runtime.resource.Resource;
30 import org.apache.velocity.runtime.resource.loader.ResourceLoader;
31 import org.apache.velocity.util.StringUtils;
32
33
34
35
36
37
38
39
40 public class AbsoluteFileResourceLoader extends ResourceLoader {
41
42 @SuppressWarnings("unchecked")
43 private Vector paths = null;
44
45
46
47
48
49 private Hashtable<String, String> templatePaths = new Hashtable<String, String>();
50
51
52
53
54
55 public void init(ExtendedProperties configuration) {
56 rsvc.info("FileResourceLoader : initialization starting.");
57
58 paths = configuration.getVector("path");
59
60
61
62
63
64 int sz = paths.size();
65
66 for (int i = 0; i < sz; i++) {
67 rsvc.info("FileResourceLoader : adding path '" + (String) paths.get(i) + "'");
68 }
69
70 rsvc.info("FileResourceLoader : initialization complete.");
71 }
72
73
74
75
76
77
78
79
80 public synchronized InputStream getResourceStream(String templateName) throws ResourceNotFoundException {
81
82
83
84 if (templateName == null || templateName.length() == 0) {
85
86
87
88
89
90 throw new ResourceNotFoundException("Need to specify a file name or file path!");
91 }
92
93 String template = StringUtils.normalizePath(templateName);
94 if (template == null || template.length() == 0) {
95 String msg = "File resource error : argument " + template + " contains .. and may be trying to access " + "content outside of template root. Rejected.";
96
97 rsvc.error("FileResourceLoader : " + msg);
98
99 throw new ResourceNotFoundException(msg);
100 }
101
102
103
104
105 if (template.startsWith("/")) {
106 template = template.substring(1);
107 }
108
109 int size = paths.size();
110 for (int i = 0; i < size; i++) {
111 String path = (String) paths.get(i);
112 InputStream inputStream = findTemplate(path, template);
113
114 if (inputStream != null) {
115
116
117
118
119
120 templatePaths.put(templateName, path);
121 return inputStream;
122 }
123 }
124
125
126
127
128
129 String msg = "FileResourceLoader Error: cannot find resource " + template;
130
131 throw new ResourceNotFoundException(msg);
132 }
133
134
135
136
137
138
139
140
141 private InputStream findTemplate(String path, String template) {
142 try {
143 File file = new File(path, template);
144 if ((path.length() == 0) && !file.exists()) {
145 file = new File(template);
146 }
147
148 if (file.canRead()) {
149 return new BufferedInputStream(new FileInputStream(file.getAbsolutePath()));
150 } else {
151 return null;
152 }
153 } catch (FileNotFoundException fnfe) {
154
155
156
157 return null;
158 }
159 }
160
161
162
163
164
165
166
167
168
169 public boolean isSourceModified(Resource resource) {
170
171
172
173
174 boolean modified = true;
175
176 String fileName = resource.getName();
177 String path = (String) templatePaths.get(fileName);
178 File currentFile = null;
179
180 for (int i = 0; currentFile == null && i < paths.size(); i++) {
181 String testPath = (String) paths.get(i);
182 File testFile = new File(testPath, fileName);
183 if (testFile.canRead()) {
184 currentFile = testFile;
185 }
186 }
187 File file = new File(path, fileName);
188 if (currentFile == null || !file.exists()) {
189
190
191
192
193
194
195
196 } else if (currentFile.equals(file) && file.canRead()) {
197
198
199
200
201
202 modified = (file.lastModified() != resource.getLastModified());
203 }
204
205
206
207
208 return modified;
209 }
210
211
212
213
214
215
216 public long getLastModified(Resource resource) {
217 String path = (String) templatePaths.get(resource.getName());
218 File file = new File(path, resource.getName());
219
220 if (file.canRead()) {
221 return file.lastModified();
222 } else {
223 return 0;
224 }
225 }
226 }