FileDocCategorySizeDatePackage
ScpToMessage.javaAPI DocApache Ant 1.709017Wed Dec 13 06:16:20 GMT 2006org.apache.tools.ant.taskdefs.optional.ssh

ScpToMessage

public class ScpToMessage extends AbstractSshMessage
Utility class to carry out an upload scp transfer.

Fields Summary
private static final int
BUFFER_SIZE
private File
localFile
private String
remotePath
private List
directoryList
Constructors Summary
public ScpToMessage(com.jcraft.jsch.Session session)
Constructor for ScpToMessage

param
session the ssh session to use


                   
       
        super(session);
    
public ScpToMessage(boolean verbose, com.jcraft.jsch.Session session)
Constructor for ScpToMessage

param
verbose if true do verbose logging
param
session the ssh session to use
since
Ant 1.7

        super(verbose, session);
    
public ScpToMessage(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.6.2

        this(verbose, session, aRemotePath);

        this.localFile = aLocalFile;
    
public ScpToMessage(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.6.2

        this(verbose, session, aRemotePath);

        this.directoryList = aDirectoryList;
    
private ScpToMessage(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 ScpToMessage(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 ScpToMessage(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()

        Channel channel = openExecChannel("scp -r -d -t " + remotePath);
        try {
            OutputStream out = channel.getOutputStream();
            InputStream in = channel.getInputStream();

            channel.connect();

            waitForAck(in);
            for (Iterator i = directoryList.iterator(); i.hasNext();) {
                Directory current = (Directory) i.next();
                sendDirectory(current, in, out);
            }
        } finally {
            if (channel != null) {
                channel.disconnect();
            }
        }
    
private voiddoSingleTransfer()

        String cmd = "scp -t " + remotePath;
        Channel channel = openExecChannel(cmd);
        try {

            OutputStream out = channel.getOutputStream();
            InputStream in = channel.getInputStream();

            channel.connect();

            waitForAck(in);
            sendFileToRemote(localFile, in, out);
        } 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(Directory current, java.io.InputStream in, java.io.OutputStream out)

        for (Iterator fileIt = current.filesIterator(); fileIt.hasNext();) {
            sendFileToRemote((File) fileIt.next(), in, out);
        }
        for (Iterator dirIt = current.directoryIterator(); dirIt.hasNext();) {
            Directory dir = (Directory) dirIt.next();
            sendDirectoryToRemote(dir, in, out);
        }
    
private voidsendDirectoryToRemote(Directory directory, java.io.InputStream in, java.io.OutputStream out)

        String command = "D0755 0 ";
        command += directory.getDirectory().getName();
        command += "\n";

        out.write(command.getBytes());
        out.flush();

        waitForAck(in);
        sendDirectory(directory, in, out);
        out.write("E\n".getBytes());
        waitForAck(in);
    
private voidsendFileToRemote(java.io.File localFile, java.io.InputStream in, java.io.OutputStream out)

        // send "C0644 filesize filename", where filename should not include '/'
        long filesize = localFile.length();
        String command = "C0644 " + filesize + " ";
        command += localFile.getName();
        command += "\n";

        out.write(command.getBytes());
        out.flush();

        waitForAck(in);

        // send a content of lfile
        FileInputStream fis = new FileInputStream(localFile);
        byte[] buf = new byte[BUFFER_SIZE];
        long startTime = System.currentTimeMillis();
        long totalLength = 0;

        // only track progress for files larger than 100kb in verbose mode
        boolean trackProgress = getVerbose() && filesize > 102400;
        // since filesize keeps on decreasing we have to store the
        // initial filesize
        long initFilesize = filesize;
        int percentTransmitted = 0;

        try {
            if (this.getVerbose()) {
                log("Sending: " + localFile.getName() + " : " + localFile.length());
            }
            while (true) {
                int len = fis.read(buf, 0, buf.length);
                if (len <= 0) {
                    break;
                }
                out.write(buf, 0, len);
                totalLength += len;

                if (trackProgress) {
                    percentTransmitted = trackProgress(initFilesize,
                                                       totalLength,
                                                       percentTransmitted);
                }
            }
            out.flush();
            sendAck(out);
            waitForAck(in);
        } finally {
            if (this.getVerbose()) {
                long endTime = System.currentTimeMillis();
                logStats(startTime, endTime, totalLength);
            }
            fis.close();
        }