ant-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r813779 - /ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/ArchiveBase.java
Date Fri, 11 Sep 2009 11:28:33 GMT
Author: bodewig
Date: Fri Sep 11 11:28:31 2009
New Revision: 813779

URL: http://svn.apache.org/viewvc?rev=813779&view=rev
Log:
prepare for replace and update modes by copying target archive, tiny extract method refactoring

Modified:
    ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/ArchiveBase.java

Modified: ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/ArchiveBase.java
URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/ArchiveBase.java?rev=813779&r1=813778&r2=813779&view=diff
==============================================================================
--- ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/ArchiveBase.java
(original)
+++ ant/sandbox/antlibs/compress/trunk/src/main/org/apache/ant/compress/taskdefs/ArchiveBase.java
Fri Sep 11 11:28:31 2009
@@ -87,6 +87,8 @@
 
     private static final String NO_SOURCES_MSG = "No sources, nothing to do.";
 
+    private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
+
     protected ArchiveBase() {}
 
     protected final void setFactory(ArchiveStreamFactory factory) {
@@ -251,7 +253,8 @@
 
     public void execute() {
         validate();
-        if (!getDest().isExists()) {
+        final Resource targetArchive = getDest();
+        if (!targetArchive.isExists()) {
             // force create mode
             mode = new Mode();
             mode.setValue(Mode.FORCE_CREATE);
@@ -269,20 +272,24 @@
                 throw new BuildException(NO_SOURCES_MSG);
             }
         } else {
+            File copyOfDest = maybeCopyTarget();
+            Resource destOrCopy = copyOfDest == null
+                ? targetArchive
+                : new FileResource(copyOfDest);
             try {
-                if (!Mode.FORCE_CREATE.equals(mode.getValue())
-                    && !Mode.FORCE_REPLACE.equals(mode.getValue())
-                    && isUpToDate(toAdd, getDest())) {
-                    log(getDest() + " is up-to-date, nothing to do.");
+                    
+                if (checkAndLogUpToDate(toAdd, destOrCopy)) {
                     return;
                 }
-            } catch (IOException ioex) {
-                throw new BuildException("Failed to read target archive", ioex);
-            }
-            try {
-                writeArchive(toAdd);
-            } catch (IOException ioex) {
-                throw new BuildException("Failed to write archive", ioex);
+                try {
+                    writeArchive(toAdd);
+                } catch (IOException ioex) {
+                    throw new BuildException("Failed to write archive", ioex);
+                }
+            } finally {
+                if (copyOfDest != null) {
+                    FILE_UTILS.tryHardToDelete(copyOfDest);
+                }
             }
         }
     }
@@ -346,6 +353,21 @@
         return (ResourceWithFlags[]) l.toArray(new ResourceWithFlags[l.size()]);
     }
 
+    private boolean checkAndLogUpToDate(ResourceWithFlags[] src,
+                                        Resource targetArchive) {
+        try {
+            if (!Mode.FORCE_CREATE.equals(mode.getValue())
+                && !Mode.FORCE_REPLACE.equals(mode.getValue())
+                && isUpToDate(src, targetArchive)) {
+                log(targetArchive + " is up-to-date, nothing to do.");
+                return true;
+            }
+        } catch (IOException ioex) {
+            throw new BuildException("Failed to read target archive", ioex);
+        }
+        return false;
+    }
+
     /**
      * Checks whether the target is more recent than the resources
      * that shall be added to it.
@@ -378,7 +400,6 @@
      */
     protected void writeArchive(ResourceWithFlags[] src)
         throws IOException {
-        FileUtils fu = FileUtils.getFileUtils();
         ArchiveOutputStream out = null;
         Set addedDirectories = new HashSet();
         try {
@@ -401,7 +422,7 @@
                         in = src[i].getResource().getInputStream();
                         IOUtils.copy(in, out);
                     } finally {
-                        fu.close(in);
+                        FILE_UTILS.close(in);
                     }
                 } else {
                     addedDirectories.add(src[i].getName());
@@ -410,7 +431,7 @@
 
             }
         } finally {
-            fu.close(out);
+            FILE_UTILS.close(out);
         }
     }
 
@@ -656,6 +677,29 @@
     }
 
     /**
+     * Creates a copy of the target archive in update or recreate mode
+     * because some entries may later be read and archived from it.
+     */
+    private File maybeCopyTarget() {
+        File copyOfDest = null;
+        try {
+            if (!Mode.FORCE_CREATE.equals(mode.getValue())
+                && !Mode.CREATE.equals(mode.getValue())) {
+                copyOfDest = FILE_UTILS.createTempFile(getTaskName(), ".tmp",
+                                                       null, true, false);
+                ResourceUtils.copyResource(getDest(),
+                                           new FileResource(copyOfDest));
+            }
+        } catch (IOException ioex) {
+            if (copyOfDest != null && copyOfDest.exists()) {
+                FILE_UTILS.tryHardToDelete(copyOfDest);
+            }
+            throw new BuildException("Failed to copy target archive", ioex);
+        }
+        return copyOfDest;
+    }
+
+    /**
      * Valid Modes for create/update/replace.
      */
     public static final class Mode extends EnumeratedAttribute {



Mime
View raw message