KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23
24 import com.jcraft.jsch.JSchException;
25 import com.jcraft.jsch.Session;
26 import com.jcraft.jsch.ChannelSftp;
27 import com.jcraft.jsch.SftpException;
28 import com.jcraft.jsch.SftpATTRS;
29 import com.jcraft.jsch.SftpProgressMonitor;
30
31 /**
32  * A helper object representing an scp download.
33  */

34 public class ScpFromMessageBySftp extends ScpFromMessage {
35
36     private String JavaDoc remoteFile;
37     private File JavaDoc localFile;
38     private boolean isRecursive = false;
39     private boolean verbose = false;
40
41     /**
42      * Constructor for ScpFromMessageBySftp.
43      * @param verbose if true log extra information
44      * @param session the Scp session to use
45      * @param aRemoteFile the remote file name
46      * @param aLocalFile the local file
47      * @param recursive if true use recursion
48      * @since Ant 1.7
49      */

50     public ScpFromMessageBySftp(boolean verbose,
51                                 Session session,
52                                 String JavaDoc aRemoteFile,
53                                 File JavaDoc aLocalFile,
54                                 boolean recursive) {
55         super(verbose, session);
56         this.verbose = verbose;
57         this.remoteFile = aRemoteFile;
58         this.localFile = aLocalFile;
59         this.isRecursive = recursive;
60     }
61
62     /**
63      * Constructor for ScpFromMessageBySftp.
64      * @param session the Scp session to use
65      * @param aRemoteFile the remote file name
66      * @param aLocalFile the local file
67      * @param recursive if true use recursion
68      */

69     public ScpFromMessageBySftp(Session session,
70                                 String JavaDoc aRemoteFile,
71                                 File JavaDoc aLocalFile,
72                                 boolean recursive) {
73         this(false, session, aRemoteFile, aLocalFile, recursive);
74     }
75
76     /**
77      * Carry out the transfer.
78      * @throws IOException on i/o errors
79      * @throws JSchException on errors detected by scp
80      */

81     public void execute() throws IOException JavaDoc, JSchException {
82         ChannelSftp channel = openSftpChannel();
83         try {
84             channel.connect();
85             try {
86                 SftpATTRS attrs = channel.stat(remoteFile);
87                 if (attrs.isDir() && !remoteFile.endsWith("/")) {
88                     remoteFile = remoteFile + "/";
89                 }
90             } catch (SftpException ee) {
91                 // Ignored
92
}
93             getDir(channel, remoteFile, localFile);
94         } catch (SftpException e) {
95             throw new JSchException(e.toString());
96         } finally {
97             if (channel != null) {
98                 channel.disconnect();
99             }
100         }
101         log("done\n");
102     }
103
104     private void getDir(ChannelSftp channel,
105                         String JavaDoc remoteFile,
106                         File JavaDoc localFile) throws IOException JavaDoc, SftpException {
107         String JavaDoc pwd = remoteFile;
108         if (remoteFile.lastIndexOf('/') != -1) {
109             if (remoteFile.length() > 1) {
110                 pwd = remoteFile.substring(0, remoteFile.lastIndexOf('/'));
111             }
112         }
113         channel.cd(pwd);
114         if (!localFile.exists()) {
115             localFile.mkdirs();
116         }
117         java.util.Vector JavaDoc files = channel.ls(remoteFile);
118         for (int i = 0; i < files.size(); i++) {
119             ChannelSftp.LsEntry le = (ChannelSftp.LsEntry) files.elementAt(i);
120             String JavaDoc name = le.getFilename();
121             if (le.getAttrs().isDir()) {
122                 if (name.equals(".") || name.equals("..")) {
123                     continue;
124                 }
125                 getDir(channel,
126                        channel.pwd() + "/" + name + "/",
127                        new File JavaDoc(localFile, le.getFilename()));
128             } else {
129                 getFile(channel, le, localFile);
130             }
131         }
132         channel.cd("..");
133     }
134
135     private void getFile(ChannelSftp channel,
136                          ChannelSftp.LsEntry le,
137                          File JavaDoc localFile) throws IOException JavaDoc, SftpException {
138         String JavaDoc remoteFile = le.getFilename();
139         if (!localFile.exists()) {
140             String JavaDoc path = localFile.getAbsolutePath();
141             int i = 0;
142             if ((i = path.lastIndexOf(File.pathSeparator)) != -1) {
143                 if (path.length() > File.pathSeparator.length()) {
144                     new File JavaDoc(path.substring(0, i)).mkdirs();
145                 }
146             }
147         }
148
149         if (localFile.isDirectory()) {
150             localFile = new File JavaDoc(localFile, remoteFile);
151         }
152
153         long startTime = System.currentTimeMillis();
154         long totalLength = le.getAttrs().getSize();
155
156         SftpProgressMonitor monitor = null;
157         boolean trackProgress = getVerbose() && totalLength > 102400;
158         if (trackProgress) {
159             monitor = getProgressMonitor();
160         }
161         try {
162             log("Receiving: " + remoteFile + " : " + le.getAttrs().getSize());
163             channel.get(remoteFile, localFile.getAbsolutePath(), monitor);
164         } finally {
165             long endTime = System.currentTimeMillis();
166             logStats(startTime, endTime, (int) totalLength);
167         }
168     }
169 }
170
Popular Tags