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 |
|
---|
18 | package org.apache.tools.ant.taskdefs.optional.ssh;
|
---|
19 |
|
---|
20 | import com.jcraft.jsch.Channel;
|
---|
21 | import com.jcraft.jsch.ChannelExec;
|
---|
22 | import com.jcraft.jsch.JSchException;
|
---|
23 | import com.jcraft.jsch.Session;
|
---|
24 |
|
---|
25 | import java.io.IOException;
|
---|
26 | import java.io.OutputStream;
|
---|
27 | import java.io.InputStream;
|
---|
28 | import java.text.NumberFormat;
|
---|
29 |
|
---|
30 | import org.apache.tools.ant.BuildException;
|
---|
31 |
|
---|
32 | public abstract class AbstractSshMessage {
|
---|
33 |
|
---|
34 | private Session session;
|
---|
35 | private boolean verbose;
|
---|
36 | private LogListener listener = new LogListener() {
|
---|
37 | public void log(String message) {
|
---|
38 | // do nothing;
|
---|
39 | }
|
---|
40 | };
|
---|
41 |
|
---|
42 | public AbstractSshMessage(Session session) {
|
---|
43 | this(false, session);
|
---|
44 | }
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * @since Ant 1.6.2
|
---|
48 | */
|
---|
49 | public AbstractSshMessage(boolean verbose, Session session) {
|
---|
50 | this.verbose = verbose;
|
---|
51 | this.session = session;
|
---|
52 | }
|
---|
53 |
|
---|
54 | protected Channel openExecChannel(String command) throws JSchException {
|
---|
55 | ChannelExec channel = (ChannelExec) session.openChannel("exec");
|
---|
56 | channel.setCommand(command);
|
---|
57 |
|
---|
58 | return channel;
|
---|
59 | }
|
---|
60 |
|
---|
61 | protected void sendAck(OutputStream out) throws IOException {
|
---|
62 | byte[] buf = new byte[1];
|
---|
63 | buf[0] = 0;
|
---|
64 | out.write(buf);
|
---|
65 | out.flush();
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Reads the response, throws a BuildException if the response
|
---|
70 | * indicates an error.
|
---|
71 | */
|
---|
72 | protected void waitForAck(InputStream in)
|
---|
73 | throws IOException, BuildException {
|
---|
74 | int b = in.read();
|
---|
75 |
|
---|
76 | // b may be 0 for success,
|
---|
77 | // 1 for error,
|
---|
78 | // 2 for fatal error,
|
---|
79 |
|
---|
80 | if (b == -1) {
|
---|
81 | // didn't receive any response
|
---|
82 | throw new BuildException("No response from server");
|
---|
83 | } else if (b != 0) {
|
---|
84 | StringBuffer sb = new StringBuffer();
|
---|
85 |
|
---|
86 | int c = in.read();
|
---|
87 | while (c > 0 && c != '\n') {
|
---|
88 | sb.append((char) c);
|
---|
89 | c = in.read();
|
---|
90 | }
|
---|
91 |
|
---|
92 | if (b == 1) {
|
---|
93 | throw new BuildException("server indicated an error: "
|
---|
94 | + sb.toString());
|
---|
95 | } else if (b == 2) {
|
---|
96 | throw new BuildException("server indicated a fatal error: "
|
---|
97 | + sb.toString());
|
---|
98 | } else {
|
---|
99 | throw new BuildException("unknown response, code " + b
|
---|
100 | + " message: " + sb.toString());
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | public abstract void execute() throws IOException, JSchException;
|
---|
106 |
|
---|
107 | public void setLogListener(LogListener aListener) {
|
---|
108 | listener = aListener;
|
---|
109 | }
|
---|
110 |
|
---|
111 | protected void log(String message) {
|
---|
112 | listener.log(message);
|
---|
113 | }
|
---|
114 |
|
---|
115 | protected void logStats(long timeStarted,
|
---|
116 | long timeEnded,
|
---|
117 | int totalLength) {
|
---|
118 | double duration = (timeEnded - timeStarted) / 1000.0;
|
---|
119 | NumberFormat format = NumberFormat.getNumberInstance();
|
---|
120 | format.setMaximumFractionDigits(2);
|
---|
121 | format.setMinimumFractionDigits(1);
|
---|
122 | listener.log("File transfer time: " + format.format(duration)
|
---|
123 | + " Average Rate: " + format.format(totalLength / duration)
|
---|
124 | + " B/s");
|
---|
125 | }
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * @since Ant 1.6.2
|
---|
129 | */
|
---|
130 | protected final boolean getVerbose() {
|
---|
131 | return verbose;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /*
|
---|
135 | * Track progress every 10% if 100kb < filesize < 1mb. For larger
|
---|
136 | * files track progress for every percent transmitted.
|
---|
137 | */
|
---|
138 | protected final int trackProgress(int filesize, int totalLength,
|
---|
139 | int percentTransmitted) {
|
---|
140 |
|
---|
141 | int percent = (int) Math.round(Math.floor((totalLength /
|
---|
142 | (double)filesize) * 100));
|
---|
143 |
|
---|
144 | if (percent > percentTransmitted) {
|
---|
145 | if (filesize < 1048576) {
|
---|
146 | if (percent % 10 == 0) {
|
---|
147 | if (percent == 100) {
|
---|
148 | System.out.println(" 100%");
|
---|
149 | } else {
|
---|
150 | System.out.print("*");
|
---|
151 | }
|
---|
152 | }
|
---|
153 | } else {
|
---|
154 | if (percent == 50) {
|
---|
155 | System.out.println(" 50%");
|
---|
156 | } else if (percent == 100) {
|
---|
157 | System.out.println(" 100%");
|
---|
158 | } else {
|
---|
159 | System.out.print(".");
|
---|
160 | }
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | return percent;
|
---|
165 | }
|
---|
166 |
|
---|
167 | }
|
---|