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

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