1 package org.apache.velocity.runtime;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.Reader;
26 import java.io.StringReader;
27 import java.io.Writer;
28 import java.util.Enumeration;
29 import java.util.HashMap;
30 import java.util.Hashtable;
31 import java.util.Map;
32 import java.util.Properties;
33
34 import org.apache.commons.collections.ExtendedProperties;
35 import org.apache.commons.lang.text.StrBuilder;
36 import org.apache.velocity.Template;
37 import org.apache.velocity.app.event.EventCartridge;
38 import org.apache.velocity.app.event.EventHandler;
39 import org.apache.velocity.app.event.IncludeEventHandler;
40 import org.apache.velocity.app.event.InvalidReferenceEventHandler;
41 import org.apache.velocity.app.event.MethodExceptionEventHandler;
42 import org.apache.velocity.app.event.NullSetEventHandler;
43 import org.apache.velocity.app.event.ReferenceInsertionEventHandler;
44 import org.apache.velocity.context.Context;
45 import org.apache.velocity.context.InternalContextAdapterImpl;
46 import org.apache.velocity.exception.MethodInvocationException;
47 import org.apache.velocity.exception.ParseErrorException;
48 import org.apache.velocity.exception.ResourceNotFoundException;
49 import org.apache.velocity.exception.TemplateInitException;
50 import org.apache.velocity.exception.VelocityException;
51 import org.apache.velocity.runtime.directive.Directive;
52 import org.apache.velocity.runtime.log.Log;
53 import org.apache.velocity.runtime.log.LogManager;
54 import org.apache.velocity.runtime.parser.ParseException;
55 import org.apache.velocity.runtime.parser.Parser;
56 import org.apache.velocity.runtime.parser.node.Node;
57 import org.apache.velocity.runtime.parser.node.SimpleNode;
58 import org.apache.velocity.runtime.resource.ContentResource;
59 import org.apache.velocity.runtime.resource.ResourceManager;
60 import org.apache.velocity.util.ClassUtils;
61 import org.apache.velocity.util.RuntimeServicesAware;
62 import org.apache.velocity.util.StringUtils;
63 import org.apache.velocity.util.introspection.Introspector;
64 import org.apache.velocity.util.introspection.Uberspect;
65 import org.apache.velocity.util.introspection.UberspectLoggable;
66 import org.apache.velocity.util.introspection.ChainableUberspector;
67 import org.apache.velocity.util.introspection.LinkingUberspector;
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 public class RuntimeInstance implements RuntimeConstants, RuntimeServices
117 {
118
119
120
121 private VelocimacroFactory vmFactory = null;
122
123
124
125
126
127
128
129 private Log log = new Log();
130
131
132
133
134 private ParserPool parserPool;
135
136
137
138
139 private boolean initializing = false;
140
141
142
143
144 private boolean initialized = false;
145
146
147
148
149
150 private ExtendedProperties overridingProperties = null;
151
152
153
154
155
156
157
158
159 private Hashtable runtimeDirectives;
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175 private ExtendedProperties configuration = new ExtendedProperties();
176
177 private ResourceManager resourceManager = null;
178
179
180
181
182
183 private EventCartridge eventCartridge = null;
184
185
186
187
188
189 private Introspector introspector = null;
190
191
192
193
194
195
196
197 private Map applicationAttributes = null;
198 private Uberspect uberSpect;
199 private String encoding;
200
201
202
203
204 public RuntimeInstance()
205 {
206
207
208
209 vmFactory = new VelocimacroFactory( this );
210
211
212
213
214 introspector = new Introspector(getLog());
215
216
217
218
219 applicationAttributes = new HashMap();
220 }
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238 public synchronized void init()
239 throws Exception
240 {
241 if (!initialized && !initializing)
242 {
243 initializing = true;
244
245 log.trace("*******************************************************************");
246 log.debug("Starting Apache Velocity v@build.version@ (compiled: @build.time@)");
247 log.trace("RuntimeInstance initializing.");
248
249 initializeProperties();
250 initializeLog();
251 initializeResourceManager();
252 initializeDirectives();
253 initializeEventHandlers();
254 initializeParserPool();
255
256 initializeIntrospection();
257
258
259
260
261 vmFactory.initVelocimacro();
262
263 log.trace("RuntimeInstance successfully initialized.");
264
265 initialized = true;
266 initializing = false;
267 }
268 }
269
270
271
272
273
274
275 public boolean isInitialized()
276 {
277 return initialized;
278 }
279
280
281
282
283 private void requireInitialization()
284 {
285 if (!initialized && !initializing)
286 {
287 log.debug("Velocity was not initialized! Calling init()...");
288 try
289 {
290 init();
291 }
292 catch (Exception e)
293 {
294 getLog().error("Could not auto-initialize Velocity", e);
295 throw new RuntimeException("Velocity could not be initialized!", e);
296 }
297 }
298 }
299
300
301
302
303
304 private void initializeIntrospection()
305 throws Exception
306 {
307 String[] uberspectors = configuration.getStringArray(RuntimeConstants.UBERSPECT_CLASSNAME);
308 for (int i=0; i <uberspectors.length;i++)
309 {
310 String rm = uberspectors[i];
311 Object o = null;
312
313 try
314 {
315 o = ClassUtils.getNewInstance( rm );
316 }
317 catch (ClassNotFoundException cnfe)
318 {
319 String err = "The specified class for Uberspect (" + rm
320 + ") does not exist or is not accessible to the current classloader.";
321 log.error(err);
322 throw new Exception(err);
323 }
324
325 if (!(o instanceof Uberspect))
326 {
327 String err = "The specified class for Uberspect ("
328 + rm + ") does not implement " + Uberspect.class.getName()
329 + "; Velocity is not initialized correctly.";
330
331 log.error(err);
332 throw new Exception(err);
333 }
334
335 Uberspect u = (Uberspect)o;
336
337 if (u instanceof UberspectLoggable)
338 {
339 ((UberspectLoggable)u).setLog(getLog());
340 }
341
342 if (u instanceof RuntimeServicesAware)
343 {
344 ((RuntimeServicesAware)u).setRuntimeServices(this);
345 }
346
347 if (uberSpect == null)
348 {
349 uberSpect = u;
350 }
351 else
352 {
353 if (u instanceof ChainableUberspector)
354 {
355 ((ChainableUberspector)u).wrap(uberSpect);
356 uberSpect = u;
357 }
358 else
359 {
360 uberSpect = new LinkingUberspector(uberSpect,u);
361 }
362 }
363 }
364
365 if(uberSpect != null)
366 {
367 uberSpect.init();
368 }
369 else
370 {
371
372
373
374
375 String err = "It appears that no class was specified as the"
376 + " Uberspect. Please ensure that all configuration"
377 + " information is correct.";
378
379 log.error(err);
380 throw new Exception(err);
381 }
382 }
383
384
385
386
387
388
389 private void setDefaultProperties()
390 {
391 InputStream inputStream = null;
392 try
393 {
394 inputStream = getClass()
395 .getResourceAsStream('/' + DEFAULT_RUNTIME_PROPERTIES);
396
397 configuration.load( inputStream );
398
399 if (log.isDebugEnabled())
400 {
401 log.debug("Default Properties File: " +
402 new File(DEFAULT_RUNTIME_PROPERTIES).getPath());
403 }
404
405
406 }
407 catch (IOException ioe)
408 {
409 String msg = "Cannot get Velocity Runtime default properties!";
410 log.error(msg, ioe);
411 throw new RuntimeException(msg, ioe);
412 }
413 finally
414 {
415 try
416 {
417 if (inputStream != null)
418 {
419 inputStream.close();
420 }
421 }
422 catch (IOException ioe)
423 {
424 String msg = "Cannot close Velocity Runtime default properties!";
425 log.error(msg, ioe);
426 throw new RuntimeException(msg, ioe);
427 }
428 }
429 }
430
431
432
433
434
435
436
437
438 public void setProperty(String key, Object value)
439 {
440 if (overridingProperties == null)
441 {
442 overridingProperties = new ExtendedProperties();
443 }
444
445 overridingProperties.setProperty(key, value);
446 }
447
448
449
450
451
452
453
454
455
456
457
458 public void setConfiguration( ExtendedProperties configuration)
459 {
460 if (overridingProperties == null)
461 {
462 overridingProperties = configuration;
463 }
464 else
465 {
466
467 if (overridingProperties != configuration)
468 {
469 overridingProperties.combine(configuration);
470 }
471 }
472 }
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493 public void addProperty(String key, Object value)
494 {
495 if (overridingProperties == null)
496 {
497 overridingProperties = new ExtendedProperties();
498 }
499
500 overridingProperties.addProperty(key, value);
501 }
502
503
504
505
506
507
508
509 public void clearProperty(String key)
510 {
511 if (overridingProperties != null)
512 {
513 overridingProperties.clearProperty(key);
514 }
515 }
516
517
518
519
520
521
522
523
524
525 public Object getProperty(String key)
526 {
527 Object o = null;
528
529
530
531
532 if (!initialized && !initializing && overridingProperties != null)
533 {
534 o = overridingProperties.get(key);
535 }
536
537
538
539
540 if (o == null)
541 {
542 o = configuration.getProperty(key);
543 }
544 if (o instanceof String)
545 {
546 return StringUtils.nullTrim((String) o);
547 }
548 else
549 {
550 return o;
551 }
552 }
553
554
555
556
557
558
559
560
561
562 private void initializeProperties()
563 {
564
565
566
567
568 if (configuration.isInitialized() == false)
569 {
570 setDefaultProperties();
571 }
572
573 if( overridingProperties != null)
574 {
575 configuration.combine(overridingProperties);
576 }
577 }
578
579
580
581
582
583
584
585
586 public void init(Properties p) throws Exception
587 {
588 setProperties(ExtendedProperties.convertProperties(p));
589 init();
590 }
591
592 private void setProperties(ExtendedProperties p)
593 {
594 if (overridingProperties == null)
595 {
596 overridingProperties = p;
597 }
598 else
599 {
600 overridingProperties.combine(p);
601 }
602 }
603
604
605
606
607
608
609
610
611 public void init(String configurationFile)
612 throws Exception
613 {
614 setProperties(new ExtendedProperties(configurationFile));
615 init();
616 }
617
618 private void initializeResourceManager()
619 throws Exception
620 {
621
622
623
624
625 String rm = getString(RuntimeConstants.RESOURCE_MANAGER_CLASS);
626
627 if (rm != null && rm.length() > 0)
628 {
629
630
631
632
633
634
635 Object o = null;
636
637 try
638 {
639 o = ClassUtils.getNewInstance( rm );
640 }
641 catch (ClassNotFoundException cnfe )
642 {
643 String err = "The specified class for ResourceManager (" + rm
644 + ") does not exist or is not accessible to the current classloader.";
645 log.error(err);
646 throw new Exception(err);
647 }
648
649 if (!(o instanceof ResourceManager))
650 {
651 String err = "The specified class for ResourceManager (" + rm
652 + ") does not implement " + ResourceManager.class.getName()
653 + "; Velocity is not initialized correctly.";
654
655 log.error(err);
656 throw new Exception(err);
657 }
658
659 resourceManager = (ResourceManager) o;
660
661 resourceManager.initialize(this);
662 }
663 else
664 {
665
666
667
668
669 String err = "It appears that no class was specified as the"
670 + " ResourceManager. Please ensure that all configuration"
671 + " information is correct.";
672
673 log.error(err);
674 throw new Exception( err );
675 }
676 }
677
678 private void initializeEventHandlers()
679 throws Exception
680 {
681
682 eventCartridge = new EventCartridge();
683
684
685
686
687
688 String[] referenceinsertion = configuration.getStringArray(RuntimeConstants.EVENTHANDLER_REFERENCEINSERTION);
689 if ( referenceinsertion != null )
690 {
691 for ( int i=0; i < referenceinsertion.length; i++ )
692 {
693 EventHandler ev = initializeSpecificEventHandler(referenceinsertion[i],RuntimeConstants.EVENTHANDLER_REFERENCEINSERTION,ReferenceInsertionEventHandler.class);
694 if (ev != null)
695 eventCartridge.addReferenceInsertionEventHandler((ReferenceInsertionEventHandler) ev);
696 }
697 }
698
699 String[] nullset = configuration.getStringArray(RuntimeConstants.EVENTHANDLER_NULLSET);
700 if ( nullset != null )
701 {
702 for ( int i=0; i < nullset.length; i++ )
703 {
704 EventHandler ev = initializeSpecificEventHandler(nullset[i],RuntimeConstants.EVENTHANDLER_NULLSET,NullSetEventHandler.class);
705 if (ev != null)
706 eventCartridge.addNullSetEventHandler((NullSetEventHandler) ev);
707 }
708 }
709
710 String[] methodexception = configuration.getStringArray(RuntimeConstants.EVENTHANDLER_METHODEXCEPTION);
711 if ( methodexception != null )
712 {
713 for ( int i=0; i < methodexception.length; i++ )
714 {
715 EventHandler ev = initializeSpecificEventHandler(methodexception[i],RuntimeConstants.EVENTHANDLER_METHODEXCEPTION,MethodExceptionEventHandler.class);
716 if (ev != null)
717 eventCartridge.addMethodExceptionHandler((MethodExceptionEventHandler) ev);
718 }
719 }
720
721 String[] includeHandler = configuration.getStringArray(RuntimeConstants.EVENTHANDLER_INCLUDE);
722 if ( includeHandler != null )
723 {
724 for ( int i=0; i < includeHandler.length; i++ )
725 {
726 EventHandler ev = initializeSpecificEventHandler(includeHandler[i],RuntimeConstants.EVENTHANDLER_INCLUDE,IncludeEventHandler.class);
727 if (ev != null)
728 eventCartridge.addIncludeEventHandler((IncludeEventHandler) ev);
729 }
730 }
731
732 String[] invalidReferenceSet = configuration.getStringArray(RuntimeConstants.EVENTHANDLER_INVALIDREFERENCES);
733 if ( invalidReferenceSet != null )
734 {
735 for ( int i=0; i < invalidReferenceSet.length; i++ )
736 {
737 EventHandler ev = initializeSpecificEventHandler(invalidReferenceSet[i],RuntimeConstants.EVENTHANDLER_INVALIDREFERENCES,InvalidReferenceEventHandler.class);
738 if (ev != null)
739 {
740 eventCartridge.addInvalidReferenceEventHandler((InvalidReferenceEventHandler) ev);
741 }
742 }
743 }
744
745
746 }
747
748 private EventHandler initializeSpecificEventHandler(String classname, String paramName, Class EventHandlerInterface)
749 throws Exception
750 {
751 if ( classname != null && classname.length() > 0)
752 {
753 Object o = null;
754 try {
755 o = ClassUtils.getNewInstance(classname);
756 }
757 catch (ClassNotFoundException cnfe )
758 {
759 String err = "The specified class for "
760 + paramName + " (" + classname
761 + ") does not exist or is not accessible to the current classloader.";
762 log.error(err);
763 throw new Exception(err);
764 }
765
766 if (!EventHandlerInterface.isAssignableFrom(EventHandlerInterface))
767 {
768 String err = "The specified class for " + paramName + " ("
769 + classname + ") does not implement "
770 + EventHandlerInterface.getName()
771 + "; Velocity is not initialized correctly.";
772
773 log.error(err);
774 throw new Exception(err);
775 }
776
777 EventHandler ev = (EventHandler) o;
778 if ( ev instanceof RuntimeServicesAware )
779 ((RuntimeServicesAware) ev).setRuntimeServices(this);
780 return ev;
781
782 } else
783 return null;
784 }
785
786
787
788
789
790
791 private void initializeLog() throws Exception
792 {
793
794
795 LogManager.updateLog(this.log, this);
796 }
797
798
799
800
801
802
803
804
805
806
807
808 private void initializeDirectives()
809 throws Exception
810 {
811
812
813
814
815 runtimeDirectives = new Hashtable();
816
817 Properties directiveProperties = new Properties();
818
819
820
821
822
823
824 InputStream inputStream = null;
825
826 try
827 {
828 inputStream = getClass().getResourceAsStream('/' + DEFAULT_RUNTIME_DIRECTIVES);
829
830 if (inputStream == null)
831 {
832 throw new Exception("Error loading directive.properties! " +
833 "Something is very wrong if these properties " +
834 "aren't being located. Either your Velocity " +
835 "distribution is incomplete or your Velocity " +
836 "jar file is corrupted!");
837 }
838
839 directiveProperties.load(inputStream);
840
841 }
842 catch (IOException ioe)
843 {
844 String msg = "Error while loading directive properties!";
845 log.error(msg, ioe);
846 throw new RuntimeException(msg, ioe);
847 }
848 finally
849 {
850 try
851 {
852 if (inputStream != null)
853 {
854 inputStream.close();
855 }
856 }
857 catch (IOException ioe)
858 {
859 String msg = "Cannot close directive properties!";
860 log.error(msg, ioe);
861 throw new RuntimeException(msg, ioe);
862 }
863 }
864
865
866
867
868
869
870
871
872 Enumeration directiveClasses = directiveProperties.elements();
873
874 while (directiveClasses.hasMoreElements())
875 {
876 String directiveClass = (String) directiveClasses.nextElement();
877 loadDirective(directiveClass);
878 log.debug("Loaded System Directive: " + directiveClass);
879 }
880
881
882
883
884
885 String[] userdirective = configuration.getStringArray("userdirective");
886
887 for( int i = 0; i < userdirective.length; i++)
888 {
889 loadDirective(userdirective[i]);
890 if (log.isDebugEnabled())
891 {
892 log.debug("Loaded User Directive: " + userdirective[i]);
893 }
894 }
895
896 }
897
898
899
900
901
902 public void addDirective(Directive directive)
903 {
904 runtimeDirectives.put(directive.getName(), directive);
905 }
906
907
908
909
910
911
912 public Directive getDirective(String name)
913 {
914 return (Directive) runtimeDirectives.get(name);
915 }
916
917
918
919
920
921 public void removeDirective(String name)
922 {
923 runtimeDirectives.remove(name);
924 }
925
926
927
928
929
930
931 private void loadDirective(String directiveClass)
932 {
933 try
934 {
935 Object o = ClassUtils.getNewInstance( directiveClass );
936
937 if (o instanceof Directive)
938 {
939 Directive directive = (Directive) o;
940 addDirective(directive);
941 }
942 else
943 {
944 String msg = directiveClass + " does not implement "
945 + Directive.class.getName() + "; it cannot be loaded.";
946 log.error(msg);
947 throw new VelocityException(msg);
948 }
949 }
950
951
952
953 catch (Exception e)
954 {
955 String msg = "Failed to load Directive: " + directiveClass;
956 log.error(msg, e);
957 throw new VelocityException(msg, e);
958 }
959 }
960
961
962
963
964
965 private void initializeParserPool() throws Exception
966 {
967
968
969
970 String pp = getString(RuntimeConstants.PARSER_POOL_CLASS);
971
972 if (pp != null && pp.length() > 0)
973 {
974
975
976
977
978
979
980 Object o = null;
981
982 try
983 {
984 o = ClassUtils.getNewInstance( pp );
985 }
986 catch (ClassNotFoundException cnfe )
987 {
988 String err = "The specified class for ParserPool ("
989 + pp
990 + ") does not exist (or is not accessible to the current classloader.";
991 log.error(err);
992 throw new Exception(err);
993 }
994
995 if (!(o instanceof ParserPool))
996 {
997 String err = "The specified class for ParserPool ("
998 + pp + ") does not implement " + ParserPool.class
999 + " Velocity not initialized correctly.";
1000
1001 log.error(err);
1002 throw new Exception(err);
1003 }
1004
1005 parserPool = (ParserPool) o;
1006
1007 parserPool.initialize(this);
1008 }
1009 else
1010 {
1011
1012
1013
1014
1015 String err = "It appears that no class was specified as the"
1016 + " ParserPool. Please ensure that all configuration"
1017 + " information is correct.";
1018
1019 log.error(err);
1020 throw new Exception( err );
1021 }
1022
1023 }
1024
1025
1026
1027
1028
1029
1030 public Parser createNewParser()
1031 {
1032 requireInitialization();
1033
1034 Parser parser = new Parser(this);
1035 parser.setDirectives(runtimeDirectives);
1036 return parser;
1037 }
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057 public SimpleNode parse(String string, String templateName)
1058 throws ParseException
1059 {
1060 return parse(new StringReader(string), templateName);
1061 }
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080 public SimpleNode parse(Reader reader, String templateName)
1081 throws ParseException
1082 {
1083
1084
1085
1086 return parse(reader, templateName, true);
1087 }
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098 public SimpleNode parse(Reader reader, String templateName, boolean dumpNamespace)
1099 throws ParseException
1100 {
1101 requireInitialization();
1102
1103 Parser parser = (Parser) parserPool.get();
1104 boolean keepParser = true;
1105 if (parser == null)
1106 {
1107
1108
1109
1110 if (log.isInfoEnabled())
1111 {
1112 log.info("Runtime : ran out of parsers. Creating a new one. "
1113 + " Please increment the parser.pool.size property."
1114 + " The current value is too small.");
1115 }
1116 parser = createNewParser();
1117 keepParser = false;
1118 }
1119
1120 try
1121 {
1122
1123
1124
1125
1126
1127 if (dumpNamespace)
1128 {
1129 dumpVMNamespace(templateName);
1130 }
1131 return parser.parse(reader, templateName);
1132 }
1133 finally
1134 {
1135 if (keepParser)
1136 {
1137 parserPool.put(parser);
1138 }
1139
1140 }
1141 }
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162 public boolean evaluate(Context context, Writer out,
1163 String logTag, String instring) throws IOException
1164 {
1165 return evaluate(context, out, logTag, new StringReader(instring));
1166 }
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188 public boolean evaluate(Context context, Writer writer,
1189 String logTag, Reader reader) throws IOException
1190 {
1191 if (logTag == null)
1192 {
1193 throw new NullPointerException("logTag (i.e. template name) cannot be null, you must provide an identifier for the content being evaluated");
1194 }
1195
1196 SimpleNode nodeTree = null;
1197 try
1198 {
1199 nodeTree = parse(reader, logTag);
1200 }
1201 catch (ParseException pex)
1202 {
1203 throw new ParseErrorException(pex);
1204 }
1205 catch (TemplateInitException pex)
1206 {
1207 throw new ParseErrorException(pex);
1208 }
1209
1210 if (nodeTree == null)
1211 {
1212 return false;
1213 }
1214 else
1215 {
1216 return render(context, writer, logTag, nodeTree);
1217 }
1218 }
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239 public boolean render(Context context, Writer writer,
1240 String logTag, SimpleNode nodeTree) throws IOException
1241 {
1242
1243
1244
1245 InternalContextAdapterImpl ica =
1246 new InternalContextAdapterImpl(context);
1247
1248 ica.pushCurrentTemplateName(logTag);
1249
1250 try
1251 {
1252 try
1253 {
1254 nodeTree.init(ica, this);
1255 }
1256 catch (TemplateInitException pex)
1257 {
1258 throw new ParseErrorException(pex);
1259 }
1260
1261
1262
1263 catch(RuntimeException e)
1264 {
1265 throw e;
1266 }
1267 catch(Exception e)
1268 {
1269 String msg = "RuntimeInstance.render(): init exception for tag = "+logTag;
1270 getLog().error(msg, e);
1271 throw new VelocityException(msg, e);
1272 }
1273
1274
1275
1276
1277 nodeTree.render(ica, writer);
1278 }
1279 finally
1280 {
1281 ica.popCurrentTemplateName();
1282 }
1283
1284 return true;
1285 }
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304 public boolean invokeVelocimacro(final String vmName, String logTag,
1305 String[] params, final Context context,
1306 final Writer writer)
1307 throws IOException
1308 {
1309
1310 if (vmName == null || context == null || writer == null)
1311 {
1312 String msg = "RuntimeInstance.invokeVelocimacro() : invalid call : vmName, context, and writer must not be null";
1313 getLog().error(msg);
1314 throw new NullPointerException(msg);
1315 }
1316
1317
1318 if (logTag == null)
1319 {
1320 logTag = vmName;
1321 }
1322 if (params == null)
1323 {
1324 params = new String[0];
1325 }
1326
1327
1328 if (!isVelocimacro(vmName, logTag))
1329 {
1330 String msg = "RuntimeInstance.invokeVelocimacro() : VM '" + vmName
1331 + "' is not registered.";
1332 getLog().error(msg);
1333 throw new VelocityException(msg);
1334 }
1335
1336
1337 StrBuilder template = new StrBuilder("#");
1338 template.append(vmName);
1339 template.append("(");
1340 for( int i = 0; i < params.length; i++)
1341 {
1342 template.append(" $");
1343 template.append(params[i]);
1344 }
1345 template.append(" )");
1346
1347 return evaluate(context, writer, logTag, template.toString());
1348 }
1349
1350
1351
1352
1353
1354 private String getDefaultEncoding()
1355 {
1356 if (encoding == null)
1357 {
1358 encoding = getString(INPUT_ENCODING, ENCODING_DEFAULT);
1359 }
1360 return encoding;
1361 }
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377 public Template getTemplate(String name)
1378 throws ResourceNotFoundException, ParseErrorException, Exception
1379 {
1380 return getTemplate(name, getDefaultEncoding());
1381 }
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395 public Template getTemplate(String name, String encoding)
1396 throws ResourceNotFoundException, ParseErrorException, Exception
1397 {
1398 requireInitialization();
1399
1400 return (Template)
1401 resourceManager.getResource(name,
1402 ResourceManager.RESOURCE_TEMPLATE, encoding);
1403 }
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417 public ContentResource getContent(String name)
1418 throws ResourceNotFoundException, ParseErrorException, Exception
1419 {
1420
1421
1422
1423
1424
1425 return getContent(name, getDefaultEncoding());
1426 }
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440 public ContentResource getContent(String name, String encoding)
1441 throws ResourceNotFoundException, ParseErrorException, Exception
1442 {
1443 requireInitialization();
1444
1445 return (ContentResource)
1446 resourceManager.getResource(name,
1447 ResourceManager.RESOURCE_CONTENT, encoding);
1448 }
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460 public String getLoaderNameForResource(String resourceName)
1461 {
1462 requireInitialization();
1463
1464 return resourceManager.getLoaderNameForResource(resourceName);
1465 }
1466
1467
1468
1469
1470
1471
1472
1473
1474 public Log getLog()
1475 {
1476 return log;
1477 }
1478
1479
1480
1481
1482
1483
1484 public void warn(Object message)
1485 {
1486 getLog().warn(message);
1487 }
1488
1489
1490
1491
1492
1493
1494 public void info(Object message)
1495 {
1496 getLog().info(message);
1497 }
1498
1499
1500
1501
1502
1503
1504 public void error(Object message)
1505 {
1506 getLog().error(message);
1507 }
1508
1509
1510
1511
1512
1513
1514 public void debug(Object message)
1515 {
1516 getLog().debug(message);
1517 }
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528 public String getString( String key, String defaultValue)
1529 {
1530 return configuration.getString(key, defaultValue);
1531 }
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542 public Directive getVelocimacro(String vmName, String templateName)
1543 {
1544 return vmFactory.getVelocimacro( vmName, templateName );
1545 }
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561 public Directive getVelocimacro(String vmName, String templateName, String renderingTemplate)
1562 {
1563 return vmFactory.getVelocimacro( vmName, templateName, renderingTemplate );
1564 }
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581 public boolean addVelocimacro( String name,
1582 String macro,
1583 String argArray[],
1584 String sourceTemplate )
1585 {
1586 return vmFactory.addVelocimacro(name, macro, argArray, sourceTemplate);
1587 }
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605 public boolean addVelocimacro( String name,
1606 Node macro,
1607 String argArray[],
1608 String sourceTemplate )
1609 {
1610 return vmFactory.addVelocimacro(name, macro, argArray, sourceTemplate);
1611 }
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621 public boolean isVelocimacro( String vmName, String templateName )
1622 {
1623 return vmFactory.isVelocimacro(vmName, templateName);
1624 }
1625
1626
1627
1628
1629
1630
1631
1632 public boolean dumpVMNamespace(String namespace)
1633 {
1634 return vmFactory.dumpVMNamespace( namespace );
1635 }
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654 public String getString(String key)
1655 {
1656 return StringUtils.nullTrim(configuration.getString(key));
1657 }
1658
1659
1660
1661
1662
1663
1664
1665 public int getInt(String key)
1666 {
1667 return configuration.getInt(key);
1668 }
1669
1670
1671
1672
1673
1674
1675
1676
1677 public int getInt(String key, int defaultValue)
1678 {
1679 return configuration.getInt(key, defaultValue);
1680 }
1681
1682
1683
1684
1685
1686
1687
1688
1689 public boolean getBoolean(String key, boolean def)
1690 {
1691 return configuration.getBoolean(key, def);
1692 }
1693
1694
1695
1696
1697
1698
1699
1700 public ExtendedProperties getConfiguration()
1701 {
1702 return configuration;
1703 }
1704
1705
1706
1707
1708
1709 public Introspector getIntrospector()
1710 {
1711 return introspector;
1712 }
1713
1714
1715
1716
1717
1718
1719 public EventCartridge getApplicationEventCartridge()
1720 {
1721 return eventCartridge;
1722 }
1723
1724
1725
1726
1727
1728
1729
1730
1731 public Object getApplicationAttribute(Object key)
1732 {
1733 return applicationAttributes.get(key);
1734 }
1735
1736
1737
1738
1739
1740
1741
1742
1743 public Object setApplicationAttribute(Object key, Object o)
1744 {
1745 return applicationAttributes.put(key, o);
1746 }
1747
1748
1749
1750
1751
1752
1753 public Uberspect getUberspect()
1754 {
1755 return uberSpect;
1756 }
1757
1758 }