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

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