Source Code Cross Referenced for ScpToMessage.java in  » Build » ANT » org » apache » tools » ant » taskdefs » optional » ssh » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Build » ANT » org.apache.tools.ant.taskdefs.optional.ssh 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         *
017:         */
018:
019:        package org.apache.tools.ant.taskdefs.optional.ssh;
020:
021:        import com.jcraft.jsch.Channel;
022:        import com.jcraft.jsch.Session;
023:        import com.jcraft.jsch.JSchException;
024:        import java.io.File;
025:        import java.io.IOException;
026:        import java.io.InputStream;
027:        import java.io.FileInputStream;
028:        import java.io.OutputStream;
029:        import java.util.List;
030:        import java.util.Iterator;
031:
032:        /**
033:         * Utility class to carry out an upload scp transfer.
034:         */
035:        public class ScpToMessage extends AbstractSshMessage {
036:
037:            private static final int BUFFER_SIZE = 1024;
038:
039:            private File localFile;
040:            private String remotePath;
041:            private List directoryList;
042:
043:            /**
044:             * Constructor for ScpToMessage
045:             * @param session the ssh session to use
046:             */
047:            public ScpToMessage(Session session) {
048:                super (session);
049:            }
050:
051:            /**
052:             * Constructor for ScpToMessage
053:             * @param verbose if true do verbose logging
054:             * @param session the ssh session to use
055:             * @since Ant 1.7
056:             */
057:            public ScpToMessage(boolean verbose, Session session) {
058:                super (verbose, session);
059:            }
060:
061:            /**
062:             * Constructor for a local file to remote.
063:             * @param verbose if true do verbose logging
064:             * @param session the scp session to use
065:             * @param aLocalFile the local file
066:             * @param aRemotePath the remote path
067:             * @since Ant 1.6.2
068:             */
069:            public ScpToMessage(boolean verbose, Session session,
070:                    File aLocalFile, String aRemotePath) {
071:                this (verbose, session, aRemotePath);
072:
073:                this .localFile = aLocalFile;
074:            }
075:
076:            /**
077:             * Constructor for a local directories to remote.
078:             * @param verbose if true do verbose logging
079:             * @param session the scp session to use
080:             * @param aDirectoryList a list of directories
081:             * @param aRemotePath the remote path
082:             * @since Ant 1.6.2
083:             */
084:            public ScpToMessage(boolean verbose, Session session,
085:                    List aDirectoryList, String aRemotePath) {
086:                this (verbose, session, aRemotePath);
087:
088:                this .directoryList = aDirectoryList;
089:            }
090:
091:            /**
092:             * Constructor for ScpToMessage.
093:             * @param verbose if true do verbose logging
094:             * @param session the scp session to use
095:             * @param aRemotePath the remote path
096:             * @since Ant 1.6.2
097:             */
098:            private ScpToMessage(boolean verbose, Session session,
099:                    String aRemotePath) {
100:                super (verbose, session);
101:                this .remotePath = aRemotePath;
102:            }
103:
104:            /**
105:             * Constructor for ScpToMessage.
106:             * @param session the scp session to use
107:             * @param aLocalFile the local file
108:             * @param aRemotePath the remote path
109:             */
110:            public ScpToMessage(Session session, File aLocalFile,
111:                    String aRemotePath) {
112:                this (false, session, aLocalFile, aRemotePath);
113:            }
114:
115:            /**
116:             * Constructor for ScpToMessage.
117:             * @param session the scp session to use
118:             * @param aDirectoryList a list of directories
119:             * @param aRemotePath the remote path
120:             */
121:            public ScpToMessage(Session session, List aDirectoryList,
122:                    String aRemotePath) {
123:                this (false, session, aDirectoryList, aRemotePath);
124:            }
125:
126:            /**
127:             * Carry out the transfer.
128:             * @throws IOException on i/o errors
129:             * @throws JSchException on errors detected by scp
130:             */
131:            public void execute() throws IOException, JSchException {
132:                if (directoryList != null) {
133:                    doMultipleTransfer();
134:                }
135:                if (localFile != null) {
136:                    doSingleTransfer();
137:                }
138:                log("done.\n");
139:            }
140:
141:            private void doSingleTransfer() throws IOException, JSchException {
142:                String cmd = "scp -t " + remotePath;
143:                Channel channel = openExecChannel(cmd);
144:                try {
145:
146:                    OutputStream out = channel.getOutputStream();
147:                    InputStream in = channel.getInputStream();
148:
149:                    channel.connect();
150:
151:                    waitForAck(in);
152:                    sendFileToRemote(localFile, in, out);
153:                } finally {
154:                    if (channel != null) {
155:                        channel.disconnect();
156:                    }
157:                }
158:            }
159:
160:            private void doMultipleTransfer() throws IOException, JSchException {
161:                Channel channel = openExecChannel("scp -r -d -t " + remotePath);
162:                try {
163:                    OutputStream out = channel.getOutputStream();
164:                    InputStream in = channel.getInputStream();
165:
166:                    channel.connect();
167:
168:                    waitForAck(in);
169:                    for (Iterator i = directoryList.iterator(); i.hasNext();) {
170:                        Directory current = (Directory) i.next();
171:                        sendDirectory(current, in, out);
172:                    }
173:                } finally {
174:                    if (channel != null) {
175:                        channel.disconnect();
176:                    }
177:                }
178:            }
179:
180:            private void sendDirectory(Directory current, InputStream in,
181:                    OutputStream out) throws IOException {
182:                for (Iterator fileIt = current.filesIterator(); fileIt
183:                        .hasNext();) {
184:                    sendFileToRemote((File) fileIt.next(), in, out);
185:                }
186:                for (Iterator dirIt = current.directoryIterator(); dirIt
187:                        .hasNext();) {
188:                    Directory dir = (Directory) dirIt.next();
189:                    sendDirectoryToRemote(dir, in, out);
190:                }
191:            }
192:
193:            private void sendDirectoryToRemote(Directory directory,
194:                    InputStream in, OutputStream out) throws IOException {
195:                String command = "D0755 0 ";
196:                command += directory.getDirectory().getName();
197:                command += "\n";
198:
199:                out.write(command.getBytes());
200:                out.flush();
201:
202:                waitForAck(in);
203:                sendDirectory(directory, in, out);
204:                out.write("E\n".getBytes());
205:                waitForAck(in);
206:            }
207:
208:            private void sendFileToRemote(File localFile, InputStream in,
209:                    OutputStream out) throws IOException {
210:                // send "C0644 filesize filename", where filename should not include '/'
211:                long filesize = localFile.length();
212:                String command = "C0644 " + filesize + " ";
213:                command += localFile.getName();
214:                command += "\n";
215:
216:                out.write(command.getBytes());
217:                out.flush();
218:
219:                waitForAck(in);
220:
221:                // send a content of lfile
222:                FileInputStream fis = new FileInputStream(localFile);
223:                byte[] buf = new byte[BUFFER_SIZE];
224:                long startTime = System.currentTimeMillis();
225:                long totalLength = 0;
226:
227:                // only track progress for files larger than 100kb in verbose mode
228:                boolean trackProgress = getVerbose() && filesize > 102400;
229:                // since filesize keeps on decreasing we have to store the
230:                // initial filesize
231:                long initFilesize = filesize;
232:                int percentTransmitted = 0;
233:
234:                try {
235:                    if (this .getVerbose()) {
236:                        log("Sending: " + localFile.getName() + " : "
237:                                + localFile.length());
238:                    }
239:                    while (true) {
240:                        int len = fis.read(buf, 0, buf.length);
241:                        if (len <= 0) {
242:                            break;
243:                        }
244:                        out.write(buf, 0, len);
245:                        totalLength += len;
246:
247:                        if (trackProgress) {
248:                            percentTransmitted = trackProgress(initFilesize,
249:                                    totalLength, percentTransmitted);
250:                        }
251:                    }
252:                    out.flush();
253:                    sendAck(out);
254:                    waitForAck(in);
255:                } finally {
256:                    if (this .getVerbose()) {
257:                        long endTime = System.currentTimeMillis();
258:                        logStats(startTime, endTime, totalLength);
259:                    }
260:                    fis.close();
261:                }
262:            }
263:
264:            /**
265:             * Get the local file
266:             * @return the local file
267:             */
268:            public File getLocalFile() {
269:                return localFile;
270:            }
271:
272:            /**
273:             * Get the remote path
274:             * @return the remote path
275:             */
276:            public String getRemotePath() {
277:                return remotePath;
278:            }
279:        }
w__w_w_._j_a___v__a_2_s._c_o_m | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.