source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/taskdefs/optional/ssh/ScpToMessage.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 7.0 KB
Line 
1/*
2 * Copyright 2003-2004 The Apache Software Foundation
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
18package org.apache.tools.ant.taskdefs.optional.ssh;
19
20import com.jcraft.jsch.Channel;
21import com.jcraft.jsch.Session;
22import com.jcraft.jsch.JSchException;
23import java.io.File;
24import java.io.IOException;
25import java.io.InputStream;
26import java.io.FileInputStream;
27import java.io.OutputStream;
28import java.util.List;
29import java.util.Iterator;
30
31public class ScpToMessage extends AbstractSshMessage {
32
33 private final int BUFFER_SIZE = 1024;
34
35 private File localFile;
36 private String remotePath;
37 private List directoryList;
38
39 /**
40 * @since Ant 1.6.2
41 */
42 public ScpToMessage(boolean verbose,
43 Session session,
44 File aLocalFile,
45 String aRemotePath) {
46 this(verbose, session, aRemotePath);
47
48 this.localFile = aLocalFile;
49 }
50
51 /**
52 * @since Ant 1.6.2
53 */
54 public ScpToMessage(boolean verbose,
55 Session session,
56 List aDirectoryList,
57 String aRemotePath) {
58 this(verbose, session, aRemotePath);
59
60 this.directoryList = aDirectoryList;
61 }
62
63 /**
64 * @since Ant 1.6.2
65 */
66 private ScpToMessage(boolean verbose,
67 Session session,
68 String aRemotePath) {
69 super(verbose, session);
70 this.remotePath = aRemotePath;
71 }
72
73 public ScpToMessage(Session session,
74 File aLocalFile,
75 String aRemotePath) {
76 this(false, session, aLocalFile, aRemotePath);
77 }
78
79 public ScpToMessage(Session session,
80 List aDirectoryList,
81 String aRemotePath) {
82 this(false, session, aDirectoryList, aRemotePath);
83 }
84
85 public void execute() throws IOException, JSchException {
86 if (directoryList != null) {
87 doMultipleTransfer();
88 }
89 if (localFile != null) {
90 doSingleTransfer();
91 }
92 log("done.\n");
93 }
94
95 private void doSingleTransfer() throws IOException, JSchException {
96 String cmd = "scp -t " + remotePath;
97 Channel channel = openExecChannel(cmd);
98 try {
99
100 OutputStream out = channel.getOutputStream();
101 InputStream in = channel.getInputStream();
102
103 channel.connect();
104
105 waitForAck(in);
106 sendFileToRemote(localFile, in, out);
107 } finally {
108 if (channel != null) {
109 channel.disconnect();
110 }
111 }
112 }
113
114 private void doMultipleTransfer() throws IOException, JSchException {
115 Channel channel = openExecChannel("scp -r -d -t " + remotePath);
116 try {
117 OutputStream out = channel.getOutputStream();
118 InputStream in = channel.getInputStream();
119
120 channel.connect();
121
122 waitForAck(in);
123 for (Iterator i = directoryList.iterator(); i.hasNext();) {
124 Directory current = (Directory) i.next();
125 sendDirectory(current, in, out);
126 }
127 } finally {
128 if (channel != null) {
129 channel.disconnect();
130 }
131 }
132 }
133
134 private void sendDirectory(Directory current,
135 InputStream in,
136 OutputStream out) throws IOException {
137 for (Iterator fileIt = current.filesIterator(); fileIt.hasNext();) {
138 sendFileToRemote((File) fileIt.next(), in, out);
139 }
140 for (Iterator dirIt = current.directoryIterator(); dirIt.hasNext();) {
141 Directory dir = (Directory) dirIt.next();
142 sendDirectoryToRemote(dir, in, out);
143 }
144 }
145
146 private void sendDirectoryToRemote(Directory directory,
147 InputStream in,
148 OutputStream out) throws IOException {
149 String command = "D0755 0 ";
150 command += directory.getDirectory().getName();
151 command += "\n";
152
153 out.write(command.getBytes());
154 out.flush();
155
156 waitForAck(in);
157 sendDirectory(directory, in, out);
158 out.write("E\n".getBytes());
159 waitForAck(in);
160 }
161
162 private void sendFileToRemote(File localFile,
163 InputStream in,
164 OutputStream out) throws IOException {
165 // send "C0644 filesize filename", where filename should not include '/'
166 int filesize = (int) localFile.length();
167 String command = "C0644 " + filesize + " ";
168 command += localFile.getName();
169 command += "\n";
170
171 out.write(command.getBytes());
172 out.flush();
173
174 waitForAck(in);
175
176 // send a content of lfile
177 FileInputStream fis = new FileInputStream(localFile);
178 byte[] buf = new byte[BUFFER_SIZE];
179 long startTime = System.currentTimeMillis();
180 int totalLength = 0;
181
182 // only track progress for files larger than 100kb in verbose mode
183 boolean trackProgress = getVerbose() && filesize > 102400;
184 // since filesize keeps on decreasing we have to store the
185 // initial filesize
186 int initFilesize = filesize;
187 int percentTransmitted = 0;
188
189 try {
190 log("Sending: " + localFile.getName() + " : " + localFile.length());
191 while (true) {
192 int len = fis.read(buf, 0, buf.length);
193 if (len <= 0) {
194 break;
195 }
196 out.write(buf, 0, len);
197 totalLength += len;
198
199 if (trackProgress) {
200 percentTransmitted = trackProgress(initFilesize,
201 totalLength,
202 percentTransmitted);
203 }
204 }
205 out.flush();
206 sendAck(out);
207 waitForAck(in);
208 } finally {
209 long endTime = System.currentTimeMillis();
210 logStats(startTime, endTime, totalLength);
211 fis.close();
212 }
213 }
214
215 public File getLocalFile() {
216 return localFile;
217 }
218
219 public String getRemotePath() {
220 return remotePath;
221 }
222}
Note: See TracBrowser for help on using the repository browser.