1 package org.apache.turbine.services.velocity;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 import java.io.ByteArrayOutputStream;
25 import java.io.IOException;
26 import java.io.OutputStream;
27 import java.io.OutputStreamWriter;
28 import java.io.Writer;
29 import java.util.Iterator;
30 import java.util.List;
31
32 import org.apache.commons.collections.ExtendedProperties;
33 import org.apache.commons.configuration.Configuration;
34 import org.apache.commons.lang.StringUtils;
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37 import org.apache.turbine.Turbine;
38 import org.apache.turbine.pipeline.PipelineData;
39 import org.apache.turbine.services.InitializationException;
40 import org.apache.turbine.services.pull.PullService;
41 import org.apache.turbine.services.pull.TurbinePull;
42 import org.apache.turbine.services.template.BaseTemplateEngineService;
43 import org.apache.turbine.util.RunData;
44 import org.apache.turbine.util.TurbineException;
45 import org.apache.velocity.VelocityContext;
46 import org.apache.velocity.app.VelocityEngine;
47 import org.apache.velocity.app.event.EventCartridge;
48 import org.apache.velocity.app.event.MethodExceptionEventHandler;
49 import org.apache.velocity.context.Context;
50 import org.apache.velocity.runtime.RuntimeConstants;
51 import org.apache.velocity.runtime.log.CommonsLogLogChute;
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 public class TurbineVelocityService
82 extends BaseTemplateEngineService
83 implements VelocityService,
84 MethodExceptionEventHandler
85 {
86
87 private static final String RESOURCE_LOADER_PATH = ".resource.loader.path";
88
89
90 private static final String DEFAULT_CHAR_SET = "ISO-8859-1";
91
92
93 private static final String JAR_PREFIX = "jar:";
94
95
96 private static final String ABSOLUTE_PREFIX = "file://";
97
98
99 private static final Log log = LogFactory.getLog(TurbineVelocityService.class);
100
101
102 private String defaultInputEncoding;
103
104
105 private String defaultOutputEncoding;
106
107
108 private boolean pullModelActive = false;
109
110
111 private boolean catchErrors = true;
112
113
114 private VelocityEngine velocity = null;
115
116
117 private PullService pullService = null;
118
119
120
121
122
123
124
125
126
127
128 @Override
129 public void init()
130 throws InitializationException
131 {
132 try
133 {
134 initVelocity();
135
136
137
138
139 if (TurbinePull.isRegistered())
140 {
141 pullModelActive = true;
142
143 pullService = TurbinePull.getService();
144
145 log.debug("Activated Pull Tools");
146 }
147
148
149 registerConfiguration(VelocityService.VELOCITY_EXTENSION);
150
151 defaultInputEncoding = getConfiguration().getString("input.encoding", DEFAULT_CHAR_SET);
152 defaultOutputEncoding = getConfiguration().getString("output.encoding", defaultInputEncoding);
153
154 setInit(true);
155 }
156 catch (Exception e)
157 {
158 throw new InitializationException(
159 "Failed to initialize TurbineVelocityService", e);
160 }
161 }
162
163
164
165
166
167
168 @Override
169 public Context getContext()
170 {
171 Context globalContext =
172 pullModelActive ? pullService.getGlobalContext() : null;
173
174 Context ctx = new VelocityContext(globalContext);
175 return ctx;
176 }
177
178
179
180
181
182
183 @Override
184 public Context getNewContext()
185 {
186 Context ctx = new VelocityContext();
187
188
189
190 EventCartridge ec = new EventCartridge();
191 ec.addEventHandler(this);
192 ec.attachToContext(ctx);
193 return ctx;
194 }
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209 @Override
210 @SuppressWarnings("rawtypes")
211 public Object methodException(Class clazz, String method, Exception e)
212 throws Exception
213 {
214 log.error("Class " + clazz.getName() + "." + method + " threw Exception", e);
215
216 if (!catchErrors)
217 {
218 throw e;
219 }
220
221 return "[Turbine caught an Error here. Look into the turbine.log for further information]";
222 }
223
224
225
226
227
228
229
230
231
232 @Override
233 public Context getContext(PipelineData pipelineData)
234 {
235
236 RunData data = (RunData)pipelineData;
237
238
239 Context context = (Context)
240 data.getTemplateInfo().getTemplateContext(VelocityService.CONTEXT);
241
242 if (context == null)
243 {
244 context = getContext();
245 context.put(VelocityService.RUNDATA_KEY, data);
246
247 context.put(VelocityService.PIPELINEDATA_KEY, pipelineData);
248
249 if (pullModelActive)
250 {
251
252
253
254
255 pullService.populateContext(context, pipelineData);
256 }
257
258 data.getTemplateInfo().setTemplateContext(
259 VelocityService.CONTEXT, context);
260 }
261 return context;
262 }
263
264
265
266
267
268
269
270
271
272
273
274
275 @Override
276 public String handleRequest(Context context, String filename)
277 throws TurbineException
278 {
279 String results = null;
280 ByteArrayOutputStream bytes = null;
281 OutputStreamWriter writer = null;
282 String charset = getOutputCharSet(context);
283
284 try
285 {
286 bytes = new ByteArrayOutputStream();
287
288 writer = new OutputStreamWriter(bytes, charset);
289
290 executeRequest(context, filename, writer);
291 writer.flush();
292 results = bytes.toString(charset);
293 }
294 catch (Exception e)
295 {
296 renderingError(filename, e);
297 }
298 finally
299 {
300 try
301 {
302 if (bytes != null)
303 {
304 bytes.close();
305 }
306 }
307 catch (IOException ignored)
308 {
309
310 }
311 }
312 return results;
313 }
314
315
316
317
318
319
320
321
322
323
324
325
326
327 @Override
328 public void handleRequest(Context context, String filename,
329 OutputStream output)
330 throws TurbineException
331 {
332 String charset = getOutputCharSet(context);
333 OutputStreamWriter writer = null;
334
335 try
336 {
337 writer = new OutputStreamWriter(output, charset);
338 executeRequest(context, filename, writer);
339 }
340 catch (Exception e)
341 {
342 renderingError(filename, e);
343 }
344 finally
345 {
346 try
347 {
348 if (writer != null)
349 {
350 writer.flush();
351 }
352 }
353 catch (Exception ignored)
354 {
355
356 }
357 }
358 }
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373 @Override
374 public void handleRequest(Context context, String filename, Writer writer)
375 throws TurbineException
376 {
377 try
378 {
379 executeRequest(context, filename, writer);
380 }
381 catch (Exception e)
382 {
383 renderingError(filename, e);
384 }
385 finally
386 {
387 try
388 {
389 if (writer != null)
390 {
391 writer.flush();
392 }
393 }
394 catch (Exception ignored)
395 {
396
397 }
398 }
399 }
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414 private void executeRequest(Context context, String filename,
415 Writer writer)
416 throws Exception
417 {
418 String encoding = getTemplateEncoding(context);
419
420 if (encoding == null)
421 {
422 encoding = defaultOutputEncoding;
423 }
424
425 velocity.mergeTemplate(filename, encoding, context, writer);
426 }
427
428
429
430
431
432
433
434 private String getOutputCharSet(Context context)
435 {
436 String charset = null;
437
438 Object data = context.get(VelocityService.RUNDATA_KEY);
439 if ((data != null) && (data instanceof RunData))
440 {
441 charset = ((RunData) data).getCharSet();
442 }
443
444 return (StringUtils.isEmpty(charset)) ? defaultOutputEncoding : charset;
445 }
446
447
448
449
450
451
452
453 private String getTemplateEncoding(Context context)
454 {
455 String encoding = null;
456
457 Object data = context.get(VelocityService.RUNDATA_KEY);
458 if ((data != null) && (data instanceof RunData))
459 {
460 encoding = ((RunData) data).getTemplateEncoding();
461 }
462
463 return encoding != null ? encoding : defaultInputEncoding;
464 }
465
466
467
468
469
470
471
472
473
474
475 private static final void renderingError(String filename, Exception e)
476 throws TurbineException
477 {
478 String err = "Error rendering Velocity template: " + filename;
479 log.error(err, e);
480 throw new TurbineException(err, e);
481 }
482
483
484
485
486
487
488
489 private synchronized void initVelocity()
490 throws Exception
491 {
492
493 Configuration conf = getConfiguration();
494
495 catchErrors = conf.getBoolean(CATCH_ERRORS_KEY, CATCH_ERRORS_DEFAULT);
496
497 conf.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,
498 CommonsLogLogChute.class.getName());
499 conf.setProperty(CommonsLogLogChute.LOGCHUTE_COMMONS_LOG_NAME,
500 "velocity");
501
502 velocity = new VelocityEngine();
503 velocity.setExtendedProperties(createVelocityProperties(conf));
504 velocity.init();
505 }
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520 public ExtendedProperties createVelocityProperties(Configuration conf)
521 throws Exception
522 {
523
524
525
526 ExtendedProperties veloConfig = new ExtendedProperties();
527
528
529
530
531
532 for (Iterator<String> i = conf.getKeys(); i.hasNext();)
533 {
534 String key = i.next();
535 if (!key.endsWith(RESOURCE_LOADER_PATH))
536 {
537 Object value = conf.getProperty(key);
538 if (value instanceof List<?>) {
539 for (Iterator<?> itr = ((List<?>)value).iterator(); itr.hasNext();)
540 {
541 veloConfig.addProperty(key, itr.next());
542 }
543 }
544 else
545 {
546 veloConfig.addProperty(key, value);
547 }
548 continue;
549 }
550
551 List<Object> paths = conf.getList(key, null);
552 if (paths == null)
553 {
554
555
556 continue;
557 }
558
559
560
561
562
563
564
565
566 for (Object p : paths)
567 {
568 String path = (String)p;
569 log.debug("Translating " + path);
570
571 if (path.startsWith(JAR_PREFIX))
572 {
573
574 if (path.substring(4).startsWith(ABSOLUTE_PREFIX))
575 {
576
577 int jarSepIndex = path.indexOf("!/");
578
579
580 path = (jarSepIndex < 0)
581 ? Turbine.getRealPath(path.substring(11))
582
583 : (Turbine.getRealPath(path.substring(11, jarSepIndex)) + path.substring(jarSepIndex));
584
585 log.debug("Result (absolute jar path): " + path);
586 }
587 }
588 else if(path.startsWith(ABSOLUTE_PREFIX))
589 {
590
591 path = Turbine.getRealPath(path.substring(7));
592
593 log.debug("Result (absolute URL Path): " + path);
594 }
595
596 else if(path.indexOf("://") < 0)
597 {
598 path = Turbine.getRealPath(path);
599
600 log.debug("Result (normal fs reference): " + path);
601 }
602
603 log.debug("Adding " + key + " -> " + path);
604
605 veloConfig.addProperty(key, path);
606 }
607 }
608 return veloConfig;
609 }
610
611
612
613
614
615
616
617
618
619 @Override
620 public boolean templateExists(String template)
621 {
622 return velocity.resourceExists(template);
623 }
624
625
626
627
628
629
630
631 @Override
632 public void requestFinished(Context context)
633 {
634 if (pullModelActive)
635 {
636 pullService.releaseTools(context);
637 }
638 }
639 }