1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts.plugins;
22
23 import org.apache.commons.digester.Digester;
24 import org.apache.commons.digester.RuleSet;
25 import org.apache.commons.digester.xmlrules.DigesterLoader;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.struts.action.ActionServlet;
29 import org.apache.struts.action.PlugIn;
30 import org.apache.struts.config.ModuleConfig;
31 import org.apache.struts.util.RequestUtils;
32 import org.xml.sax.SAXException;
33
34 import javax.servlet.ServletException;
35
36 import java.io.File;
37 import java.io.IOException;
38
39 import java.net.URL;
40 import java.net.URLConnection;
41
42
43
44
45
46
47
48
49
50
51 public class DigestingPlugIn implements PlugIn {
52
53
54
55 private static Log log = LogFactory.getLog(DigestingPlugIn.class);
56 protected static final String SOURCE_CLASSPATH = "classpath";
57 protected static final String SOURCE_FILE = "file";
58 protected static final String SOURCE_SERVLET = "servlet";
59 protected String configPath = null;
60 protected String configSource = SOURCE_SERVLET;
61 protected String digesterPath = null;
62 protected String digesterSource = SOURCE_SERVLET;
63 protected String key = null;
64 protected ModuleConfig moduleConfig = null;
65 protected String rulesets = null;
66 protected ActionServlet servlet = null;
67 protected boolean push = false;
68
69
70
71
72 public DigestingPlugIn() {
73 super();
74 }
75
76
77
78
79 public void destroy() {
80 this.servlet = null;
81 this.moduleConfig = null;
82 }
83
84
85
86
87
88
89
90
91
92
93
94
95
96 public void init(ActionServlet servlet, ModuleConfig config)
97 throws ServletException {
98 this.servlet = servlet;
99 this.moduleConfig = config;
100
101 Object obj = null;
102
103 Digester digester = this.initializeDigester();
104
105 if (this.push) {
106 log.debug("push == true; pushing plugin onto digester stack");
107 digester.push(this);
108 }
109
110 try {
111 log.debug("XML data file: [path: " + this.configPath + ", source: "
112 + this.configSource + "]");
113
114 URL configURL =
115 this.getConfigURL(this.configPath, this.configSource);
116
117 if (configURL == null) {
118 throw new ServletException(
119 "Unable to locate XML data file at [path: "
120 + this.configPath + ", source: " + this.configSource + "]");
121 }
122
123 URLConnection conn = configURL.openConnection();
124
125 conn.setUseCaches(false);
126 conn.connect();
127 obj = digester.parse(conn.getInputStream());
128 } catch (IOException e) {
129
130 log.error("Exception processing config", e);
131 throw new ServletException(e);
132 } catch (SAXException e) {
133
134 log.error("Exception processing config", e);
135 throw new ServletException(e);
136 }
137
138 this.storeGeneratedObject(obj);
139 }
140
141
142
143
144
145
146
147
148 protected Digester initializeDigester()
149 throws ServletException {
150 Digester digester = null;
151
152 if ((this.digesterPath != null) && (this.digesterSource != null)) {
153 try {
154 log.debug("Initialize digester from XML [path: "
155 + this.digesterPath + "; source: " + this.digesterSource
156 + "]");
157 digester =
158 this.digesterFromXml(this.digesterPath, this.digesterSource);
159 } catch (IOException e) {
160
161 log.error("Exception instantiating digester from XML ", e);
162 throw new ServletException(e);
163 }
164 } else {
165 log.debug("No XML rules for digester; call newDigesterInstance()");
166 digester = this.newDigesterInstance();
167 }
168
169 this.applyRuleSets(digester);
170
171 return digester;
172 }
173
174
175
176
177
178
179
180
181 protected Digester newDigesterInstance() {
182 return new Digester();
183 }
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198 protected Digester digesterFromXml(String path, String source)
199 throws IOException {
200 URL configURL = this.getConfigURL(path, source);
201
202 if (configURL == null) {
203 throw new NullPointerException("No resource '" + path
204 + "' found in '" + source + "'");
205 }
206
207 return DigesterLoader.createDigester(configURL);
208 }
209
210
211
212
213
214
215
216
217
218 protected void applyRuleSets(Digester digester)
219 throws ServletException {
220 if ((this.rulesets == null) || (this.rulesets.trim().length() == 0)) {
221 return;
222 }
223
224 rulesets = rulesets.trim();
225
226 String ruleSet = null;
227
228 while (rulesets.length() > 0) {
229 int comma = rulesets.indexOf(",");
230
231 if (comma < 0) {
232 ruleSet = rulesets.trim();
233 rulesets = "";
234 } else {
235 ruleSet = rulesets.substring(0, comma).trim();
236 rulesets = rulesets.substring(comma + 1).trim();
237 }
238
239 if (log.isDebugEnabled()) {
240
241 log.debug("Configuring custom Digester Ruleset of type "
242 + ruleSet);
243 }
244
245 try {
246 RuleSet instance =
247 (RuleSet) RequestUtils.applicationInstance(ruleSet);
248
249 digester.addRuleSet(instance);
250 } catch (Exception e) {
251
252 log.error("Exception configuring custom Digester RuleSet", e);
253 throw new ServletException(e);
254 }
255 }
256 }
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282 protected URL getConfigURL(String path, String source)
283 throws IOException {
284 if (SOURCE_CLASSPATH.equals(source)) {
285 return this.getClassPathURL(path);
286 }
287
288 if (SOURCE_FILE.equals(source)) {
289 return this.getFileURL(path);
290 }
291
292 if (SOURCE_SERVLET.equals(source)) {
293 return this.getServletContextURL(path);
294 }
295
296
297 throw new IllegalArgumentException("ConfigSource " + source
298 + " is not recognized");
299 }
300
301
302
303
304
305
306
307
308 protected URL getClassPathURL(String path) {
309 return getClass().getClassLoader().getResource(path);
310 }
311
312
313
314
315
316
317
318
319
320 protected URL getServletContextURL(String path)
321 throws IOException {
322 return this.servlet.getServletContext().getResource(path);
323 }
324
325
326
327
328
329
330
331
332
333 protected URL getFileURL(String path)
334 throws IOException {
335 File file = new File(path);
336
337 return file.toURL();
338 }
339
340
341
342
343
344
345 public void setConfigPath(String configPath) {
346 this.configPath = configPath;
347 }
348
349
350
351
352
353 public String getConfigPath() {
354 return configPath;
355 }
356
357
358
359
360
361
362
363
364
365
366
367 public void setConfigSource(String configSource) {
368 this.configSource = configSource;
369 }
370
371
372
373
374
375
376 public String getConfigSource() {
377 return configSource;
378 }
379
380
381
382
383
384
385
386
387
388 protected void storeGeneratedObject(Object obj) {
389 log.debug("Put [" + obj + "] into application context [key:" + this.key
390 + "]");
391 this.servlet.getServletContext().setAttribute(this.getKey(), obj);
392 }
393
394
395
396
397
398 public void setKey(String key) {
399 this.key = key;
400 }
401
402
403
404
405
406 public String getKey() {
407 return key;
408 }
409
410
411
412
413
414 public void setRulesets(String ruleSets) {
415 this.rulesets = ruleSets;
416 }
417
418
419
420
421 public String getRulesets() {
422 return this.rulesets;
423 }
424
425
426
427
428
429
430
431
432 public void setDigesterPath(String digesterPath) {
433 this.digesterPath = digesterPath;
434 }
435
436
437
438
439
440
441 public String getDigesterPath() {
442 return digesterPath;
443 }
444
445
446
447
448
449
450
451
452 public void setDigesterSource(String digesterSource) {
453 this.digesterSource = digesterSource;
454 }
455
456
457
458
459
460
461 public String getDigesterSource() {
462 return this.digesterSource;
463 }
464
465
466
467
468
469
470
471
472 public void setPush(boolean push) {
473 this.push = push;
474 }
475
476
477
478
479
480
481 public boolean getPush() {
482 return this.push;
483 }
484 }