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

ScpFromMessageBySftp

public class ScpFromMessageBySftp extends ScpFromMessage
A helper object representing an scp download.

Fields Summary
private String
remoteFile
private File
localFile
private boolean
isRecursive
private boolean
verbose
Constructors Summary
public ScpFromMessageBySftp(boolean verbose, com.jcraft.jsch.Session session, String aRemoteFile, File aLocalFile, boolean recursive)
Constructor for ScpFromMessageBySftp.

param
verbose if true log extra information
param
session the Scp session to use
param
aRemoteFile the remote file name
param
aLocalFile the local file
param
recursive if true use recursion
since
Ant 1.7


                                                 
      
                                 
                                 
                                 
                                  
        super(verbose, session);
        this.verbose = verbose;
        this.remoteFile = aRemoteFile;
        this.localFile = aLocalFile;
        this.isRecursive = recursive;
    
public ScpFromMessageBySftp(com.jcraft.jsch.Session session, String aRemoteFile, File aLocalFile, boolean recursive)
Constructor for ScpFromMessageBySftp.

param
session the Scp session to use
param
aRemoteFile the remote file name
param
aLocalFile the local file
param
recursive if true use recursion

        this(false, session, aRemoteFile, aLocalFile, recursive);
    
Methods Summary
public voidexecute()
Carry out the transfer.

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

        ChannelSftp channel = openSftpChannel();
        try {
            channel.connect();
            try {
                SftpATTRS attrs = channel.stat(remoteFile);
                if (attrs.isDir() && !remoteFile.endsWith("/")) {
                    remoteFile = remoteFile + "/";
                }
            } catch (SftpException ee) {
                // Ignored
            }
            getDir(channel, remoteFile, localFile);
        } catch (SftpException e) {
            throw new JSchException(e.toString());
        } finally {
            if (channel != null) {
                channel.disconnect();
            }
        }
        log("done\n");
    
private voidgetDir(com.jcraft.jsch.ChannelSftp channel, java.lang.String remoteFile, java.io.File localFile)

        String pwd = remoteFile;
        if (remoteFile.lastIndexOf('/") != -1) {
            if (remoteFile.length() > 1) {
                pwd = remoteFile.substring(0, remoteFile.lastIndexOf('/"));
            }
        }
        channel.cd(pwd);
        if (!localFile.exists()) {
            localFile.mkdirs();
        }
        java.util.Vector files = channel.ls(remoteFile);
        for (int i = 0; i < files.size(); i++) {
            ChannelSftp.LsEntry le = (ChannelSftp.LsEntry) files.elementAt(i);
            String name = le.getFilename();
            if (le.getAttrs().isDir()) {
                if (name.equals(".") || name.equals("..")) {
                    continue;
                }
                getDir(channel,
                       channel.pwd() + "/" + name + "/",
                       new File(localFile, le.getFilename()));
            } else {
                getFile(channel, le, localFile);
            }
        }
        channel.cd("..");
    
private voidgetFile(com.jcraft.jsch.ChannelSftp channel, ChannelSftp.LsEntry le, java.io.File localFile)

        String remoteFile = le.getFilename();
        if (!localFile.exists()) {
            String path = localFile.getAbsolutePath();
            int i = 0;
            if ((i = path.lastIndexOf(File.pathSeparator)) != -1) {
                if (path.length() > File.pathSeparator.length()) {
                    new File(path.substring(0, i)).mkdirs();
                }
            }
        }

        if (localFile.isDirectory()) {
            localFile = new File(localFile, remoteFile);
        }

        long startTime = System.currentTimeMillis();
        long totalLength = le.getAttrs().getSize();

        SftpProgressMonitor monitor = null;
        boolean trackProgress = getVerbose() && totalLength > 102400;
        if (trackProgress) {
            monitor = getProgressMonitor();
        }
        try {
            log("Receiving: " + remoteFile + " : " + le.getAttrs().getSize());
            channel.get(remoteFile, localFile.getAbsolutePath(), monitor);
        } finally {
            long endTime = System.currentTimeMillis();
            logStats(startTime, endTime, (int) totalLength);
        }