velocity-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nbu...@apache.org
Subject svn commit: r687469 - in /velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource: ResourceManagerImpl.java loader/FileResourceLoader.java loader/ResourceLoader.java loader/StringResourceLoader.java
Date Wed, 20 Aug 2008 21:16:21 GMT
Author: nbubna
Date: Wed Aug 20 14:16:21 2008
New Revision: 687469

URL: http://svn.apache.org/viewvc?rev=687469&view=rev
Log:
VELOCITY-439 improve resource existence checking

Modified:
    velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/ResourceManagerImpl.java
    velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/loader/FileResourceLoader.java
    velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/loader/ResourceLoader.java
    velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/loader/StringResourceLoader.java

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=687469&r1=687468&r2=687469&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
Wed Aug 20 14:16:21 2008
@@ -602,48 +602,11 @@
         for (Iterator it = resourceLoaders.iterator(); it.hasNext(); )
         {
             ResourceLoader resourceLoader = (ResourceLoader) it.next();
-
-            InputStream is = null;
-
-            /*
-             *  if we find one that can provide the resource,
-             *  return the name of the loaders's Class
-             */
-            try
+            if (resourceLoader.resourceExists(resourceName))
             {
-                is = resourceLoader.getResourceStream(resourceName);
-
-                if (is != null)
-                {
-                    return resourceLoader.getClass().toString();
-                }
-            }
-            catch (ResourceNotFoundException rnfe)
-            {
-                /*
-                 * this isn't a problem.  keep going
-                 */
-            }
-            finally
-            {
-
-                /*
-                 *  if we did find one, clean up because we were
-                 *  returned an open stream
-                 */
-                if (is != null)
-                {
-
-                    try
-                    {
-                        is.close();
-                    }
-                    catch (IOException supressed)
-                    { }
-                }
+                return resourceLoader.getClass().toString();
             }
         }
-
         return null;
     }
 }

Modified: velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/loader/FileResourceLoader.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/loader/FileResourceLoader.java?rev=687469&r1=687468&r2=687469&view=diff
==============================================================================
--- velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/loader/FileResourceLoader.java
(original)
+++ velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/loader/FileResourceLoader.java
Wed Aug 20 14:16:21 2008
@@ -179,6 +179,39 @@
     }
 
     /**
+     * Overrides superclass for better performance.
+     */
+    public boolean resourceExists(String name)
+    {
+        if (name == null)
+        {
+            return false;
+        }
+        name = StringUtils.normalizePath(name);
+        if (name == null || name.length() == 0)
+        {
+            return false;
+        }
+
+        int size = paths.size();
+        for (int i = 0; i < size; i++)
+        {
+            String path = (String)paths.get(i);
+            try
+            {
+                File file = getFile(path, name);
+                return file.canRead();
+            }
+            catch (Exception ioe)
+            {
+                String msg = "Exception while checking for template " + name;
+                log.debug(msg, ioe);
+            }
+        }
+        return false;
+    }
+
+    /**
      * Try to find a template given a normalized path.
      *
      * @param path a normalized path

Modified: velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/loader/ResourceLoader.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/loader/ResourceLoader.java?rev=687469&r1=687468&r2=687469&view=diff
==============================================================================
--- velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/loader/ResourceLoader.java
(original)
+++ velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/loader/ResourceLoader.java
Wed Aug 20 14:16:21 2008
@@ -206,4 +206,51 @@
     {
         return modificationCheckInterval;
     }
+
+    /**
+     * Check whether any given resource exists. This is not really
+     * a very efficient test and it can and should be overridden in the
+     * subclasses extending ResourceLoader. 
+     *
+     * @param resourceName The name of a resource.
+     * @return true if a resource exists and can be accessed.
+     */
+    public boolean resourceExists(final String resourceName)
+    {
+        InputStream is = null;
+        try
+        {
+            is = getResourceStream(resourceName);
+        }
+        catch (ResourceNotFoundException e)
+        {
+            if (log.isDebugEnabled())
+            {
+                log.debug("Could not load resource '" + resourceName 
+                        + "' from ResourceLoader " + this.getClass().getName() 
+                        + ": ", e);
+            }
+        }
+        finally
+        {
+            try
+            {
+                if (is != null)
+                {
+                    is.close();
+                }
+            }
+            catch (Exception e)
+            {
+                if (log.isErrorEnabled())
+                {
+                    String msg = "While closing InputStream for resource '" + resourceName
+                        + "' from ResourceLoader "+this.getClass().getName();
+                    log.error(msg, e);
+                    throw new VelocityException(msg, e);
+                }
+            }
+        }
+        return (is != null);
+    }
 }

Modified: velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/loader/StringResourceLoader.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/loader/StringResourceLoader.java?rev=687469&r1=687468&r2=687469&view=diff
==============================================================================
--- velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/loader/StringResourceLoader.java
(original)
+++ velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/loader/StringResourceLoader.java
Wed Aug 20 14:16:21 2008
@@ -323,6 +323,17 @@
         return repo;
     }
 
+    /**
+     * Overrides superclass for better performance.
+     */
+    public boolean resourceExists(final String name)
+    {
+        if (name == null)
+        {
+            return false;
+        }
+        return (this.repository.getStringResource(name) != null);
+    }
 
     /**
      * Get an InputStream so that the Runtime can build a



Mime
View raw message