webwork/src/main/webwork/view/velocity VelocityHelper.java, 1.4, 1.5

[email protected] Sun, 29 Apr 2007 01:44:19 -0700
Newsgroups gmane.comp.java.open-symphony.cvs
Message-ID <[email protected]>
Update of /cvsroot/opensymphony/webwork/src/main/webwork/view/velocity
In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv24346

Modified Files:
	VelocityHelper.java 
Log Message:
Checkin for Jed's patch on WW-1454.  Cursory check, trusting that Jed did what he said he did

Index: VelocityHelper.java
===================================================================
RCS file: /cvsroot/opensymphony/webwork/src/main/webwork/view/velocity/VelocityHelper.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- VelocityHelper.java	20 Jan 2005 05:26:24 -0000	1.4
+++ VelocityHelper.java	29 Apr 2007 08:44:16 -0000	1.5
@@ -42,91 +42,94 @@
   static final String WEBWORK_UTIL = "webwork";
   private static boolean initialized = false;
   public static final String VELO_CONTEXT = "__webwork__velocity__context";
-
+  private static final Object INIT_MUTEX = new Object();
   /**
    *   Hook up Velocity with the WebWork configuration.
    */
-  public static synchronized void initVelocity(ServletContext context) throws Exception
+  public static void initVelocity(ServletContext context) throws Exception
   {
-    // WebWork configuration provides main config
-    final Properties conf = new Properties()
+    synchronized (INIT_MUTEX)
     {
-      public Object get(Object key)
+      // WebWork configuration provides main config
+      final Properties conf = new Properties()
       {
-        return Configuration.get(key.toString());
-      }
+        public Object get(Object key)
+        {
+          return Configuration.get(key.toString());
+        }
 
-      public String getProperty(String key)
-      {
-        return Configuration.getString(key.toString());
-      }
+        public String getProperty(String key)
+        {
+          return Configuration.getString(key.toString());
+        }
 
-      public Enumeration keys()
-      {
-        final Iterator list = Configuration.list();
-        return new Enumeration()
+        public Enumeration keys()
         {
-          public Object nextElement()
+          final Iterator list = Configuration.list();
+          return new Enumeration()
           {
-            return list.next();
-          }
+            public Object nextElement()
+            {
+              return list.next();
+            }
 
-          public boolean hasMoreElements()
-          {
-            return list.hasNext();
-          }
-        };
-      }
-    };
+            public boolean hasMoreElements()
+            {
+              return list.hasNext();
+            }
+          };
+        }
+      };
 
-    // Set dynamic properties here
-    // The properties not set here are taken from the WebWork configuration
-    Properties p = new Properties(conf)
-    {
-      public Enumeration keys()
+      // Set dynamic properties here
+      // The properties not set here are taken from the WebWork configuration
+      Properties p = new Properties(conf)
       {
-        return conf.keys();
-      }
-    };
-
-    /*
-    *  first, normalize our velocity log file to be in the
-    *  webapp
-    */
+        public Enumeration keys()
+        {
+          return conf.keys();
+        }
+      };
 
-    String log = p.getProperty(Velocity.RUNTIME_LOG);
+      /*
+       *  first, normalize our velocity log file to be in the
+       *  webapp
+       */
 
-    if(log != null)
-    {
-      log = context.getRealPath(log);
+      String log = p.getProperty(Velocity.RUNTIME_LOG);
 
       if(log != null)
       {
-        p.setProperty(Velocity.RUNTIME_LOG, log);
+        log = context.getRealPath(log);
+
+        if(log != null)
+        {
+          p.setProperty(Velocity.RUNTIME_LOG, log);
+        }
       }
-    }
 
 
-    /*
-    *  If there is a file loader resource path, treat it the
-    *  same way, but only if it doesn't start with /. In that case
-    *  we use it as-is to allow the templates to be taken from some
-    *  repository (!very useful during development!).
-    */
-    String path = p.getProperty(Velocity.FILE_RESOURCE_LOADER_PATH);
+      /*
+       *  If there is a file loader resource path, treat it the
+       *  same way, but only if it doesn't start with /. In that case
+       *  we use it as-is to allow the templates to be taken from some
+       *  repository (!very useful during development!).
+       */
+      String path = p.getProperty(Velocity.FILE_RESOURCE_LOADER_PATH);
 
-    if(path != null && (path.equals("/") || !path.startsWith("/")))
-    {
-      path = context.getRealPath(path);
-      if(path != null)
+      if(path != null && (path.equals("/") || !path.startsWith("/")))
       {
-        p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path);
+        path = context.getRealPath(path);
+        if(path != null)
+        {
+          p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path);
+        }
       }
-    }
 
-    Velocity.setApplicationAttribute(ServletContext.class.getName(), context); 
-    Velocity.init(p);
-    initialized = true;
+      Velocity.setApplicationAttribute(ServletContext.class.getName(), context); 
+      Velocity.init(p);
+      initialized = true;
+    }
   }
 
   public static void merge(Context context, String templateName, Writer writer)
@@ -145,25 +148,51 @@
     }
   }
 
-    public static Context getContext(ServletContext context, ServletRequest request, ServletResponse response)
-    {
-        return getContext(context, request, response, Collections.EMPTY_MAP);
-    }
+  /**
+   * Get the Velocity Context from the request and response. Additionally, initialize Velocity
+   * if WebWork hasn't already initialized it.
+   * <p>
+   * Note: This method should only be used if WebWork is responsible for initializing Velocity.
+   * If Velocity's lifecycle is being managed else where, use 
+   * {@link #getContextWithoutInit(ServletRequest, ServletResponse, Map)} instead, and only 
+   * <strong>after</strong> Velocity has been initialized.
+   */
+  public static Context getContext(ServletContext context, ServletRequest request, ServletResponse response)
+  {
+    return getContext(context, request, response, Collections.EMPTY_MAP);
+  }
 
+  /**
+   * Get the Velocity Context from the request, response and any additional context parameters.
+   * Additionally, initialize Velocity if WebWork hasn't already initialized it.
+   * <p>
+   * Note: This method should only be used if WebWork is responsible for initializing Velocity.
+   * If Velocity's lifecycle is being managed else where, use 
+   * {@link #getContextWithoutInit(ServletRequest, ServletResponse, Map)} instead, and only 
+   * <strong>after</strong> Velocity has been initialized.
+   */
   public static Context getContext(ServletContext context, ServletRequest request, ServletResponse response, Map extraContextParams)
   {
-    if(!initialized)
+    try
     {
-      try
-      {
-        initVelocity(context);
-      }
-      catch(Exception e)
-      {
-        log.error(e.getMessage(), e);
-        return null;
-      }
+      checkInited(context);
     }
+    catch(Exception e)
+    {
+      log.error(e.getMessage(), e);
+      return null;
+    }
+    return getContextWithoutInit(request, response, extraContextParams);
+  }
+
+  /**
+   * Get the Velocity Context from the request, response and any additional context parameters.
+   * <p>
+   * Note: This method should only be used if <strong>after</strong> Velocity has been 
+   * initialized elsewhere.
+   */
+  public static Context getContextWithoutInit(ServletRequest request, ServletResponse response, Map extraContextParams)
+  {
     WebWorkVelocityContext ctx = (WebWorkVelocityContext)request.getAttribute(VELO_CONTEXT);
     if(ctx==null)
     {
@@ -177,6 +206,17 @@
     return ctx;
   }
 
+  private static void checkInited(ServletContext context) throws Exception
+  {
+    synchronized (INIT_MUTEX)
+    {
+      if(!initialized)
+      {
+        initVelocity(context);
+      }
+    }
+  }
+
   /**
    * WebWork specific Velocity context implementation.
    */


-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/