1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
/*
* Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
* Copyright (C) 2008, 2022 Shawn O. Pearce <spearce@spearce.org> and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0 which is available at
* https://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package org.eclipse.jgit.transport;
import java.util.Iterator;
import java.util.ServiceLoader;
import org.eclipse.jgit.errors.TransportException;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.util.FS;
import org.eclipse.jgit.util.SystemReader;
/**
* Creates and destroys SSH connections to a remote system.
* <p>
* Different implementations of the session factory may be used to control
* communicating with the end-user as well as reading their personal SSH
* configuration settings, such as known hosts and private keys.
* </p>
* <p>
* A {@link RemoteSession} must be returned to the factory that created it.
* Callers are encouraged to retain the SshSessionFactory for the duration of
* the period they are using the session.
* </p>
*/
public abstract class SshSessionFactory {
private static class DefaultFactory {
private static volatile SshSessionFactory INSTANCE = loadSshSessionFactory();
private static SshSessionFactory loadSshSessionFactory() {
ServiceLoader<SshSessionFactory> loader = ServiceLoader
.load(SshSessionFactory.class);
Iterator<SshSessionFactory> iter = loader.iterator();
if (iter.hasNext()) {
return iter.next();
}
return null;
}
private DefaultFactory() {
// No instantiation
}
public static SshSessionFactory getInstance() {
return INSTANCE;
}
public static void setInstance(SshSessionFactory newFactory) {
if (newFactory != null) {
INSTANCE = newFactory;
} else {
INSTANCE = loadSshSessionFactory();
}
}
}
/**
* Gets the currently configured JVM-wide factory.
* <p>
* By default the factory will read from the user's {@code $HOME/.ssh} and
* assume OpenSSH compatibility.
* </p>
*
* @return factory the current factory for this JVM.
*/
public static SshSessionFactory getInstance() {
return DefaultFactory.getInstance();
}
/**
* Changes the JVM-wide factory to a different implementation.
*
* @param newFactory
* factory for future sessions to be created through; if
* {@code null} the default factory will be restored.
*/
public static void setInstance(SshSessionFactory newFactory) {
DefaultFactory.setInstance(newFactory);
}
/**
* Retrieves the local user name as defined by the system property
* "user.name".
*
* @return the user name
* @since 5.2
*/
public static String getLocalUserName() {
return SystemReader.getInstance()
.getProperty(Constants.OS_USER_NAME_KEY);
}
/**
* Opens (or reuses) a session to a host. The returned session is connected
* and authenticated and is ready for further use.
*
* @param uri
* URI of the remote host to connect to
* @param credentialsProvider
* provider to support authentication, may be {@code null} if no
* user input for authentication is needed
* @param fs
* the file system abstraction to use for certain file
* operations, such as reading configuration files
* @param tms
* connection timeout for creating the session, in milliseconds
* @return a connected and authenticated session for communicating with the
* remote host given by the {@code uri}
* @throws org.eclipse.jgit.errors.TransportException
* if the session could not be created
*/
public abstract RemoteSession getSession(URIish uri,
CredentialsProvider credentialsProvider, FS fs, int tms)
throws TransportException;
/**
* The name of the type of session factory.
*
* @return the name of the type of session factory.
*
* @since 5.8
*/
public abstract String getType();
/**
* Closes (or recycles) a session to a host.
*
* @param session
* a session previously obtained from this factory's
* {@link #getSession(URIish, CredentialsProvider, FS, int)}
* method.
*/
public void releaseSession(RemoteSession session) {
session.disconnect();
}
}
|