1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package edu.internet2.middleware.shibboleth.common.util;
19
20 import java.io.ByteArrayInputStream;
21 import java.io.InputStream;
22 import java.io.UnsupportedEncodingException;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import org.apache.commons.collections.ExtendedProperties;
28 import org.apache.commons.lang.StringUtils;
29 import org.apache.velocity.exception.ResourceNotFoundException;
30 import org.apache.velocity.exception.VelocityException;
31 import org.apache.velocity.runtime.resource.Resource;
32 import org.apache.velocity.runtime.resource.loader.ResourceLoader;
33 import org.apache.velocity.runtime.resource.util.StringResource;
34 import org.apache.velocity.runtime.resource.util.StringResourceRepository;
35 import org.apache.velocity.runtime.resource.util.StringResourceRepositoryImpl;
36 import org.apache.velocity.util.ClassUtils;
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124 public class StringResourceLoader extends ResourceLoader {
125
126 public static final String REPOSITORY_STATIC = "repository.static";
127
128
129 public static final boolean REPOSITORY_STATIC_DEFAULT = true;
130
131
132 public static final String REPOSITORY_CLASS = "repository.class";
133
134
135 public static final String REPOSITORY_CLASS_DEFAULT = StringResourceRepositoryImpl.class.getName();
136
137
138 public static final String REPOSITORY_NAME = "repository.name";
139
140
141
142
143
144 public static final String REPOSITORY_NAME_DEFAULT = StringResourceRepository.class.getName();
145
146
147 public static final String REPOSITORY_ENCODING = "repository.encoding";
148
149
150 public static final String REPOSITORY_ENCODING_DEFAULT = "UTF-8";
151
152 protected static final Map STATIC_REPOSITORIES = Collections.synchronizedMap(new HashMap());
153
154
155
156
157 public static StringResourceRepository getRepository() {
158 return getRepository(REPOSITORY_NAME_DEFAULT);
159 }
160
161
162
163
164 public static StringResourceRepository getRepository(String name) {
165 return (StringResourceRepository) STATIC_REPOSITORIES.get(name);
166 }
167
168
169
170
171 public static void setRepository(String name, StringResourceRepository repo) {
172 STATIC_REPOSITORIES.put(name, repo);
173 }
174
175
176
177
178 public static StringResourceRepository removeRepository(String name) {
179 return (StringResourceRepository) STATIC_REPOSITORIES.remove(name);
180 }
181
182
183
184
185 public static void clearRepositories() {
186 STATIC_REPOSITORIES.clear();
187 }
188
189
190 protected StringResourceRepository repository;
191
192
193
194
195 public void init(final ExtendedProperties configuration) {
196 log.trace("StringResourceLoader : initialization starting.");
197
198
199 String repoClass = configuration.getString(REPOSITORY_CLASS, REPOSITORY_CLASS_DEFAULT);
200 String repoName = configuration.getString(REPOSITORY_NAME, REPOSITORY_NAME_DEFAULT);
201 boolean isStatic = configuration.getBoolean(REPOSITORY_STATIC, REPOSITORY_STATIC_DEFAULT);
202 String encoding = configuration.getString(REPOSITORY_ENCODING);
203
204
205 if (isStatic) {
206 this.repository = getRepository(repoName);
207 if (repository != null && log.isDebugEnabled()) {
208 log.debug("Loaded repository '" + repoName + "' from static repo store");
209 }
210 } else {
211 this.repository = (StringResourceRepository) rsvc.getApplicationAttribute(repoName);
212 if (repository != null && log.isDebugEnabled()) {
213 log.debug("Loaded repository '" + repoName + "' from application attributes");
214 }
215 }
216
217 if (this.repository == null) {
218
219 this.repository = createRepository(repoClass, encoding);
220
221
222 if (isStatic) {
223 setRepository(repoName, this.repository);
224 } else {
225 rsvc.setApplicationAttribute(repoName, this.repository);
226 }
227 } else {
228
229
230 if (!this.repository.getClass().getName().equals(repoClass)) {
231 log.warn("Cannot change class of string repository '" + repoName + "' from "
232 + this.repository.getClass().getName() + " to " + repoClass);
233 }
234
235
236 if (encoding != null && !this.repository.getEncoding().equals(encoding)) {
237 if (log.isInfoEnabled()) {
238 log.info("Changing the default encoding of string repository '" + repoName + "' from "
239 + this.repository.getEncoding() + " to " + encoding);
240 }
241 this.repository.setEncoding(encoding);
242 }
243 }
244
245 log.trace("StringResourceLoader : initialization complete.");
246 }
247
248 public StringResourceRepository createRepository(final String className, final String encoding) {
249 if (log.isDebugEnabled()) {
250 log.debug("Creating string repository using class " + className + "...");
251 }
252
253 StringResourceRepository repo;
254 try {
255 repo = (StringResourceRepository) ClassUtils.getNewInstance(className);
256 } catch (ClassNotFoundException cnfe) {
257 throw new VelocityException("Could not find '" + className + "'", cnfe);
258 } catch (IllegalAccessException iae) {
259 throw new VelocityException("Could not access '" + className + "'", iae);
260 } catch (InstantiationException ie) {
261 throw new VelocityException("Could not instantiate '" + className + "'", ie);
262 }
263
264 if (encoding != null) {
265 repo.setEncoding(encoding);
266 } else {
267 repo.setEncoding(REPOSITORY_ENCODING_DEFAULT);
268 }
269
270 if (log.isDebugEnabled()) {
271 log.debug("Default repository encoding is " + repo.getEncoding());
272 }
273 return repo;
274 }
275
276
277
278
279
280
281
282
283 public InputStream getResourceStream(final String name) throws ResourceNotFoundException {
284 if (StringUtils.isEmpty(name)) {
285 throw new ResourceNotFoundException("No template name provided");
286 }
287
288 StringResource resource = this.repository.getStringResource(name);
289
290 if (resource == null) {
291 throw new ResourceNotFoundException("Could not locate resource '" + name + "'");
292 }
293
294 byte[] byteArray = null;
295
296 try {
297 byteArray = resource.getBody().getBytes(resource.getEncoding());
298 return new ByteArrayInputStream(byteArray);
299 } catch (UnsupportedEncodingException ue) {
300 throw new VelocityException("Could not convert String using encoding " + resource.getEncoding(), ue);
301 }
302 }
303
304
305
306
307 public boolean isSourceModified(final Resource resource) {
308 StringResource original = null;
309 boolean result = true;
310
311 original = this.repository.getStringResource(resource.getName());
312
313 if (original != null) {
314 result = original.getLastModified() != resource.getLastModified();
315 }
316
317 return result;
318 }
319
320
321
322
323 public long getLastModified(final Resource resource) {
324 StringResource original = null;
325
326 original = this.repository.getStringResource(resource.getName());
327
328 return (original != null) ? original.getLastModified() : 0;
329 }
330
331 }