You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by cz...@apache.org on 2002/06/03 14:11:10 UTC

cvs commit: jakarta-avalon-excalibur/sourceresolve/src/java/org/apache/excalibur/source/impl/validity FileTimeStampValidity.java

cziegeler    2002/06/03 05:11:10

  Modified:    sourceresolve/src/java/org/apache/excalibur/source/impl
                        URLSource.java
  Added:       sourceresolve/src/java/org/apache/excalibur/source/impl/validity
                        FileTimeStampValidity.java
  Log:
  Performance improvements submitted by Volker Schmitt [volker.schmitt@basf-it-services.com]
  
  Revision  Changes    Path
  1.11      +38 -16    jakarta-avalon-excalibur/sourceresolve/src/java/org/apache/excalibur/source/impl/URLSource.java
  
  Index: URLSource.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/sourceresolve/src/java/org/apache/excalibur/source/impl/URLSource.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- URLSource.java	24 May 2002 09:09:29 -0000	1.10
  +++ URLSource.java	3 Jun 2002 12:11:10 -0000	1.11
  @@ -24,13 +24,14 @@
   import org.apache.excalibur.source.SourceParameters;
   import org.apache.excalibur.source.SourceUtil;
   import org.apache.excalibur.source.SourceValidity;
  +import org.apache.excalibur.source.impl.validity.FileTimeStampValidity;
   import org.apache.excalibur.source.impl.validity.TimeStampValidity;
   
   /**
    * Description of a source which is described by an URL.
    *
    * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
  - * @version CVS $Revision: 1.10 $ $Date: 2002/05/24 09:09:29 $
  + * @version CVS $Revision: 1.11 $ $Date: 2002/06/03 12:11:10 $
    */
   
   public class URLSource
  @@ -62,8 +63,8 @@
       /** The connection for a real URL */
       protected URLConnection connection;
   
  -    /** Is this a file or a "real" URL */
  -    protected boolean isFile;
  +    /** The file, if URL is a file */
  +    protected File file;
   
       /** Are we initialized? */
       protected boolean gotInfos;
  @@ -74,6 +75,12 @@
       /** Is this a post? */
       protected boolean isPost = false;
   
  +
  +    /** the prev returned SourceValidity */
  +    protected SourceValidity cachedValidity;
  +
  +    protected long cachedLastModificationDate;
  +
       /**
        * Constructor
        */
  @@ -90,7 +97,14 @@
           throws IOException
       {
           this.systemId = url.toExternalForm();
  -        this.isFile = systemId.startsWith( FILE );
  +        if (systemId.startsWith( FILE ))
  +        {
  +            this.file = new File( this.systemId.substring( FILE.length() ) );
  +        }
  +        else
  +        {
  +            this.file = null;
  +        }
           this.url = url;
           this.gotInfos = false;
           this.isPost = false;
  @@ -101,7 +115,7 @@
               if( "POST".equalsIgnoreCase( method ) )
                   this.isPost = true;
           }
  -        if( !isFile
  +        if( null == this.file
               && null != this.parameters
               && this.parameters.hasParameters()
               && !this.isPost )
  @@ -140,10 +154,9 @@
       {
           if( !this.gotInfos )
           {
  -            if( this.isFile )
  +            if( null != this.file )
               {
  -                File file = new File( this.systemId.substring( FILE.length() ) );
  -                this.lastModificationDate = file.lastModified();
  +                this.lastModificationDate = this.file.lastModified();
               }
               else
               {
  @@ -201,9 +214,9 @@
           {
               getInfos();
               InputStream input = null;
  -            if( this.isFile == true )
  +            if( null != this.file )
               {
  -                input = new FileInputStream( this.systemId.substring( FILE.length() ) );
  +                input = new FileInputStream( this.file );
               }
               else
               {
  @@ -337,14 +350,23 @@
       public SourceValidity getValidity()
       {
           final long lm = this.getLastModified();
  -        if( lm == -1 )
  -        {
  -            return null;
  -        }
  -        else
  +        if( lm > 0 )
           {
  -            return new TimeStampValidity( lm );
  +            if (lm == this.cachedLastModificationDate)
  +                return this.cachedValidity;
  +
  +            this.cachedLastModificationDate = lm;
  +            if (file != null)
  +            {
  +                this.cachedValidity = new FileTimeStampValidity(file, lm);
  +            }
  +            else
  +            {
  +                this.cachedValidity = new TimeStampValidity( lm );
  +            }
  +            return this.cachedValidity;
           }
  +        return null;
       }
   
       /**
  
  
  
  1.1                  jakarta-avalon-excalibur/sourceresolve/src/java/org/apache/excalibur/source/impl/validity/FileTimeStampValidity.java
  
  Index: FileTimeStampValidity.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.excalibur.source.impl.validity;
  
  import java.io.File;
  
  import org.apache.excalibur.source.SourceValidity;
  
  /**
   * A validation object for time-stamps.
   *
   * @author: <a href="mailto:volker.schmitt@basf-it-services.com">Volker Schmitt</a> 
   * @version CVS $Revision: 1.1 $
   */
  public final class FileTimeStampValidity
      implements SourceValidity
  {
  
      private long timeStamp;
      private File file;
  
      public FileTimeStampValidity( String filename )
      {
          this(new File(filename));
      }
      
      public FileTimeStampValidity( File file )
      {
          this(file, file.lastModified());
      }
      
      public FileTimeStampValidity( File file, long  timeStamp )
      {
          this.file = file;
          this.timeStamp = timeStamp;
      }
  
      /**
       * Check if the component is still valid.
       * If <code>false</code> is returned the isValid(SourceValidity) must be
       * called afterwards!
       */
      public boolean isValid()
      {
          return file.lastModified() == this.timeStamp;
      }
  
      public boolean isValid( SourceValidity newValidity )
      {
          if( newValidity instanceof FileTimeStampValidity)
          {
              return this.timeStamp == ( (FileTimeStampValidity)newValidity ).getTimeStamp();
          }
          return false;
      }
      
      public File getFile()
      {
          return this.file;
      }
  
      public long getTimeStamp()
      {
          return this.timeStamp;
      }
  
      public String toString()
      {
          return "FileTimeStampValidity: " + file.getPath() + ": " + this.timeStamp;
      }
  
  }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>