Changeset 2512
- Timestamp:
- Jun 18, 2014, 10:52:55 AM (9 years ago)
- Location:
- extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/ssh
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/ssh/CopyFilesToBase.java
r2417 r2512 1 1 package net.sf.basedb.reggie.ssh; 2 2 3 import java.io.InputStream; 3 4 import java.util.Arrays; 4 5 … … 7 8 8 9 import net.schmizz.sshj.SSHClient; 10 import net.schmizz.sshj.sftp.SFTPClient; 9 11 import net.sf.basedb.core.AnyToAny; 10 12 import net.sf.basedb.core.Application; … … 99 101 DbControl dc = null; 100 102 SSHClient ssh = null; 103 SFTPClient sftp = null; 101 104 try 102 105 { … … 116 119 try 117 120 { 121 sftp = ssh.newSFTPClient(); 118 122 // Calculate total file size 119 123 long totalSize = 0; … … 123 127 totalSize += ((Number)jsonFile.get("size")).longValue(); 124 128 } 125 System.out.println("item AnyToAny: " + item);126 129 long copiedSoFar = 0; 127 130 int numCopiedFiles = 0; … … 171 174 file.setRemoved(false); 172 175 173 // Actual SCP transfer 174 from.downloadFile(ssh, rootFolder+"/"+path, new BaseFileDestFile(file)); 176 InputStream in = from.readFile(sftp, rootFolder+"/"+path); 177 try 178 { 179 file.upload(in, false); 180 } 181 finally 182 { 183 in.close(); 184 } 175 185 } 176 186 … … 233 243 { 234 244 if (dc != null) dc.close(); 245 if (sftp != null) SshUtil.close(sftp); 235 246 if (ssh != null) SshUtil.close(ssh); 236 247 } -
extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/ssh/SshHost.java
r2361 r2512 3 3 import java.io.ByteArrayOutputStream; 4 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.io.OutputStream; 5 7 import java.security.PublicKey; 6 8 import java.text.SimpleDateFormat; 7 9 import java.util.Date; 10 import java.util.EnumSet; 8 11 import java.util.HashMap; 9 12 import java.util.Map; … … 20 23 import net.schmizz.sshj.connection.channel.direct.Session; 21 24 import net.schmizz.sshj.connection.channel.direct.Session.Command; 25 import net.schmizz.sshj.sftp.FileAttributes; 26 import net.schmizz.sshj.sftp.OpenMode; 27 import net.schmizz.sshj.sftp.RemoteFile; 28 import net.schmizz.sshj.sftp.SFTPClient; 22 29 import net.schmizz.sshj.transport.verification.PromiscuousVerifier; 23 30 import net.schmizz.sshj.xfer.LocalDestFile; … … 26 33 import net.sf.basedb.reggie.converter.StringToDateConverter; 27 34 import net.sf.basedb.util.FileCopyRunnable; 35 import net.sf.basedb.util.uri.CloseResourceInputStream; 28 36 29 37 /** … … 326 334 } 327 335 336 public OutputStream writeFile(SFTPClient sftp, String toPath, FileAttributes attributes) 337 { 338 if (logger.isDebugEnabled()) 339 { 340 logger.debug("Writing to " + getAddress() + ":" + getPort() + ":" + toPath); 341 } 342 try 343 { 344 String toDir = toPath.substring(0, toPath.lastIndexOf("/")); 345 sftp.mkdirs(toDir); 346 RemoteFile rf = sftp.open(toPath, EnumSet.of(OpenMode.CREAT, OpenMode.TRUNC, OpenMode.WRITE), attributes); 347 return new CloseResourceOutputStream(rf.new RemoteFileOutputStream(), rf); 348 } 349 catch (IOException ex) 350 { 351 logger.error("File upload failed", ex); 352 throw new RuntimeException(ex); 353 } 354 finally 355 {} 356 } 357 328 358 public void uploadFile(SSHClient ssh, LocalSourceFile file, String toPath) 329 359 { … … 357 387 } 358 388 389 /** 390 Read from a file on the remote server. This method need an 391 SFTPClient object which can be created from SSHClient.newSFTPClient(). 392 393 @param sftp A SFTPClient instance connected to the remote host 394 @param fromPath The path of the remote file 395 @return An InputStream reading from the remote file 396 */ 397 public InputStream readFile(SFTPClient sftp, String fromPath) 398 { 399 if (logger.isDebugEnabled()) 400 { 401 logger.debug("Reading file '" + fromPath + "' from " + getAddress() + ":" + getPort()); 402 } 403 try 404 { 405 RemoteFile rf = sftp.open(fromPath, EnumSet.of(OpenMode.READ)); 406 return new CloseResourceInputStream(rf.new RemoteFileInputStream(), rf); 407 } 408 catch (IOException ex) 409 { 410 logger.error("File download failed", ex); 411 throw new RuntimeException(ex); 412 } 413 finally 414 {} 415 } 416 417 /** 418 Download a remote file to a local destination. 419 @param ssh A SSHClient instance connected to the remote host 420 @param fromPath The path of the remote file 421 @param file The local destination the remote file is downloaded to 422 */ 359 423 public void downloadFile(SSHClient ssh, String fromPath, LocalDestFile file) 360 424 {
Note: See TracChangeset
for help on using the changeset viewer.