1 /*
2 * Copyright (C) 2009, Constantine Plotnikov <constantine.plotnikov@gmail.com>
3 * Copyright (C) 2008-2009, Google Inc.
4 * Copyright (C) 2009, Google, Inc.
5 * Copyright (C) 2009, JetBrains s.r.o.
6 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
7 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
8 * and other copyright owners as documented in the project's IP log.
9 *
10 * This program and the accompanying materials are made available
11 * under the terms of the Eclipse Distribution License v1.0 which
12 * accompanies this distribution, is reproduced below, and is
13 * available at http://www.eclipse.org/org/documents/edl-v10.php
14 *
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or
18 * without modification, are permitted provided that the following
19 * conditions are met:
20 *
21 * - Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 *
24 * - Redistributions in binary form must reproduce the above
25 * copyright notice, this list of conditions and the following
26 * disclaimer in the documentation and/or other materials provided
27 * with the distribution.
28 *
29 * - Neither the name of the Eclipse Foundation, Inc. nor the
30 * names of its contributors may be used to endorse or promote
31 * products derived from this software without specific prior
32 * written permission.
33 *
34 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
35 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
36 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
39 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
43 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
46 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47 */
48
49 package org.eclipse.jgit.transport;
50
51 import java.io.BufferedOutputStream;
52 import java.io.IOException;
53 import java.io.InputStream;
54 import java.io.OutputStream;
55
56 import org.eclipse.jgit.errors.TransportException;
57 import org.eclipse.jgit.internal.JGitText;
58 import org.eclipse.jgit.util.io.IsolatedOutputStream;
59
60 import com.jcraft.jsch.Channel;
61 import com.jcraft.jsch.ChannelExec;
62 import com.jcraft.jsch.JSchException;
63 import com.jcraft.jsch.Session;
64
65 /**
66 * Run remote commands using Jsch.
67 * <p>
68 * This class is the default session implementation using Jsch. Note that
69 * {@link JschConfigSessionFactory} is used to create the actual session passed
70 * to the constructor.
71 */
72 public class JschSession implements RemoteSession {
73 final Session sock;
74 final URIish uri;
75
76 /**
77 * Create a new session object by passing the real Jsch session and the URI
78 * information.
79 *
80 * @param session
81 * the real Jsch session created elsewhere.
82 * @param uri
83 * the URI information for the remote connection
84 */
85 public JschSession(final Session session, URIish uri) {
86 sock = session;
87 this.uri = uri;
88 }
89
90 public Process exec(String command, int timeout) throws IOException {
91 return new JschProcess(command, timeout);
92 }
93
94 public void disconnect() {
95 if (sock.isConnected())
96 sock.disconnect();
97 }
98
99 /**
100 * A kludge to allow {@link TransportSftp} to get an Sftp channel from Jsch.
101 * Ideally, this method would be generic, which would require implementing
102 * generic Sftp channel operations in the RemoteSession class.
103 *
104 * @return a channel suitable for Sftp operations.
105 * @throws JSchException
106 * on problems getting the channel.
107 */
108 public Channel getSftpChannel() throws JSchException {
109 return sock.openChannel("sftp"); //$NON-NLS-1$
110 }
111
112 /**
113 * Implementation of Process for running a single command using Jsch.
114 * <p>
115 * Uses the Jsch session to do actual command execution and manage the
116 * execution.
117 */
118 private class JschProcess extends Process {
119 private ChannelExec channel;
120
121 final int timeout;
122
123 private InputStream inputStream;
124
125 private OutputStream outputStream;
126
127 private InputStream errStream;
128
129 /**
130 * Opens a channel on the session ("sock") for executing the given
131 * command, opens streams, and starts command execution.
132 *
133 * @param commandName
134 * the command to execute
135 * @param tms
136 * the timeout value, in seconds, for the command.
137 * @throws TransportException
138 * on problems opening a channel or connecting to the remote
139 * host
140 * @throws IOException
141 * on problems opening streams
142 */
143 JschProcess(final String commandName, int tms)
144 throws TransportException, IOException {
145 timeout = tms;
146 try {
147 channel = (ChannelExec) sock.openChannel("exec"); //$NON-NLS-1$
148 channel.setCommand(commandName);
149 setupStreams();
150 channel.connect(timeout > 0 ? timeout * 1000 : 0);
151 if (!channel.isConnected()) {
152 closeOutputStream();
153 throw new TransportException(uri,
154 JGitText.get().connectionFailed);
155 }
156 } catch (JSchException e) {
157 closeOutputStream();
158 throw new TransportException(uri, e.getMessage(), e);
159 }
160 }
161
162 private void closeOutputStream() {
163 if (outputStream != null) {
164 try {
165 outputStream.close();
166 } catch (IOException ioe) {
167 // ignore
168 }
169 }
170 }
171
172 private void setupStreams() throws IOException {
173 inputStream = channel.getInputStream();
174
175 // JSch won't let us interrupt writes when we use our InterruptTimer
176 // to break out of a long-running write operation. To work around
177 // that we spawn a background thread to shuttle data through a pipe,
178 // as we can issue an interrupted write out of that. Its slower, so
179 // we only use this route if there is a timeout.
180 OutputStream out = channel.getOutputStream();
181 if (timeout <= 0) {
182 outputStream = out;
183 } else {
184 IsolatedOutputStream i = new IsolatedOutputStream(out);
185 outputStream = new BufferedOutputStream(i, 16 * 1024);
186 }
187
188 errStream = channel.getErrStream();
189 }
190
191 @Override
192 public InputStream getInputStream() {
193 return inputStream;
194 }
195
196 @Override
197 public OutputStream getOutputStream() {
198 return outputStream;
199 }
200
201 @Override
202 public InputStream getErrorStream() {
203 return errStream;
204 }
205
206 @Override
207 public int exitValue() {
208 if (isRunning())
209 throw new IllegalStateException();
210 return channel.getExitStatus();
211 }
212
213 private boolean isRunning() {
214 return channel.getExitStatus() < 0 && channel.isConnected();
215 }
216
217 @Override
218 public void destroy() {
219 if (channel.isConnected())
220 channel.disconnect();
221 }
222
223 @Override
224 public int waitFor() throws InterruptedException {
225 while (isRunning())
226 Thread.sleep(100);
227 return exitValue();
228 }
229 }
230 }