jvanzyl     00/10/22 14:05:21

  Modified:    src/java/org/apache/velocity/runtime Runtime.java
  Added:       src/java/org/apache/velocity/runtime velocity.properties
  Log:
  - added Runtime.init() so that the Velocity Runtime can be initialized
    without specifying a properties file. A default properties file
    org/apache/velocity/runtime/velocity.properties will be used
    when Runtime.init() is called or when Runtime.init(props) fails.
  
  Revision  Changes    Path
  1.24      +45 -17    
jakarta-velocity/src/java/org/apache/velocity/runtime/Runtime.java
  
  Index: Runtime.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/Runtime.java,v
  retrieving revision 1.23
  retrieving revision 1.24
  diff -u -r1.23 -r1.24
  --- Runtime.java      2000/10/22 01:27:34     1.23
  +++ Runtime.java      2000/10/22 21:05:16     1.24
  @@ -139,7 +139,7 @@
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a>
    * @author <a href="mailto:[EMAIL PROTECTED]">Jeff Bowden</a>
  - * @version $Id: Runtime.java,v 1.23 2000/10/22 01:27:34 jvanzyl Exp $
  + * @version $Id: Runtime.java,v 1.24 2000/10/22 21:05:16 jvanzyl Exp $
    */
   public class Runtime
   {
  @@ -155,31 +155,25 @@
       /** Specify template caching true/false */
       public static final String TEMPLATE_CACHE = "template.cache";
       
  -    /** Template processor to use. A processor is associated
  -      * with a parser generator tool, and its utilities
  -      * used for parsing.
  -      */
  -    public static final String TEMPLATE_PROCESSOR = "template.processor";
  -    
       /** The encoding to use for the template */
       public static final String TEMPLATE_ENCODING = "template.encoding";
       
       /** Enable the speed up provided by FastWriter */
       public static final String TEMPLATE_ASCIIHACK = "template.asciihack";
   
  +    /** How often to check for modified templates. */
  +    public static final String TEMPLATE_MOD_CHECK_INTERVAL =
  +        "template.modificationCheckInterval";
  +    
       /** Initial counter value in #foreach directives */
       public static final String COUNTER_NAME = "counter.name";
   
       /** Initial counter value in #foreach directives */
       public static final String COUNTER_INITIAL_VALUE = "counter.initial.value";
   
  -    /** Initial counter value in #foreach directives */
  +    /** Content type */
       public static final String DEFAULT_CONTENT_TYPE = "default.contentType";
   
  -    /** How often to check for modified templates. */
  -    public static final String TEMPLATE_MOD_CHECK_INTERVAL = 
  -        "template.modificationCheckInterval";
  -
       /** Prefix for warning messages */
       private final static String WARN  = "  [warn] ";
       
  @@ -220,6 +214,34 @@
       private static StringBuffer pendingMessages = new StringBuffer();
   
       private static Properties properties;
  +    
  +    /**
  +     * Initializes the Velocity Runtime with a set of
  +     * values from a default velocity.properties that
  +     * is on the classpath. This default properties file
  +     * will be included in the distribution jar file to
  +     * make the Velocity Runtime easy to init.
  +     */
  +    public synchronized static void init() throws Exception
  +    {
  +        System.out.println("using init()");
  +        Properties properties = new Properties();
  +        ClassLoader classLoader = Runtime.class.getClassLoader();
  +        
  +        try
  +        {
  +            InputStream inputStream = classLoader.getResourceAsStream(
  +                "org/apache/velocity/runtime/velocity.properties");
  +        
  +            properties.load(inputStream);
  +        }
  +        catch (IOException ioe)
  +        {
  +            System.err.println("Cannot initialize Velocity Runtime!");
  +        }
  +        
  +        init(properties);
  +    }
   
       /**
        * Initializes the Velocity Runtime.
  @@ -229,16 +251,22 @@
       {
           Properties properties = new Properties();
           File file = new File( propertiesFileName );
  +        
  +        /*
  +         * Try loading the properties from the named properties
  +         * file. If that fails then set the default values.
  +         * From the command line and for testing the default
  +         * values should work fine, and makes initializing
  +         * the Velocity runtime as easy as Runtime.init();
  +         */
  +        
           try
           {
  -            if (file.exists()) 
  -            {
  -                properties.load( new FileInputStream(file) );
  -            }
  +            properties.load( new FileInputStream(file) );
           }
           catch(Exception ex) 
           {
  -            throw new Exception( "Cannot load properties file: " + ex );
  +            init();
           }
           init( properties );
       }
  
  
  
  1.1                  
jakarta-velocity/src/java/org/apache/velocity/runtime/velocity.properties
  
  Index: velocity.properties
  ===================================================================
  # These are the default properties for the
  # Velocity Runtime. These values are used when
  # Runtime.init() is called, Runtime.init(properties)
  # fails to find the specificed properties file.
  
  runtime.log = velocity.log
  template.loader=org.apache.velocity.runtime.loader.FileTemplateLoader
  template.modificationCheckInterval = 2
  template.path=.
  template.cache=false
  template.encoding = UTF8
  template.asciihack = true
  counter.name = velocityCounter
  counter.initial.value = 1
  default.contentType=text/html
  
  
  

Reply via email to