1 18 19 package org.apache.tools.ant.taskdefs.optional.ssh; 20 21 import com.jcraft.jsch.Channel; 22 import com.jcraft.jsch.ChannelExec; 23 import com.jcraft.jsch.JSchException; 24 import com.jcraft.jsch.Session; 25 import com.jcraft.jsch.ChannelSftp; 26 import com.jcraft.jsch.SftpProgressMonitor; 27 28 import java.io.IOException ; 29 import java.io.OutputStream ; 30 import java.io.InputStream ; 31 import java.text.NumberFormat ; 32 33 import org.apache.tools.ant.BuildException; 34 35 38 public abstract class AbstractSshMessage { 39 40 private Session session; 41 private boolean verbose; 42 private LogListener listener = new LogListener() { 43 public void log(String message) { 44 } 46 }; 47 48 52 public AbstractSshMessage(Session session) { 53 this(false, session); 54 } 55 56 62 public AbstractSshMessage(boolean verbose, Session session) { 63 this.verbose = verbose; 64 this.session = session; 65 } 66 67 73 protected Channel openExecChannel(String command) throws JSchException { 74 ChannelExec channel = (ChannelExec) session.openChannel("exec"); 75 channel.setCommand(command); 76 77 return channel; 78 } 79 80 85 protected ChannelSftp openSftpChannel() throws JSchException { 86 ChannelSftp channel = (ChannelSftp) session.openChannel("sftp"); 87 88 return channel; 89 } 90 91 96 protected void sendAck(OutputStream out) throws IOException { 97 byte[] buf = new byte[1]; 98 buf[0] = 0; 99 out.write(buf); 100 out.flush(); 101 } 102 103 110 protected void waitForAck(InputStream in) 111 throws IOException , BuildException { 112 int b = in.read(); 113 114 118 if (b == -1) { 119 throw new BuildException("No response from server"); 121 } else if (b != 0) { 122 StringBuffer sb = new StringBuffer (); 123 124 int c = in.read(); 125 while (c > 0 && c != '\n') { 126 sb.append((char) c); 127 c = in.read(); 128 } 129 130 if (b == 1) { 131 throw new BuildException("server indicated an error: " 132 + sb.toString()); 133 } else if (b == 2) { 134 throw new BuildException("server indicated a fatal error: " 135 + sb.toString()); 136 } else { 137 throw new BuildException("unknown response, code " + b 138 + " message: " + sb.toString()); 139 } 140 } 141 } 142 143 148 public abstract void execute() throws IOException , JSchException; 149 150 154 public void setLogListener(LogListener aListener) { 155 listener = aListener; 156 } 157 158 162 protected void log(String message) { 163 listener.log(message); 164 } 165 166 172 protected void logStats(long timeStarted, 173 long timeEnded, 174 long totalLength) { 175 double duration = (timeEnded - timeStarted) / 1000.0; 176 NumberFormat format = NumberFormat.getNumberInstance(); 177 format.setMaximumFractionDigits(2); 178 format.setMinimumFractionDigits(1); 179 listener.log("File transfer time: " + format.format(duration) 180 + " Average Rate: " + format.format(totalLength / duration) 181 + " B/s"); 182 } 183 184 189 protected final boolean getVerbose() { 190 return verbose; 191 } 192 193 201 protected final int trackProgress(long filesize, long totalLength, 202 int percentTransmitted) { 203 204 int percent = (int) Math.round(Math.floor((totalLength 205 / (double) filesize) * 100)); 206 207 if (percent > percentTransmitted) { 208 if (filesize < 1048576) { 209 if (percent % 10 == 0) { 210 if (percent == 100) { 211 System.out.println(" 100%"); 212 } else { 213 System.out.print("*"); 214 } 215 } 216 } else { 217 if (percent == 50) { 218 System.out.println(" 50%"); 219 } else if (percent == 100) { 220 System.out.println(" 100%"); 221 } else { 222 System.out.print("."); 223 } 224 } 225 } 226 227 return percent; 228 } 229 230 private ProgressMonitor monitor = null; 231 232 236 protected SftpProgressMonitor getProgressMonitor() { 237 if (monitor == null) { 238 monitor = new ProgressMonitor(); 239 } 240 return monitor; 241 } 242 243 private class ProgressMonitor implements SftpProgressMonitor { 244 private long initFileSize = 0; 245 private long totalLength = 0; 246 private int percentTransmitted = 0; 247 248 public void init(int op, String src, String dest, long max) { 249 initFileSize = max; 250 totalLength = 0; 251 percentTransmitted = 0; 252 } 253 254 public boolean count(long len) { 255 totalLength += len; 256 percentTransmitted = trackProgress(initFileSize, 257 totalLength, 258 percentTransmitted); 259 return true; 260 } 261 262 public void end() { 263 } 264 265 public long getTotalLength() { 266 return totalLength; 267 } 268 } 269 } 270 | Popular Tags |