]>
git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/ScpFrom.java
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org
.argeo
.slc
.jsch
;
20 import java
.io
.FileOutputStream
;
21 import java
.io
.IOException
;
22 import java
.io
.InputStream
;
23 import java
.io
.OutputStream
;
25 import org
.apache
.commons
.io
.IOUtils
;
26 import org
.apache
.commons
.logging
.Log
;
27 import org
.apache
.commons
.logging
.LogFactory
;
28 import org
.argeo
.slc
.SlcException
;
29 import org
.springframework
.core
.io
.Resource
;
31 import com
.jcraft
.jsch
.Channel
;
32 import com
.jcraft
.jsch
.ChannelExec
;
33 import com
.jcraft
.jsch
.Session
;
35 public class ScpFrom
extends AbstractJschTask
{
36 private final static Log log
= LogFactory
.getLog(ScpFrom
.class);
38 private Resource localResource
;
39 private String remotePath
;
40 private Boolean mkdir
= false;
42 public void run(Session session
) {
43 if (localResource
!= null) {
46 lFile
= localResource
.getFile();
47 } catch (IOException e
) {
48 throw new SlcException("Cannot interpret resource "
49 + localResource
+ " as file.", e
);
51 downloadFile(session
, lFile
, remotePath
);
55 protected void downloadFile(Session session
, File localFile
,
57 OutputStream out
= null;
58 OutputStream channelOut
;
59 InputStream channelIn
;
61 // exec 'scp -f rfile' remotely
62 String command
= "scp -f " + remoteFile
;
63 Channel channel
= session
.openChannel("exec");
64 ((ChannelExec
) channel
).setCommand(command
);
66 // get I/O streams for remote scp
67 channelOut
= channel
.getOutputStream();
68 channelIn
= channel
.getInputStream();
72 byte[] buf
= new byte[1024];
76 channelOut
.write(buf
, 0, 1);
80 int c
= checkAck(channelIn
);
86 channelIn
.read(buf
, 0, 5);
90 if (channelIn
.read(buf
, 0, 1) < 0) {
96 filesize
= filesize
* 10L + (long) (buf
[0] - '0');
99 String remoteFileName
= null;
100 for (int i
= 0;; i
++) {
101 channelIn
.read(buf
, i
, 1);
102 if (buf
[i
] == (byte) 0x0a) {
103 remoteFileName
= new String(buf
, 0, i
);
108 // System.out.println("filesize="+filesize+", file="+file);
112 channelOut
.write(buf
, 0, 1);
115 // Create a s adirectory if it doesn't exists
116 if (!localFile
.exists() && mkdir
)
119 // read a content of lfile
120 String localPath
= localFile
.isDirectory() ? localFile
122 + File
.separator
+ remoteFileName
: localFile
.getPath();
124 out
= new FileOutputStream(localPath
);
127 if (buf
.length
< filesize
)
130 foo
= (int) filesize
;
131 foo
= channelIn
.read(buf
, 0, foo
);
136 out
.write(buf
, 0, foo
);
146 channelOut
.write(buf
, 0, 1);
149 if (log
.isDebugEnabled())
150 log
.debug("Finished downloading " + remoteFile
+ " on "
151 + getSshTarget() + " to " + localPath
);
154 channel
.disconnect();
155 // session.disconnect();
156 } catch (Exception e
) {
157 throw new SlcException("Cannot download " + remoteFile
+ " to "
160 IOUtils
.closeQuietly(out
);
164 public void setLocalResource(Resource localFile
) {
165 this.localResource
= localFile
;
168 public void setRemotePath(String remoteFile
) {
169 this.remotePath
= remoteFile
;
172 public void setMkdir(Boolean mkdir
) {