]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/ScpFrom.java
Improve Linux support
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / jsch / ScpFrom.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 package org.argeo.slc.jsch;
18
19 import java.io.File;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24
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;
30
31 import com.jcraft.jsch.Channel;
32 import com.jcraft.jsch.ChannelExec;
33 import com.jcraft.jsch.Session;
34
35 public class ScpFrom extends AbstractJschTask {
36 private final static Log log = LogFactory.getLog(ScpFrom.class);
37
38 private Resource localResource;
39 private String remotePath;
40 private Boolean mkdir = false;
41
42 public void run(Session session) {
43 if (localResource != null) {
44 File lFile;
45 try {
46 lFile = localResource.getFile();
47 } catch (IOException e) {
48 throw new SlcException("Cannot interpret resource "
49 + localResource + " as file.", e);
50 }
51 downloadFile(session, lFile, remotePath);
52 }
53 }
54
55 protected void downloadFile(Session session, File localFile,
56 String remoteFile) {
57 OutputStream out = null;
58 OutputStream channelOut;
59 InputStream channelIn;
60 try {
61 // exec 'scp -f rfile' remotely
62 String command = "scp -f " + remoteFile;
63 Channel channel = session.openChannel("exec");
64 ((ChannelExec) channel).setCommand(command);
65
66 // get I/O streams for remote scp
67 channelOut = channel.getOutputStream();
68 channelIn = channel.getInputStream();
69
70 channel.connect();
71
72 byte[] buf = new byte[1024];
73
74 // send '\0'
75 buf[0] = 0;
76 channelOut.write(buf, 0, 1);
77 channelOut.flush();
78
79 while (true) {
80 int c = checkAck(channelIn);
81 if (c != 'C') {
82 break;
83 }
84
85 // read '0644 '
86 channelIn.read(buf, 0, 5);
87
88 long filesize = 0L;
89 while (true) {
90 if (channelIn.read(buf, 0, 1) < 0) {
91 // error
92 break;
93 }
94 if (buf[0] == ' ')
95 break;
96 filesize = filesize * 10L + (long) (buf[0] - '0');
97 }
98
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);
104 break;
105 }
106 }
107
108 // System.out.println("filesize="+filesize+", file="+file);
109
110 // send '\0'
111 buf[0] = 0;
112 channelOut.write(buf, 0, 1);
113 channelOut.flush();
114
115 // Create a s adirectory if it doesn't exists
116 if (!localFile.exists() && mkdir)
117 localFile.mkdirs();
118
119 // read a content of lfile
120 String localPath = localFile.isDirectory() ? localFile
121 .getPath()
122 + File.separator + remoteFileName : localFile.getPath();
123
124 out = new FileOutputStream(localPath);
125 int foo;
126 while (true) {
127 if (buf.length < filesize)
128 foo = buf.length;
129 else
130 foo = (int) filesize;
131 foo = channelIn.read(buf, 0, foo);
132 if (foo < 0) {
133 // error
134 break;
135 }
136 out.write(buf, 0, foo);
137 filesize -= foo;
138 if (filesize == 0L)
139 break;
140 }
141
142 checkAck(channelIn);
143
144 // send '\0'
145 buf[0] = 0;
146 channelOut.write(buf, 0, 1);
147 channelOut.flush();
148
149 if (log.isDebugEnabled())
150 log.debug("Finished downloading " + remoteFile + " on "
151 + getSshTarget() + " to " + localPath);
152 }
153
154 channel.disconnect();
155 // session.disconnect();
156 } catch (Exception e) {
157 throw new SlcException("Cannot download " + remoteFile + " to "
158 + localFile, e);
159 } finally {
160 IOUtils.closeQuietly(out);
161 }
162 }
163
164 public void setLocalResource(Resource localFile) {
165 this.localResource = localFile;
166 }
167
168 public void setRemotePath(String remoteFile) {
169 this.remotePath = remoteFile;
170 }
171
172 public void setMkdir(Boolean mkdir) {
173 this.mkdir = mkdir;
174 }
175 }