FileDocCategorySizeDatePackage
ScpToMessageBySftp.javaAPI DocApache Ant 1.707755Wed Dec 13 06:16:18 GMT 2006org.apache.tools.ant.taskdefs.optional.ssh

ScpToMessageBySftp

public class ScpToMessageBySftp extends ScpToMessage
Utility class to carry out an upload by sftp.

Fields Summary
private File
localFile
private String
remotePath
private List
directoryList
Constructors Summary
public ScpToMessageBySftp(boolean verbose, com.jcraft.jsch.Session session, File aLocalFile, String aRemotePath)
Constructor for a local file to remote.

param
verbose if true do verbose logging
param
session the scp session to use
param
aLocalFile the local file
param
aRemotePath the remote path
since
Ant 1.7

        this(verbose, session, aRemotePath);

        this.localFile = aLocalFile;
    
public ScpToMessageBySftp(boolean verbose, com.jcraft.jsch.Session session, List aDirectoryList, String aRemotePath)
Constructor for a local directories to remote.

param
verbose if true do verbose logging
param
session the scp session to use
param
aDirectoryList a list of directories
param
aRemotePath the remote path
since
Ant 1.7

        this(verbose, session, aRemotePath);

        this.directoryList = aDirectoryList;
    
private ScpToMessageBySftp(boolean verbose, com.jcraft.jsch.Session session, String aRemotePath)
Constructor for ScpToMessage.

param
verbose if true do verbose logging
param
session the scp session to use
param
aRemotePath the remote path
since
Ant 1.6.2

        super(verbose, session);
        this.remotePath = aRemotePath;
    
public ScpToMessageBySftp(com.jcraft.jsch.Session session, File aLocalFile, String aRemotePath)
Constructor for ScpToMessage.

param
session the scp session to use
param
aLocalFile the local file
param
aRemotePath the remote path

        this(false, session, aLocalFile, aRemotePath);
    
public ScpToMessageBySftp(com.jcraft.jsch.Session session, List aDirectoryList, String aRemotePath)
Constructor for ScpToMessage.

param
session the scp session to use
param
aDirectoryList a list of directories
param
aRemotePath the remote path

        this(false, session, aDirectoryList, aRemotePath);
    
Methods Summary
private voiddoMultipleTransfer()

        ChannelSftp channel = openSftpChannel();
        try {
            channel.connect();

            try {
                channel.cd(remotePath);
                for (Iterator i = directoryList.iterator(); i.hasNext();) {
                    Directory current = (Directory) i.next();
                    sendDirectory(channel, current);
                }
            } catch (SftpException e) {
                throw new JSchException(e.toString());
            }
        } finally {
            if (channel != null) {
                channel.disconnect();
            }
        }
    
private voiddoSingleTransfer()

        ChannelSftp channel = openSftpChannel();
        try {
            channel.connect();
            try {
                sendFileToRemote(channel, localFile, remotePath);
            } catch (SftpException e) {
                throw new JSchException(e.toString());
            }
        } finally {
            if (channel != null) {
                channel.disconnect();
            }
        }
    
public voidexecute()
Carry out the transfer.

throws
IOException on i/o errors
throws
JSchException on errors detected by scp

        if (directoryList != null) {
            doMultipleTransfer();
        }
        if (localFile != null) {
            doSingleTransfer();
        }
        log("done.\n");
    
public java.io.FilegetLocalFile()
Get the local file.

return
the local file.

        return localFile;
    
public java.lang.StringgetRemotePath()
Get the remote path.

return
the remote path.

        return remotePath;
    
private voidsendDirectory(com.jcraft.jsch.ChannelSftp channel, Directory current)

        for (Iterator fileIt = current.filesIterator(); fileIt.hasNext();) {
            sendFileToRemote(channel, (File) fileIt.next(), null);
        }
        for (Iterator dirIt = current.directoryIterator(); dirIt.hasNext();) {
            Directory dir = (Directory) dirIt.next();
            sendDirectoryToRemote(channel, dir);
        }
    
private voidsendDirectoryToRemote(com.jcraft.jsch.ChannelSftp channel, Directory directory)

        String dir = directory.getDirectory().getName();
        try {
            channel.stat(dir);
        } catch (SftpException e) {
            // dir does not exist.
            if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) {
                channel.mkdir(dir);
            }
        }
        channel.cd(dir);
        sendDirectory(channel, directory);
        channel.cd("..");
    
private voidsendFileToRemote(com.jcraft.jsch.ChannelSftp channel, java.io.File localFile, java.lang.String remotePath)

        long filesize = localFile.length();

        if (remotePath == null) {
            remotePath = localFile.getName();
        }

        long startTime = System.currentTimeMillis();
        long totalLength = filesize;

        // only track progress for files larger than 100kb in verbose mode
        boolean trackProgress = getVerbose() && filesize > 102400;

        SftpProgressMonitor monitor = null;
        if (trackProgress) {
            monitor = getProgressMonitor();
        }

        try {
            if (this.getVerbose()) {
                log("Sending: " + localFile.getName() + " : " + filesize);
            }
            channel.put(localFile.getAbsolutePath(), remotePath, monitor);
        } finally {
            if (this.getVerbose()) {
                long endTime = System.currentTimeMillis();
                logStats(startTime, endTime, (int) totalLength);
            }
        }