Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

JschSession.java 7.4KB

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