]>
git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/ScpFrom.java
ced80eee03ceeb6fa62d4020b091cc391bfe4ad4
1 package org
.argeo
.slc
.jsch
;
4 import java
.io
.FileOutputStream
;
5 import java
.io
.IOException
;
6 import java
.io
.InputStream
;
7 import java
.io
.OutputStream
;
9 import org
.apache
.commons
.io
.IOUtils
;
10 import org
.apache
.commons
.logging
.Log
;
11 import org
.apache
.commons
.logging
.LogFactory
;
12 import org
.argeo
.slc
.SlcException
;
13 import org
.springframework
.core
.io
.Resource
;
15 import com
.jcraft
.jsch
.Channel
;
16 import com
.jcraft
.jsch
.ChannelExec
;
17 import com
.jcraft
.jsch
.Session
;
19 public class ScpFrom
extends AbstractJschTask
{
20 private final static Log log
= LogFactory
.getLog(ScpFrom
.class);
22 private Resource localResource
;
23 private String remotePath
;
24 private Boolean mkdir
= false;
26 public void run(Session session
) {
27 if (localResource
!= null) {
30 lFile
= localResource
.getFile();
31 } catch (IOException e
) {
32 throw new SlcException("Cannot interpret resource "
33 + localResource
+ " as file.", e
);
35 downloadFile(session
, lFile
, remotePath
);
39 protected void downloadFile(Session session
, File localFile
,
41 OutputStream out
= null;
42 OutputStream channelOut
;
43 InputStream channelIn
;
45 // exec 'scp -f rfile' remotely
46 String command
= "scp -f " + remoteFile
;
47 Channel channel
= session
.openChannel("exec");
48 ((ChannelExec
) channel
).setCommand(command
);
50 // get I/O streams for remote scp
51 channelOut
= channel
.getOutputStream();
52 channelIn
= channel
.getInputStream();
56 byte[] buf
= new byte[1024];
60 channelOut
.write(buf
, 0, 1);
64 int c
= checkAck(channelIn
);
70 channelIn
.read(buf
, 0, 5);
74 if (channelIn
.read(buf
, 0, 1) < 0) {
80 filesize
= filesize
* 10L + (long) (buf
[0] - '0');
83 String remoteFileName
= null;
84 for (int i
= 0;; i
++) {
85 channelIn
.read(buf
, i
, 1);
86 if (buf
[i
] == (byte) 0x0a) {
87 remoteFileName
= new String(buf
, 0, i
);
92 // System.out.println("filesize="+filesize+", file="+file);
96 channelOut
.write(buf
, 0, 1);
99 // Create a s adirectory if it doesn't exists
100 if (!localFile
.exists() && mkdir
)
103 // read a content of lfile
104 String localPath
= localFile
.isDirectory() ? localFile
106 + File
.separator
+ remoteFileName
: localFile
.getPath();
108 out
= new FileOutputStream(localPath
);
111 if (buf
.length
< filesize
)
114 foo
= (int) filesize
;
115 foo
= channelIn
.read(buf
, 0, foo
);
120 out
.write(buf
, 0, foo
);
130 channelOut
.write(buf
, 0, 1);
133 if (log
.isDebugEnabled())
134 log
.debug("Finished downloading " + remoteFile
+ " on "
135 + getSshTarget() + " to " + localPath
);
138 channel
.disconnect();
139 // session.disconnect();
140 } catch (Exception e
) {
141 throw new SlcException("Cannot download " + remoteFile
+ " to "
144 IOUtils
.closeQuietly(out
);
148 public void setLocalResource(Resource localFile
) {
149 this.localResource
= localFile
;
152 public void setRemotePath(String remoteFile
) {
153 this.remotePath
= remoteFile
;
156 public void setMkdir(Boolean mkdir
) {