]>
git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/ScpFrom.java
3f83778a887d5c7d1714b322ae4586b48680cadd
2 * Copyright (C) 2007-2012 Argeo GmbH
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.
16 package org
.argeo
.slc
.jsch
;
19 import java
.io
.FileOutputStream
;
20 import java
.io
.IOException
;
21 import java
.io
.InputStream
;
22 import java
.io
.OutputStream
;
24 import org
.apache
.commons
.io
.IOUtils
;
25 import org
.apache
.commons
.logging
.Log
;
26 import org
.apache
.commons
.logging
.LogFactory
;
27 import org
.argeo
.slc
.SlcException
;
28 import org
.springframework
.core
.io
.Resource
;
30 import com
.jcraft
.jsch
.Channel
;
31 import com
.jcraft
.jsch
.ChannelExec
;
32 import com
.jcraft
.jsch
.Session
;
34 public class ScpFrom
extends AbstractJschTask
{
35 private final static Log log
= LogFactory
.getLog(ScpFrom
.class);
37 private Resource localResource
;
38 private String remotePath
;
39 private Boolean mkdir
= false;
41 public void run(Session session
) {
42 if (localResource
!= null) {
45 lFile
= localResource
.getFile();
46 } catch (IOException e
) {
47 throw new SlcException("Cannot interpret resource "
48 + localResource
+ " as file.", e
);
50 downloadFile(session
, lFile
, remotePath
);
54 protected void downloadFile(Session session
, File localFile
,
56 OutputStream out
= null;
57 OutputStream channelOut
;
58 InputStream channelIn
;
60 // exec 'scp -f rfile' remotely
61 String command
= "scp -f " + remoteFile
;
62 Channel channel
= session
.openChannel("exec");
63 ((ChannelExec
) channel
).setCommand(command
);
65 // get I/O streams for remote scp
66 channelOut
= channel
.getOutputStream();
67 channelIn
= channel
.getInputStream();
71 byte[] buf
= new byte[1024];
75 channelOut
.write(buf
, 0, 1);
79 int c
= checkAck(channelIn
);
85 channelIn
.read(buf
, 0, 5);
89 if (channelIn
.read(buf
, 0, 1) < 0) {
95 filesize
= filesize
* 10L + (long) (buf
[0] - '0');
98 String remoteFileName
= null;
99 for (int i
= 0;; i
++) {
100 channelIn
.read(buf
, i
, 1);
101 if (buf
[i
] == (byte) 0x0a) {
102 remoteFileName
= new String(buf
, 0, i
);
107 // System.out.println("filesize="+filesize+", file="+file);
111 channelOut
.write(buf
, 0, 1);
114 // Create a s adirectory if it doesn't exists
115 if (!localFile
.exists() && mkdir
)
118 // read a content of lfile
119 String localPath
= localFile
.isDirectory() ? localFile
121 + File
.separator
+ remoteFileName
: localFile
.getPath();
123 out
= new FileOutputStream(localPath
);
126 if (buf
.length
< filesize
)
129 foo
= (int) filesize
;
130 foo
= channelIn
.read(buf
, 0, foo
);
135 out
.write(buf
, 0, foo
);
145 channelOut
.write(buf
, 0, 1);
148 if (log
.isDebugEnabled())
149 log
.debug("Finished downloading " + remoteFile
+ " on "
150 + getSshTarget() + " to " + localPath
);
153 channel
.disconnect();
154 // session.disconnect();
155 } catch (Exception e
) {
156 throw new SlcException("Cannot download " + remoteFile
+ " to "
159 IOUtils
.closeQuietly(out
);
163 public void setLocalResource(Resource localFile
) {
164 this.localResource
= localFile
;
167 public void setRemotePath(String remoteFile
) {
168 this.remotePath
= remoteFile
;
171 public void setMkdir(Boolean mkdir
) {