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 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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.IOException;
  50. import java.io.InputStream;
  51. import java.io.OutputStream;
  52. import java.io.PipedInputStream;
  53. import java.io.PipedOutputStream;
  54. import org.eclipse.jgit.errors.TransportException;
  55. import org.eclipse.jgit.util.io.StreamCopyThread;
  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. private final Session sock;
  69. private 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. private 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. private 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. throw new TransportException(uri, "connection failed");
  138. } catch (JSchException e) {
  139. throw new TransportException(uri, e.getMessage(), e);
  140. }
  141. }
  142. private void setupStreams() throws IOException {
  143. inputStream = channel.getInputStream();
  144. // JSch won't let us interrupt writes when we use our InterruptTimer
  145. // to break out of a long-running write operation. To work around
  146. // that we spawn a background thread to shuttle data through a pipe,
  147. // as we can issue an interrupted write out of that. Its slower, so
  148. // we only use this route if there is a timeout.
  149. final OutputStream out = channel.getOutputStream();
  150. if (timeout <= 0) {
  151. outputStream = out;
  152. } else {
  153. final PipedInputStream pipeIn = new PipedInputStream();
  154. final StreamCopyThread copier = new StreamCopyThread(pipeIn,
  155. out);
  156. final PipedOutputStream pipeOut = new PipedOutputStream(pipeIn) {
  157. @Override
  158. public void flush() throws IOException {
  159. super.flush();
  160. copier.flush();
  161. }
  162. @Override
  163. public void close() throws IOException {
  164. super.close();
  165. try {
  166. copier.join(timeout * 1000);
  167. } catch (InterruptedException e) {
  168. // Just wake early, the thread will terminate
  169. // anyway.
  170. }
  171. }
  172. };
  173. copier.start();
  174. outputStream = pipeOut;
  175. }
  176. errStream = channel.getErrStream();
  177. }
  178. @Override
  179. public InputStream getInputStream() {
  180. return inputStream;
  181. }
  182. @Override
  183. public OutputStream getOutputStream() {
  184. return outputStream;
  185. }
  186. @Override
  187. public InputStream getErrorStream() {
  188. return errStream;
  189. }
  190. @Override
  191. public int exitValue() {
  192. if (isRunning())
  193. throw new IllegalStateException();
  194. return channel.getExitStatus();
  195. }
  196. private boolean isRunning() {
  197. return channel.getExitStatus() < 0 && channel.isConnected();
  198. }
  199. @Override
  200. public void destroy() {
  201. if (channel.isConnected())
  202. channel.disconnect();
  203. }
  204. @Override
  205. public int waitFor() throws InterruptedException {
  206. while (isRunning())
  207. Thread.sleep(100);
  208. return exitValue();
  209. }
  210. }
  211. }