You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JschSession.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. package org.eclipse.jgit.transport;
  49. import java.io.BufferedOutputStream;
  50. import java.io.IOException;
  51. import java.io.InputStream;
  52. import java.io.OutputStream;
  53. import org.eclipse.jgit.errors.TransportException;
  54. import org.eclipse.jgit.internal.JGitText;
  55. import org.eclipse.jgit.util.io.IsolatedOutputStream;
  56. import com.jcraft.jsch.Channel;
  57. import com.jcraft.jsch.ChannelExec;
  58. import com.jcraft.jsch.JSchException;
  59. import com.jcraft.jsch.Session;
  60. /**
  61. * Run remote commands using Jsch.
  62. * <p>
  63. * This class is the default session implementation using Jsch. Note that
  64. * {@link JschConfigSessionFactory} is used to create the actual session passed
  65. * to the constructor.
  66. */
  67. public class JschSession implements RemoteSession {
  68. final Session sock;
  69. final URIish uri;
  70. /**
  71. * Create a new session object by passing the real Jsch session and the URI
  72. * information.
  73. *
  74. * @param session
  75. * the real Jsch session created elsewhere.
  76. * @param uri
  77. * the URI information for the remote connection
  78. */
  79. public JschSession(final Session session, URIish uri) {
  80. sock = session;
  81. this.uri = uri;
  82. }
  83. @Override
  84. public Process exec(String command, int timeout) throws IOException {
  85. return new JschProcess(command, timeout);
  86. }
  87. @Override
  88. public void disconnect() {
  89. if (sock.isConnected())
  90. sock.disconnect();
  91. }
  92. /**
  93. * A kludge to allow {@link TransportSftp} to get an Sftp channel from Jsch.
  94. * Ideally, this method would be generic, which would require implementing
  95. * generic Sftp channel operations in the RemoteSession class.
  96. *
  97. * @return a channel suitable for Sftp operations.
  98. * @throws JSchException
  99. * on problems getting the channel.
  100. */
  101. public Channel getSftpChannel() throws JSchException {
  102. return sock.openChannel("sftp"); //$NON-NLS-1$
  103. }
  104. /**
  105. * Implementation of Process for running a single command using Jsch.
  106. * <p>
  107. * Uses the Jsch session to do actual command execution and manage the
  108. * execution.
  109. */
  110. private class JschProcess extends Process {
  111. private ChannelExec channel;
  112. final int timeout;
  113. private InputStream inputStream;
  114. private OutputStream outputStream;
  115. private InputStream errStream;
  116. /**
  117. * Opens a channel on the session ("sock") for executing the given
  118. * command, opens streams, and starts command execution.
  119. *
  120. * @param commandName
  121. * the command to execute
  122. * @param tms
  123. * the timeout value, in seconds, for the command.
  124. * @throws TransportException
  125. * on problems opening a channel or connecting to the remote
  126. * host
  127. * @throws IOException
  128. * on problems opening streams
  129. */
  130. JschProcess(final String commandName, int tms)
  131. throws TransportException, IOException {
  132. timeout = tms;
  133. try {
  134. channel = (ChannelExec) sock.openChannel("exec"); //$NON-NLS-1$
  135. channel.setCommand(commandName);
  136. setupStreams();
  137. channel.connect(timeout > 0 ? timeout * 1000 : 0);
  138. if (!channel.isConnected()) {
  139. closeOutputStream();
  140. throw new TransportException(uri,
  141. JGitText.get().connectionFailed);
  142. }
  143. } catch (JSchException e) {
  144. closeOutputStream();
  145. throw new TransportException(uri, e.getMessage(), e);
  146. }
  147. }
  148. private void closeOutputStream() {
  149. if (outputStream != null) {
  150. try {
  151. outputStream.close();
  152. } catch (IOException ioe) {
  153. // ignore
  154. }
  155. }
  156. }
  157. private void setupStreams() throws IOException {
  158. inputStream = channel.getInputStream();
  159. // JSch won't let us interrupt writes when we use our InterruptTimer
  160. // to break out of a long-running write operation. To work around
  161. // that we spawn a background thread to shuttle data through a pipe,
  162. // as we can issue an interrupted write out of that. Its slower, so
  163. // we only use this route if there is a timeout.
  164. OutputStream out = channel.getOutputStream();
  165. if (timeout <= 0) {
  166. outputStream = out;
  167. } else {
  168. IsolatedOutputStream i = new IsolatedOutputStream(out);
  169. outputStream = new BufferedOutputStream(i, 16 * 1024);
  170. }
  171. errStream = channel.getErrStream();
  172. }
  173. @Override
  174. public InputStream getInputStream() {
  175. return inputStream;
  176. }
  177. @Override
  178. public OutputStream getOutputStream() {
  179. return outputStream;
  180. }
  181. @Override
  182. public InputStream getErrorStream() {
  183. return errStream;
  184. }
  185. @Override
  186. public int exitValue() {
  187. if (isRunning())
  188. throw new IllegalStateException();
  189. return channel.getExitStatus();
  190. }
  191. private boolean isRunning() {
  192. return channel.getExitStatus() < 0 && channel.isConnected();
  193. }
  194. @Override
  195. public void destroy() {
  196. if (channel.isConnected())
  197. channel.disconnect();
  198. }
  199. @Override
  200. public int waitFor() throws InterruptedException {
  201. while (isRunning())
  202. Thread.sleep(100);
  203. return exitValue();
  204. }
  205. }
  206. }