View Javadoc
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  
15  package org.eclipse.jgit.transport;
16  
17  import org.eclipse.jgit.errors.TransportException;
18  import org.eclipse.jgit.internal.JGitText;
19  import org.eclipse.jgit.lib.Repository;
20  import org.eclipse.jgit.util.FS;
21  
22  /**
23   * The base class for transports that use SSH protocol. This class allows
24   * customizing SSH connection settings.
25   */
26  public abstract class SshTransport extends TcpTransport {
27  
28  	private SshSessionFactory sch;
29  
30  	/**
31  	 * The open SSH session
32  	 */
33  	private RemoteSession sock;
34  
35  	/**
36  	 * Create a new transport instance.
37  	 *
38  	 * @param local
39  	 *            the repository this instance will fetch into, or push out of.
40  	 *            This must be the repository passed to
41  	 *            {@link #open(Repository, URIish)}.
42  	 * @param uri
43  	 *            the URI used to access the remote repository. This must be the
44  	 *            URI passed to {@link #open(Repository, URIish)}.
45  	 */
46  	protected SshTransport(Repository local, URIish uri) {
47  		super(local, uri);
48  		sch = SshSessionFactory.getInstance();
49  	}
50  
51  	/**
52  	 * Create a new transport instance without a local repository.
53  	 *
54  	 * @param uri the URI used to access the remote repository. This must be the
55  	 *            URI passed to {@link #open(URIish)}.
56  	 * @since 3.5
57  	 */
58  	protected SshTransport(URIish uri) {
59  		super(uri);
60  		sch = SshSessionFactory.getInstance();
61  	}
62  
63  	/**
64  	 * Set SSH session factory instead of the default one for this instance of
65  	 * the transport.
66  	 *
67  	 * @param factory
68  	 *            a factory to set, must not be null
69  	 * @throws java.lang.IllegalStateException
70  	 *             if session has been already created.
71  	 */
72  	public void setSshSessionFactory(SshSessionFactory factory) {
73  		if (factory == null)
74  			throw new NullPointerException(JGitText.get().theFactoryMustNotBeNull);
75  		if (sock != null)
76  			throw new IllegalStateException(
77  					JGitText.get().anSSHSessionHasBeenAlreadyCreated);
78  		sch = factory;
79  	}
80  
81  	/**
82  	 * Get the SSH session factory
83  	 *
84  	 * @return the SSH session factory that will be used for creating SSH
85  	 *         sessions
86  	 */
87  	public SshSessionFactory getSshSessionFactory() {
88  		return sch;
89  	}
90  
91  	/**
92  	 * Get the default SSH session
93  	 *
94  	 * @return a remote session
95  	 * @throws org.eclipse.jgit.errors.TransportException
96  	 *             in case of error with opening SSH session
97  	 */
98  	protected RemoteSession getSession() throws TransportException {
99  		if (sock != null)
100 			return sock;
101 
102 		final int tms = getTimeout() > 0 ? getTimeout() * 1000 : 0;
103 
104 		final FS fs = local == null ? FS.detect() : local.getFS();
105 
106 		sock = sch
107 				.getSession(uri, getCredentialsProvider(), fs, tms);
108 		return sock;
109 	}
110 
111 	/** {@inheritDoc} */
112 	@Override
113 	public void close() {
114 		if (sock != null) {
115 			try {
116 				sch.releaseSession(sock);
117 			} finally {
118 				sock = null;
119 			}
120 		}
121 	}
122 }