git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@269478 13f79535-47bb-0310-9956-ffa450edef68master
@@ -59,6 +59,7 @@ import java.util.*; | |||
import java.text.*; | |||
import org.apache.tools.ant.types.FilterSet; | |||
import org.apache.tools.ant.util.FileUtils; | |||
/** | |||
* Central representation of an Ant project. This class defines a | |||
@@ -140,7 +141,10 @@ public class Project { | |||
} | |||
} | |||
private FileUtils fileUtils; | |||
public Project() { | |||
fileUtils = FileUtils.getFileUtils(); | |||
} | |||
/** | |||
@@ -690,7 +694,7 @@ public class Project { | |||
* @deprecated | |||
*/ | |||
public void copyFile(String sourceFile, String destFile) throws IOException { | |||
FileUtils.copyFile(sourceFile, destFile); | |||
fileUtils.copyFile(sourceFile, destFile); | |||
} | |||
/** | |||
@@ -703,7 +707,7 @@ public class Project { | |||
*/ | |||
public void copyFile(String sourceFile, String destFile, boolean filtering) | |||
throws IOException { | |||
FileUtils.copyFile(sourceFile, destFile, filtering ? globalFilterSet : null); | |||
fileUtils.copyFile(sourceFile, destFile, filtering ? globalFilterSet : null); | |||
} | |||
/** | |||
@@ -717,7 +721,7 @@ public class Project { | |||
*/ | |||
public void copyFile(String sourceFile, String destFile, boolean filtering, | |||
boolean overwrite) throws IOException { | |||
FileUtils.copyFile(sourceFile, destFile, filtering ? globalFilterSet : null, overwrite); | |||
fileUtils.copyFile(sourceFile, destFile, filtering ? globalFilterSet : null, overwrite); | |||
} | |||
/** | |||
@@ -734,7 +738,7 @@ public class Project { | |||
public void copyFile(String sourceFile, String destFile, boolean filtering, | |||
boolean overwrite, boolean preserveLastModified) | |||
throws IOException { | |||
FileUtils.copyFile(sourceFile, destFile, filtering ? globalFilterSet : null, | |||
fileUtils.copyFile(sourceFile, destFile, filtering ? globalFilterSet : null, | |||
overwrite, preserveLastModified); | |||
} | |||
@@ -747,7 +751,7 @@ public class Project { | |||
* @deprecated | |||
*/ | |||
public void copyFile(File sourceFile, File destFile) throws IOException { | |||
FileUtils.copyFile(sourceFile, destFile); | |||
fileUtils.copyFile(sourceFile, destFile); | |||
} | |||
/** | |||
@@ -760,7 +764,7 @@ public class Project { | |||
*/ | |||
public void copyFile(File sourceFile, File destFile, boolean filtering) | |||
throws IOException { | |||
FileUtils.copyFile(sourceFile, destFile, filtering ? globalFilterSet : null); | |||
fileUtils.copyFile(sourceFile, destFile, filtering ? globalFilterSet : null); | |||
} | |||
/** | |||
@@ -774,7 +778,7 @@ public class Project { | |||
*/ | |||
public void copyFile(File sourceFile, File destFile, boolean filtering, | |||
boolean overwrite) throws IOException { | |||
FileUtils.copyFile(sourceFile, destFile, filtering ? globalFilterSet : null, overwrite); | |||
fileUtils.copyFile(sourceFile, destFile, filtering ? globalFilterSet : null, overwrite); | |||
} | |||
/** | |||
@@ -791,7 +795,7 @@ public class Project { | |||
public void copyFile(File sourceFile, File destFile, boolean filtering, | |||
boolean overwrite, boolean preserveLastModified) | |||
throws IOException { | |||
FileUtils.copyFile(sourceFile, destFile, filtering ? globalFilterSet : null, | |||
fileUtils.copyFile(sourceFile, destFile, filtering ? globalFilterSet : null, | |||
overwrite, preserveLastModified); | |||
} | |||
@@ -806,7 +810,7 @@ public class Project { | |||
+ " in JDK 1.1", Project.MSG_WARN); | |||
return; | |||
} | |||
FileUtils.setFileLastModified(file, time); | |||
fileUtils.setFileLastModified(file, time); | |||
log("Setting modification time for " + file, MSG_VERBOSE); | |||
} | |||
@@ -93,7 +93,14 @@ public class Copy extends Task { | |||
protected Mapper mapperElement = null; | |||
private Vector filterSets = new Vector(); | |||
private FileUtils fileUtils; | |||
public Copy() { | |||
fileUtils = FileUtils.getFileUtils(); | |||
} | |||
protected FileUtils getFileUtils() {return fileUtils;} | |||
/** | |||
* Sets a single source file to copy. | |||
*/ | |||
@@ -365,7 +372,7 @@ public class Copy extends Task { | |||
for (Enumeration filterEnum = filterSets.elements(); filterEnum.hasMoreElements();) { | |||
executionFilterSet.addFilterSet((FilterSet)filterEnum.nextElement()); | |||
} | |||
FileUtils.copyFile(fromFile, toFile, executionFilterSet, | |||
fileUtils.copyFile(fromFile, toFile, executionFilterSet, | |||
forceOverwrite, preserveLastModified); | |||
} catch (IOException ioe) { | |||
String msg = "Failed to copy " + fromFile + " to " + toFile | |||
@@ -108,8 +108,8 @@ public class Move extends Copy { | |||
for (Enumeration filterEnum = getFilterSets().elements(); filterEnum.hasMoreElements();) { | |||
executionFilterSet.addFilterSet((FilterSet)filterEnum.nextElement()); | |||
} | |||
FileUtils.copyFile(fromFile, toFile, executionFilterSet, | |||
forceOverwrite); | |||
getFileUtils().copyFile(fromFile, toFile, executionFilterSet, | |||
forceOverwrite); | |||
File f = new File(fromFile); | |||
if (!f.delete()) { | |||
@@ -89,6 +89,11 @@ public class Touch extends Task { | |||
private long millis = -1; | |||
private String dateTime; | |||
private Vector filesets = new Vector(); | |||
private FileUtils fileUtils; | |||
public Touch() { | |||
fileUtils = FileUtils.getFileUtils(); | |||
} | |||
/** | |||
* Sets a single source file to touch. If the file does not exist | |||
@@ -204,9 +209,9 @@ public class Touch extends Task { | |||
} | |||
if (millis < 0) { | |||
project.setFileLastModified(file, System.currentTimeMillis()); | |||
fileUtils.setFileLastModified(file, System.currentTimeMillis()); | |||
} else { | |||
project.setFileLastModified(file, millis); | |||
fileUtils.setFileLastModified(file, millis); | |||
} | |||
} | |||
@@ -52,11 +52,13 @@ | |||
* <http://www.apache.org/>. | |||
*/ | |||
package org.apache.tools.ant; | |||
package org.apache.tools.ant.util; | |||
import java.io.*; | |||
import java.util.*; | |||
import java.lang.reflect.Method; | |||
import org.apache.tools.ant.BuildException; | |||
import org.apache.tools.ant.Project; | |||
import org.apache.tools.ant.types.FilterSet; | |||
/** | |||
@@ -76,13 +78,25 @@ public class FileUtils { | |||
private static Object lockReflection = new Object(); | |||
private static java.lang.reflect.Method setLastModified = null; | |||
/** | |||
* Factory method. | |||
*/ | |||
public static FileUtils getFileUtils() { | |||
return new FileUtils(); | |||
} | |||
/** | |||
* Empty constructor. | |||
*/ | |||
protected FileUtils() {} | |||
/** | |||
* Convienence method to copy a file from a source to a destination. | |||
* No filtering is performed. | |||
* | |||
* @throws IOException | |||
*/ | |||
public static void copyFile(String sourceFile, String destFile) throws IOException { | |||
public void copyFile(String sourceFile, String destFile) throws IOException { | |||
copyFile(new File(sourceFile), new File(destFile), null, false, false); | |||
} | |||
@@ -92,7 +106,7 @@ public class FileUtils { | |||
* | |||
* @throws IOException | |||
*/ | |||
public static void copyFile(String sourceFile, String destFile, FilterSet filterSet) | |||
public void copyFile(String sourceFile, String destFile, FilterSet filterSet) | |||
throws IOException | |||
{ | |||
copyFile(new File(sourceFile), new File(destFile), filterSet, false, false); | |||
@@ -105,7 +119,7 @@ public class FileUtils { | |||
* | |||
* @throws IOException | |||
*/ | |||
public static void copyFile(String sourceFile, String destFile, FilterSet filterSet, | |||
public void copyFile(String sourceFile, String destFile, FilterSet filterSet, | |||
boolean overwrite) throws IOException { | |||
copyFile(new File(sourceFile), new File(destFile), filterSet, | |||
overwrite, false); | |||
@@ -120,7 +134,7 @@ public class FileUtils { | |||
* | |||
* @throws IOException | |||
*/ | |||
public static void copyFile(String sourceFile, String destFile, FilterSet filterSet, | |||
public void copyFile(String sourceFile, String destFile, FilterSet filterSet, | |||
boolean overwrite, boolean preserveLastModified) | |||
throws IOException { | |||
copyFile(new File(sourceFile), new File(destFile), filterSet, | |||
@@ -133,7 +147,7 @@ public class FileUtils { | |||
* | |||
* @throws IOException | |||
*/ | |||
public static void copyFile(File sourceFile, File destFile) throws IOException { | |||
public void copyFile(File sourceFile, File destFile) throws IOException { | |||
copyFile(sourceFile, destFile, null, false, false); | |||
} | |||
@@ -143,7 +157,7 @@ public class FileUtils { | |||
* | |||
* @throws IOException | |||
*/ | |||
public static void copyFile(File sourceFile, File destFile, FilterSet filterSet) | |||
public void copyFile(File sourceFile, File destFile, FilterSet filterSet) | |||
throws IOException { | |||
copyFile(sourceFile, destFile, filterSet, false, false); | |||
} | |||
@@ -155,7 +169,7 @@ public class FileUtils { | |||
* | |||
* @throws IOException | |||
*/ | |||
public static void copyFile(File sourceFile, File destFile, FilterSet filterSet, | |||
public void copyFile(File sourceFile, File destFile, FilterSet filterSet, | |||
boolean overwrite) throws IOException { | |||
copyFile(sourceFile, destFile, filterSet, overwrite, false); | |||
} | |||
@@ -169,7 +183,7 @@ public class FileUtils { | |||
* | |||
* @throws IOException | |||
*/ | |||
public static void copyFile(File sourceFile, File destFile, FilterSet filterSet, | |||
public void copyFile(File sourceFile, File destFile, FilterSet filterSet, | |||
boolean overwrite, boolean preserveLastModified) | |||
throws IOException { | |||
@@ -229,11 +243,11 @@ public class FileUtils { | |||
} | |||
/** | |||
* Calls File.setLastModified(long time) in a Java 1.1 compatible way. | |||
* see whether we have a setLastModified method in File and return it. | |||
*/ | |||
public static void setFileLastModified(File file, long time) throws BuildException { | |||
protected final Method getSetLastModified() { | |||
if (Project.getJavaVersion() == Project.JAVA_1_1) { | |||
return; | |||
return null; | |||
} | |||
if (setLastModified == null) { | |||
synchronized (lockReflection) { | |||
@@ -249,14 +263,25 @@ public class FileUtils { | |||
} | |||
} | |||
} | |||
return setLastModified; | |||
} | |||
/** | |||
* Calls File.setLastModified(long time) in a Java 1.1 compatible way. | |||
*/ | |||
public void setFileLastModified(File file, long time) throws BuildException { | |||
if (Project.getJavaVersion() == Project.JAVA_1_1) { | |||
return; | |||
} | |||
Long[] times = new Long[1]; | |||
if (time < 0) { | |||
times[0] = new Long(System.currentTimeMillis()); | |||
} else { | |||
times[0] = new Long(time); | |||
} | |||
try { | |||
setLastModified.invoke(file, times); | |||
getSetLastModified().invoke(file, times); | |||
} catch (java.lang.reflect.InvocationTargetException ite) { | |||
Throwable nested = ite.getTargetException(); | |||
throw new BuildException("Exception setting the modification time " |