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/04 02:07:06 UTC

svn commit: r673880 - /velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/ServletUtils.java

Author: nbubna
Date: Thu Jul  3 17:07:05 2008
New Revision: 673880

URL: http://svn.apache.org/viewvc?rev=673880&view=rev
Log:
make it easy to use a VelocityView subclass in VVS, VLS, and VVT

Modified:
    velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/ServletUtils.java

Modified: velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/ServletUtils.java
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/ServletUtils.java?rev=673880&r1=673879&r2=673880&view=diff
==============================================================================
--- velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/ServletUtils.java (original)
+++ velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/ServletUtils.java Thu Jul  3 17:07:05 2008
@@ -19,11 +19,13 @@
  * under the License.
  */
 
+import java.lang.reflect.Constructor;
 import javax.servlet.FilterConfig;
 import javax.servlet.ServletConfig;
 import javax.servlet.ServletContext;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpSession;
+import org.apache.velocity.tools.ClassUtils;
 import org.apache.velocity.tools.Toolbox;
 
 /**
@@ -35,6 +37,8 @@
 {
     public static final String VELOCITY_VIEW_KEY =
         VelocityView.class.getName();
+    public static final String ALT_VELOCITY_VIEW_KEY =
+        "org.apache.velocity.tools.view.class";
 
     public static final ServletUtils INSTANCE = new ServletUtils();
 
@@ -107,7 +111,7 @@
         if (view == null)
         {
             // only create a new one if we don't already have one
-            view = new VelocityView(config);
+            view = createView(config);
 
             // and store it in the application attributes, so other
             // servlets, filters, or tags can use it
@@ -116,6 +120,44 @@
         return view;
     }
 
+    private static VelocityView createView(JeeConfig config)
+    {
+        String cls = config.findInitParameter(ALT_VELOCITY_VIEW_KEY);
+        if (cls == null)
+        {
+            return new VelocityView(config);
+        }
+        try
+        {
+            return createView(ClassUtils.getClass(cls), config);
+        }
+        catch (ClassNotFoundException cnfe)
+        {
+            throw new IllegalArgumentException("Could not find class "+cls, cnfe);
+        }
+    }
+
+    private static VelocityView createView(Class klass, JeeConfig config)
+    {
+        if (!VelocityView.class.isAssignableFrom(klass))
+        {
+            throw new IllegalArgumentException(klass+" must extend "+VelocityView.class);
+        }
+        try
+        {
+            Constructor ctor = klass.getConstructor(JeeConfig.class);
+            return (VelocityView)ctor.newInstance(config);
+        }
+        catch (NoSuchMethodException nsme)
+        {
+            throw new IllegalArgumentException(klass+" must have a constructor that takes "+JeeConfig.class, nsme);
+        }
+        catch (Exception e)
+        {
+            throw new RuntimeException("Could not instantiate "+klass+" with "+config, e);
+        }
+    }
+
     /**
      * Returns the shared {@link VelocityView} for the specified
      * {@link ServletContext}. If one has not yet been created,
@@ -123,7 +165,7 @@
      */
     public static VelocityView getVelocityView(ServletContext application)
     {
-        return getVelocityView(application, true);
+        return getVelocityView(new JeeConfig(application));
     }
 
     /**
@@ -138,8 +180,7 @@
             (VelocityView)application.getAttribute(VELOCITY_VIEW_KEY);
         if (view == null && createIfMissing)
         {
-            view = new VelocityView(application);
-            application.setAttribute(VELOCITY_VIEW_KEY, view);
+            return getVelocityView(application);
         }
         return view;
     }