Author: nbubna
Date: Thu Feb 19 07:00:42 2009
New Revision: 745760

URL: http://svn.apache.org/viewvc?rev=745760&view=rev
Log:
VELOCITY-702 fix obscure caching issue w/multiple resource loaders and 
resources that come and go

Added:
    
velocity/engine/branches/1.6.x/src/test/org/apache/velocity/test/issues/Velocity702TestCase.java
      - copied, changed from r745757, 
velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity702TestCase.java
Modified:
    velocity/engine/branches/1.6.x/src/changes/changes.xml
    
velocity/engine/branches/1.6.x/src/java/org/apache/velocity/runtime/resource/ResourceManagerImpl.java
    
velocity/engine/branches/1.6.x/src/test/org/apache/velocity/test/BaseEvalTestCase.java

Modified: velocity/engine/branches/1.6.x/src/changes/changes.xml
URL: 
http://svn.apache.org/viewvc/velocity/engine/branches/1.6.x/src/changes/changes.xml?rev=745760&r1=745759&r2=745760&view=diff
==============================================================================
--- velocity/engine/branches/1.6.x/src/changes/changes.xml (original)
+++ velocity/engine/branches/1.6.x/src/changes/changes.xml Thu Feb 19 07:00:42 
2009
@@ -28,6 +28,12 @@
 
     <release version="1.6.2" date="2009-02-21">
 
+      <action type="fix" dev="nbubna" issue="VELOCITY-702">
+        Fix obscure caching problem in multiple resource loader situations
+        where resources may exist in more than one loader and appear and
+        disappear from loaders.
+      </action>
+
       <action type="fix" dev="nbubna" issue="VELOCITY-701">
         Fix old regression from 1.4 in supporting methods declared as abstract
         in a public class but implemented in a non-public class.

Modified: 
velocity/engine/branches/1.6.x/src/java/org/apache/velocity/runtime/resource/ResourceManagerImpl.java
URL: 
http://svn.apache.org/viewvc/velocity/engine/branches/1.6.x/src/java/org/apache/velocity/runtime/resource/ResourceManagerImpl.java?rev=745760&r1=745759&r2=745760&view=diff
==============================================================================
--- 
velocity/engine/branches/1.6.x/src/java/org/apache/velocity/runtime/resource/ResourceManagerImpl.java
 (original)
+++ 
velocity/engine/branches/1.6.x/src/java/org/apache/velocity/runtime/resource/ResourceManagerImpl.java
 Thu Feb 19 07:00:42 2009
@@ -525,6 +525,19 @@
          */
         resource.touch();
 
+        /* check whether this can now be found in a higher priority
+         * resource loader.  if so, pass the request off to loadResource.
+         */
+        ResourceLoader loader = resource.getResourceLoader();
+        if (resourceLoaders.size() > 0 && resourceLoaders.indexOf(loader) > 0)
+        {
+            String name = resource.getName();
+            if (loader != getLoaderForResource(name))
+            {
+                return loadResource(name, resource.getType(), encoding);
+            }
+        }
+
         if (resource.isSourceModified())
         {
             /*
@@ -547,7 +560,7 @@
              *  read how old the resource is _before_
              *  processing (=>reading) it
              */
-            long howOldItWas = 
resource.getResourceLoader().getLastModified(resource);
+            long howOldItWas = loader.getLastModified(resource);
 
             String resourceKey = resource.getType() + resource.getName();
 
@@ -562,8 +575,8 @@
             newResource.setRuntimeServices(rsvc);
             newResource.setName(resource.getName());
             newResource.setEncoding(resource.getEncoding());
-            newResource.setResourceLoader(resource.getResourceLoader());
-            
newResource.setModificationCheckInterval(resource.getResourceLoader().getModificationCheckInterval());
+            newResource.setResourceLoader(loader);
+            
newResource.setModificationCheckInterval(loader.getModificationCheckInterval());
 
             newResource.process();
             newResource.setLastModified(howOldItWas);
@@ -608,17 +621,29 @@
      */
     public String getLoaderNameForResource(String resourceName)
     {
-        /*
-         *  loop through our loaders...
-         */
-        for (Iterator it = resourceLoaders.iterator(); it.hasNext(); )
+        ResourceLoader loader = getLoaderForResource(resourceName);
+        if (loader == null)
         {
-            ResourceLoader resourceLoader = (ResourceLoader) it.next();
-            if (resourceLoader.resourceExists(resourceName))
+            return null;
+        }
+        return loader.getClass().toString();
+    }
+
+    /**
+     * Returns the first {...@link ResourceLoader} in which the specified
+     * resource exists.
+     */
+    private ResourceLoader getLoaderForResource(String resourceName)
+    {
+        for (Iterator i = resourceLoaders.iterator(); i.hasNext(); )
+        {
+            ResourceLoader loader = (ResourceLoader)i.next();
+            if (loader.resourceExists(resourceName))
             {
-                return resourceLoader.getClass().toString();
+                return loader;
             }
         }
         return null;
     }
+
 }

Modified: 
velocity/engine/branches/1.6.x/src/test/org/apache/velocity/test/BaseEvalTestCase.java
URL: 
http://svn.apache.org/viewvc/velocity/engine/branches/1.6.x/src/test/org/apache/velocity/test/BaseEvalTestCase.java?rev=745760&r1=745759&r2=745760&view=diff
==============================================================================
--- 
velocity/engine/branches/1.6.x/src/test/org/apache/velocity/test/BaseEvalTestCase.java
 (original)
+++ 
velocity/engine/branches/1.6.x/src/test/org/apache/velocity/test/BaseEvalTestCase.java
 Thu Feb 19 07:00:42 2009
@@ -200,4 +200,43 @@
             throw new RuntimeException(e);
         }
     }
+
+    /**
+     * Compare an expected string with the given loaded template
+     */
+    protected void assertTmplEquals(String expected, String template)
+    {        
+        if (DEBUG)
+        {
+            engine.getLog().info("Expected:  '" + expected + "'");
+        }
+
+        StringWriter writer = new StringWriter();
+        try
+        {          
+            engine.mergeTemplate(template, "utf-8", context, writer);
+        }
+        catch (RuntimeException re)
+        {
+            if (DEBUG)
+            {
+                engine.getLog().info("RuntimeException!", re);
+            }
+            throw re;
+        }
+        catch (Exception e)
+        {
+            if (DEBUG)
+            {
+                engine.getLog().info("Exception!", e);
+            }
+            throw new RuntimeException(e);
+        }        
+
+        if (DEBUG)
+        {
+            engine.getLog().info("Result:  '" + writer.toString() + "'");
+        }
+        assertEquals(expected, writer.toString());  
+    }
 }

Copied: 
velocity/engine/branches/1.6.x/src/test/org/apache/velocity/test/issues/Velocity702TestCase.java
 (from r745757, 
velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity702TestCase.java)
URL: 
http://svn.apache.org/viewvc/velocity/engine/branches/1.6.x/src/test/org/apache/velocity/test/issues/Velocity702TestCase.java?p2=velocity/engine/branches/1.6.x/src/test/org/apache/velocity/test/issues/Velocity702TestCase.java&p1=velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity702TestCase.java&r1=745757&r2=745760&rev=745760&view=diff
==============================================================================
--- 
velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity702TestCase.java
 (original)
+++ 
velocity/engine/branches/1.6.x/src/test/org/apache/velocity/test/issues/Velocity702TestCase.java
 Thu Feb 19 07:00:42 2009
@@ -19,7 +19,7 @@
  * under the License.    
  */
 
-import org.apache.velocity.test.BaseTestCase;
+import org.apache.velocity.test.BaseEvalTestCase;
 import org.apache.velocity.runtime.RuntimeConstants;
 import org.apache.velocity.test.misc.TestLogChute;
 import org.apache.velocity.app.VelocityEngine;
@@ -29,15 +29,17 @@
 /**
  * This class tests VELOCITY-702.
  */
-public class Velocity702TestCase extends BaseTestCase
+public class Velocity702TestCase extends BaseEvalTestCase
 {
     public Velocity702TestCase(String name)
     {
         super(name);
+        //DEBUG = true;
     }
 
-    public void setUpEngine(VelocityEngine engine)
+    public void setUp() throws Exception
     {
+        super.setUp();
         engine.setProperty(RuntimeConstants.RESOURCE_LOADER, "high,low");
         engine.addProperty("high.resource.loader.class", 
StringResourceLoader.class.getName());
         engine.addProperty("high.resource.loader.cache", "false");


Reply via email to