From eb6babe8ae7b3f7250872ac4ddef7fb9ab9dbc41 Mon Sep 17 00:00:00 2001 From: Constantine Plotnikov Date: Wed, 15 Apr 2009 17:26:34 +0400 Subject: [PATCH] Moved common SSH related functionality to the base class SshTransport The common SSH-related functionality moved from subclasses to base class (creating and closing sessions). The ability to configure SSH session factory was added as well. Signed-off-by: Constantine Plotnikov Signed-off-by: Shawn O. Pearce --- .../org/spearce/jgit/transport/SshTransport.java | 84 +++++++++++++++++++++- .../spearce/jgit/transport/TransportGitSsh.java | 40 ----------- .../org/spearce/jgit/transport/TransportSftp.java | 41 ----------- 3 files changed, 82 insertions(+), 83 deletions(-) diff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/SshTransport.java b/org.spearce.jgit/src/org/spearce/jgit/transport/SshTransport.java index 7a1e9698..127096c9 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/transport/SshTransport.java +++ b/org.spearce.jgit/src/org/spearce/jgit/transport/SshTransport.java @@ -37,14 +37,28 @@ */ package org.spearce.jgit.transport; +import java.net.ConnectException; +import java.net.UnknownHostException; + +import org.spearce.jgit.errors.TransportException; import org.spearce.jgit.lib.Repository; +import com.jcraft.jsch.JSchException; +import com.jcraft.jsch.Session; + /** - * The base class for transports that use SSH protocol. This class - * allows customizing SSH connection settings. + * The base class for transports that use SSH protocol. This class allows + * customizing SSH connection settings. */ public abstract class SshTransport extends TcpTransport { + private SshSessionFactory sch; + + /** + * The open SSH session + */ + protected Session sock; + /** * Create a new transport instance. * @@ -58,5 +72,71 @@ public abstract class SshTransport extends TcpTransport { */ protected SshTransport(Repository local, URIish uri) { super(local, uri); + sch = SshSessionFactory.getInstance(); + } + + /** + * Set SSH session factory instead of the default one for this instance of + * the transport. + * + * @param factory + * a factory to set, must not be null + * @throws IllegalStateException + * if session has been already created. + */ + public void setSshSessionFactory(SshSessionFactory factory) { + if (factory == null) + throw new NullPointerException("The factory must not be null"); + if (sock != null) + throw new IllegalStateException( + "An SSH session has been already created"); + sch = factory; + } + + /** + * @return the SSH session factory that will be used for creating SSH sessions + */ + public SshSessionFactory getSshSessionFactory() { + return sch; + } + + + /** + * Initialize SSH session + * + * @throws TransportException + * in case of error with opening SSH session + */ + protected void initSession() throws TransportException { + if (sock != null) + return; + + final String user = uri.getUser(); + final String pass = uri.getPass(); + final String host = uri.getHost(); + final int port = uri.getPort(); + try { + sock = sch.getSession(user, pass, host, port); + if (!sock.isConnected()) + sock.connect(); + } catch (JSchException je) { + final Throwable c = je.getCause(); + if (c instanceof UnknownHostException) + throw new TransportException(uri, "unknown host"); + if (c instanceof ConnectException) + throw new TransportException(uri, c.getMessage()); + throw new TransportException(uri, je.getMessage(), je); + } + } + + @Override + public void close() { + if (sock != null) { + try { + sch.releaseSession(sock); + } finally { + sock = null; + } + } } } diff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/TransportGitSsh.java b/org.spearce.jgit/src/org/spearce/jgit/transport/TransportGitSsh.java index 1f69ed7a..a24878ae 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/transport/TransportGitSsh.java +++ b/org.spearce.jgit/src/org/spearce/jgit/transport/TransportGitSsh.java @@ -41,8 +41,6 @@ package org.spearce.jgit.transport; import java.io.IOException; import java.io.OutputStream; -import java.net.ConnectException; -import java.net.UnknownHostException; import org.spearce.jgit.errors.NoRemoteRepositoryException; import org.spearce.jgit.errors.TransportException; @@ -51,7 +49,6 @@ import org.spearce.jgit.util.QuotedString; import com.jcraft.jsch.ChannelExec; import com.jcraft.jsch.JSchException; -import com.jcraft.jsch.Session; /** * Transport through an SSH tunnel. @@ -80,15 +77,10 @@ public class TransportGitSsh extends SshTransport implements PackTransport { return false; } - private final SshSessionFactory sch; - - private Session sock; - OutputStream errStream; TransportGitSsh(final Repository local, final URIish uri) { super(local, uri); - sch = SshSessionFactory.getInstance(); } @Override @@ -101,17 +93,6 @@ public class TransportGitSsh extends SshTransport implements PackTransport { return new SshPushConnection(); } - @Override - public void close() { - if (sock != null) { - try { - sch.releaseSession(sock); - } finally { - sock = null; - } - } - } - private static void sqMinimal(final StringBuilder cmd, final String val) { if (val.matches("^[a-zA-Z0-9._/-]*$")) { // If the string matches only generally safe characters @@ -135,27 +116,6 @@ public class TransportGitSsh extends SshTransport implements PackTransport { cmd.append(QuotedString.BOURNE.quote(val)); } - private void initSession() throws TransportException { - if (sock != null) - return; - - final String user = uri.getUser(); - final String pass = uri.getPass(); - final String host = uri.getHost(); - final int port = uri.getPort(); - try { - sock = sch.getSession(user, pass, host, port); - if (!sock.isConnected()) - sock.connect(); - } catch (JSchException je) { - final Throwable c = je.getCause(); - if (c instanceof UnknownHostException) - throw new TransportException(uri, "unknown host"); - if (c instanceof ConnectException) - throw new TransportException(uri, c.getMessage()); - throw new TransportException(uri, je.getMessage(), je); - } - } ChannelExec exec(final String exe) throws TransportException { initSession(); diff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/TransportSftp.java b/org.spearce.jgit/src/org/spearce/jgit/transport/TransportSftp.java index 50bd5648..e18d1283 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/transport/TransportSftp.java +++ b/org.spearce.jgit/src/org/spearce/jgit/transport/TransportSftp.java @@ -41,8 +41,6 @@ import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.IOException; import java.io.OutputStream; -import java.net.ConnectException; -import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -63,7 +61,6 @@ import org.spearce.jgit.lib.Ref.Storage; import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.JSchException; -import com.jcraft.jsch.Session; import com.jcraft.jsch.SftpATTRS; import com.jcraft.jsch.SftpException; @@ -92,13 +89,8 @@ public class TransportSftp extends SshTransport implements WalkTransport { return uri.isRemote() && "sftp".equals(uri.getScheme()); } - private final SshSessionFactory sch; - - private Session sock; - TransportSftp(final Repository local, final URIish uri) { super(local, uri); - sch = SshSessionFactory.getInstance(); } @Override @@ -117,39 +109,6 @@ public class TransportSftp extends SshTransport implements WalkTransport { return r; } - @Override - public void close() { - if (sock != null) { - try { - sch.releaseSession(sock); - } finally { - sock = null; - } - } - } - - private void initSession() throws TransportException { - if (sock != null) - return; - - final String user = uri.getUser(); - final String pass = uri.getPass(); - final String host = uri.getHost(); - final int port = uri.getPort(); - try { - sock = sch.getSession(user, pass, host, port); - if (!sock.isConnected()) - sock.connect(); - } catch (JSchException je) { - final Throwable c = je.getCause(); - if (c instanceof UnknownHostException) - throw new TransportException(uri, "unknown host"); - if (c instanceof ConnectException) - throw new TransportException(uri, c.getMessage()); - throw new TransportException(uri, je.getMessage(), je); - } - } - ChannelSftp newSftp() throws TransportException { initSession(); -- 2.11.4.GIT