]>
git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/org.argeo.slc.support/src/org/argeo/slc/jsch/ScpFrom.java
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
.argeo
.api
.cms
.CmsLog
;
11 import org
.argeo
.slc
.SlcException
;
12 import org
.springframework
.core
.io
.Resource
;
14 import com
.jcraft
.jsch
.Channel
;
15 import com
.jcraft
.jsch
.ChannelExec
;
16 import com
.jcraft
.jsch
.Session
;
18 public class ScpFrom
extends AbstractJschTask
{
19 private final static CmsLog log
= CmsLog
.getLog(ScpFrom
.class);
21 private Resource localResource
;
22 private String remotePath
;
23 private Boolean mkdir
= false;
25 public void run(Session session
) {
26 if (localResource
!= null) {
29 lFile
= localResource
.getFile();
30 } catch (IOException e
) {
31 throw new SlcException("Cannot interpret resource "
32 + localResource
+ " as file.", e
);
34 downloadFile(session
, lFile
, remotePath
);
38 protected void downloadFile(Session session
, File localFile
,
40 OutputStream out
= null;
41 OutputStream channelOut
;
42 InputStream channelIn
;
44 // exec 'scp -f rfile' remotely
45 String command
= "scp -f " + remoteFile
;
46 Channel channel
= session
.openChannel("exec");
47 ((ChannelExec
) channel
).setCommand(command
);
49 // get I/O streams for remote scp
50 channelOut
= channel
.getOutputStream();
51 channelIn
= channel
.getInputStream();
55 byte[] buf
= new byte[1024];
59 channelOut
.write(buf
, 0, 1);
63 int c
= checkAck(channelIn
);
69 channelIn
.read(buf
, 0, 5);
73 if (channelIn
.read(buf
, 0, 1) < 0) {
79 filesize
= filesize
* 10L + (long) (buf
[0] - '0');
82 String remoteFileName
= null;
83 for (int i
= 0;; i
++) {
84 channelIn
.read(buf
, i
, 1);
85 if (buf
[i
] == (byte) 0x0a) {
86 remoteFileName
= new String(buf
, 0, i
);
91 // System.out.println("filesize="+filesize+", file="+file);
95 channelOut
.write(buf
, 0, 1);
98 // Create a s adirectory if it doesn't exists
99 if (!localFile
.exists() && mkdir
)
102 // read a content of lfile
103 String localPath
= localFile
.isDirectory() ? localFile
105 + File
.separator
+ remoteFileName
: localFile
.getPath();
107 out
= new FileOutputStream(localPath
);
110 if (buf
.length
< filesize
)
113 foo
= (int) filesize
;
114 foo
= channelIn
.read(buf
, 0, foo
);
119 out
.write(buf
, 0, foo
);
129 channelOut
.write(buf
, 0, 1);
132 if (log
.isDebugEnabled())
133 log
.debug("Finished downloading " + remoteFile
+ " on "
134 + getSshTarget() + " to " + localPath
);
137 channel
.disconnect();
138 // session.disconnect();
139 } catch (Exception e
) {
140 throw new SlcException("Cannot download " + remoteFile
+ " to "
143 IOUtils
.closeQuietly(out
);
147 public void setLocalResource(Resource localFile
) {
148 this.localResource
= localFile
;
151 public void setRemotePath(String remoteFile
) {
152 this.remotePath
= remoteFile
;
155 public void setMkdir(Boolean mkdir
) {