1 18 19 package org.apache.tools.ant.taskdefs.optional.ssh; 20 21 import com.jcraft.jsch.Channel; 22 import com.jcraft.jsch.Session; 23 import com.jcraft.jsch.JSchException; 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.FileInputStream ; 28 import java.io.OutputStream ; 29 import java.util.List ; 30 import java.util.Iterator ; 31 32 35 public class ScpToMessage extends AbstractSshMessage { 36 37 private static final int BUFFER_SIZE = 1024; 38 39 private File localFile; 40 private String remotePath; 41 private List directoryList; 42 43 47 public ScpToMessage(Session session) { 48 super(session); 49 } 50 51 57 public ScpToMessage(boolean verbose, Session session) { 58 super(verbose, session); 59 } 60 61 69 public ScpToMessage(boolean verbose, 70 Session session, 71 File aLocalFile, 72 String aRemotePath) { 73 this(verbose, session, aRemotePath); 74 75 this.localFile = aLocalFile; 76 } 77 78 86 public ScpToMessage(boolean verbose, 87 Session session, 88 List aDirectoryList, 89 String aRemotePath) { 90 this(verbose, session, aRemotePath); 91 92 this.directoryList = aDirectoryList; 93 } 94 95 102 private ScpToMessage(boolean verbose, 103 Session session, 104 String aRemotePath) { 105 super(verbose, session); 106 this.remotePath = aRemotePath; 107 } 108 109 115 public ScpToMessage(Session session, 116 File aLocalFile, 117 String aRemotePath) { 118 this(false, session, aLocalFile, aRemotePath); 119 } 120 121 127 public ScpToMessage(Session session, 128 List aDirectoryList, 129 String aRemotePath) { 130 this(false, session, aDirectoryList, aRemotePath); 131 } 132 133 138 public void execute() throws IOException , JSchException { 139 if (directoryList != null) { 140 doMultipleTransfer(); 141 } 142 if (localFile != null) { 143 doSingleTransfer(); 144 } 145 log("done.\n"); 146 } 147 148 private void doSingleTransfer() throws IOException , JSchException { 149 String cmd = "scp -t " + remotePath; 150 Channel channel = openExecChannel(cmd); 151 try { 152 153 OutputStream out = channel.getOutputStream(); 154 InputStream in = channel.getInputStream(); 155 156 channel.connect(); 157 158 waitForAck(in); 159 sendFileToRemote(localFile, in, out); 160 } finally { 161 if (channel != null) { 162 channel.disconnect(); 163 } 164 } 165 } 166 167 private void doMultipleTransfer() throws IOException , JSchException { 168 Channel channel = openExecChannel("scp -r -d -t " + remotePath); 169 try { 170 OutputStream out = channel.getOutputStream(); 171 InputStream in = channel.getInputStream(); 172 173 channel.connect(); 174 175 waitForAck(in); 176 for (Iterator i = directoryList.iterator(); i.hasNext();) { 177 Directory current = (Directory) i.next(); 178 sendDirectory(current, in, out); 179 } 180 } finally { 181 if (channel != null) { 182 channel.disconnect(); 183 } 184 } 185 } 186 187 private void sendDirectory(Directory current, 188 InputStream in, 189 OutputStream out) throws IOException { 190 for (Iterator fileIt = current.filesIterator(); fileIt.hasNext();) { 191 sendFileToRemote((File ) fileIt.next(), in, out); 192 } 193 for (Iterator dirIt = current.directoryIterator(); dirIt.hasNext();) { 194 Directory dir = (Directory) dirIt.next(); 195 sendDirectoryToRemote(dir, in, out); 196 } 197 } 198 199 private void sendDirectoryToRemote(Directory directory, 200 InputStream in, 201 OutputStream out) throws IOException { 202 String command = "D0755 0 "; 203 command += directory.getDirectory().getName(); 204 command += "\n"; 205 206 out.write(command.getBytes()); 207 out.flush(); 208 209 waitForAck(in); 210 sendDirectory(directory, in, out); 211 out.write("E\n".getBytes()); 212 waitForAck(in); 213 } 214 215 private void sendFileToRemote(File localFile, 216 InputStream in, 217 OutputStream out) throws IOException { 218 long filesize = localFile.length(); 220 String command = "C0644 " + filesize + " "; 221 command += localFile.getName(); 222 command += "\n"; 223 224 out.write(command.getBytes()); 225 out.flush(); 226 227 waitForAck(in); 228 229 FileInputStream fis = new FileInputStream (localFile); 231 byte[] buf = new byte[BUFFER_SIZE]; 232 long startTime = System.currentTimeMillis(); 233 long totalLength = 0; 234 235 boolean trackProgress = getVerbose() && filesize > 102400; 237 long initFilesize = filesize; 240 int percentTransmitted = 0; 241 242 try { 243 if (this.getVerbose()) { 244 log("Sending: " + localFile.getName() + " : " + localFile.length()); 245 } 246 while (true) { 247 int len = fis.read(buf, 0, buf.length); 248 if (len <= 0) { 249 break; 250 } 251 out.write(buf, 0, len); 252 totalLength += len; 253 254 if (trackProgress) { 255 percentTransmitted = trackProgress(initFilesize, 256 totalLength, 257 percentTransmitted); 258 } 259 } 260 out.flush(); 261 sendAck(out); 262 waitForAck(in); 263 } finally { 264 if (this.getVerbose()) { 265 long endTime = System.currentTimeMillis(); 266 logStats(startTime, endTime, totalLength); 267 } 268 fis.close(); 269 } 270 } 271 272 276 public File getLocalFile() { 277 return localFile; 278 } 279 280 284 public String getRemotePath() { 285 return remotePath; 286 } 287 } 288 | Popular Tags |