KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > ssh > ScpToMessageBySftp


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs.optional.ssh;
20
21 import com.jcraft.jsch.Session;
22 import com.jcraft.jsch.ChannelSftp;
23 import com.jcraft.jsch.JSchException;
24 import com.jcraft.jsch.SftpException;
25 import com.jcraft.jsch.SftpProgressMonitor;
26
27 import java.io.File JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 /**
33  * Utility class to carry out an upload by sftp.
34  */

35 public class ScpToMessageBySftp extends ScpToMessage/*AbstractSshMessage*/ {
36
37     private File JavaDoc localFile;
38     private String JavaDoc remotePath;
39     private List JavaDoc directoryList;
40
41     /**
42      * Constructor for a local file to remote.
43      * @param verbose if true do verbose logging
44      * @param session the scp session to use
45      * @param aLocalFile the local file
46      * @param aRemotePath the remote path
47      * @since Ant 1.7
48      */

49     public ScpToMessageBySftp(boolean verbose,
50                               Session session,
51                               File JavaDoc aLocalFile,
52                               String JavaDoc aRemotePath) {
53         this(verbose, session, aRemotePath);
54
55         this.localFile = aLocalFile;
56     }
57
58     /**
59      * Constructor for a local directories to remote.
60      * @param verbose if true do verbose logging
61      * @param session the scp session to use
62      * @param aDirectoryList a list of directories
63      * @param aRemotePath the remote path
64      * @since Ant 1.7
65      */

66     public ScpToMessageBySftp(boolean verbose,
67                               Session session,
68                               List JavaDoc aDirectoryList,
69                               String JavaDoc aRemotePath) {
70         this(verbose, session, aRemotePath);
71
72         this.directoryList = aDirectoryList;
73     }
74
75     /**
76      * Constructor for ScpToMessage.
77      * @param verbose if true do verbose logging
78      * @param session the scp session to use
79      * @param aRemotePath the remote path
80      * @since Ant 1.6.2
81      */

82     private ScpToMessageBySftp(boolean verbose,
83                                Session session,
84                                String JavaDoc aRemotePath) {
85         super(verbose, session);
86         this.remotePath = aRemotePath;
87     }
88
89     /**
90      * Constructor for ScpToMessage.
91      * @param session the scp session to use
92      * @param aLocalFile the local file
93      * @param aRemotePath the remote path
94      */

95     public ScpToMessageBySftp(Session session,
96                               File JavaDoc aLocalFile,
97                               String JavaDoc aRemotePath) {
98         this(false, session, aLocalFile, aRemotePath);
99     }
100
101     /**
102      * Constructor for ScpToMessage.
103      * @param session the scp session to use
104      * @param aDirectoryList a list of directories
105      * @param aRemotePath the remote path
106      */

107     public ScpToMessageBySftp(Session session,
108                               List JavaDoc aDirectoryList,
109                               String JavaDoc aRemotePath) {
110         this(false, session, aDirectoryList, aRemotePath);
111     }
112
113     /**
114      * Carry out the transfer.
115      * @throws IOException on i/o errors
116      * @throws JSchException on errors detected by scp
117      */

118     public void execute() throws IOException JavaDoc, JSchException {
119         if (directoryList != null) {
120             doMultipleTransfer();
121         }
122         if (localFile != null) {
123             doSingleTransfer();
124         }
125         log("done.\n");
126     }
127
128     private void doSingleTransfer() throws IOException JavaDoc, JSchException {
129         ChannelSftp channel = openSftpChannel();
130         try {
131             channel.connect();
132             try {
133                 sendFileToRemote(channel, localFile, remotePath);
134             } catch (SftpException e) {
135                 throw new JSchException(e.toString());
136             }
137         } finally {
138             if (channel != null) {
139                 channel.disconnect();
140             }
141         }
142     }
143
144     private void doMultipleTransfer() throws IOException JavaDoc, JSchException {
145         ChannelSftp channel = openSftpChannel();
146         try {
147             channel.connect();
148
149             try {
150                 channel.cd(remotePath);
151                 for (Iterator JavaDoc i = directoryList.iterator(); i.hasNext();) {
152                     Directory current = (Directory) i.next();
153                     sendDirectory(channel, current);
154                 }
155             } catch (SftpException e) {
156                 throw new JSchException(e.toString());
157             }
158         } finally {
159             if (channel != null) {
160                 channel.disconnect();
161             }
162         }
163     }
164
165     private void sendDirectory(ChannelSftp channel,
166                                Directory current)
167         throws IOException JavaDoc, SftpException {
168         for (Iterator JavaDoc fileIt = current.filesIterator(); fileIt.hasNext();) {
169             sendFileToRemote(channel, (File JavaDoc) fileIt.next(), null);
170         }
171         for (Iterator JavaDoc dirIt = current.directoryIterator(); dirIt.hasNext();) {
172             Directory dir = (Directory) dirIt.next();
173             sendDirectoryToRemote(channel, dir);
174         }
175     }
176
177     private void sendDirectoryToRemote(ChannelSftp channel,
178                                        Directory directory)
179         throws IOException JavaDoc, SftpException {
180         String JavaDoc dir = directory.getDirectory().getName();
181         try {
182             channel.stat(dir);
183         } catch (SftpException e) {
184             // dir does not exist.
185
if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) {
186                 channel.mkdir(dir);
187             }
188         }
189         channel.cd(dir);
190         sendDirectory(channel, directory);
191         channel.cd("..");
192     }
193
194     private void sendFileToRemote(ChannelSftp channel,
195                                   File JavaDoc localFile,
196                                   String JavaDoc remotePath)
197         throws IOException JavaDoc, SftpException {
198         long filesize = localFile.length();
199
200         if (remotePath == null) {
201             remotePath = localFile.getName();
202         }
203
204         long startTime = System.currentTimeMillis();
205         long totalLength = filesize;
206
207         // only track progress for files larger than 100kb in verbose mode
208
boolean trackProgress = getVerbose() && filesize > 102400;
209
210         SftpProgressMonitor monitor = null;
211         if (trackProgress) {
212             monitor = getProgressMonitor();
213         }
214
215         try {
216             if (this.getVerbose()) {
217                 log("Sending: " + localFile.getName() + " : " + filesize);
218             }
219             channel.put(localFile.getAbsolutePath(), remotePath, monitor);
220         } finally {
221             if (this.getVerbose()) {
222                 long endTime = System.currentTimeMillis();
223                 logStats(startTime, endTime, (int) totalLength);
224             }
225         }
226     }
227
228     /**
229      * Get the local file.
230      * @return the local file.
231      */

232     public File JavaDoc getLocalFile() {
233         return localFile;
234     }
235
236     /**
237      * Get the remote path.
238      * @return the remote path.
239      */

240     public String JavaDoc getRemotePath() {
241         return remotePath;
242     }
243 }
244
Popular Tags