1 package org.apache.velocity.runtime; 2 3 18 19 import java.io.InputStream ; 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.io.Reader ; 23 24 import java.util.Map ; 25 import java.util.Hashtable ; 26 import java.util.Properties ; 27 import java.util.Enumeration ; 28 import java.util.HashMap ; 29 30 import org.apache.velocity.Template; 31 32 import org.apache.velocity.runtime.log.LogManager; 33 import org.apache.velocity.runtime.log.LogSystem; 34 import org.apache.velocity.runtime.log.PrimordialLogSystem; 35 import org.apache.velocity.runtime.log.NullLogSystem; 36 37 import org.apache.velocity.runtime.parser.Parser; 38 import org.apache.velocity.runtime.parser.ParseException; 39 import org.apache.velocity.runtime.parser.node.SimpleNode; 40 41 import org.apache.velocity.runtime.directive.Directive; 42 import org.apache.velocity.runtime.VelocimacroFactory; 43 44 import org.apache.velocity.runtime.resource.ContentResource; 45 import org.apache.velocity.runtime.resource.ResourceManager; 46 47 import org.apache.velocity.util.SimplePool; 48 import org.apache.velocity.util.StringUtils; 49 50 import org.apache.velocity.util.introspection.Introspector; 51 import org.apache.velocity.util.introspection.Uberspect; 52 import org.apache.velocity.util.introspection.UberspectLoggable; 53 54 import org.apache.velocity.exception.ResourceNotFoundException; 55 import org.apache.velocity.exception.ParseErrorException; 56 57 import org.apache.commons.collections.ExtendedProperties; 58 59 106 public class RuntimeInstance implements RuntimeConstants, RuntimeServices 107 { 108 111 private VelocimacroFactory vmFactory = null; 112 113 119 private LogSystem logSystem = new PrimordialLogSystem(); 120 121 124 private SimplePool parserPool; 125 126 129 private boolean initialized; 130 131 135 private ExtendedProperties overridingProperties = null; 136 137 144 private Hashtable runtimeDirectives; 145 146 160 private ExtendedProperties configuration = new ExtendedProperties(); 161 162 private ResourceManager resourceManager = null; 163 164 168 private Introspector introspector = null; 169 170 171 176 private Map applicationAttributes = null; 177 178 179 private Uberspect uberSpect; 180 181 public RuntimeInstance() 182 { 183 187 188 vmFactory = new VelocimacroFactory( this ); 189 190 193 194 introspector = new Introspector( this ); 195 196 199 200 applicationAttributes = new HashMap (); 201 } 202 203 217 public synchronized void init() 218 throws Exception 219 { 220 if (initialized == false) 221 { 222 info("************************************************************** "); 223 info("Starting Jakarta Velocity v1.4"); 224 info("RuntimeInstance initializing."); 225 initializeProperties(); 226 initializeLogger(); 227 initializeResourceManager(); 228 initializeDirectives(); 229 initializeParserPool(); 230 231 initializeIntrospection(); 232 236 vmFactory.initVelocimacro(); 237 238 info("Velocity successfully started."); 239 240 initialized = true; 241 } 242 } 243 244 245 249 private void initializeIntrospection() 250 throws Exception 251 { 252 String rm = getString(RuntimeConstants.UBERSPECT_CLASSNAME); 253 254 if (rm != null && rm.length() > 0) 255 { 256 Object o = null; 257 258 try 259 { 260 o = Class.forName(rm).newInstance(); 261 } 262 catch (ClassNotFoundException cnfe) 263 { 264 String err = "The specified class for Uberspect (" 265 + rm 266 + ") does not exist (or is not accessible to the current classlaoder."; 267 error(err); 268 throw new Exception (err); 269 } 270 271 if (!(o instanceof Uberspect)) 272 { 273 String err = "The specified class for Uberspect (" 274 + rm 275 + ") does not implement org.apache.velocity.util.introspector.Uberspect." 276 + " Velocity not initialized correctly."; 277 278 error(err); 279 throw new Exception (err); 280 } 281 282 uberSpect = (Uberspect) o; 283 284 if (uberSpect instanceof UberspectLoggable) 285 { 286 ((UberspectLoggable) uberSpect).setRuntimeLogger(this); 287 } 288 289 uberSpect.init(); 290 } 291 else 292 { 293 296 297 String err = "It appears that no class was specified as the" 298 + " Uberspect. Please ensure that all configuration" 299 + " information is correct."; 300 301 error(err); 302 throw new Exception (err); 303 } 304 } 305 306 311 private void setDefaultProperties() 312 { 313 try 314 { 315 InputStream inputStream = getClass() 316 .getResourceAsStream('/' + DEFAULT_RUNTIME_PROPERTIES); 317 318 configuration.load( inputStream ); 319 320 info ("Default Properties File: " + 321 new File (DEFAULT_RUNTIME_PROPERTIES).getPath()); 322 } 323 catch (IOException ioe) 324 { 325 System.err.println("Cannot get Velocity Runtime default properties!"); 326 } 327 } 328 329 336 public void setProperty(String key, Object value) 337 { 338 if (overridingProperties == null) 339 { 340 overridingProperties = new ExtendedProperties(); 341 } 342 343 overridingProperties.setProperty( key, value ); 344 } 345 346 356 public void setConfiguration( ExtendedProperties configuration) 357 { 358 if (overridingProperties == null) 359 { 360 overridingProperties = configuration; 361 } 362 else 363 { 364 if (overridingProperties != configuration) 366 { 367 overridingProperties.combine(configuration); 368 } 369 } 370 } 371 372 391 public void addProperty(String key, Object value) 392 { 393 if (overridingProperties == null) 394 { 395 overridingProperties = new ExtendedProperties(); 396 } 397 398 overridingProperties.addProperty( key, value ); 399 } 400 401 407 public void clearProperty(String key) 408 { 409 if (overridingProperties != null) 410 { 411 overridingProperties.clearProperty(key); 412 } 413 } 414 415 422 public Object getProperty( String key ) 423 { 424 return configuration.getProperty( key ); 425 } 426 427 435 private void initializeProperties() 436 { 437 441 if (configuration.isInitialized() == false) 442 { 443 setDefaultProperties(); 444 } 445 446 if( overridingProperties != null) 447 { 448 configuration.combine(overridingProperties); 449 } 450 } 451 452 458 public void init(Properties p) throws Exception 459 { 460 overridingProperties = ExtendedProperties.convertProperties(p); 461 init(); 462 } 463 464 470 public void init(String configurationFile) 471 throws Exception 472 { 473 overridingProperties = new ExtendedProperties(configurationFile); 474 init(); 475 } 476 477 private void initializeResourceManager() 478 throws Exception 479 { 480 483 484 String rm = getString( RuntimeConstants.RESOURCE_MANAGER_CLASS ); 485 486 if ( rm != null && rm.length() > 0 ) 487 { 488 493 494 Object o = null; 495 496 try 497 { 498 o = Class.forName( rm ).newInstance(); 499 } 500 catch (ClassNotFoundException cnfe ) 501 { 502 String err = "The specified class for Resourcemanager (" 503 + rm 504 + ") does not exist (or is not accessible to the current classlaoder."; 505 error( err ); 506 throw new Exception ( err ); 507 } 508 509 if (!(o instanceof ResourceManager) ) 510 { 511 String err = "The specified class for ResourceManager (" 512 + rm 513 + ") does not implement org.apache.runtime.resource.ResourceManager." 514 + " Velocity not initialized correctly."; 515 516 error( err); 517 throw new Exception (err); 518 } 519 520 resourceManager = (ResourceManager) o; 521 522 resourceManager.initialize( this ); 523 } 524 else 525 { 526 529 530 String err = "It appears that no class was specified as the" 531 + " ResourceManager. Please ensure that all configuration" 532 + " information is correct."; 533 534 error( err); 535 throw new Exception ( err ); 536 } 537 } 538 539 544 private void initializeLogger() throws Exception 545 { 546 550 if (logSystem instanceof PrimordialLogSystem ) 551 { 552 PrimordialLogSystem pls = (PrimordialLogSystem) logSystem; 553 logSystem = LogManager.createLogSystem( this ); 554 555 559 560 if (logSystem == null) 561 { 562 logSystem = new NullLogSystem(); 563 } 564 else 565 { 566 pls.dumpLogMessages( logSystem ); 567 } 568 } 569 } 570 571 572 581 private void initializeDirectives() throws Exception 582 { 583 587 runtimeDirectives = new Hashtable (); 588 589 Properties directiveProperties = new Properties (); 590 591 595 596 InputStream inputStream = 597 getClass().getResourceAsStream('/' + DEFAULT_RUNTIME_DIRECTIVES); 598 599 if (inputStream == null) 600 throw new Exception ("Error loading directive.properties! " + 601 "Something is very wrong if these properties " + 602 "aren't being located. Either your Velocity " + 603 "distribution is incomplete or your Velocity " + 604 "jar file is corrupted!"); 605 606 directiveProperties.load(inputStream); 607 608 614 Enumeration directiveClasses = directiveProperties.elements(); 615 616 while (directiveClasses.hasMoreElements()) 617 { 618 String directiveClass = (String ) directiveClasses.nextElement(); 619 loadDirective( directiveClass, "System" ); 620 } 621 622 625 626 String [] userdirective = configuration.getStringArray("userdirective"); 627 628 for( int i = 0; i < userdirective.length; i++) 629 { 630 loadDirective( userdirective[i], "User"); 631 } 632 633 } 634 635 640 private void loadDirective( String directiveClass, String caption ) 641 { 642 try 643 { 644 Object o = Class.forName( directiveClass ).newInstance(); 645 646 if ( o instanceof Directive ) 647 { 648 Directive directive = (Directive) o; 649 runtimeDirectives.put(directive.getName(), directive); 650 651 info("Loaded " + caption + " Directive: " 652 + directiveClass); 653 } 654 else 655 { 656 error( caption + " Directive " + directiveClass 657 + " is not org.apache.velocity.runtime.directive.Directive." 658 + " Ignoring. " ); 659 } 660 } 661 catch (Exception e) 662 { 663 error("Exception Loading " + caption + " Directive: " 664 + directiveClass + " : " + e); 665 } 666 } 667 668 669 673 private void initializeParserPool() 674 { 675 int numParsers = getInt( PARSER_POOL_SIZE, NUMBER_OF_PARSERS); 676 677 parserPool = new SimplePool( numParsers); 678 679 for (int i=0; i < numParsers ;i++ ) 680 { 681 parserPool.put (createNewParser()); 682 } 683 684 info ("Created: " + numParsers + " parsers."); 685 } 686 687 692 public Parser createNewParser() 693 { 694 Parser parser = new Parser( this ); 695 parser.setDirectives(runtimeDirectives); 696 return parser; 697 } 698 699 714 public SimpleNode parse( Reader reader, String templateName ) 715 throws ParseException 716 { 717 720 return parse( reader, templateName, true ); 721 } 722 723 730 public SimpleNode parse( Reader reader, String templateName, boolean dumpNamespace ) 731 throws ParseException 732 { 733 734 SimpleNode ast = null; 735 Parser parser = (Parser) parserPool.get(); 736 boolean madeNew = false; 737 738 if (parser == null) 739 { 740 744 745 error("Runtime : ran out of parsers. Creating new. " 746 + " Please increment the parser.pool.size property." 747 + " The current value is too small."); 748 749 parser = createNewParser(); 750 751 if( parser != null ) 752 { 753 madeNew = true; 754 } 755 } 756 757 760 761 if (parser != null) 762 { 763 try 764 { 765 770 771 if ( dumpNamespace ) 772 { 773 dumpVMNamespace( templateName ); 774 } 775 776 ast = parser.parse( reader, templateName ); 777 } 778 finally 779 { 780 783 if (!madeNew) 784 { 785 parserPool.put(parser); 786 } 787 } 788 } 789 else 790 { 791 error("Runtime : ran out of parsers and unable to create more."); 792 } 793 return ast; 794 } 795 796 810 public Template getTemplate(String name) 811 throws ResourceNotFoundException, ParseErrorException, Exception 812 { 813 return getTemplate( name, getString( INPUT_ENCODING, ENCODING_DEFAULT) ); 814 } 815 816 828 public Template getTemplate(String name, String encoding) 829 throws ResourceNotFoundException, ParseErrorException, Exception 830 { 831 return (Template) resourceManager.getResource(name, ResourceManager.RESOURCE_TEMPLATE, encoding); 832 } 833 834 844 public ContentResource getContent(String name) 845 throws ResourceNotFoundException, ParseErrorException, Exception 846 { 847 851 852 return getContent( name, getString( INPUT_ENCODING, ENCODING_DEFAULT)); 853 } 854 855 865 public ContentResource getContent( String name, String encoding ) 866 throws ResourceNotFoundException, ParseErrorException, Exception 867 { 868 return (ContentResource) resourceManager.getResource(name,ResourceManager.RESOURCE_CONTENT, encoding ); 869 } 870 871 872 881 public String getLoaderNameForResource( String resourceName ) 882 { 883 return resourceManager.getLoaderNameForResource( resourceName ); 884 } 885 886 892 private boolean showStackTrace() 893 { 894 if (configuration.isInitialized()) 895 { 896 return getBoolean(RUNTIME_LOG_WARN_STACKTRACE, false); 897 } 898 else 899 { 900 return false; 901 } 902 } 903 904 909 private void log(int level, Object message) 910 { 911 String out; 912 913 917 if ( showStackTrace() && 918 (message instanceof Throwable || message instanceof Exception ) ) 919 { 920 out = StringUtils.stackTrace((Throwable )message); 921 } 922 else 923 { 924 out = message.toString(); 925 } 926 927 931 logSystem.logVelocityMessage( level, out); 932 } 933 934 939 public void warn(Object message) 940 { 941 log(LogSystem.WARN_ID, message); 942 } 943 944 949 public void info(Object message) 950 { 951 log(LogSystem.INFO_ID, message); 952 } 953 954 959 public void error(Object message) 960 { 961 log(LogSystem.ERROR_ID, message); 962 } 963 964 969 public void debug(Object message) 970 { 971 log(LogSystem.DEBUG_ID, message); 972 } 973 974 983 public String getString( String key, String defaultValue) 984 { 985 return configuration.getString(key, defaultValue); 986 } 987 988 995 public Directive getVelocimacro( String vmName, String templateName ) 996 { 997 return vmFactory.getVelocimacro( vmName, templateName ); 998 } 999 1000 1010 public boolean addVelocimacro( String name, 1011 String macro, 1012 String argArray[], 1013 String sourceTemplate ) 1014 { 1015 return vmFactory.addVelocimacro( name, macro, argArray, sourceTemplate ); 1016 } 1017 1018 1024 public boolean isVelocimacro( String vmName, String templateName ) 1025 { 1026 return vmFactory.isVelocimacro( vmName, templateName ); 1027 } 1028 1029 1033 public boolean dumpVMNamespace( String namespace ) 1034 { 1035 return vmFactory.dumpVMNamespace( namespace ); 1036 } 1037 1038 1049 1050 1055 public String getString(String key) 1056 { 1057 return configuration.getString( key ); 1058 } 1059 1060 1066 public int getInt( String key ) 1067 { 1068 return configuration.getInt( key ); 1069 } 1070 1071 1078 public int getInt( String key, int defaultValue ) 1079 { 1080 return configuration.getInt( key, defaultValue ); 1081 } 1082 1083 1090 public boolean getBoolean( String key, boolean def ) 1091 { 1092 return configuration.getBoolean( key, def ); 1093 } 1094 1095 1101 public ExtendedProperties getConfiguration() 1102 { 1103 return configuration; 1104 } 1105 1106 1109 public Introspector getIntrospector() 1110 { 1111 return introspector; 1112 } 1113 1114 public Object getApplicationAttribute( Object key) 1115 { 1116 return applicationAttributes.get( key ); 1117 } 1118 1119 public Object setApplicationAttribute( Object key, Object o ) 1120 { 1121 return applicationAttributes.put( key, o ); 1122 } 1123 1124 public Uberspect getUberspect() 1125 { 1126 return uberSpect; 1127 } 1128 1129} 1130 | Popular Tags |