You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by nb...@apache.org on 2006/03/08 23:19:58 UTC

svn commit: r384344 - in /jakarta/velocity/engine/trunk/src/java/org/apache/velocity: app/Velocity.java app/VelocityEngine.java runtime/RuntimeInstance.java

Author: nbubna
Date: Wed Mar  8 14:19:55 2006
New Revision: 384344

URL: http://svn.apache.org/viewcvs?rev=384344&view=rev
Log:
automatically call RuntimeInstance.init() when Velocity is used (where necessary) without being manually initialized by the user

Modified:
    jakarta/velocity/engine/trunk/src/java/org/apache/velocity/app/Velocity.java
    jakarta/velocity/engine/trunk/src/java/org/apache/velocity/app/VelocityEngine.java
    jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeInstance.java

Modified: jakarta/velocity/engine/trunk/src/java/org/apache/velocity/app/Velocity.java
URL: http://svn.apache.org/viewcvs/jakarta/velocity/engine/trunk/src/java/org/apache/velocity/app/Velocity.java?rev=384344&r1=384343&r2=384344&view=diff
==============================================================================
--- jakarta/velocity/engine/trunk/src/java/org/apache/velocity/app/Velocity.java (original)
+++ jakarta/velocity/engine/trunk/src/java/org/apache/velocity/app/Velocity.java Wed Mar  8 14:19:55 2006
@@ -205,18 +205,9 @@
         throws ParseErrorException, MethodInvocationException,
             ResourceNotFoundException, IOException
     {
-        /**
-         * Check to see the engine was initialized.
-         */
-        if (!RuntimeSingleton.isInitialized()) 
-        {
-            throw new IllegalStateException ("Cannot evaluate string as Velocity has not been initialized.");
-        }
-        
         /*
          *  first, parse - convert ParseException if thrown
          */
-
         BufferedReader br  = null;
         String encoding = null;
 

Modified: jakarta/velocity/engine/trunk/src/java/org/apache/velocity/app/VelocityEngine.java
URL: http://svn.apache.org/viewcvs/jakarta/velocity/engine/trunk/src/java/org/apache/velocity/app/VelocityEngine.java?rev=384344&r1=384343&r2=384344&view=diff
==============================================================================
--- jakarta/velocity/engine/trunk/src/java/org/apache/velocity/app/VelocityEngine.java (original)
+++ jakarta/velocity/engine/trunk/src/java/org/apache/velocity/app/VelocityEngine.java Wed Mar  8 14:19:55 2006
@@ -239,18 +239,9 @@
         throws ParseErrorException, MethodInvocationException,
             ResourceNotFoundException, IOException
     {
-        /**
-         * Check to see the engine was initialized.
-         */
-        if (!ri.isInitialized()) 
-        {
-            throw new IllegalStateException ("Cannot evaluate string as Velocity has not been initialized.");
-        }
-        
         /*
          *  first, parse - convert ParseException if thrown
          */
-
         BufferedReader br  = null;
         String encoding = null;
 
@@ -290,15 +281,6 @@
         throws ParseErrorException, MethodInvocationException,
             ResourceNotFoundException,IOException
     {
-
-        /**
-         * Check to see the engine was initialized.
-         */
-        if (!ri.isInitialized()) 
-        {
-            throw new IllegalStateException ("Cannot evaluate string as Velocity has not been initialized.");
-        }
-                
         SimpleNode nodeTree = null;
 
         try
@@ -477,14 +459,6 @@
                                       Context context, Writer writer )
         throws ResourceNotFoundException, ParseErrorException, MethodInvocationException, Exception
     {
-        /**
-         * Check to see the engine was initialized.
-         */
-        if (!ri.isInitialized()) 
-        {
-            throw new IllegalStateException ("Cannot merge template as Velocity has not been initialized.");
-        }
-        
         Template template = ri.getTemplate(templateName, encoding);
 
         if ( template == null )
@@ -515,14 +489,6 @@
     public Template getTemplate(String name)
         throws ResourceNotFoundException, ParseErrorException, Exception
     {
-        /**
-         * Check to see the engine was initialized.
-         */
-        if (!ri.isInitialized()) 
-        {
-            throw new IllegalStateException ("Cannot get template as Velocity has not been initialized.");
-        }
-        
         return ri.getTemplate( name );
     }
 

Modified: jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeInstance.java
URL: http://svn.apache.org/viewcvs/jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeInstance.java?rev=384344&r1=384343&r2=384344&view=diff
==============================================================================
--- jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeInstance.java (original)
+++ jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeInstance.java Wed Mar  8 14:19:55 2006
@@ -120,9 +120,14 @@
     private  SimplePool parserPool;
 
     /**
+     * Indicate whether the Runtime is in the midst of initialization.
+     */
+    private boolean initializing = false;
+
+    /**
      * Indicate whether the Runtime has been fully initialized.
      */
-    private  boolean initialized;
+    private boolean initialized = false;
 
     /**
      * These are the properties that are laid down over top
@@ -183,22 +188,18 @@
     public RuntimeInstance()
     {
         /*
-         *  create a VM factory, resource manager
-         *  and introspector
+         *  create a VM factory, introspector, and application attributes
          */
-
         vmFactory = new VelocimacroFactory( this );
 
         /*
          *  make a new introspector and initialize it
          */
-
         introspector = new Introspector(getLog());
 
         /*
          * and a store for the application attributes
          */
-
         applicationAttributes = new HashMap();
     }
 
@@ -220,8 +221,10 @@
     public synchronized void init()
         throws Exception
     {
-        if (initialized == false)
+        if (!initialized && !initializing)
         {
+            initializing = true;
+
             log.trace("*******************************************************************");
             log.debug("Starting Jakarta Velocity v@build.version@ (compiled: @build.time@)");
             log.trace("RuntimeInstance initializing.");
@@ -243,6 +246,7 @@
             log.trace("RuntimeInstance successfully initialized.");
 
             initialized = true;
+            initializing = false;
         }
     }
 
@@ -835,6 +839,21 @@
      */
     public Parser createNewParser()
     {
+        /* must be initialized before we use runtimeDirectives */
+        if (!initialized && !initializing)
+        {
+            log.debug("Velocity was not initialized! Calling init()...");
+            try
+            {
+                init();
+            }
+            catch (Exception e)
+            {
+                getLog().error("Could not auto-initialize Velocity", e);
+                throw new IllegalStateException("Velocity could not be initialized!");
+            }
+        }
+
         Parser parser = new Parser(this);
         parser.setDirectives(runtimeDirectives);
         return parser;
@@ -874,6 +893,20 @@
     public SimpleNode parse(Reader reader, String templateName, boolean dumpNamespace)
         throws ParseException
     {
+        /* must be initialized before using parserPool */
+        if (!initialized && !initializing)
+        {
+            log.debug("Velocity was not initialized! Calling init()...");
+            try
+            {
+                init();
+            }
+            catch (Exception e)
+            {
+                getLog().error("Could not auto-initialize Velocity", e);
+                throw new IllegalStateException("Velocity could not be initialized!");
+            }
+        }
 
         SimpleNode ast = null;
         Parser parser = (Parser) parserPool.get();
@@ -975,10 +1008,11 @@
     public Template getTemplate(String name, String  encoding)
         throws ResourceNotFoundException, ParseErrorException, Exception
     {
-        if (resourceManager == null)
+        /* must be initialized before using resourceManager */
+        if (!initialized && !initializing)
         {
-            throw new IllegalStateException
-              ("Cannot retrieve template as Velocity was not initialized.");
+            log.info("Velocity not initialized yet. Calling init()...");
+            init();
         }
 
         return (Template)
@@ -1020,10 +1054,11 @@
     public ContentResource getContent(String name, String encoding)
         throws ResourceNotFoundException, ParseErrorException, Exception
     {
-        if (resourceManager == null)
+        /* must be initialized before using resourceManager */
+        if (!initialized && !initializing)
         {
-            throw new IllegalStateException
-              ("Cannot retrieve content as Velocity was not initialized.");
+            log.info("Velocity not initialized yet. Calling init()...");
+            init();
         }
 
         return (ContentResource)
@@ -1033,9 +1068,9 @@
 
 
     /**
-     *  Determines is a template exists, and returns name of the loader that
+     *  Determines if a template exists and returns name of the loader that
      *  provides it.  This is a slightly less hokey way to support
-     *  the Velocity.templateExists() utility method, which was broken
+     *  the Velocity.resourceExists() utility method, which was broken
      *  when per-template encoding was introduced.  We can revisit this.
      *
      *  @param resourceName Name of template or content resource
@@ -1043,10 +1078,19 @@
      */
     public String getLoaderNameForResource(String resourceName)
     {
-        if (resourceManager == null)
+        /* must be initialized before using resourceManager */
+        if (!initialized && !initializing)
         {
-            throw new IllegalStateException
-              ("Cannot retrieve template information as Velocity was not initialized.");
+            log.debug("Velocity was not initialized! Calling init()...");
+            try
+            {
+                init();
+            }
+            catch (Exception e)
+            {
+                getLog().error("Could not initialize Velocity", e);
+                throw new IllegalStateException("Velocity could not be initialized!");
+            }
         }
 
         return resourceManager.getLoaderNameForResource(resourceName);
@@ -1235,10 +1279,10 @@
     /**
      * Returns the event handlers for the application.
      */
-     public EventCartridge getApplicationEventCartridge()
-     {
-         return eventCartridge;
-     }
+    public EventCartridge getApplicationEventCartridge()
+    {
+        return eventCartridge;
+    }
 
 
     /**



---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org