You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by bo...@locus.apache.org on 2000/06/29 12:23:49 UTC

cvs commit: jakarta-ant/src/main/org/apache/tools/ant/taskdefs Touch.java Expand.java Untar.java defaults.properties

bodewig     00/06/29 03:23:46

  Modified:    docs     index.html
               src/main/org/apache/tools/ant Task.java
               src/main/org/apache/tools/ant/taskdefs Expand.java
                        Untar.java defaults.properties
  Added:       src/main/org/apache/tools/ant/taskdefs Touch.java
  Log:
  New task Touch. Expand now also retains the time from the Zipfile.
  
  Touch works somewhat like the Unix touch(1) command. It changes the
  modification time of files and maybe creates a new file if the
  requested one doesn't exist.
  
  In JDK 1.1 only the creation of new files will work, all other cases
  generate warning messages.
  
  Expand and Untar reuse Touch to set the modification times of the
  expanded files from the information inside the archive.
  
  I had to make Task.setProject public to ease reuse.
  
  Revision  Changes    Path
  1.33      +46 -1     jakarta-ant/docs/index.html
  
  Index: index.html
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/docs/index.html,v
  retrieving revision 1.32
  retrieving revision 1.33
  diff -u -r1.32 -r1.33
  --- index.html	2000/06/27 13:07:34	1.32
  +++ index.html	2000/06/29 10:23:27	1.33
  @@ -22,7 +22,7 @@
     <li>Sam Ruby (<a href="mailto:rubys@us.ibm.com">rubys@us.ibm.com</a>)</li>
   </ul>
   
  -<p>Version 1.0.8.1 - 2000/06/27</p>
  +<p>Version 1.0.8.1 - 2000/06/28</p>
   
   <hr>
   <h2>Table of Contents</h2>
  @@ -525,6 +525,7 @@
     <li><a href="#rmic">Rmic</a></li>
     <li><a href="#tar">Tar</a></li>
     <li><a href="#taskdef">Taskdef</a></li>
  +  <li><a href="#touch">Touch</a></li>
     <li><a href="#tstamp">Tstamp</a></li>
     <li><a href="#style">Style</a></li>
     <li><a href="#untar">Untar</a></li>
  @@ -1017,6 +1018,8 @@
   <h2><a name="expand">Expand</a></h2>
   <h3>Description</h3>
   <p>Unzips a zipfile.</p>
  +<p>For JDK 1.1 "last modified time" field is set to current time instead of being 
  +carried from zipfile.</p>
   <h3>Parameters</h3>
   <table border="1" cellpadding="2" cellspacing="0">
     <tr>
  @@ -2425,6 +2428,48 @@
   <pre>  &lt;taskdef name=&quot;myjavadoc&quot; classname=&quot;com.mydomain.JavadocTask&quot; /&gt;</pre>
   <p>makes a task called <code>myjavadoc</code> available to Ant. The class <code>com.mydomain.JavadocTask</code>
   implements the task.</p>
  +<hr>
  +<h2><a name="touch">Touch</a></h2>
  +<h3>Description</h3>
  +<p>Changes the modification time of a file and possibly creates it at
  +the same time.</p>
  +<p>For JDK 1.1 only the creation of new files with a modification time
  +of now works, all other cases will emit a warning.</p>
  +<h3>Parameters</h3>
  +<table border="1" cellpadding="2" cellspacing="0">
  +  <tr>
  +    <td valign="top"><b>Attribute</b></td>
  +    <td valign="top"><b>Description</b></td>
  +    <td align="center" valign="top"><b>Required</b></td>
  +  </tr>
  +  <tr>
  +    <td valign="top">file</td>
  +    <td valign="top">the name of the file</td>
  +    <td valign="top" align="center">Yes</td>
  +  </tr>
  +  <tr>
  +    <td valign="top">millis</td>
  +    <td valign="top">specifies the new modification time of the file
  +       in milliseconds since midnight Jan 1 1970</td>
  +    <td valign="top" align="center">No</td>
  +  </tr>
  +  <tr>
  +    <td valign="top">datetime</td>
  +    <td valign="top">specifies the new modification time of the file
  +       in the format MM/DD/YYYY HH:MM AM_or_PM.</td>
  +    <td valign="top" align="center">No</td>
  +  </tr>
  +</table>
  +<p>If both <code>millis</code> and <code>datetime</code> are ommited
  +the current time is assumed.</p>
  +<h3>Examples</h3>
  +<pre>  &lt;touch file=&quot;myfile&quot; /&gt;</pre>
  +<p>creates <code>myfile</code> if it doesn't exist and changes the
  +modification time to the current time.</p>
  +<pre>  &lt;touch file=&quot;myfile&quot; datetime=&quot;06/28/2000 2:02 pm&quot; /&gt;</pre>
  +<p>creates <code>myfile</code> if it doesn't exist and changes the
  +modification time to Jun, 28 2000 2:02 pm (14:02 for those used to 24
  +hour times).</p>
   <hr>
   <h2><a name="tstamp">Tstamp</a></h2>
   <h3>Description</h3>
  
  
  
  1.6       +1 -1      jakarta-ant/src/main/org/apache/tools/ant/Task.java
  
  Index: Task.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/Task.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- Task.java	2000/04/26 19:09:17	1.5
  +++ Task.java	2000/06/29 10:23:35	1.6
  @@ -73,7 +73,7 @@
        *
        * @param project Project in whose scope this task belongs.
        */
  -    void setProject(Project project) {
  +    public void setProject(Project project) {
           this.project = project;
       }
   
  
  
  
  1.3       +12 -0     jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Expand.java
  
  Index: Expand.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Expand.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Expand.java	2000/01/14 02:13:19	1.2
  +++ Expand.java	2000/06/29 10:23:39	1.3
  @@ -61,6 +61,7 @@
    * Unzip a file. 
    *
    * @author costin@dnt.ro
  + * @author Stefan Bodewig <a href="mailto:stefan.bodewig@megabit.net">stefan.bodewig@megabit.net</a>
    */
   public class Expand extends Task {
       private String dest; // req
  @@ -73,6 +74,10 @@
        */
       // XXX move it to util or tools
       public void execute() throws BuildException {
  +        Touch touch = new Touch();
  +        touch.setProject(project);
  +        touch.setTarget(target);
  +                    
   	try {
   	    File srcF=project.resolveFile(source);
   	    File dir=project.resolveFile(dest);
  @@ -103,6 +108,13 @@
   			
   			fos.close();
   		    }
  +
  +                    if (project.getJavaVersion() != Project.JAVA_1_1) {
  +                        touch.setFile(f.getAbsolutePath());
  +                        touch.setMillis(ze.getTime());
  +                        touch.touch();
  +                    }
  +
   		} catch( FileNotFoundException ex ) {
   		    System.out.println("FileNotFoundException: " +  ze.getName()  );
   		}
  
  
  
  1.3       +13 -31    jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Untar.java
  
  Index: Untar.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Untar.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Untar.java	2000/06/17 12:57:48	1.2
  +++ Untar.java	2000/06/29 10:23:41	1.3
  @@ -57,8 +57,6 @@
   import org.apache.tools.ant.*;
   import org.apache.tools.tar.*;
   import java.io.*;
  -import java.lang.reflect.InvocationTargetException;
  -import java.lang.reflect.Method;
   
   /**
    * Untar a file.
  @@ -78,35 +76,23 @@
        */
       public void execute() throws BuildException {
   
  -        Method setLastModified = null;
  -        Long[] times = null;
  -        // 1.0 is ruled out anyway, so this ensures 1.2 or above
  -        if (project.getJavaVersion() != Project.JAVA_1_1) {
  -            try {
  -                setLastModified = 
  -                    java.io.File.class.getMethod("setLastModified", 
  -                                                 new Class[] {Long.TYPE});
  -
  -                times = new Long[1];
  -            } catch (Exception e) {
  -                project.log("File.setLastModified(long) not found",
  -                            Project.MSG_VERBOSE);
  -            }
  -        }
  +        Touch touch = new Touch();
  +        touch.setProject(project);
  +        touch.setTarget(target);
                       
           try {
               if (source == null) {
  -                throw new BuildException("No source specified");
  +                throw new BuildException("No source specified", location);
               }
               File srcF=project.resolveFile(source);
               if (!srcF.exists()) {
  -                throw new BuildException("source doesn't exist");
  +                throw new BuildException("source "+srcF+" doesn't exist",
  +                                         location);
               }
   
               if (dest == null) {
  -                throw new BuildException("No destination specified");
  +                throw new BuildException("No destination specified", location);
               }
  -
               File dir=project.resolveFile(dest);
   
               project.log("Expanding: " + srcF + " into " + dir, Project.MSG_INFO);
  @@ -117,7 +103,8 @@
               while ((te = tis.getNextEntry()) != null) {
                   try {
                       File f = new File(dir, project.translatePath(te.getName()));
  -                    project.log("expand-file " + te.getName() , "untar", Project.MSG_VERBOSE );
  +                    project.log("expand-file " + te.getName() , "untar", 
  +                                Project.MSG_VERBOSE );
                       // create intermediary directories - sometimes tar don't add them
                       File dirF=new File(f.getParent());
                       dirF.mkdirs();
  @@ -136,15 +123,10 @@
                           fos.close();
                       }
   
  -                    if (setLastModified != null) {
  -                        times[0] = new Long(te.getModTime().getTime());
  -                        try {
  -                            setLastModified.invoke(f, times);
  -                        } catch (Exception e) {
  -                            project.log("cannot invoke File.setLastModified(long)",
  -                                        Project.MSG_VERBOSE);
  -                            setLastModified = null;
  -                        }
  +                    if (project.getJavaVersion() != Project.JAVA_1_1) {
  +                        touch.setFile(f.getAbsolutePath());
  +                        touch.setMillis(te.getModTime().getTime());
  +                        touch.touch();
                       }
   
                   } catch(FileNotFoundException ex) {
  
  
  
  1.16      +1 -0      jakarta-ant/src/main/org/apache/tools/ant/taskdefs/defaults.properties
  
  Index: defaults.properties
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/defaults.properties,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- defaults.properties	2000/06/23 16:47:04	1.15
  +++ defaults.properties	2000/06/29 10:23:41	1.16
  @@ -32,6 +32,7 @@
   patch=org.apache.tools.ant.taskdefs.Patch
   compileTask=org.apache.tools.ant.taskdefs.CompileTask
   style=org.apache.tools.ant.taskdefs.XSLTProcess
  +touch=org.apache.tools.ant.taskdefs.Touch
   
   # optional tasks
   script=org.apache.tools.ant.taskdefs.optional.Script
  
  
  
  1.1                  jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Touch.java
  
  Index: Touch.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.tools.ant.taskdefs;
  
  import org.apache.tools.ant.*;
  
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.lang.reflect.InvocationTargetException;
  import java.lang.reflect.Method;
  import java.text.DateFormat;
  import java.text.ParseException;
  import java.util.Date;
  import java.util.Locale;
  
  /**
   * Touch a file - corresponds to the Unix touch command.
   *
   * <p>If the file to touch doesn't exist, an empty one is
   * created. Setting the modification time of files is not supported in
   * JDK 1.1.
   *
   * @author Stefan Bodewig <a href="mailto:stefan.bodewig@megabit.net">stefan.bodewig@megabit.net</a> 
   */
  public class Touch extends Task {
  
      private File file;              // required
      private long millis = -1;
      private String dateTime;
  
      private static Method setLastModified = null;
      private static Object lockReflection = new Object();
  
      /**
       * The name of the file to touch.
       */
      public void setFile(String name) {
          file = project.resolveFile(name);
      }
  
      /**
       * Milliseconds since 01/01/1970 00:00 am.
       */
      public void setMillis(long millis) {
          this.millis = millis;
      }
  
      /**
       * Date in the format DD/MM/YYYY HH:MM AM_PM.
       */
      public void setDatetime(String dateTime) {
          this.dateTime = dateTime;
      }
  
      /**
       * Do the work.
       *
       * @exception BuildException Thrown in unrecoverable error.
       */
      public void execute() throws BuildException {
          if (file.exists() && project.getJavaVersion() == Project.JAVA_1_1) {
              project.log("Cannot change the modification time of "
                          + file + " in JDK 1.1",
                          Project.MSG_WARN);
              return;
          }
          
          if (dateTime != null) {
              DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT,
                                                             DateFormat.SHORT,
                                                             Locale.US);
              try {
                  setMillis(df.parse(dateTime).getTime());
              } catch (ParseException pe) {
                  throw new BuildException(pe.getMessage(), pe, location);
              }
          }
  
          if (millis >= 0 && project.getJavaVersion() == Project.JAVA_1_1) {
              project.log(file + 
                          " will be created but its modification time cannot be set in JDK 1.1",
                          Project.MSG_WARN);
          }
  
          touch();
      }
  
      /**
       * Does the actual work. Entry point for Untar and Expand as well.
       */
      void touch() throws BuildException {
          if (!file.exists()) {
              project.log("Creating "+file, Project.MSG_INFO);
              try {
                  FileOutputStream fos = new FileOutputStream(file);
                  fos.write(new byte[0]);
                  fos.close();
              } catch (IOException ioe) {
                  throw new BuildException("Could not create "+file, ioe, 
                                           location);
              }
          }
  
          if (project.getJavaVersion() == Project.JAVA_1_1) {
              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, location);
                      }
                  }
              }
          }
          
          Long[] times = new Long[1];
          if (millis < 0) {
              times[0] = new Long(System.currentTimeMillis());
          } else {
              times[0] = new Long(millis);
          }
  
          try {
              project.log("Setting modification time for "+file, 
                          Project.MSG_VERBOSE);
  
              setLastModified.invoke(file, times);
          } catch (InvocationTargetException ite) {
              Throwable nested = ite.getTargetException();
              throw new BuildException("Exception setting the modification time of "
                                       + file, nested, location);
          } catch (Throwable other) {
              throw new BuildException("Exception setting the modification time of "
                                       + file, other, location);
          }
      }
  
  }