donaldp     00/12/15 01:49:32

  Modified:    src/main/org/apache/tools/ant Project.java
               src/main/org/apache/tools/ant/taskdefs Copy.java
  Log:
  Added the attribute preservelastmodified to copy task.
  
  Sunmitted By: Jaco de Groot <[EMAIL PROTECTED]>
  
  Revision  Changes    Path
  1.49      +81 -2     jakarta-ant/src/main/org/apache/tools/ant/Project.java
  
  Index: Project.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/Project.java,v
  retrieving revision 1.48
  retrieving revision 1.49
  diff -u -r1.48 -r1.49
  --- Project.java      2000/11/28 02:51:45     1.48
  +++ Project.java      2000/12/15 09:49:29     1.49
  @@ -107,6 +107,9 @@
   
       private Vector listeners = new Vector();
   
  +    private static java.lang.reflect.Method setLastModified = null;
  +    private static Object lockReflection = new Object();
  +
       static {
   
           // Determine the Java version by looking at available classes
  @@ -672,6 +675,22 @@
                    overwrite);
       }
   
  +     /**
  +     * Convienence method to copy a file from a source to a
  +     * destination specifying if token filtering must be used, if
  +     * source files may overwrite newer destination files and the
  +     * last modified time of <code>destFile</code> file should be made equal
  +     * to the last modified time of <code>sourceFile</code>.
  +     *
  +     * @throws IOException 
  +     */
  +    public void copyFile(String sourceFile, String destFile, boolean 
filtering,
  +                         boolean overwrite, boolean preserveLastModified)
  +        throws IOException {
  +        copyFile(new File(sourceFile), new File(destFile), filtering, 
  +                 overwrite, preserveLastModified);
  +    }
  +
       /**
        * Convienence method to copy a file from a source to a destination.
        * No filtering is performed.
  @@ -689,8 +708,7 @@
        * @throws IOException
        */
       public void copyFile(File sourceFile, File destFile, boolean filtering)
  -        throws IOException
  -    {
  +        throws IOException {
           copyFile(sourceFile, destFile, filtering, false);
       }
   
  @@ -703,7 +721,22 @@
        */
       public void copyFile(File sourceFile, File destFile, boolean filtering,
                            boolean overwrite) throws IOException {
  +        copyFile(sourceFile, destFile, filtering, overwrite, false);
  +    }
   
  +    /**
  +     * Convienence method to copy a file from a source to a
  +     * destination specifying if token filtering must be used, if
  +     * source files may overwrite newer destination files and the
  +     * last modified time of <code>destFile</code> file should be made equal
  +     * to the last modified time of <code>sourceFile</code>.
  +     *
  +     * @throws IOException 
  +     */
  +    public void copyFile(File sourceFile, File destFile, boolean filtering,
  +                         boolean overwrite, boolean preserveLastModified)
  +        throws IOException {
  +        
           if (overwrite ||
               destFile.lastModified() < sourceFile.lastModified()) {
               log("Copy: " + sourceFile.getAbsolutePath() + " > "
  @@ -750,6 +783,52 @@
                   in.close();
                   out.close();
               }
  +
  +            if (preserveLastModified) {
  +                setFileLastModified(destFile, sourceFile.lastModified());
  +            }
  +        }
  +    }
  +
  +    /**
  +     * Calls File.setLastModified(long time) in a Java 1.1 compatible way.
  +     */
  +    void setFileLastModified(File file, long time) throws BuildException {
  +        if (getJavaVersion() == JAVA_1_1) {
  +            log("Cannot change the modification time of " + file
  +                + " in JDK 1.1", Project.MSG_WARN);
  +            return;
  +        }
  +        if (setLastModified == null) {
  +            synchronized (lockReflection) {
  +                if (setLastModified == null) {
  +                    try {
  +                        setLastModified = 
  +                            java.io.File.class.getMethod("setLastModified", 
  +                                                         new Class[] 
{Long.TYPE});
  +                    } catch (NoSuchMethodException nse) {
  +                        throw new BuildException("File.setlastModified not 
in JDK > 1.1?",
  +                                                 nse);
  +                    }
  +                }
  +            }
  +        }
  +        Long[] times = new Long[1];
  +        if (time < 0) {
  +            times[0] = new Long(System.currentTimeMillis());
  +        } else {
  +            times[0] = new Long(time);
  +        }
  +        try {
  +            log("Setting modification time for " + file, MSG_VERBOSE);
  +            setLastModified.invoke(file, times);
  +        } catch (java.lang.reflect.InvocationTargetException ite) {
  +            Throwable nested = ite.getTargetException();
  +            throw new BuildException("Exception setting the modification 
time "
  +                                     + "of " + file, nested);
  +        } catch (Throwable other) {
  +            throw new BuildException("Exception setting the modification 
time "
  +                                     + "of " + file, other);
           }
       }
   
  
  
  
  1.12      +10 -1     
jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Copy.java
  
  Index: Copy.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Copy.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- Copy.java 2000/12/07 13:13:32     1.11
  +++ Copy.java 2000/12/15 09:49:30     1.12
  @@ -81,6 +81,7 @@
       protected Vector filesets = new Vector();
   
       protected boolean filtering = false;
  +    protected boolean preserveLastModified = false;
       protected boolean forceOverwrite = false;
       protected boolean flatten = false;
       protected int verbosity = Project.MSG_VERBOSE;
  @@ -113,6 +114,13 @@
       }
   
       /**
  +     * Give the copied files the same last modified time as the original 
files.
  +     */
  +    public void setPreserveLastModified(String preserve) {
  +        preserveLastModified = Project.toBoolean(preserve);
  +    }
  +
  +    /**
        * Sets filtering.
        */
       public void setFiltering(boolean filtering) {
  @@ -327,7 +335,8 @@
                       project.copyFile(fromFile, 
                                        toFile, 
                                        filtering, 
  -                                     forceOverwrite);
  +                                     forceOverwrite,
  +                                     preserveLastModified);
                   } catch (IOException ioe) {
                       String msg = "Failed to copy " + fromFile + " to " + 
toFile
                           + " due to " + ioe.getMessage();
  
  
  

Reply via email to