SshSessionFactory.java

  1. /*
  2.  * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  3.  * Copyright (C) 2008, 2020 Shawn O. Pearce <spearce@spearce.org> and others
  4.  *
  5.  * This program and the accompanying materials are made available under the
  6.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  7.  * https://www.eclipse.org/org/documents/edl-v10.php.
  8.  *
  9.  * SPDX-License-Identifier: BSD-3-Clause
  10.  */

  11. package org.eclipse.jgit.transport;

  12. import java.security.AccessController;
  13. import java.security.PrivilegedAction;
  14. import java.util.Iterator;
  15. import java.util.ServiceLoader;

  16. import org.eclipse.jgit.errors.TransportException;
  17. import org.eclipse.jgit.lib.Constants;
  18. import org.eclipse.jgit.util.FS;
  19. import org.eclipse.jgit.util.SystemReader;

  20. /**
  21.  * Creates and destroys SSH connections to a remote system.
  22.  * <p>
  23.  * Different implementations of the session factory may be used to control
  24.  * communicating with the end-user as well as reading their personal SSH
  25.  * configuration settings, such as known hosts and private keys.
  26.  * </p>
  27.  * <p>
  28.  * A {@link RemoteSession} must be returned to the factory that created it.
  29.  * Callers are encouraged to retain the SshSessionFactory for the duration of
  30.  * the period they are using the session.
  31.  * </p>
  32.  */
  33. public abstract class SshSessionFactory {

  34.     private static SshSessionFactory INSTANCE = loadSshSessionFactory();

  35.     private static SshSessionFactory loadSshSessionFactory() {
  36.         ServiceLoader<SshSessionFactory> loader = ServiceLoader.load(SshSessionFactory.class);
  37.         Iterator<SshSessionFactory> iter = loader.iterator();
  38.         if(iter.hasNext()) {
  39.             return iter.next();
  40.         }
  41.         return null;
  42.     }

  43.     /**
  44.      * Gets the currently configured JVM-wide factory.
  45.      * <p>
  46.      * By default the factory will read from the user's {@code $HOME/.ssh} and
  47.      * assume OpenSSH compatibility.
  48.      * </p>
  49.      *
  50.      * @return factory the current factory for this JVM.
  51.      */
  52.     public static SshSessionFactory getInstance() {
  53.         return INSTANCE;
  54.     }

  55.     /**
  56.      * Changes the JVM-wide factory to a different implementation.
  57.      *
  58.      * @param newFactory
  59.      *            factory for future sessions to be created through; if
  60.      *            {@code null} the default factory will be restored.
  61.      */
  62.     public static void setInstance(SshSessionFactory newFactory) {
  63.         if (newFactory != null) {
  64.             INSTANCE = newFactory;
  65.         } else {
  66.             INSTANCE = loadSshSessionFactory();
  67.         }
  68.     }

  69.     /**
  70.      * Retrieves the local user name as defined by the system property
  71.      * "user.name".
  72.      *
  73.      * @return the user name
  74.      * @since 5.2
  75.      */
  76.     public static String getLocalUserName() {
  77.         return AccessController
  78.                 .doPrivileged((PrivilegedAction<String>) () -> SystemReader
  79.                         .getInstance().getProperty(Constants.OS_USER_NAME_KEY));
  80.     }

  81.     /**
  82.      * Opens (or reuses) a session to a host. The returned session is connected
  83.      * and authenticated and is ready for further use.
  84.      *
  85.      * @param uri
  86.      *            URI of the remote host to connect to
  87.      * @param credentialsProvider
  88.      *            provider to support authentication, may be {@code null} if no
  89.      *            user input for authentication is needed
  90.      * @param fs
  91.      *            the file system abstraction to use for certain file
  92.      *            operations, such as reading configuration files
  93.      * @param tms
  94.      *            connection timeout for creating the session, in milliseconds
  95.      * @return a connected and authenticated session for communicating with the
  96.      *         remote host given by the {@code uri}
  97.      * @throws org.eclipse.jgit.errors.TransportException
  98.      *             if the session could not be created
  99.      */
  100.     public abstract RemoteSession getSession(URIish uri,
  101.             CredentialsProvider credentialsProvider, FS fs, int tms)
  102.             throws TransportException;

  103.     /**
  104.      * The name of the type of session factory.
  105.      *
  106.      * @return the name of the type of session factory.
  107.      *
  108.      * @since 5.8
  109.      */
  110.     public abstract String getType();

  111.     /**
  112.      * Closes (or recycles) a session to a host.
  113.      *
  114.      * @param session
  115.      *            a session previously obtained from this factory's
  116.      *            {@link #getSession(URIish, CredentialsProvider, FS, int)}
  117.      *            method.
  118.      */
  119.     public void releaseSession(RemoteSession session) {
  120.         session.disconnect();
  121.     }
  122. }