FileDocCategorySizeDatePackage
FTP.javaAPI DocApache Ant 1.7096816Wed Dec 13 06:16:18 GMT 2006org.apache.tools.ant.taskdefs.optional.net

FTP

public class FTP extends org.apache.tools.ant.Task
Basic FTP client. Performs the following actions:
  • send - send files to a remote server. This is the default action.
  • get - retrieve files from a remote server.
  • del - delete files from a remote server.
  • list - create a file listing.
  • chmod - change unix file permissions.
  • rmdir - remove directories, if empty, from a remote server.
Note: Some FTP servers - notably the Solaris server - seem to hold data ports open after a "retr" operation, allowing them to timeout instead of shutting them down cleanly. This happens in active or passive mode, and the ports will remain open even after ending the FTP session. FTP "send" operations seem to close ports immediately. This behavior may cause problems on some systems when downloading large sets of files.
since
Ant 1.3

Fields Summary
protected static final int
SEND_FILES
protected static final int
GET_FILES
protected static final int
DEL_FILES
protected static final int
LIST_FILES
protected static final int
MK_DIR
protected static final int
CHMOD
protected static final int
RM_DIR
protected static final int
SITE_CMD
private static final int
CODE_521
return code of ftp - not implemented in commons-net version 1.0
private static final long
GRANULARITY_MINUTE
adjust uptodate calculations where server timestamps are HH:mm and client's are HH:mm:ss
public static final int
DEFAULT_FTP_PORT
Default port for FTP
private static final org.apache.tools.ant.util.FileUtils
FILE_UTILS
private String
remotedir
private String
server
private String
userid
private String
password
private String
account
private File
listing
private boolean
binary
private boolean
passive
private boolean
verbose
private boolean
newerOnly
private long
timeDiffMillis
private long
granularityMillis
private boolean
timeDiffAuto
private int
action
private Vector
filesets
private Vector
dirCache
private int
transferred
private String
remoteFileSep
private int
port
private boolean
skipFailedTransfers
private int
skipped
private boolean
ignoreNoncriticalErrors
private boolean
preserveLastModified
private String
chmod
private String
umask
private FTPSystemType
systemTypeKey
private String
defaultDateFormatConfig
private String
recentDateFormatConfig
private LanguageCode
serverLanguageCodeConfig
private String
serverTimeZoneConfig
private String
shortMonthNamesConfig
private Granularity
timestampGranularity
private boolean
isConfigurationSet
private int
retriesAllowed
private String
siteCommand
private String
initialSiteCommand
protected static final String[]
ACTION_STRS
protected static final String[]
COMPLETED_ACTION_STRS
protected static final String[]
ACTION_TARGET_STRS
private static final SimpleDateFormat
TIMESTAMP_LOGGING_SDF
Constructors Summary
Methods Summary
public voidaddFileset(org.apache.tools.ant.types.FileSet set)
A set of files to upload or download

param
set the set of files to be added to the list of files to be transferred.

        filesets.addElement(set);
    
protected voidcheckAttributes()
Checks to see that all required parameters are set.

throws
BuildException if the configuration is not valid.

        if (server == null) {
            throw new BuildException("server attribute must be set!");
        }
        if (userid == null) {
            throw new BuildException("userid attribute must be set!");
        }
        if (password == null) {
            throw new BuildException("password attribute must be set!");
        }

        if ((action == LIST_FILES) && (listing == null)) {
            throw new BuildException("listing attribute must be set for list "
                 + "action!");
        }

        if (action == MK_DIR && remotedir == null) {
            throw new BuildException("remotedir attribute must be set for "
                 + "mkdir action!");
        }

        if (action == CHMOD && chmod == null) {
            throw new BuildException("chmod attribute must be set for chmod "
                 + "action!");
        }
        if (action == SITE_CMD && siteCommand == null) {
            throw new BuildException("sitecommand attribute must be set for site "
                 + "action!");
        }


        if (this.isConfigurationSet) {
            try {
                Class.forName("org.apache.commons.net.ftp.FTPClientConfig");
            } catch (ClassNotFoundException e) {
                throw new BuildException(
                 "commons-net.jar >= 1.4.0 is required for at least one"
                 + " of the attributes specified.");
            }
        }
    
private voidconfigurationHasBeenSet()

        this.isConfigurationSet = true;
    
protected voidcreateParents(org.apache.commons.net.ftp.FTPClient ftp, java.lang.String filename)
Creates all parent directories specified in a complete relative pathname. Attempts to create existing directories will not cause errors.

param
ftp the FTP client instance to use to execute FTP actions on the remote server.
param
filename the name of the file whose parents should be created.
throws
IOException under non documented circumstances
throws
BuildException if it is impossible to cd to a remote directory


        File dir = new File(filename);
        if (dirCache.contains(dir)) {
            return;
        }


        Vector parents = new Vector();
        String dirname;

        while ((dirname = dir.getParent()) != null) {
            File checkDir = new File(dirname);
            if (dirCache.contains(checkDir)) {
                break;
            }
            dir = checkDir;
            parents.addElement(dir);
        }

        // find first non cached dir
        int i = parents.size() - 1;

        if (i >= 0) {
            String cwd = ftp.printWorkingDirectory();
            String parent = dir.getParent();
            if (parent != null) {
                if (!ftp.changeWorkingDirectory(resolveFile(parent))) {
                    throw new BuildException("could not change to "
                        + "directory: " + ftp.getReplyString());
                }
            }

            while (i >= 0) {
                dir = (File) parents.elementAt(i--);
                // check if dir exists by trying to change into it.
                if (!ftp.changeWorkingDirectory(dir.getName())) {
                    // could not change to it - try to create it
                    log("creating remote directory "
                        + resolveFile(dir.getPath()), Project.MSG_VERBOSE);
                    if (!ftp.makeDirectory(dir.getName())) {
                        handleMkDirFailure(ftp);
                    }
                    if (!ftp.changeWorkingDirectory(dir.getName())) {
                        throw new BuildException("could not change to "
                            + "directory: " + ftp.getReplyString());
                    }
                }
                dirCache.addElement(dir);
            }
            ftp.changeWorkingDirectory(cwd);
        }
    
protected voiddelFile(org.apache.commons.net.ftp.FTPClient ftp, java.lang.String filename)
Delete a file from the remote host.

param
ftp ftp client
param
filename file to delete
throws
IOException in unknown circumstances
throws
BuildException if skipFailedTransfers is set to false and the deletion could not be done

        if (verbose) {
            log("deleting " + filename);
        }

        if (!ftp.deleteFile(resolveFile(filename))) {
            String s = "could not delete file: " + ftp.getReplyString();

            if (skipFailedTransfers) {
                log(s, Project.MSG_WARN);
                skipped++;
            } else {
                throw new BuildException(s);
            }
        } else {
            log("File " + filename + " deleted from " + server,
                Project.MSG_VERBOSE);
            transferred++;
        }
    
protected voiddoSiteCommand(org.apache.commons.net.ftp.FTPClient ftp, java.lang.String theCMD)
Sends a site command to the ftp server

param
ftp ftp client
param
theCMD command to execute
throws
IOException in unknown circumstances
throws
BuildException in unknown circumstances

        boolean rc;
        String[] myReply = null;

        log("Doing Site Command: " + theCMD, Project.MSG_VERBOSE);

        rc = ftp.sendSiteCommand(theCMD);

        if (!rc) {
            log("Failed to issue Site Command: " + theCMD, Project.MSG_WARN);
        } else {

            myReply = ftp.getReplyStrings();

            for (int x = 0; x < myReply.length; x++) {
                if (myReply[x].indexOf("200") == -1) {
                    log(myReply[x], Project.MSG_WARN);
                }
            }
        }
    
public voidexecute()
Runs the task.

throws
BuildException if the task fails or is not configured correctly.

        checkAttributes();

        FTPClient ftp = null;

        try {
            log("Opening FTP connection to " + server, Project.MSG_VERBOSE);

            ftp = new FTPClient();
            if (this.isConfigurationSet) {
                ftp = FTPConfigurator.configure(ftp, this);
            }

            ftp.connect(server, port);
            if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                throw new BuildException("FTP connection failed: "
                     + ftp.getReplyString());
            }

            log("connected", Project.MSG_VERBOSE);
            log("logging in to FTP server", Project.MSG_VERBOSE);

            if ((this.account != null && !ftp.login(userid, password, account))
                    || (this.account == null && !ftp.login(userid, password))) {
                throw new BuildException("Could not login to FTP server");
            }

            log("login succeeded", Project.MSG_VERBOSE);

            if (binary) {
                ftp.setFileType(org.apache.commons.net.ftp.FTP.IMAGE_FILE_TYPE);
                if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                    throw new BuildException("could not set transfer type: "
                        + ftp.getReplyString());
                }
            } else {
                ftp.setFileType(org.apache.commons.net.ftp.FTP.ASCII_FILE_TYPE);
                if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                    throw new BuildException("could not set transfer type: "
                        + ftp.getReplyString());
                }
            }

            if (passive) {
                log("entering passive mode", Project.MSG_VERBOSE);
                ftp.enterLocalPassiveMode();
                if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                    throw new BuildException("could not enter into passive "
                         + "mode: " + ftp.getReplyString());
                }
            }

            // If an initial command was configured then send it.
            // Some FTP servers offer different modes of operation,
            // E.G. switching between a UNIX file system mode and
            // a legacy file system.
            if (this.initialSiteCommand != null) {
                RetryHandler h = new RetryHandler(this.retriesAllowed, this);
                final FTPClient lftp = ftp;
                executeRetryable(h, new Retryable() {
                    public void execute() throws IOException {
                        doSiteCommand(lftp, FTP.this.initialSiteCommand);
                    }
                }, "initial site command: " + this.initialSiteCommand);
            }


            // For a unix ftp server you can set the default mask for all files
            // created.

            if (umask != null) {
                RetryHandler h = new RetryHandler(this.retriesAllowed, this);
                final FTPClient lftp = ftp;
                executeRetryable(h, new Retryable() {
                    public void execute() throws IOException {
                        doSiteCommand(lftp, "umask " + umask);
                    }
                }, "umask " + umask);
            }

            // If the action is MK_DIR, then the specified remote
            // directory is the directory to create.

            if (action == MK_DIR) {
                RetryHandler h = new RetryHandler(this.retriesAllowed, this);
                final FTPClient lftp = ftp;
                executeRetryable(h, new Retryable() {
                    public void execute() throws IOException {
                        makeRemoteDir(lftp, remotedir);
                    }
                }, remotedir);
            } else if (action == SITE_CMD) {
                    RetryHandler h = new RetryHandler(this.retriesAllowed, this);
                    final FTPClient lftp = ftp;
                    executeRetryable(h, new Retryable() {
                        public void execute() throws IOException {
                            doSiteCommand(lftp, FTP.this.siteCommand);
                        }
                    }, "Site Command: " + this.siteCommand);
            } else {
                if (remotedir != null) {
                    log("changing the remote directory", Project.MSG_VERBOSE);
                    ftp.changeWorkingDirectory(remotedir);
                    if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                        throw new BuildException("could not change remote "
                             + "directory: " + ftp.getReplyString());
                    }
                }
                if (newerOnly && timeDiffAuto) {
                // in this case we want to find how much time span there is between local
                // and remote
                    timeDiffMillis = getTimeDiff(ftp);
                }
                log(ACTION_STRS[action] + " " + ACTION_TARGET_STRS[action]);
                transferFiles(ftp);
            }

        } catch (IOException ex) {
            throw new BuildException("error during FTP transfer: " + ex, ex);
        } finally {
            if (ftp != null && ftp.isConnected()) {
                try {
                    log("disconnecting", Project.MSG_VERBOSE);
                    ftp.logout();
                    ftp.disconnect();
                } catch (IOException ex) {
                    // ignore it
                }
            }
        }
    
protected voidexecuteRetryable(org.apache.tools.ant.util.RetryHandler h, org.apache.tools.ant.util.Retryable r, java.lang.String descr)
Executable a retryable object.

param
h the retry hander.
param
r the object that should be retried until it succeeds or the number of retrys is reached.
param
descr a description of the command that is being run.
throws
IOException if there is a problem.

        h.execute(r, descr);
    
private java.io.FilefindFileName(org.apache.commons.net.ftp.FTPClient ftp)
find a suitable name for local and remote temporary file

        FTPFile [] theFiles = null;
        final int maxIterations = 1000;
        for (int counter = 1; counter < maxIterations; counter++) {
            File localFile = FILE_UTILS.createTempFile("ant" + Integer.toString(counter), ".tmp",
                null);
            String fileName = localFile.getName();
            boolean found = false;
            try {
                if (counter == 1) {
                    theFiles = ftp.listFiles();
                }
                for (int counter2 = 0; counter2 < theFiles.length; counter2++) {
                    if (theFiles[counter2].getName().equals(fileName)) {
                        found = true;
                        break;
                    }
                }
            } catch (IOException ioe) {
                throw new BuildException(ioe, getLocation());
            }
            if (!found) {
                localFile.deleteOnExit();
                return localFile;
            }
        }
        return null;
    
java.lang.StringgetDefaultDateFormatConfig()

return
Returns the defaultDateFormatConfig.

        return defaultDateFormatConfig;
    
protected voidgetFile(org.apache.commons.net.ftp.FTPClient ftp, java.lang.String dir, java.lang.String filename)
Retrieve a single file from the remote host. filename may contain a relative path specification.

The file will then be retreived using the entire relative path spec - no attempt is made to change directories. It is anticipated that this may eventually cause problems with some FTP servers, but it simplifies the coding.

param
ftp the ftp client
param
dir local base directory to which the file should go back
param
filename relative path of the file based upon the ftp remote directory and/or the local base directory (dir)
throws
IOException in unknown circumstances
throws
BuildException if skipFailedTransfers is false and the file cannot be retrieved.

        OutputStream outstream = null;
        try {
            File file = getProject().resolveFile(new File(dir, filename).getPath());

            if (newerOnly && isUpToDate(ftp, file, resolveFile(filename))) {
                return;
            }

            if (verbose) {
                log("transferring " + filename + " to "
                     + file.getAbsolutePath());
            }

            File pdir = file.getParentFile();

            if (!pdir.exists()) {
                pdir.mkdirs();
            }
            outstream = new BufferedOutputStream(new FileOutputStream(file));
            ftp.retrieveFile(resolveFile(filename), outstream);

            if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                String s = "could not get file: " + ftp.getReplyString();

                if (skipFailedTransfers) {
                    log(s, Project.MSG_WARN);
                    skipped++;
                } else {
                    throw new BuildException(s);
                }

            } else {
                log("File " + file.getAbsolutePath() + " copied from "
                     + server, Project.MSG_VERBOSE);
                transferred++;
                if (preserveLastModified) {
                    outstream.close();
                    outstream = null;
                    FTPFile[] remote = ftp.listFiles(resolveFile(filename));
                    if (remote.length > 0) {
                        FILE_UTILS.setFileLastModified(file,
                                                      remote[0].getTimestamp()
                                                      .getTime().getTime());
                    }
                }
            }
        } finally {
            if (outstream != null) {
                try {
                    outstream.close();
                } catch (IOException ex) {
                    // ignore it
                }
            }
        }
    
java.lang.StringgetRecentDateFormatConfig()

return
Returns the recentDateFormatConfig.

        return recentDateFormatConfig;
    
java.lang.StringgetServerLanguageCodeConfig()

return
Returns the serverLanguageCodeConfig.

        return serverLanguageCodeConfig.getValue();
    
java.lang.StringgetServerTimeZoneConfig()

return
Returns the serverTimeZoneConfig.

        return serverTimeZoneConfig;
    
java.lang.StringgetShortMonthNamesConfig()

return
Returns the shortMonthNamesConfig.

        return shortMonthNamesConfig;
    
java.lang.StringgetSystemTypeKey()

return
Returns the systemTypeKey.

        return systemTypeKey.getValue();
    
private longgetTimeDiff(org.apache.commons.net.ftp.FTPClient ftp)
auto find the time difference between local and remote

param
ftp handle to ftp client
return
number of millis to add to remote time to make it comparable to local time
since
ant 1.6

        long returnValue = 0;
        File tempFile = findFileName(ftp);
        try {
            // create a local temporary file
            FILE_UTILS.createNewFile(tempFile);
            long localTimeStamp = tempFile.lastModified();
            BufferedInputStream instream = new BufferedInputStream(new FileInputStream(tempFile));
            ftp.storeFile(tempFile.getName(), instream);
            instream.close();
            boolean success = FTPReply.isPositiveCompletion(ftp.getReplyCode());
            if (success) {
                FTPFile [] ftpFiles = ftp.listFiles(tempFile.getName());
                if (ftpFiles.length == 1) {
                    long remoteTimeStamp = ftpFiles[0].getTimestamp().getTime().getTime();
                    returnValue = localTimeStamp - remoteTimeStamp;
                }
                ftp.deleteFile(ftpFiles[0].getName());
            }
            // delegate the deletion of the local temp file to the delete task
            // because of race conditions occuring on Windows
            Delete mydelete = new Delete();
            mydelete.bindToOwner(this);
            mydelete.setFile(tempFile.getCanonicalFile());
            mydelete.execute();
        } catch (Exception e) {
            throw new BuildException(e, getLocation());
        }
        return returnValue;
    
org.apache.tools.ant.taskdefs.optional.net.FTP$GranularitygetTimestampGranularity()

return
Returns the timestampGranularity.

        return timestampGranularity;
    
private voidhandleMkDirFailure(org.apache.commons.net.ftp.FTPClient ftp)
look at the response for a failed mkdir action, decide whether it matters or not. If it does, we throw an exception

param
ftp current ftp connection
throws
BuildException if this is an error to signal

        int rc = ftp.getReplyCode();
        if (!(ignoreNoncriticalErrors
             && (rc == FTPReply.CODE_550 || rc == FTPReply.CODE_553 || rc == CODE_521))) {
            throw new BuildException("could not create directory: "
                + ftp.getReplyString());
        }
    
private booleanisFunctioningAsDirectory(org.apache.commons.net.ftp.FTPClient ftp, java.lang.String dir, org.apache.commons.net.ftp.FTPFile file)
check FTPFiles to check whether they function as directories too the FTPFile API seem to make directory and symbolic links incompatible we want to find out if we can cd to a symbolic link

param
dir the parent directory of the file to test
param
file the file to test
return
true if it is possible to cd to this directory
since
ant 1.6

        boolean result = false;
        String currentWorkingDir = null;
        if (file.isDirectory()) {
            return true;
        } else if (file.isFile()) {
            return false;
        }
        try {
            currentWorkingDir = ftp.printWorkingDirectory();
        } catch (IOException ioe) {
            getProject().log("could not find current working directory " + dir
                + " while checking a symlink",
                Project.MSG_DEBUG);
        }
        if (currentWorkingDir != null) {
            try {
                result = ftp.changeWorkingDirectory(file.getLink());
            } catch (IOException ioe) {
                getProject().log("could not cd to " + file.getLink() + " while checking a symlink",
                    Project.MSG_DEBUG);
            }
            if (result) {
                boolean comeback = false;
                try {
                    comeback = ftp.changeWorkingDirectory(currentWorkingDir);
                } catch (IOException ioe) {
                    getProject().log("could not cd back to " + dir + " while checking a symlink",
                        Project.MSG_ERR);
                } finally {
                    if (!comeback) {
                        throw new BuildException("could not cd back to " + dir
                            + " while checking a symlink");
                    }
                }
            }
        }
        return result;
    
private booleanisFunctioningAsFile(org.apache.commons.net.ftp.FTPClient ftp, java.lang.String dir, org.apache.commons.net.ftp.FTPFile file)
check FTPFiles to check whether they function as directories too the FTPFile API seem to make directory and symbolic links incompatible we want to find out if we can cd to a symbolic link

param
dir the parent directory of the file to test
param
file the file to test
return
true if it is possible to cd to this directory
since
ant 1.6

        if (file.isDirectory()) {
            return false;
        } else if (file.isFile()) {
            return true;
        }
        return !isFunctioningAsDirectory(ftp, dir, file);
    
protected booleanisUpToDate(org.apache.commons.net.ftp.FTPClient ftp, java.io.File localFile, java.lang.String remoteFile)
Checks to see if the remote file is current as compared with the local file. Returns true if the target file is up to date.

param
ftp ftpclient
param
localFile local file
param
remoteFile remote file
return
true if the target file is up to date
throws
IOException in unknown circumstances
throws
BuildException if the date of the remote files cannot be found and the action is GET_FILES


                                                                              
         
                                  
            
        log("checking date for " + remoteFile, Project.MSG_VERBOSE);

        FTPFile[] files = ftp.listFiles(remoteFile);

        // For Microsoft's Ftp-Service an Array with length 0 is
        // returned if configured to return listings in "MS-DOS"-Format
        if (files == null || files.length == 0) {
            // If we are sending files, then assume out of date.
            // If we are getting files, then throw an error

            if (action == SEND_FILES) {
                log("Could not date test remote file: " + remoteFile
                     + "assuming out of date.", Project.MSG_VERBOSE);
                return false;
            } else {
                throw new BuildException("could not date test remote file: "
                    + ftp.getReplyString());
            }
        }

        long remoteTimestamp = files[0].getTimestamp().getTime().getTime();
        long localTimestamp = localFile.lastModified();
        long adjustedRemoteTimestamp =
            remoteTimestamp + this.timeDiffMillis + this.granularityMillis;

        StringBuffer msg = new StringBuffer("   [")
                .append(TIMESTAMP_LOGGING_SDF.format(new Date(localTimestamp)))
                .append("] local");
        log(msg.toString(), Project.MSG_VERBOSE);

        msg = new StringBuffer("   [")
                  .append(TIMESTAMP_LOGGING_SDF.format(new Date(adjustedRemoteTimestamp)))
                .append("] remote");
        if (remoteTimestamp != adjustedRemoteTimestamp) {
            msg.append(" - (raw: ")
                .append(TIMESTAMP_LOGGING_SDF.format(new Date(remoteTimestamp)))
            .append(")");
        }
        log(msg.toString(), Project.MSG_VERBOSE);



        if (this.action == SEND_FILES) {
            return adjustedRemoteTimestamp >= localTimestamp;
        } else {
            return localTimestamp >= adjustedRemoteTimestamp;
        }
    
protected voidlistFile(org.apache.commons.net.ftp.FTPClient ftp, java.io.BufferedWriter bw, java.lang.String filename)
List information about a single file from the remote host. filename may contain a relative path specification.

The file listing will then be retrieved using the entire relative path spec - no attempt is made to change directories. It is anticipated that this may eventually cause problems with some FTP servers, but it simplifies the coding.

param
ftp ftp client
param
bw buffered writer
param
filename the directory one wants to list
throws
IOException in unknown circumstances
throws
BuildException in unknown circumstances

        if (verbose) {
            log("listing " + filename);
        }
        FTPFile[] ftpfiles = ftp.listFiles(resolveFile(filename));

        if (ftpfiles != null && ftpfiles.length > 0) {
            bw.write(ftpfiles[0].toString());
            bw.newLine();
            transferred++;
        }
    
protected voidmakeRemoteDir(org.apache.commons.net.ftp.FTPClient ftp, java.lang.String dir)
Create the specified directory on the remote host.

param
ftp The FTP client connection
param
dir The directory to create (format must be correct for host type)
throws
IOException in unknown circumstances
throws
BuildException if ignoreNoncriticalErrors has not been set to true and a directory could not be created, for instance because it was already existing. Precisely, the codes 521, 550 and 553 will trigger a BuildException

        String workingDirectory = ftp.printWorkingDirectory();
        if (verbose) {
            log("Creating directory: " + dir);
        }
        if (dir.indexOf("/") == 0) {
            ftp.changeWorkingDirectory("/");
        }
        String subdir = new String();
        StringTokenizer st = new StringTokenizer(dir, "/");
        while (st.hasMoreTokens()) {
            subdir = st.nextToken();
            log("Checking " + subdir, Project.MSG_DEBUG);
            if (!ftp.changeWorkingDirectory(subdir)) {
                if (!ftp.makeDirectory(subdir)) {
                    // codes 521, 550 and 553 can be produced by FTP Servers
                    //  to indicate that an attempt to create a directory has
                    //  failed because the directory already exists.
                    int rc = ftp.getReplyCode();
                    if (!(ignoreNoncriticalErrors
                        && (rc == FTPReply.CODE_550 || rc == FTPReply.CODE_553
                        || rc == CODE_521))) {
                        throw new BuildException("could not create directory: "
                            + ftp.getReplyString());
                    }
                    if (verbose) {
                        log("Directory already exists");
                    }
                } else {
                    if (verbose) {
                        log("Directory created OK");
                    }
                    ftp.changeWorkingDirectory(subdir);
                }
            }
        }
        if (workingDirectory != null) {
            ftp.changeWorkingDirectory(workingDirectory);
        }
    
protected java.lang.StringresolveFile(java.lang.String file)
Correct a file path to correspond to the remote host requirements. This implementation currently assumes that the remote end can handle Unix-style paths with forward-slash separators. This can be overridden with the separator task parameter. No attempt is made to determine what syntax is appropriate for the remote host.

param
file the remote file name to be resolved
return
the filename as it will appear on the server.

        return file.replace(System.getProperty("file.separator").charAt(0),
            remoteFileSep.charAt(0));
    
protected voidrmDir(org.apache.commons.net.ftp.FTPClient ftp, java.lang.String dirname)
Delete a directory, if empty, from the remote host.

param
ftp ftp client
param
dirname directory to delete
throws
IOException in unknown circumstances
throws
BuildException if skipFailedTransfers is set to false and the deletion could not be done

        if (verbose) {
            log("removing " + dirname);
        }

        if (!ftp.removeDirectory(resolveFile(dirname))) {
            String s = "could not remove directory: " + ftp.getReplyString();

            if (skipFailedTransfers) {
                log(s, Project.MSG_WARN);
                skipped++;
            } else {
                throw new BuildException(s);
            }
        } else {
            log("Directory " + dirname + " removed from " + server,
                Project.MSG_VERBOSE);
            transferred++;
        }
    
protected voidsendFile(org.apache.commons.net.ftp.FTPClient ftp, java.lang.String dir, java.lang.String filename)
Sends a single file to the remote host. filename may contain a relative path specification. When this is the case, sendFile will attempt to create any necessary parent directories before sending the file. The file will then be sent using the entire relative path spec - no attempt is made to change directories. It is anticipated that this may eventually cause problems with some FTP servers, but it simplifies the coding.

param
ftp ftp client
param
dir base directory of the file to be sent (local)
param
filename relative path of the file to be send locally relative to dir remotely relative to the remotedir attribute
throws
IOException in unknown circumstances
throws
BuildException in unknown circumstances

        InputStream instream = null;

        try {
            // XXX - why not simply new File(dir, filename)?
            File file = getProject().resolveFile(new File(dir, filename).getPath());

            if (newerOnly && isUpToDate(ftp, file, resolveFile(filename))) {
                return;
            }

            if (verbose) {
                log("transferring " + file.getAbsolutePath());
            }

            instream = new BufferedInputStream(new FileInputStream(file));

            createParents(ftp, filename);

            ftp.storeFile(resolveFile(filename), instream);

            boolean success = FTPReply.isPositiveCompletion(ftp.getReplyCode());

            if (!success) {
                String s = "could not put file: " + ftp.getReplyString();

                if (skipFailedTransfers) {
                    log(s, Project.MSG_WARN);
                    skipped++;
                } else {
                    throw new BuildException(s);
                }

            } else {
                // see if we should issue a chmod command
                if (chmod != null) {
                    doSiteCommand(ftp, "chmod " + chmod + " " + resolveFile(filename));
                }
                log("File " + file.getAbsolutePath() + " copied to " + server,
                    Project.MSG_VERBOSE);
                transferred++;
            }
        } finally {
            if (instream != null) {
                try {
                    instream.close();
                } catch (IOException ex) {
                    // ignore it
                }
            }
        }
    
public voidsetAccount(java.lang.String pAccount)
Sets the login account to use on the specified server.

param
pAccount the account name on remote system
since
Ant 1.7

        this.account = pAccount;
    
public voidsetAction(java.lang.String action)
Sets the FTP action to be taken. Currently accepts "put", "get", "del", "mkdir", "chmod", "list", and "site".

deprecated
since 1.5.x. setAction(String) is deprecated and is replaced with setAction(FTP.Action) to make Ant's Introspection mechanism do the work and also to encapsulate operations on the type in its own class.
ant.attribute
ignore="true"
param
action the FTP action to be performed.
throws
BuildException if the action is not a valid action.

        log("DEPRECATED - The setAction(String) method has been deprecated."
             + " Use setAction(FTP.Action) instead.");

        Action a = new Action();

        a.setValue(action);
        this.action = a.getAction();
    
public voidsetAction(org.apache.tools.ant.taskdefs.optional.net.FTP$Action action)
Sets the FTP action to be taken. Currently accepts "put", "get", "del", "mkdir", "chmod", "list", and "site".

param
action the FTP action to be performed.
throws
BuildException if the action is not a valid action.

        this.action = action.getAction();
    
public voidsetBinary(boolean binary)
If true, uses binary mode, otherwise text mode (default is binary).

param
binary if true use binary mode in transfers.

        this.binary = binary;
    
public voidsetChmod(java.lang.String theMode)
Sets the file permission mode (Unix only) for files sent to the server.

param
theMode unix style file mode for the files sent to the remote system.

        this.chmod = theMode;
    
public voidsetDefaultDateFormatConfig(java.lang.String defaultDateFormat)
Sets the defaultDateFormatConfig attribute.

param
defaultDateFormat configuration to be set, unless it is null or empty string, in which case ignored.
see
org.apache.commons.net.ftp.FTPClientConfig

        if (defaultDateFormat != null && !defaultDateFormat.equals("")) {
            this.defaultDateFormatConfig = defaultDateFormat;
            configurationHasBeenSet();
        }
    
public voidsetDepends(boolean depends)
Set to true to transmit only files that are new or changed from their remote counterparts. The default is to transmit all files.

param
depends if true only transfer newer files.

        this.newerOnly = depends;
    
public voidsetIgnoreNoncriticalErrors(boolean ignoreNoncriticalErrors)
set the flag to skip errors on directory creation. (and maybe later other server specific errors)

param
ignoreNoncriticalErrors true if non-critical errors should not cause a failure.

        this.ignoreNoncriticalErrors = ignoreNoncriticalErrors;
    
public voidsetInitialSiteCommand(java.lang.String initialCommand)
Sets the initialSiteCommand attribute. This attribute names a site command that will be executed immediately after connection.

param
initialCommand The initialSiteCommand to set.

        this.initialSiteCommand = initialCommand;
    
public voidsetListing(java.io.File listing)
The output file for the "list" action. This attribute is ignored for any other actions.

param
listing file in which to store the listing.

        this.listing = listing;
    
public voidsetNewer(boolean newer)
A synonym for depends. Set to true to transmit only new or changed files. See the related attributes timediffmillis and timediffauto.

param
newer if true only transfer newer files.

        this.newerOnly = newer;
    
public voidsetPassive(boolean passive)
Specifies whether to use passive mode. Set to true if you are behind a firewall and cannot connect without it. Passive mode is disabled by default.

param
passive true is passive mode should be used.

        this.passive = passive;
    
public voidsetPassword(java.lang.String password)
Sets the login password for the given user id.

param
password the password on the remote system.

        this.password = password;
    
public voidsetPort(int port)
Sets the FTP port used by the remote server.

param
port the port on which the remote server is listening.

        this.port = port;
    
public voidsetPreserveLastModified(boolean preserveLastModified)
Set to true to preserve modification times for "gotten" files.

param
preserveLastModified if true preserver modification times.

        this.preserveLastModified = preserveLastModified;
    
public voidsetRecentDateFormatConfig(java.lang.String recentDateFormat)
Sets the recentDateFormatConfig attribute.

param
recentDateFormat configuration to be set, unless it is null or empty string, in which case ignored.
see
org.apache.commons.net.ftp.FTPClientConfig

        if (recentDateFormat != null && !recentDateFormat.equals("")) {
            this.recentDateFormatConfig = recentDateFormat;
            configurationHasBeenSet();
        }
    
public voidsetRemotedir(java.lang.String dir)
Sets the remote directory where files will be placed. This may be a relative or absolute path, and must be in the path syntax expected by the remote server. No correction of path syntax will be performed.

param
dir the remote directory name.

        this.remotedir = dir;
    
public voidsetRetriesAllowed(java.lang.String retriesAllowed)
Defines how many times to retry executing FTP command before giving up. Default is 0 - try once and if failure then give up.

param
retriesAllowed number of retries to allow. -1 means keep trying forever. "forever" may also be specified as a synonym for -1.

        if ("FOREVER".equalsIgnoreCase(retriesAllowed)) {
            this.retriesAllowed = Retryable.RETRY_FOREVER;
        } else {
            try {
                int retries = Integer.parseInt(retriesAllowed);
                if (retries < Retryable.RETRY_FOREVER) {
                    throw new BuildException(
                            "Invalid value for retriesAllowed attribute: "
                            + retriesAllowed);

                }
                this.retriesAllowed = retries;
            } catch (NumberFormatException px) {
                throw new BuildException(
                        "Invalid value for retriesAllowed attribute: "
                        + retriesAllowed);

            }

        }
    
public voidsetSeparator(java.lang.String separator)
Sets the remote file separator character. This normally defaults to the Unix standard forward slash, but can be manually overridden using this call if the remote server requires some other separator. Only the first character of the string is used.

param
separator the file separator on the remote system.

        remoteFileSep = separator;
    
public voidsetServer(java.lang.String server)
Sets the FTP server to send files to.

param
server the remote server name.

        this.server = server;
    
public voidsetServerLanguageCodeConfig(org.apache.tools.ant.taskdefs.optional.net.FTP$LanguageCode serverLanguageCode)
Sets the serverLanguageCode attribute.

param
serverLanguageCode configuration to be set, unless it is null or empty string, in which case ignored.
see
org.apache.commons.net.ftp.FTPClientConfig

        if (serverLanguageCode != null && !serverLanguageCode.equals("")) {
            this.serverLanguageCodeConfig = serverLanguageCode;
            configurationHasBeenSet();
        }
    
public voidsetServerTimeZoneConfig(java.lang.String serverTimeZoneId)
Sets the serverTimeZoneConfig attribute.

param
serverTimeZoneId configuration to be set, unless it is null or empty string, in which case ignored.
see
org.apache.commons.net.ftp.FTPClientConfig

        if (serverTimeZoneId != null && !serverTimeZoneId.equals("")) {
            this.serverTimeZoneConfig = serverTimeZoneId;
            configurationHasBeenSet();
        }
    
public voidsetShortMonthNamesConfig(java.lang.String shortMonthNames)
Sets the shortMonthNamesConfig attribute

param
shortMonthNames configuration to be set, unless it is null or empty string, in which case ignored.
see
org.apache.commons.net.ftp.FTPClientConfig

        if (shortMonthNames != null && !shortMonthNames.equals("")) {
            this.shortMonthNamesConfig = shortMonthNames;
            configurationHasBeenSet();
        }
    
public voidsetSiteCommand(java.lang.String siteCommand)
Sets the siteCommand attribute. This attribute names the command that will be executed if the action is "site".

param
siteCommand The siteCommand to set.

        this.siteCommand = siteCommand;
    
public voidsetSkipFailedTransfers(boolean skipFailedTransfers)
If true, enables unsuccessful file put, delete and get operations to be skipped with a warning and the remainder of the files still transferred.

param
skipFailedTransfers true if failures in transfers are ignored.

        this.skipFailedTransfers = skipFailedTransfers;
    
public voidsetSystemTypeKey(org.apache.tools.ant.taskdefs.optional.net.FTP$FTPSystemType systemKey)
Sets the systemTypeKey attribute. Method for setting FTPClientConfig remote system key.

param
systemKey the key to be set - BUT if blank the default value of null (which signifies "autodetect") will be kept.
see
org.apache.commons.net.ftp.FTPClientConfig

        if (systemKey != null && !systemKey.getValue().equals("")) {
            this.systemTypeKey = systemKey;
            configurationHasBeenSet();
        }
    
public voidsetTimeDiffAuto(boolean timeDiffAuto)
"true" to find out automatically the time difference between local and remote machine. This requires right to create and delete a temporary file in the remote directory.

param
timeDiffAuto true = find automatically the time diff
since
ant 1.6

        this.timeDiffAuto = timeDiffAuto;
    
public voidsetTimeDiffMillis(long timeDiffMillis)
number of milliseconds to add to the time on the remote machine to get the time on the local machine. use in conjunction with newer

param
timeDiffMillis number of milliseconds
since
ant 1.6

        this.timeDiffMillis = timeDiffMillis;
    
public voidsetTimestampGranularity(org.apache.tools.ant.taskdefs.optional.net.FTP$Granularity timestampGranularity)
Sets the timestampGranularity attribute

param
timestampGranularity The timestampGranularity to set.

        if (null == timestampGranularity || "".equals(timestampGranularity)) {
            return;
        }
        this.timestampGranularity = timestampGranularity;
     
public voidsetUmask(java.lang.String theUmask)
Sets the default mask for file creation on a unix server.

param
theUmask unix style umask for files created on the remote server.

        this.umask = theUmask;
    
public voidsetUserid(java.lang.String userid)
Sets the login user id to use on the specified server.

param
userid remote system userid.

        this.userid = userid;
    
public voidsetVerbose(boolean verbose)
Set to true to receive notification about each file as it is transferred.

param
verbose true if verbose notifications are required.

        this.verbose = verbose;
    
protected inttransferFiles(org.apache.commons.net.ftp.FTPClient ftp, org.apache.tools.ant.types.FileSet fs)
For each file in the fileset, do the appropriate action: send, get, delete, or list.

param
ftp the FTPClient instance used to perform FTP actions
param
fs the fileset on which the actions are performed.
return
the number of files to be transferred.
throws
IOException if there is a problem reading a file
throws
BuildException if there is a problem in the configuration.

        DirectoryScanner ds;
        if (action == SEND_FILES) {
            ds = fs.getDirectoryScanner(getProject());
        } else {
            // warn that selectors are not supported
            if (fs.getSelectors(getProject()).length != 0) {
                getProject().log("selectors are not supported in remote filesets",
                    Project.MSG_WARN);
            }
            ds = new FTPDirectoryScanner(ftp);
            fs.setupDirectoryScanner(ds, getProject());
            ds.setFollowSymlinks(fs.isFollowSymlinks());
            ds.scan();
        }

        String[] dsfiles = null;
        if (action == RM_DIR) {
            dsfiles = ds.getIncludedDirectories();
        } else {
            dsfiles = ds.getIncludedFiles();
        }
        String dir = null;

        if ((ds.getBasedir() == null)
             && ((action == SEND_FILES) || (action == GET_FILES))) {
            throw new BuildException("the dir attribute must be set for send "
                 + "and get actions");
        } else {
            if ((action == SEND_FILES) || (action == GET_FILES)) {
                dir = ds.getBasedir().getAbsolutePath();
            }
        }

        // If we are doing a listing, we need the output stream created now.
        BufferedWriter bw = null;

        try {
            if (action == LIST_FILES) {
                File pd = listing.getParentFile();

                if (!pd.exists()) {
                    pd.mkdirs();
                }
                bw = new BufferedWriter(new FileWriter(listing));
            }
            RetryHandler h = new RetryHandler(this.retriesAllowed, this);
            if (action == RM_DIR) {
                // to remove directories, start by the end of the list
                // the trunk does not let itself be removed before the leaves
                for (int i = dsfiles.length - 1; i >= 0; i--) {
                    final String dsfile = dsfiles[i];
                    executeRetryable(h, new Retryable() {
                        public void execute() throws IOException {
                            rmDir(ftp, dsfile);
                        }
                    }, dsfile);
                }
            } else {
                final BufferedWriter fbw = bw;
                final String fdir = dir;
                if (this.newerOnly) {
                    this.granularityMillis =
                        this.timestampGranularity.getMilliseconds(action);
                }
                for (int i = 0; i < dsfiles.length; i++) {
                    final String dsfile = dsfiles[i];
                    executeRetryable(h, new Retryable() {
                        public void execute() throws IOException {
                            switch (action) {
                                case SEND_FILES:
                                    sendFile(ftp, fdir, dsfile);
                                    break;
                                case GET_FILES:
                                    getFile(ftp, fdir, dsfile);
                                    break;
                                case DEL_FILES:
                                    delFile(ftp, dsfile);
                                    break;
                                case LIST_FILES:
                                    listFile(ftp, fbw, dsfile);
                                    break;
                                case CHMOD:
                                    doSiteCommand(ftp, "chmod " + chmod
                                                  + " " + resolveFile(dsfile));
                                    transferred++;
                                    break;
                                default:
                                    throw new BuildException("unknown ftp action " + action);
                            }
                        }
                    }, dsfile);
                }
            }
        } finally {
            if (bw != null) {
                bw.close();
            }
        }

        return dsfiles.length;
    
protected voidtransferFiles(org.apache.commons.net.ftp.FTPClient ftp)
Sends all files specified by the configured filesets to the remote server.

param
ftp the FTPClient instance used to perform FTP actions
throws
IOException if there is a problem reading a file
throws
BuildException if there is a problem in the configuration.

        transferred = 0;
        skipped = 0;

        if (filesets.size() == 0) {
            throw new BuildException("at least one fileset must be specified.");
        } else {
            // get files from filesets
            for (int i = 0; i < filesets.size(); i++) {
                FileSet fs = (FileSet) filesets.elementAt(i);

                if (fs != null) {
                    transferFiles(ftp, fs);
                }
            }
        }

        log(transferred + " " + ACTION_TARGET_STRS[action] + " "
            + COMPLETED_ACTION_STRS[action]);
        if (skipped != 0) {
            log(skipped + " " + ACTION_TARGET_STRS[action]
                + " were not successfully " + COMPLETED_ACTION_STRS[action]);
        }