You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by nb...@apache.org on 2008/07/26 01:05:58 UTC

svn commit: r679924 - /velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeInstance.java

Author: nbubna
Date: Fri Jul 25 16:05:57 2008
New Revision: 679924

URL: http://svn.apache.org/viewvc?rev=679924&view=rev
Log:
DRY regarding auto-initialization

Modified:
    velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeInstance.java

Modified: velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeInstance.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeInstance.java?rev=679924&r1=679923&r2=679924&view=diff
==============================================================================
--- velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeInstance.java (original)
+++ velocity/engine/trunk/src/java/org/apache/velocity/runtime/RuntimeInstance.java Fri Jul 25 16:05:57 2008
@@ -275,6 +275,26 @@
     }
 
     /**
+     * Init or die! (with some log help, of course)
+     */
+    private void requireInitialization()
+    {
+        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!", e);
+            }
+        }
+    }
+
+    /**
      *  Gets the classname for the Uberspect introspection package and
      *  instantiates an instance.
      */
@@ -962,20 +982,7 @@
      */
     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!");
-            }
-        }
+        requireInitialization();
 
         Parser parser = new Parser(this);
         parser.setDirectives(runtimeDirectives);
@@ -1020,20 +1027,7 @@
     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!");
-            }
-        }
+        requireInitialization();
 
         Parser parser = (Parser) parserPool.get();
         boolean keepParser = true;
@@ -1327,12 +1321,7 @@
     public Template getTemplate(String name, String  encoding)
         throws ResourceNotFoundException, ParseErrorException, Exception
     {
-        /* must be initialized before using resourceManager */
-        if (!initialized && !initializing)
-        {
-            log.debug("Velocity not initialized yet. Calling init()...");
-            init();
-        }
+        requireInitialization();
 
         return (Template)
                 resourceManager.getResource(name,
@@ -1377,12 +1366,7 @@
     public ContentResource getContent(String name, String encoding)
         throws ResourceNotFoundException, ParseErrorException, Exception
     {
-        /* must be initialized before using resourceManager */
-        if (!initialized && !initializing)
-        {
-            log.debug("Velocity not initialized yet. Calling init()...");
-            init();
-        }
+        requireInitialization();
 
         return (ContentResource)
                 resourceManager.getResource(name,
@@ -1401,20 +1385,7 @@
      */
     public String getLoaderNameForResource(String resourceName)
     {
-        /* must be initialized before using resourceManager */
-        if (!initialized && !initializing)
-        {
-            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!");
-            }
-        }
+        requireInitialization();
 
         return resourceManager.getLoaderNameForResource(resourceName);
     }