Search in sources :

Example 1 with RetryHandler

use of org.apache.tools.ant.util.RetryHandler in project ant by apache.

the class FTP method execute.

/**
 * Runs the task.
 *
 * @throws BuildException if the task fails or is not configured
 *         correctly.
 */
@Override
public void execute() throws BuildException {
    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.setRemoteVerificationEnabled(enableRemoteVerification);
        ftp.connect(server, port);
        if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
            throw new BuildException("FTP connection failed: %s", 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.BINARY_FILE_TYPE);
            if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                throw new BuildException("could not set transfer type: %s", 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: %s", 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: %s", ftp.getReplyString());
            }
        }
        // a legacy file system.
        if (this.initialSiteCommand != null) {
            final FTPClient lftp = ftp;
            executeRetryable(new RetryHandler(this.retriesAllowed, this), () -> doSiteCommand(lftp, FTP.this.initialSiteCommand), "initial site command: " + this.initialSiteCommand);
        }
        if (umask != null) {
            final FTPClient lftp = ftp;
            executeRetryable(new RetryHandler(this.retriesAllowed, this), () -> doSiteCommand(lftp, "umask " + umask), "umask " + umask);
        }
        if (action == MK_DIR) {
            final FTPClient lftp = ftp;
            executeRetryable(new RetryHandler(this.retriesAllowed, this), () -> makeRemoteDir(lftp, remotedir), remotedir);
        } else if (action == SITE_CMD) {
            final FTPClient lftp = ftp;
            executeRetryable(new RetryHandler(this.retriesAllowed, this), () -> doSiteCommand(lftp, FTP.this.siteCommand), "Site Command: " + this.siteCommand);
        } else {
            if (remotedir != null) {
                log("changing the remote directory to " + remotedir, Project.MSG_VERBOSE);
                ftp.changeWorkingDirectory(remotedir);
                if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                    throw new BuildException("could not change remote directory: %s", 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
            }
        }
    }
}
Also used : RetryHandler(org.apache.tools.ant.util.RetryHandler) BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException) FTPClient(org.apache.commons.net.ftp.FTPClient)

Example 2 with RetryHandler

use of org.apache.tools.ant.util.RetryHandler in project ant by apache.

the class FTPTaskMirrorImpl method transferFiles.

/**
 * 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.
 */
protected int transferFiles(final FTPClient ftp, FileSet fs) throws IOException, BuildException {
    DirectoryScanner ds;
    if (task.getAction() == FTPTask.SEND_FILES) {
        ds = fs.getDirectoryScanner(task.getProject());
    } else {
        ds = new FTPDirectoryScanner(ftp);
        fs.setupDirectoryScanner(ds, task.getProject());
        ds.setFollowSymlinks(fs.isFollowSymlinks());
        ds.scan();
    }
    String[] dsfiles = null;
    if (task.getAction() == FTPTask.RM_DIR) {
        dsfiles = ds.getIncludedDirectories();
    } else {
        dsfiles = ds.getIncludedFiles();
    }
    if ((ds.getBasedir() == null) && ((task.getAction() == FTPTask.SEND_FILES) || (task.getAction() == FTPTask.GET_FILES))) {
        throw new BuildException("the dir attribute must be set for send and get actions");
    }
    String dir = null;
    if ((task.getAction() == FTPTask.SEND_FILES) || (task.getAction() == FTPTask.GET_FILES)) {
        dir = ds.getBasedir().getAbsolutePath();
    }
    // If we are doing a listing, we need the output stream created now.
    BufferedWriter bw = null;
    try {
        if (task.getAction() == FTPTask.LIST_FILES) {
            File pd = task.getListing().getParentFile();
            if (!pd.exists()) {
                pd.mkdirs();
            }
            bw = new BufferedWriter(new FileWriter(task.getListing()));
        }
        RetryHandler h = new RetryHandler(task.getRetriesAllowed(), task);
        if (task.getAction() == FTPTask.RM_DIR) {
            // to remove directories, start by the end of the list
            // the trunk does not let itself be removed before the leaves
            Arrays.sort(dsfiles, Comparator.reverseOrder());
            for (final String dsfile : dsfiles) {
                executeRetryable(h, () -> rmDir(ftp, dsfile), dsfile);
            }
        } else {
            final BufferedWriter fbw = bw;
            final String fdir = dir;
            if (task.isNewer()) {
                task.setGranularityMillis(task.getTimestampGranularity().getMilliseconds(task.getAction()));
            }
            for (final String dsfile : dsfiles) {
                executeRetryable(h, () -> {
                    switch(task.getAction()) {
                        case FTPTask.SEND_FILES:
                            sendFile(ftp, fdir, dsfile);
                            break;
                        case FTPTask.GET_FILES:
                            getFile(ftp, fdir, dsfile);
                            break;
                        case FTPTask.DEL_FILES:
                            delFile(ftp, dsfile);
                            break;
                        case FTPTask.LIST_FILES:
                            listFile(ftp, fbw, dsfile);
                            break;
                        case FTPTask.CHMOD:
                            doSiteCommand(ftp, "chmod " + task.getChmod() + " " + resolveFile(dsfile));
                            transferred++;
                            break;
                        default:
                            throw new BuildException("unknown ftp action %s", task.getAction());
                    }
                }, dsfile);
            }
        }
    } finally {
        if (bw != null) {
            bw.close();
        }
    }
    return dsfiles.length;
}
Also used : RetryHandler(org.apache.tools.ant.util.RetryHandler) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) FileWriter(java.io.FileWriter) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) FTPFile(org.apache.commons.net.ftp.FTPFile) BufferedWriter(java.io.BufferedWriter)

Example 3 with RetryHandler

use of org.apache.tools.ant.util.RetryHandler in project ant by apache.

the class FTP method transferFiles.

/**
 * 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.
 */
protected int transferFiles(final FTPClient ftp, FileSet fs) throws IOException, BuildException {
    DirectoryScanner ds;
    if (action == SEND_FILES) {
        ds = fs.getDirectoryScanner(getProject());
    } else {
        ds = new FTPDirectoryScanner(ftp);
        fs.setupDirectoryScanner(ds, getProject());
        ds.setFollowSymlinks(fs.isFollowSymlinks());
        ds.scan();
    }
    String[] dsfiles;
    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");
    }
    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
            Arrays.sort(dsfiles, Comparator.reverseOrder());
            for (final String dsfile : dsfiles) {
                executeRetryable(h, () -> rmDir(ftp, dsfile), dsfile);
            }
        } else {
            final BufferedWriter fbw = bw;
            final String fdir = dir;
            if (this.newerOnly) {
                this.granularityMillis = this.timestampGranularity.getMilliseconds(action);
            }
            for (final String dsfile : dsfiles) {
                executeRetryable(h, () -> {
                    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 {
        FileUtils.close(bw);
    }
    return dsfiles.length;
}
Also used : RetryHandler(org.apache.tools.ant.util.RetryHandler) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) FileWriter(java.io.FileWriter) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) FTPFile(org.apache.commons.net.ftp.FTPFile) BufferedWriter(java.io.BufferedWriter)

Example 4 with RetryHandler

use of org.apache.tools.ant.util.RetryHandler in project ant by apache.

the class FTPTaskMirrorImpl method doFTP.

@Override
public void doFTP() throws BuildException {
    FTPClient ftp = null;
    try {
        task.log("Opening FTP connection to " + task.getServer(), Project.MSG_VERBOSE);
        ftp = new FTPClient();
        if (task.isConfigurationSet()) {
            ftp = FTPConfigurator.configure(ftp, task);
        }
        ftp.setRemoteVerificationEnabled(task.getEnableRemoteVerification());
        ftp.connect(task.getServer(), task.getPort());
        if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
            throw new BuildException("FTP connection failed: %s", ftp.getReplyString());
        }
        task.log("connected", Project.MSG_VERBOSE);
        task.log("logging in to FTP server", Project.MSG_VERBOSE);
        if ((task.getAccount() != null && !ftp.login(task.getUserid(), task.getPassword(), task.getAccount())) || (task.getAccount() == null && !ftp.login(task.getUserid(), task.getPassword()))) {
            throw new BuildException("Could not login to FTP server");
        }
        task.log("login succeeded", Project.MSG_VERBOSE);
        if (task.isBinary()) {
            ftp.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
            if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                throw new BuildException("could not set transfer type: %s", 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: %s", ftp.getReplyString());
            }
        }
        if (task.isPassive()) {
            task.log("entering passive mode", Project.MSG_VERBOSE);
            ftp.enterLocalPassiveMode();
            if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                throw new BuildException("could not enter into passive mode: %s", ftp.getReplyString());
            }
        }
        // a legacy file system.
        if (task.getInitialSiteCommand() != null) {
            final FTPClient lftp = ftp;
            executeRetryable(new RetryHandler(task.getRetriesAllowed(), task), () -> doSiteCommand(lftp, task.getInitialSiteCommand()), "initial site command: " + task.getInitialSiteCommand());
        }
        if (task.getUmask() != null) {
            final FTPClient lftp = ftp;
            executeRetryable(new RetryHandler(task.getRetriesAllowed(), task), () -> doSiteCommand(lftp, "umask " + task.getUmask()), "umask " + task.getUmask());
        }
        if (task.getAction() == FTPTask.MK_DIR) {
            final FTPClient lftp = ftp;
            executeRetryable(new RetryHandler(task.getRetriesAllowed(), task), () -> makeRemoteDir(lftp, task.getRemotedir()), task.getRemotedir());
        } else if (task.getAction() == FTPTask.SITE_CMD) {
            final FTPClient lftp = ftp;
            executeRetryable(new RetryHandler(task.getRetriesAllowed(), task), () -> doSiteCommand(lftp, task.getSiteCommand()), "Site Command: " + task.getSiteCommand());
        } else {
            if (task.getRemotedir() != null) {
                task.log("changing the remote directory", Project.MSG_VERBOSE);
                ftp.changeWorkingDirectory(task.getRemotedir());
                if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                    throw new BuildException("could not change remote directory: %s", ftp.getReplyString());
                }
            }
            if (task.isNewer() && task.isTimeDiffAuto()) {
                // in this case we want to find how much time span there is between local
                // and remote
                task.setTimeDiffMillis(getTimeDiff(ftp));
            }
            task.log(FTPTask.ACTION_STRS[task.getAction()] + " " + FTPTask.ACTION_TARGET_STRS[task.getAction()]);
            transferFiles(ftp);
        }
    } catch (IOException ex) {
        throw new BuildException("error during FTP transfer: " + ex, ex);
    } finally {
        if (ftp != null && ftp.isConnected()) {
            try {
                task.log("disconnecting", Project.MSG_VERBOSE);
                ftp.logout();
                ftp.disconnect();
            } catch (IOException ex) {
            // ignore it
            }
        }
    }
}
Also used : RetryHandler(org.apache.tools.ant.util.RetryHandler) BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException) FTPClient(org.apache.commons.net.ftp.FTPClient)

Aggregations

BuildException (org.apache.tools.ant.BuildException)4 RetryHandler (org.apache.tools.ant.util.RetryHandler)4 BufferedWriter (java.io.BufferedWriter)2 File (java.io.File)2 FileWriter (java.io.FileWriter)2 IOException (java.io.IOException)2 FTPClient (org.apache.commons.net.ftp.FTPClient)2 FTPFile (org.apache.commons.net.ftp.FTPFile)2 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)2