Author: nbubna
Date: Thu Oct 30 22:49:31 2008
New Revision: 709355

URL: http://svn.apache.org/viewvc?rev=709355&view=rev
Log:
add very basic support for bodyContent template caching under default 
StringResourceLoader settings only (until we require Engine 1.6)

Modified:
    
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/jsp/VelocityViewTag.java
    
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/velocity.properties

Modified: 
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/jsp/VelocityViewTag.java
URL: 
http://svn.apache.org/viewvc/velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/jsp/VelocityViewTag.java?rev=709355&r1=709354&r2=709355&view=diff
==============================================================================
--- 
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/jsp/VelocityViewTag.java
 (original)
+++ 
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/jsp/VelocityViewTag.java
 Thu Oct 30 22:49:31 2008
@@ -27,6 +27,7 @@
 import javax.servlet.jsp.tagext.BodyTagSupport;
 import org.apache.velocity.Template;
 import org.apache.velocity.app.VelocityEngine;
+import org.apache.velocity.runtime.resource.loader.StringResourceLoader;
 import org.apache.velocity.tools.view.ServletUtils;
 import org.apache.velocity.tools.view.ViewToolContext;
 import org.apache.velocity.tools.view.VelocityView;
@@ -61,6 +62,8 @@
     protected String scope;
     protected String template;
     protected String bodyContentKey = DEFAULT_BODY_CONTENT_KEY;
+    private boolean cacheable = true;
+    private boolean uncached = true;
 
     public VelocityViewTag()
     {
@@ -240,14 +243,27 @@
 
     protected void renderBody(Writer out) throws Exception
     {
-        VelocityEngine engine = getVelocityView().getVelocityEngine();
+        // if it hasn't been cached, try that
+        if (uncached && cacheable)
+        {
+            cache(getId(), getBodyContent().getString());
+        }
+        // if it can't be cached, eval it
+        if (!cacheable)
+        {
+            evalBody(out);
+        }
+        else
+        {
+            // load template from cache
+            Template template = getVelocityView().getTemplate(getId());
+            template.merge(getViewToolContext(), out);
+        }
+    }
 
-        /*
-         * Eventually, it'd be nice to utilize some AST caching here.
-         * But that will likely need to wait until the StringResourceLoader
-         * is ready for general use (Velocity 1.6), unless we want to
-         * duplicate that minimally here in Tools 2.0
-         */
+    protected void evalBody(Writer out) throws Exception
+    {
+        VelocityEngine engine = getVelocityView().getVelocityEngine();
         engine.evaluate(getViewToolContext(), out, getId(),
                         getBodyContent().getReader());
     }
@@ -277,4 +293,17 @@
         throw new IllegalArgumentException("Unknown scope: "+scope);
     }
 
+    private void cache(String name, String template)
+    {
+        try
+        {
+            StringResourceLoader.getRepository().putStringResource(name, 
template);
+            uncached = false;
+        }
+        catch (Exception cnfe)
+        {
+            cacheable = false;
+        }
+    }
+
 }

Modified: 
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/velocity.properties
URL: 
http://svn.apache.org/viewvc/velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/velocity.properties?rev=709355&r1=709354&r2=709355&view=diff
==============================================================================
--- 
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/velocity.properties
 (original)
+++ 
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/velocity.properties
 Thu Oct 30 22:49:31 2008
@@ -18,6 +18,7 @@
 # default to servletlogger, which logs to the servlet engines log
 runtime.log.logsystem.class = 
org.apache.velocity.runtime.log.ServletLogChute,org.apache.velocity.tools.view.ServletLogChute
 
-# by default, load resources with webapp resource loader
-resource.loader = webapp
+# by default, load resources with webapp resource loader and string resource 
loader (in that order)
+resource.loader = webapp,string
 webapp.resource.loader.class = 
org.apache.velocity.tools.view.WebappResourceLoader
+string.resource.loader.class = 
org.apache.velocity.runtime.resource.loader.StringResourceLoader


Reply via email to