]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/ScpTo.java
Introduce revision build numbers
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / jsch / ScpTo.java
1 package org.argeo.slc.jsch;
2
3 import java.io.File;
4 import java.io.InputStream;
5 import java.io.OutputStream;
6
7 import org.apache.commons.io.IOUtils;
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10 import org.argeo.slc.SlcException;
11 import org.springframework.core.io.Resource;
12
13 import com.jcraft.jsch.Channel;
14 import com.jcraft.jsch.ChannelExec;
15 import com.jcraft.jsch.Session;
16
17 public class ScpTo extends AbstractJschTask {
18 private final static Log log = LogFactory.getLog(ScpTo.class);
19
20 private Resource localFile;
21 private String remoteFile;
22
23 public void run() {
24 InputStream in = null;
25 OutputStream channelOut;
26 InputStream channelIn;
27
28 Session session = openSession();
29 try {
30
31 // exec 'scp -t rfile' remotely
32 String command = "scp -p -t " + remoteFile;
33 Channel channel = session.openChannel("exec");
34 ((ChannelExec) channel).setCommand(command);
35
36 // get I/O streams for remote scp
37 channelOut = channel.getOutputStream();
38 channelIn = channel.getInputStream();
39
40 channel.connect();
41 checkAck(channelIn);
42
43 // send "C0644 filesize filename", where filename should not include
44 // '/'
45 File lFile = localFile.getFile();
46 long filesize = lFile.length();
47 command = "C0644 " + filesize + " ";
48 int index = lFile.getPath().lastIndexOf('/');
49 if (index > 0) {
50 command += lFile.getPath().substring(index + 1);
51 } else {
52 command += lFile.getPath();
53 }
54 command += "\n";
55
56 channelOut.write(command.getBytes());
57 channelOut.flush();
58 checkAck(channelIn);
59
60 if (log.isDebugEnabled())
61 log.debug("Start copy of " + localFile + " to " + remoteFile
62 + " on " + getSshTarget() + "...");
63
64 // send a content of lfile
65 in = localFile.getInputStream();
66 byte[] buf = new byte[1024];
67 long cycleCount = 0;
68 while (true) {
69 int len = in.read(buf, 0, buf.length);
70 if (len <= 0)
71 break;
72 channelOut.write(buf, 0, len); // out.flush();
73 if ((cycleCount % 1024) == 0)// each 1 MB
74 System.out.print('#');
75 cycleCount++;
76 }
77 // send '\0'
78 buf[0] = 0;
79 channelOut.write(buf, 0, 1);
80 channelOut.flush();
81 checkAck(channelIn);
82
83 if (log.isDebugEnabled())
84 log.debug("Finished copy of " + localFile + " to " + remoteFile
85 + " on " + getSshTarget() + "...");
86
87 IOUtils.closeQuietly(channelOut);
88
89 channel.disconnect();
90 session.disconnect();
91
92 } catch (Exception e) {
93 throw new SlcException("Cannot copy " + localFile + " to "
94 + remoteFile, e);
95 } finally {
96 IOUtils.closeQuietly(in);
97 }
98 }
99
100 public void setLocalFile(Resource localFile) {
101 this.localFile = localFile;
102 }
103
104 public void setRemoteFile(String remoteFile) {
105 this.remoteFile = remoteFile;
106 }
107
108 }