velocity-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nbu...@apache.org
Subject svn commit: r745757 - in /velocity/engine/trunk/src: changes/changes.xml java/org/apache/velocity/runtime/resource/ResourceManagerImpl.java test/org/apache/velocity/test/issues/Velocity702TestCase.java
Date Thu, 19 Feb 2009 06:48:11 GMT
Author: nbubna
Date: Thu Feb 19 06:48:10 2009
New Revision: 745757

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

Added:
    velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity702TestCase.java
  (with props)
Modified:
    velocity/engine/trunk/src/changes/changes.xml
    velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/ResourceManagerImpl.java

Modified: velocity/engine/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/changes/changes.xml?rev=745757&r1=745756&r2=745757&view=diff
==============================================================================
--- velocity/engine/trunk/src/changes/changes.xml (original)
+++ velocity/engine/trunk/src/changes/changes.xml Thu Feb 19 06:48:10 2009
@@ -124,6 +124,12 @@
 
     <release version="1.6.2" date="In Subversion">
 
+      <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/trunk/src/java/org/apache/velocity/runtime/resource/ResourceManagerImpl.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/ResourceManagerImpl.java?rev=745757&r1=745756&r2=745757&view=diff
==============================================================================
--- velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/ResourceManagerImpl.java
(original)
+++ velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/ResourceManagerImpl.java
Thu Feb 19 06:48:10 2009
@@ -513,6 +513,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())
         {
             /*
@@ -535,7 +548,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();
 
@@ -550,8 +563,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);
@@ -596,17 +609,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;
     }
+
 }

Added: velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity702TestCase.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity702TestCase.java?rev=745757&view=auto
==============================================================================
--- velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity702TestCase.java
(added)
+++ velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity702TestCase.java
Thu Feb 19 06:48:10 2009
@@ -0,0 +1,99 @@
+package org.apache.velocity.test.issues;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+
+import org.apache.velocity.test.BaseTestCase;
+import org.apache.velocity.runtime.RuntimeConstants;
+import org.apache.velocity.test.misc.TestLogChute;
+import org.apache.velocity.app.VelocityEngine;
+import org.apache.velocity.runtime.resource.loader.StringResourceLoader;
+import org.apache.velocity.runtime.resource.util.StringResourceRepository;
+
+/**
+ * This class tests VELOCITY-702.
+ */
+public class Velocity702TestCase extends BaseTestCase
+{
+    public Velocity702TestCase(String name)
+    {
+        super(name);
+    }
+
+    public void setUpEngine(VelocityEngine engine)
+    {
+        engine.setProperty(RuntimeConstants.RESOURCE_LOADER, "high,low");
+        engine.addProperty("high.resource.loader.class", StringResourceLoader.class.getName());
+        engine.addProperty("high.resource.loader.cache", "false");
+        engine.addProperty("high.resource.loader.repository.name", "high");
+        engine.addProperty("high.resource.loader.repository.static", "false");
+        engine.addProperty("high.resource.loader.modificationCheckInterval", "1");
+        engine.addProperty("low.resource.loader.class", StringResourceLoader.class.getName());
+        engine.addProperty("low.resource.loader.cache", "true");
+        engine.addProperty("low.resource.loader.repository.name", "low");
+        engine.addProperty("low.resource.loader.repository.static", "false");
+        engine.addProperty("low.resource.loader.modificationCheckInterval", "1");
+        engine.init();
+    }
+
+    public void testIt() throws Exception
+    {
+        addToHigh("foo", "foo");
+        addToLow("foo", "bar");
+        assertTmplEquals("foo", "foo");
+
+        removeFromHigh("foo");
+        assertTmplEquals("bar", "foo");
+
+        Thread.sleep(1500);
+        addToHigh("foo", "woogie");
+        assertTmplEquals("woogie", "foo");
+    }
+
+    private void addToHigh(String name, String content)
+    {
+        getHighRepo().putStringResource(name, content);
+    }
+
+    private void removeFromHigh(String name)
+    {
+        getHighRepo().removeStringResource(name);
+    }
+
+    private StringResourceRepository getHighRepo()
+    {
+        return (StringResourceRepository)engine.getApplicationAttribute("high");
+    }
+
+    private void addToLow(String name, String content)
+    {
+        getLowRepo().putStringResource(name, content);
+    }
+
+    private void removeFromLow(String name)
+    {
+        getLowRepo().removeStringResource(name);
+    }
+
+    private StringResourceRepository getLowRepo()
+    {
+        return (StringResourceRepository)engine.getApplicationAttribute("low");
+    }
+
+}

Propchange: velocity/engine/trunk/src/test/org/apache/velocity/test/issues/Velocity702TestCase.java
------------------------------------------------------------------------------
    svn:executable = *



Mime
View raw message