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.

SshTransport.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (C) 2009, Constantine Plotnikov <constantine.plotnikov@gmail.com>
  3. * Copyright (C) 2009, Google Inc.
  4. * Copyright (C) 2009, JetBrains s.r.o.
  5. * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
  6. * Copyright (C) 2008-2009, Shawn O. Pearce <spearce@spearce.org> and others
  7. *
  8. * This program and the accompanying materials are made available under the
  9. * terms of the Eclipse Distribution License v. 1.0 which is available at
  10. * https://www.eclipse.org/org/documents/edl-v10.php.
  11. *
  12. * SPDX-License-Identifier: BSD-3-Clause
  13. */
  14. package org.eclipse.jgit.transport;
  15. import org.eclipse.jgit.errors.TransportException;
  16. import org.eclipse.jgit.internal.JGitText;
  17. import org.eclipse.jgit.lib.Repository;
  18. import org.eclipse.jgit.util.FS;
  19. /**
  20. * The base class for transports that use SSH protocol. This class allows
  21. * customizing SSH connection settings.
  22. */
  23. public abstract class SshTransport extends TcpTransport {
  24. private SshSessionFactory sch;
  25. /**
  26. * The open SSH session
  27. */
  28. private RemoteSession sock;
  29. /**
  30. * Create a new transport instance.
  31. *
  32. * @param local
  33. * the repository this instance will fetch into, or push out of.
  34. * This must be the repository passed to
  35. * {@link #open(Repository, URIish)}.
  36. * @param uri
  37. * the URI used to access the remote repository. This must be the
  38. * URI passed to {@link #open(Repository, URIish)}.
  39. */
  40. protected SshTransport(Repository local, URIish uri) {
  41. super(local, uri);
  42. sch = SshSessionFactory.getInstance();
  43. }
  44. /**
  45. * Create a new transport instance without a local repository.
  46. *
  47. * @param uri the URI used to access the remote repository. This must be the
  48. * URI passed to {@link #open(URIish)}.
  49. * @since 3.5
  50. */
  51. protected SshTransport(URIish uri) {
  52. super(uri);
  53. sch = SshSessionFactory.getInstance();
  54. }
  55. /**
  56. * Set SSH session factory instead of the default one for this instance of
  57. * the transport.
  58. *
  59. * @param factory
  60. * a factory to set, must not be null
  61. * @throws java.lang.IllegalStateException
  62. * if session has been already created.
  63. */
  64. public void setSshSessionFactory(SshSessionFactory factory) {
  65. if (factory == null)
  66. throw new NullPointerException(JGitText.get().theFactoryMustNotBeNull);
  67. if (sock != null)
  68. throw new IllegalStateException(
  69. JGitText.get().anSSHSessionHasBeenAlreadyCreated);
  70. sch = factory;
  71. }
  72. /**
  73. * Get the SSH session factory
  74. *
  75. * @return the SSH session factory that will be used for creating SSH
  76. * sessions
  77. */
  78. public SshSessionFactory getSshSessionFactory() {
  79. return sch;
  80. }
  81. /**
  82. * Get the default SSH session
  83. *
  84. * @return a remote session
  85. * @throws org.eclipse.jgit.errors.TransportException
  86. * in case of error with opening SSH session
  87. */
  88. protected RemoteSession getSession() throws TransportException {
  89. if (sock != null)
  90. return sock;
  91. final int tms = getTimeout() > 0 ? getTimeout() * 1000 : 0;
  92. final FS fs = local == null ? FS.detect() : local.getFS();
  93. sock = sch
  94. .getSession(uri, getCredentialsProvider(), fs, tms);
  95. return sock;
  96. }
  97. /** {@inheritDoc} */
  98. @Override
  99. public void close() {
  100. if (sock != null) {
  101. try {
  102. sch.releaseSession(sock);
  103. } finally {
  104. sock = null;
  105. }
  106. }
  107. }
  108. }