From b498a4535a2ffe115717d13666573de1be0724f1 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Wed, 25 Jun 2003 11:52:47 +0000 Subject: [PATCH] Use the new FileUtils#rename method git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@274714 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/tools/ant/taskdefs/Rename.java | 18 ++++++++++-------- .../org/apache/tools/ant/taskdefs/Replace.java | 9 +-------- .../org/apache/tools/ant/taskdefs/Zip.java | 12 +++++++----- .../ant/taskdefs/optional/ReplaceRegExp.java | 10 ++++------ .../optional/ejb/WeblogicDeploymentTool.java | 9 +++++---- .../optional/ejb/WebsphereDeploymentTool.java | 11 +++++++---- .../ant/taskdefs/optional/unix/Symlink.java | 10 +++++++--- 7 files changed, 41 insertions(+), 38 deletions(-) diff --git a/src/main/org/apache/tools/ant/taskdefs/Rename.java b/src/main/org/apache/tools/ant/taskdefs/Rename.java index a4315cda3..c210010e1 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Rename.java +++ b/src/main/org/apache/tools/ant/taskdefs/Rename.java @@ -1,7 +1,7 @@ /* * The Apache Software License, Version 1.1 * - * Copyright (c) 2000-2002 The Apache Software Foundation. All rights + * Copyright (c) 2000-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -55,9 +55,11 @@ package org.apache.tools.ant.taskdefs; import java.io.File; +import java.io.IOException; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; import org.apache.tools.ant.Task; +import org.apache.tools.ant.util.FileUtils; /** * Renames a file. @@ -115,15 +117,15 @@ public class Rename extends Task { throw new BuildException("src attribute is required", getLocation()); } - if (replace && dest.exists()) { - if (!dest.delete()) { - throw new BuildException("Unable to remove existing file " + - dest); - } + if (!replace && dest.exists()) { + throw new BuildException(dest + " already exists."); } - if (!src.renameTo(dest)) { + + try { + FileUtils.newFileUtils().rename(src, dest); + } catch (IOException e) { throw new BuildException("Unable to rename " + src + " to " + - dest); + dest, e, getLocation()); } } } diff --git a/src/main/org/apache/tools/ant/taskdefs/Replace.java b/src/main/org/apache/tools/ant/taskdefs/Replace.java index 0c2252eaa..e4f070415 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Replace.java +++ b/src/main/org/apache/tools/ant/taskdefs/Replace.java @@ -445,14 +445,7 @@ public class Replace extends MatchingTask { // otherwise, delete the new one if (changes) { ++fileCount; - if (!src.delete()) { - throw new BuildException("Couldn't delete " + src, - getLocation()); - } - if (!temp.renameTo(src)) { - throw new BuildException("Couldn't rename temporary file " - + temp, getLocation()); - } + fileUtils.rename(temp, src); temp = null; } } catch (IOException ioe) { diff --git a/src/main/org/apache/tools/ant/taskdefs/Zip.java b/src/main/org/apache/tools/ant/taskdefs/Zip.java index a6993fae4..f4ee8e130 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Zip.java +++ b/src/main/org/apache/tools/ant/taskdefs/Zip.java @@ -400,13 +400,13 @@ public class Zip extends MatchingTask { fileUtils.getParentFile(zipFile)); try { - if (!zipFile.renameTo(renamedFile)) { - throw new BuildException("Unable to rename old file " - + "to temporary file"); - } + fileUtils.rename(zipFile, renamedFile); } catch (SecurityException e) { throw new BuildException("Not allowed to rename old file " + "to temporary file"); + } catch (IOException e) { + throw new BuildException("Unable to rename old file " + + "to temporary file"); } } @@ -498,7 +498,9 @@ public class Zip extends MatchingTask { } if (doUpdate && renamedFile != null) { - if (!renamedFile.renameTo(zipFile)) { + try { + fileUtils.rename(renamedFile, zipFile); + } catch (IOException e) { msg += " (and I couldn't rename the temporary file " + renamedFile.getName() + " back)"; } diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java b/src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java index ba1372c12..0e2249d23 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java @@ -437,15 +437,13 @@ public class ReplaceRegExp extends Task { w = null; if (changes) { - if (!f.delete()) { - throw new BuildException("Couldn't delete " + f, - getLocation()); - } - if (!temp.renameTo(f)) { + try { + fileUtils.rename(temp, f); + temp = null; + } catch (IOException e) { throw new BuildException("Couldn't rename temporary file " + temp, getLocation()); } - temp = null; } } finally { try { diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/ejb/WeblogicDeploymentTool.java b/src/main/org/apache/tools/ant/taskdefs/optional/ejb/WeblogicDeploymentTool.java index 8b9270cec..c0d74c4d6 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/ejb/WeblogicDeploymentTool.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/ejb/WeblogicDeploymentTool.java @@ -680,7 +680,7 @@ public class WeblogicDeploymentTool extends GenericDeploymentTool { * actual bean changes without changing the the method signatures then * only the bean classfile needs to be updated and the rest of the * weblogic jar file can remain the same. If the Interfaces, ie. the - * method signatures change or if the xml deployment dicriptors changed, + * method signatures change or if the xml deployment descriptors changed, * the whole jar needs to be rebuilt with ejbc. This is not strictly true * for the xml files. If the JNDI name changes then the jar doesnt have to * be rebuild, but if the resources references change then it does. At @@ -864,9 +864,10 @@ public class WeblogicDeploymentTool extends GenericDeploymentTool { } catch (IOException closeException) { } - weblogicJarFile.delete(); - newWLJarFile.renameTo(weblogicJarFile); - if (!weblogicJarFile.exists()) { + try { + fileUtils.rename(newWLJarFile, weblogicJarFile); + } catch (IOException renameException) { + log(renameException.getMessage(), Project.MSG_WARN); rebuild = true; } } diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/ejb/WebsphereDeploymentTool.java b/src/main/org/apache/tools/ant/taskdefs/optional/ejb/WebsphereDeploymentTool.java index 7f567aa11..ef56cdf36 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/ejb/WebsphereDeploymentTool.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/ejb/WebsphereDeploymentTool.java @@ -70,6 +70,7 @@ import org.apache.tools.ant.types.Commandline; import org.apache.tools.ant.types.EnumeratedAttribute; import org.apache.tools.ant.types.Environment; import org.apache.tools.ant.types.Path; +import org.apache.tools.ant.util.FileUtils; /** * Websphere deployment tool that augments the ejbjar task. @@ -693,7 +694,7 @@ public class WebsphereDeploymentTool extends GenericDeploymentTool { * If the actual bean changes without changing the the method signatures * then only the bean classfile needs to be updated and the rest of the * websphere jar file can remain the same. If the Interfaces, ie. the - * method signatures change or if the xml deployment dicriptors changed, + * method signatures change or if the xml deployment descriptors changed, * the whole jar needs to be rebuilt with ejbdeploy. This is not strictly * true for the xml files. If the JNDI name changes then the jar doesnt * have to be rebuild, but if the resources references change then it @@ -871,9 +872,11 @@ public class WebsphereDeploymentTool extends GenericDeploymentTool { } catch (IOException closeException) { } - websphereJarFile.delete(); - newwasJarFile.renameTo(websphereJarFile); - if (!websphereJarFile.exists()) { + try { + FileUtils.newFileUtils().rename(newwasJarFile, + websphereJarFile); + } catch (IOException renameException) { + log(renameException.getMessage(), Project.MSG_WARN); rebuild = true; } } diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/unix/Symlink.java b/src/main/org/apache/tools/ant/taskdefs/optional/unix/Symlink.java index 6e2ef26fd..b66c4b1db 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/unix/Symlink.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/unix/Symlink.java @@ -428,11 +428,13 @@ public class Symlink extends Task { FileUtils fu = FileUtils.newFileUtils(); File temp = fu.createTempFile("symlink",".tmp", parentDir); try { - if (!canfil.renameTo(temp)) { + try { + fu.rename(canfil, temp); + } catch (IOException e) { throw new IOException("Couldn't rename resource when " + "attempting to delete " + linkfil); } - + // delete the (now) broken link if(!linkfil.delete()) { throw new IOException("Couldn't delete symlink: " + linkfil + @@ -441,7 +443,9 @@ public class Symlink extends Task { } } finally { // return the resource to its original name. - if (!temp.renameTo(canfil)) { + try { + fu.rename(temp, canfil); + } catch (IOException e) { throw new IOException("Couldn't return resource " + temp + " its original name: " + canstr + "\n THE RESOURCE'S NAME ON DISK HAS " +