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

AbstractSshMessage

public abstract class AbstractSshMessage extends Object
Abstract class for ssh upload and download

Fields Summary
private com.jcraft.jsch.Session
session
private boolean
verbose
private LogListener
listener
private ProgressMonitor
monitor
Constructors Summary
public AbstractSshMessage(com.jcraft.jsch.Session session)
Constructor for AbstractSshMessage

param
session the ssh session to use


                   
       
        this(false, session);
    
public AbstractSshMessage(boolean verbose, com.jcraft.jsch.Session session)
Constructor for AbstractSshMessage

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

        this.verbose = verbose;
        this.session = session;
    
Methods Summary
public abstract voidexecute()
Carry out the transfer.

throws
IOException on I/O errors
throws
JSchException on ssh errors

protected com.jcraft.jsch.SftpProgressMonitorgetProgressMonitor()
Get the progress monitor.

return
the progress monitor.


                 
       
        if (monitor == null) {
            monitor = new ProgressMonitor();
        }
        return monitor;
    
protected final booleangetVerbose()
Is the verbose attribute set.

return
true if the verbose attribute is set
since
Ant 1.6.2

        return verbose;
    
protected voidlog(java.lang.String message)
Log a message to the log listener.

param
message the message to log

        listener.log(message);
    
protected voidlogStats(long timeStarted, long timeEnded, long totalLength)
Log transfer stats to the log listener.

param
timeStarted the time started
param
timeEnded the finishing time
param
totalLength the total length

        double duration = (timeEnded - timeStarted) / 1000.0;
        NumberFormat format = NumberFormat.getNumberInstance();
        format.setMaximumFractionDigits(2);
        format.setMinimumFractionDigits(1);
        listener.log("File transfer time: " + format.format(duration)
            + " Average Rate: " + format.format(totalLength / duration)
            + " B/s");
    
protected com.jcraft.jsch.ChannelopenExecChannel(java.lang.String command)
Open an ssh channel.

param
command the command to use
return
the channel
throws
JSchException on error

        ChannelExec channel = (ChannelExec) session.openChannel("exec");
        channel.setCommand(command);

        return channel;
    
protected com.jcraft.jsch.ChannelSftpopenSftpChannel()
Open an ssh sftp channel.

return
the channel
throws
JSchException on error

        ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");

        return channel;
    
protected voidsendAck(java.io.OutputStream out)
Send an ack.

param
out the output stream to use
throws
IOException on error

        byte[] buf = new byte[1];
        buf[0] = 0;
        out.write(buf);
        out.flush();
    
public voidsetLogListener(LogListener aListener)
Set a log listener.

param
aListener the log listener

        listener = aListener;
    
protected final inttrackProgress(long filesize, long totalLength, int percentTransmitted)
Track progress every 10% if 100kb < filesize < 1mb. For larger files track progress for every percent transmitted.

param
filesize the size of the file been transmitted
param
totalLength the total transmission size
param
percentTransmitted the current percent transmitted
return
the percent that the file is of the total


        int percent = (int) Math.round(Math.floor((totalLength
                                                   / (double) filesize) * 100));

        if (percent > percentTransmitted) {
            if (filesize < 1048576) {
                if (percent % 10 == 0) {
                    if (percent == 100) {
                        System.out.println(" 100%");
                    } else {
                        System.out.print("*");
                    }
                }
            } else {
                if (percent == 50) {
                    System.out.println(" 50%");
                } else if (percent == 100) {
                    System.out.println(" 100%");
                } else {
                    System.out.print(".");
                }
            }
        }

        return percent;
    
protected voidwaitForAck(java.io.InputStream in)
Reads the response, throws a BuildException if the response indicates an error.

param
in the input stream to use
throws
IOException on I/O error
throws
BuildException on other errors

        int b = in.read();

        // b may be 0 for success,
        //          1 for error,
        //          2 for fatal error,

        if (b == -1) {
            // didn't receive any response
            throw new BuildException("No response from server");
        } else if (b != 0) {
            StringBuffer sb = new StringBuffer();

            int c = in.read();
            while (c > 0 && c != '\n") {
                sb.append((char) c);
                c = in.read();
            }

            if (b == 1) {
                throw new BuildException("server indicated an error: "
                                         + sb.toString());
            } else if (b == 2) {
                throw new BuildException("server indicated a fatal error: "
                                         + sb.toString());
            } else {
                throw new BuildException("unknown response, code " + b
                                         + " message: " + sb.toString());
            }
        }