Take care of errors reported from the server when upload command is started
[egit/zawir.git] / org.spearce.jgit / src / org / spearce / jgit / transport / SshSessionFactory.java
blobf03e80c75928be042ef264522dac199a4080f0ab
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 java.io.OutputStream;
43 import com.jcraft.jsch.JSchException;
44 import com.jcraft.jsch.Session;
46 /**
47 * Creates and destroys SSH connections to a remote system.
48 * <p>
49 * Different implementations of the session factory may be used to control
50 * communicating with the end-user as well as reading their personal SSH
51 * configuration settings, such as known hosts and private keys.
52 * <p>
53 * A {@link Session} must be returned to the factory that created it. Callers
54 * are encouraged to retain the SshSessionFactory for the duration of the period
55 * they are using the Session.
57 public abstract class SshSessionFactory {
58 private static SshSessionFactory INSTANCE = new DefaultSshSessionFactory();
60 /**
61 * Get the currently configured JVM-wide factory.
62 * <p>
63 * A factory is always available. By default the factory will read from the
64 * user's <code>$HOME/.ssh</code> and assume OpenSSH compatibility.
66 * @return factory the current factory for this JVM.
68 public static SshSessionFactory getInstance() {
69 return INSTANCE;
72 /**
73 * Change the JVM-wide factory to a different implementation.
75 * @param newFactory
76 * factory for future sessions to be created through. If null the
77 * default factory will be restored.s
79 public static void setInstance(final SshSessionFactory newFactory) {
80 if (newFactory != null)
81 INSTANCE = newFactory;
82 else
83 INSTANCE = new DefaultSshSessionFactory();
86 /**
87 * Open (or reuse) a session to a host.
88 * <p>
89 * A reasonable UserInfo that can interact with the end-user (if necessary)
90 * is installed on the returned session by this method.
91 * <p>
92 * The caller must connect the session by invoking <code>connect()</code>
93 * if it has not already been connected.
95 * @param user
96 * username to authenticate as. If null a reasonable default must
97 * be selected by the implementation. This may be
98 * <code>System.getProperty("user.name")</code>.
99 * @param pass
100 * optional user account password or passphrase. If not null a
101 * UserInfo that supplies this value to the SSH library will be
102 * configured.
103 * @param host
104 * hostname (or IP address) to connect to. Must not be null.
105 * @param port
106 * port number the server is listening for connections on. May be <=
107 * 0 to indicate the IANA registered port of 22 should be used.
108 * @return a session that can contact the remote host.
109 * @throws JSchException
110 * the session could not be created.
112 public abstract Session getSession(String user, String pass, String host,
113 int port) throws JSchException;
116 * Close (or recycle) a session to a host.
118 * @param session
119 * a session previously obtained from this factory's
120 * {@link #getSession(String,String, String, int)} method.s
122 public void releaseSession(final Session session) {
123 if (session.isConnected())
124 session.disconnect();
128 * Find or create an OutputStream for Ssh to use. For a command line client
129 * this is probably System.err.
131 * @return an OutputStream to receive the SSH error stream.
133 public abstract OutputStream getErrorStream();