You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by dl...@apache.org on 2003/02/18 19:41:37 UTC

cvs commit: jakarta-velocity/src/java/org/apache/velocity/runtime/resource/loader FileResourceLoader.java

dlr         2003/02/18 10:41:37

  Modified:    src/java/org/apache/velocity/runtime/resource/loader Tag:
                        VEL_1_3_BRANCH FileResourceLoader.java
  Log:
  Backported a fairly significant bug fix and two optimizations from the
  1.4 branch to 1.3:
  
  * src/java/org/apache/velocity/runtime/resource/loader/FileResourceLoader.java
  
    (revision 1.18): We have arranged to use the search paths to allow
    overrides to be made without changing the original versions of the templates.
    However, when we turn on Velocity caching, the search paths are
    ignored so long as the file is not modified.
    http://nagoya.apache.org/bugzilla/show_bug.cgi?id=15717
  
    (revision 1.17): FileReousrceLoader currently repeats argument
    validation within its search loop. Moving this outside would
    optimize performance and memory consumption (simple change, but diff
    seems to make it look complicated).
  
    (revision 1.16): Slimmed down implementation of isSourceModified(Resource).
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.15.2.1  +78 -55    jakarta-velocity/src/java/org/apache/velocity/runtime/resource/loader/FileResourceLoader.java
  
  Index: FileResourceLoader.java
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/resource/loader/FileResourceLoader.java,v
  retrieving revision 1.15
  retrieving revision 1.15.2.1
  diff -u -u -r1.15 -r1.15.2.1
  --- FileResourceLoader.java	7 Feb 2002 06:21:14 -0000	1.15
  +++ FileResourceLoader.java	18 Feb 2003 18:41:36 -0000	1.15.2.1
  @@ -124,48 +124,44 @@
       public synchronized InputStream getResourceStream(String templateName)
           throws ResourceNotFoundException
       {
  -        String template = null;
  -        int size = paths.size();
  -
  -        for (int i = 0; i < size; i++)
  +        /*
  +         * Make sure we have a valid templateName.
  +         */
  +        if (templateName == null || templateName.length() == 0)
           {
  -            String path = (String) paths.get(i);
  -        
               /*
  -             * Make sure we have a valid templateName.
  +             * If we don't get a properly formed templateName then
  +             * there's not much we can do. So we'll forget about
  +             * trying to search any more paths for the template.
                */
  -            if (templateName == null || templateName.length() == 0)
  -            {
  -                /*
  -                 * If we don't get a properly formed templateName
  -                 * then there's not much we can do. So
  -                 * we'll forget about trying to search
  -                 * any more paths for the template.
  -                 */
  -                throw new ResourceNotFoundException(
  -                    "Need to specify a file name or file path!");
  -            }
  +            throw new ResourceNotFoundException(
  +                "Need to specify a file name or file path!");
  +        }
   
  -            template = StringUtils.normalizePath(templateName);
  -            if ( template == null || template.length() == 0 )
  -            {
  -                String msg = "File resource error : argument " + template + 
  -                    " contains .. and may be trying to access " + 
  -                    "content outside of template root.  Rejected.";
  +        String template = StringUtils.normalizePath(templateName);
  +        if ( template == null || template.length() == 0 )
  +        {
  +            String msg = "File resource error : argument " + template + 
  +                " contains .. and may be trying to access " + 
  +                "content outside of template root.  Rejected.";
   
  -                rsvc.error( "FileResourceLoader : " + msg );
  +            rsvc.error( "FileResourceLoader : " + msg );
         
  -                throw new ResourceNotFoundException ( msg );
  -            }
  +            throw new ResourceNotFoundException ( msg );
  +        }
   
  -            /*
  -             *  if a / leads off, then just nip that :)
  -             */
  -            if ( template.startsWith("/") )
  -            {
  -                template = template.substring(1);
  -            }
  +        /*
  +         *  if a / leads off, then just nip that :)
  +         */
  +        if (template.startsWith("/"))
  +        {
  +            template = template.substring(1);
  +        }
   
  +        int size = paths.size();
  +        for (int i = 0; i < size; i++)
  +        {
  +            String path = (String) paths.get(i);
               InputStream inputStream = findTemplate(path, template);
               
               if (inputStream != null)
  @@ -226,34 +222,61 @@
       
       /**
        * How to keep track of all the modified times
  -     * across the paths.
  +     * across the paths.  Note that a file might have
  +     * appeared in a directory which is earlier in the
  +     * path; so we should search the path and see if
  +     * the file we find that way is the same as the one
  +     * that we have cached.
        */
       public boolean isSourceModified(Resource resource)
       {
  -        String path = (String) templatePaths.get(resource.getName());
  -        File file = new File( path, resource.getName() );           
  -        
  -        if ( file.canRead() )
  +        /*
  +         * we assume that the file needs to be reloaded; 
  +         * if we find the original file and it's unchanged,
  +         * then we'll flip this.
  +         */
  +        boolean modified = true;
  +
  +        String fileName = resource.getName();
  +        String path = (String) templatePaths.get(fileName);
  +        File currentFile = null;
  +
  +        for (int i = 0; currentFile == null && i < paths.size(); i++)
           {
  -            if (file.lastModified() != resource.getLastModified())
  -            {
  -                return true;
  -            }                
  -            else
  +            String testPath = (String) paths.get(i);
  +            File testFile = new File(testPath, fileName);
  +            if (testFile.canRead())
               {
  -                return false;
  -            }                
  +                currentFile = testFile;
  +            }
           }
  -        
  +        File file = new File(path, fileName);
  +        if (currentFile == null || !file.exists())
  +        {
  +            /*
  +             * noop: if the file is missing now (either the cached
  +             * file is gone, or the file can no longer be found)
  +             * then we leave modified alone (it's set to true); a 
  +             * reload attempt will be done, which will either use
  +             * a new template or fail with an appropriate message
  +             * about how the file couldn't be found.
  +             */
  +        }
  +        else if (currentFile.equals(file) && file.canRead())
  +        {
  +            /*
  +             * if only if currentFile is the same as file and
  +             * file.lastModified() is the same as
  +             * resource.getLastModified(), then we should use the
  +             * cached version.
  +             */
  +            modified = (file.lastModified() != resource.getLastModified());
  +        }
  +
           /*
  -         * If the file is now unreadable, or it has
  -         * just plain disappeared then we'll just say
  -         * that it's modified :-) When the loader attempts
  -         * to load the stream it will fail and the error
  -         * will be reported then.
  +         * rsvc.debug("isSourceModified for " + fileName + ": " + modified);
            */
  -        
  -        return true;
  +        return modified;
       }
   
       public long getLastModified(Resource resource)
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org