Switch jgit library to the EDL (3-clause BSD)
[jgit.git] / org.spearce.jgit / src / org / spearce / jgit / transport / SshSessionFactory.java
blob7f8136db885608edf903841da38e4c1ca40176e2
1 /*
2 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
9 * conditions are met:
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
19 * - Neither the name of the Git Development Community nor the
20 * names of its contributors may be used to endorse or promote
21 * products derived from this software without specific prior
22 * written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
25 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
26 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 package org.spearce.jgit.transport;
41 import com.jcraft.jsch.JSchException;
42 import com.jcraft.jsch.Session;
44 /**
45 * Creates and destroys SSH connections to a remote system.
46 * <p>
47 * Different implementations of the session factory may be used to control
48 * communicating with the end-user as well as reading their personal SSH
49 * configuration settings, such as known hosts and private keys.
50 * <p>
51 * A {@link Session} must be returned to the factory that created it. Callers
52 * are encouraged to retain the SshSessionFactory for the duration of the period
53 * they are using the Session.
55 public abstract class SshSessionFactory {
56 private static SshSessionFactory INSTANCE = new DefaultSshSessionFactory();
58 /**
59 * Get the currently configured JVM-wide factory.
60 * <p>
61 * A factory is always available. By default the factory will read from the
62 * user's <code>$HOME/.ssh</code> and assume OpenSSH compatibility.
64 * @return factory the current factory for this JVM.
66 public static SshSessionFactory getInstance() {
67 return INSTANCE;
70 /**
71 * Change the JVM-wide factory to a different implementation.
73 * @param newFactory
74 * factory for future sessions to be created through. If null the
75 * default factory will be restored.s
77 public static void setInstance(final SshSessionFactory newFactory) {
78 if (newFactory != null)
79 INSTANCE = newFactory;
80 else
81 INSTANCE = new DefaultSshSessionFactory();
84 /**
85 * Open (or reuse) a session to a host.
86 * <p>
87 * A reasonable UserInfo that can interact with the end-user (if necessary)
88 * is installed on the returned session by this method.
89 * <p>
90 * The caller must connect the session by invoking <code>connect()</code>
91 * if it has not already been connected.
93 * @param user
94 * username to authenticate as. If null a reasonable default must
95 * be selected by the implementation. This may be
96 * <code>System.getProperty("user.name")</code>.
97 * @param pass
98 * optional user account password or passphrase. If not null a
99 * UserInfo that supplies this value to the SSH library will be
100 * configured.
101 * @param host
102 * hostname (or IP address) to connect to. Must not be null.
103 * @param port
104 * port number the server is listening for connections on. May be <=
105 * 0 to indicate the IANA registered port of 22 should be used.
106 * @return a session that can contact the remote host.
107 * @throws JSchException
108 * the session could not be created.
110 public abstract Session getSession(String user, String pass, String host,
111 int port) throws JSchException;
114 * Close (or recycle) a session to a host.
116 * @param session
117 * a session previously obtained from this factory's
118 * {@link #getSession(String,String, String, int)} method.s
120 public void releaseSession(final Session session) {
121 if (session.isConnected())
122 session.disconnect();