velocity-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nbu...@apache.org
Subject svn commit: r774375 - in /velocity/tools/trunk/src/main/java: META-INF/velocity-view.tld org/apache/velocity/tools/view/jsp/VelocityViewTag.java
Date Wed, 13 May 2009 14:10:04 GMT
Author: nbubna
Date: Wed May 13 14:10:03 2009
New Revision: 774375

URL: http://svn.apache.org/viewvc?rev=774375&view=rev
Log:
VelocityViewTag body caching had a variety of problems.  this fixes them, but also turns it
off by default.  either set an id=foo or cache=true to turn it on.

Modified:
    velocity/tools/trunk/src/main/java/META-INF/velocity-view.tld
    velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/jsp/VelocityViewTag.java

Modified: velocity/tools/trunk/src/main/java/META-INF/velocity-view.tld
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/src/main/java/META-INF/velocity-view.tld?rev=774375&r1=774374&r2=774375&view=diff
==============================================================================
--- velocity/tools/trunk/src/main/java/META-INF/velocity-view.tld (original)
+++ velocity/tools/trunk/src/main/java/META-INF/velocity-view.tld Wed May 13 14:10:03 2009
@@ -38,7 +38,13 @@
       <name>id</name>
       <required>false</required>
       <rtexprvalue>true</rtexprvalue>
-      <description><![CDATA[A id unique to this usage of the VelocityViewTag.  This
id is used to uniquely identify this tag in log messages and hopefully at some point serve
as a key under which any body for this tag may be cached as an already-parsed template for
improved performance. If no id is specified, then a unique is automatically generated, though
that will understandably be less useful in log messages.]]></description>
+      <description><![CDATA[An id unique to this particular usage of the VelocityViewTag
in the application.  This id will then be used to cache the body in a StringResourceLoader
repository and will also identify it in log messages.  Setting this automatically sets the
cache attribute to "true".]]></description>
+    </attribute>
+    <attribute>
+      <name>cache</name>
+      <required>false</required>
+      <rtexprvalue>true</rtexprvalue>
+      <description><![CDATA[Either "true" or "false" to indicate whether the body
content template should be cached by the StringResourceLoader.  This is automatically set
to true if an id is set.  If set to true without an id value, then the template will be used
as its own id.  It is false by default.]]></description>
     </attribute>
     <attribute>
       <name>var</name>

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=774375&r1=774374&r2=774375&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
Wed May 13 14:10:03 2009
@@ -28,6 +28,7 @@
 import org.apache.velocity.Template;
 import org.apache.velocity.app.VelocityEngine;
 import org.apache.velocity.runtime.resource.loader.StringResourceLoader;
+import org.apache.velocity.runtime.resource.util.StringResourceRepository;
 import org.apache.velocity.tools.view.ServletUtils;
 import org.apache.velocity.tools.view.ViewToolContext;
 import org.apache.velocity.tools.view.VelocityView;
@@ -50,37 +51,53 @@
 public class VelocityViewTag extends BodyTagSupport
 {
     public static final String DEFAULT_BODY_CONTENT_KEY = "bodyContent";
-    public static final String DEFAULT_NAME =
-        VelocityViewTag.class.getSimpleName();
-
-    private static int count = 0;
     private static final long serialVersionUID = -3329444102562079189L;
 
     protected transient VelocityView view;
     protected transient ViewToolContext context;
+    protected transient StringResourceRepository repository;
+
     protected String var;
     protected String scope;
     protected String template;
     protected String bodyContentKey = DEFAULT_BODY_CONTENT_KEY;
-    private boolean cacheable = true;
-    private boolean uncached = true;
+    private boolean cache = false;
 
-    public VelocityViewTag()
-    {
-        // always try to have some sort of unique id set, since
-        // this serves as a log tag and may serve as a cache name later
-        setId(DEFAULT_NAME + count++);
+    /**
+     * Release any per-invocation resources, resetting any resources or state
+     * that should be cleared between successive invocations of
+     * {@link javax.servlet.jsp.tagext.Tag#doEndTag()} and
+     * {@link javax.servlet.jsp.tagext.Tag#doStartTag()}.
+     */
+    protected void reset()
+    {
+        super.setId(null);
+        var = null;
+        scope = null;
+        template = null;
+        bodyContentKey = DEFAULT_BODY_CONTENT_KEY;
+        cache = false;
     }
 
     public void setId(String id)
     {
-        // always try to have some sort of id set because
-        // this is the log tag and cache name
         if (id == null)
         {
             throw new NullPointerException("id cannot be null");
         }
         super.setId(id);
+        // assume they want this cached
+        cache = true;
+    }
+
+    protected String getLogId()
+    {
+        String id = super.getId();
+        if (id == null)
+        {
+            id = getClass().getSimpleName();
+        }
+        return id;
     }
 
     public void setVar(String var)
@@ -123,6 +140,16 @@
         return this.bodyContentKey;
     }
 
+    public void setCache(String s)
+    {
+        this.cache = "true".equalsIgnoreCase(s);
+    }
+
+    public String getCache()
+    {
+        return String.valueOf(this.cache);
+    }
+
     public VelocityView getVelocityView()
     {
         return this.view;
@@ -143,6 +170,19 @@
         this.context = context;
     }
 
+    public StringResourceRepository getRepository()
+    {
+        if (this.repository == null)
+        {
+            setRepository(StringResourceLoader.getRepository());
+        }
+        return this.repository;
+    }
+
+    public void setRepository(StringResourceRepository repo)
+    {
+        this.repository = repo;
+    }
 
     public int doStartTag() throws JspException
     {
@@ -180,7 +220,7 @@
             catch (Exception e)
             {
                 throw new JspException("Failed to render " + getClass() +
-                                       ": "+getId(), e);
+                                       ": "+getLogId(), e);
             }
         }
         return EVAL_PAGE;
@@ -241,22 +281,34 @@
         return out.toString();
     }
 
+    protected boolean isCached()
+    {
+        return getRepository().getStringResource(getId()) != null;
+    }
+
     protected void renderBody(Writer out) throws Exception
     {
+        String name = getId();
         // if it hasn't been cached, try that
-        if (uncached && cacheable)
+        if (cache && !isCached())
         {
-            cache(getId(), getBodyContent().getString());
+            String template = getBodyContent().getString();
+            // if no id was set, use the template as the id
+            if (name == null)
+            {
+                name = template;
+            }
+            cache(name, template);
         }
         // if it can't be cached, eval it
-        if (!cacheable)
+        if (!cache)
         {
             evalBody(out);
         }
         else
         {
             // load template from cache
-            Template template = getVelocityView().getTemplate(getId());
+            Template template = getVelocityView().getTemplate(name);
             template.merge(getViewToolContext(), out);
         }
     }
@@ -264,7 +316,7 @@
     protected void evalBody(Writer out) throws Exception
     {
         VelocityEngine engine = getVelocityView().getVelocityEngine();
-        engine.evaluate(getViewToolContext(), out, getId(),
+        engine.evaluate(getViewToolContext(), out, getLogId(),
                         getBodyContent().getReader());
     }
 
@@ -293,17 +345,31 @@
         throw new IllegalArgumentException("Unknown scope: "+scope);
     }
 
-    private void cache(String name, String template)
+    protected void cache(String name, String template)
     {
         try
         {
-            StringResourceLoader.getRepository().putStringResource(name, template);
-            uncached = false;
+            getRepository().putStringResource(name, template);
         }
         catch (Exception cnfe)
         {
-            cacheable = false;
+            getVelocityView().getLog()
+                .error("Could not cache body in a StringResourceRepository", cnfe);
+            cache = false;
         }
     }
 
+    /**
+     * Release any per-instance resources, releasing any resources or state
+     * before this tag instance is disposed.
+     *
+     * @see javax.servlet.jsp.tagext.Tag#release()
+     */
+    @Override
+    public void release()
+    {
+        super.release();
+        reset();
+    }
+
 }



Mime
View raw message