{"thread":{"id":"18918","subject":"[JGIT PATCH] Refactor EclipseSshSessionFactory to extend SshConfigSessionFactory","startedAt":"2009-04-17T15:35:10Z","lastAt":"2009-04-17T15:35:10Z","messageCount":1,"participants":["Shawn O. Pearce"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"111497","messageId":"1239982510-20431-1-git-send-email-spearce@spearce.org","threadId":"18918","inReplyTo":null,"subject":"[JGIT PATCH] Refactor EclipseSshSessionFactory to extend SshConfigSessionFactory","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2009-04-17T15:35:10Z","receivedAt":"2009-04-17T15:35:10Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"Keeping the EclipseSshSessionFactory in sync with our OpenSSH compliant\nDefaultSshSessionFactory was a problem; e.g. a few commits ago we had\nsupport for StrictHostKeyChecking added, but it was missed in the fork\nwe use under Eclipse.\n\nConstantine Plotnikov's recent patch pointed out we should clean up\nthe code and make EclipseSshSessionFactory use the same code as the\nother JGit clients use.  This makes it possible by refactoring the\nway we create the Session and obtain a handle on the JSch.\n\nSigned-off-by: Shawn O. Pearce <spearce@spearce.org>\n---\n .../spearce/egit/ui/EclipseSshSessionFactory.java  |   86 +++-----------------\n .../jgit/transport/SshConfigSessionFactory.java    |   34 +++++++-\n 2 files changed, 42 insertions(+), 78 deletions(-)\n\ndiff --git a/org.spearce.egit.ui/src/org/spearce/egit/ui/EclipseSshSessionFactory.java b/org.spearce.egit.ui/src/org/spearce/egit/ui/EclipseSshSessionFactory.java\nindex 098d234..2cafdfe 100644\n--- a/org.spearce.egit.ui/src/org/spearce/egit/ui/EclipseSshSessionFactory.java\n+++ b/org.spearce.egit.ui/src/org/spearce/egit/ui/EclipseSshSessionFactory.java\n@@ -8,96 +8,36 @@\n  *******************************************************************************/\n package org.spearce.egit.ui;\n \n-import java.io.File;\n-import java.io.IOException;\n-import java.io.OutputStream;\n-import java.util.HashSet;\n-import java.util.Set;\n-\n import org.eclipse.jsch.core.IJSchService;\n import org.eclipse.jsch.ui.UserInfoPrompter;\n-import org.spearce.jgit.transport.OpenSshConfig;\n-import org.spearce.jgit.transport.SshSessionFactory;\n+import org.spearce.jgit.transport.SshConfigSessionFactory;\n+import org.spearce.jgit.transport.OpenSshConfig.Host;\n \n+import com.jcraft.jsch.JSch;\n import com.jcraft.jsch.JSchException;\n import com.jcraft.jsch.Session;\n \n-class EclipseSshSessionFactory extends SshSessionFactory {\n+class EclipseSshSessionFactory extends SshConfigSessionFactory {\n \tprivate final IJSchService provider;\n \n-\tprivate final Set<String> loadedIdentities = new HashSet<String>();\n-\n-\tprivate OpenSshConfig config;\n-\n \tEclipseSshSessionFactory(final IJSchService p) {\n \t\tprovider = p;\n \t}\n \n \t@Override\n-\tpublic Session getSession(String user, String pass, String host, int port)\n-\t\t\tthrows JSchException {\n-\t\tfinal OpenSshConfig.Host hc = getConfig().lookup(host);\n-\t\thost = hc.getHostName();\n-\t\tif (port <= 0)\n-\t\t\tport = hc.getPort();\n-\t\tif (user == null)\n-\t\t\tuser = hc.getUser();\n-\n-\t\tfinal Session session = provider.createSession(host, port, user);\n-\t\tif (hc.getIdentityFile() != null)\n-\t\t\taddIdentity(hc.getIdentityFile());\n-\t\tif (pass != null)\n-\t\t\tsession.setPassword(pass);\n-\t\telse if (!hc.isBatchMode())\n-\t\t\tnew UserInfoPrompter(session);\n-\n-\t\tfinal String pauth = hc.getPreferredAuthentications();\n-\t\tif (pauth != null)\n-\t\t\tsession.setConfig(\"PreferredAuthentications\", pauth);\n-\t\treturn session;\n-\t}\n-\n-\tprivate synchronized OpenSshConfig getConfig() {\n-\t\tif (config == null)\n-\t\t\tconfig = OpenSshConfig.get();\n-\t\treturn config;\n+\tprotected Session createSession(final String user, final String host,\n+\t\t\tfinal int port) throws JSchException {\n+\t\treturn provider.createSession(host, port, user);\n \t}\n \n-\tprivate void addIdentity(final File identityFile)\n-\t\t\tthrows JSchException {\n-\t\tfinal String path = identityFile.getAbsolutePath();\n-\t\tif (loadedIdentities.add(path))\n-\t\t\tprovider.getJSch().addIdentity(path);\n+\t@Override\n+\tprotected void configure(final Host hc, final Session session) {\n+\t\tif (!hc.isBatchMode())\n+\t\t\tnew UserInfoPrompter(session);\n \t}\n \n \t@Override\n-\tpublic OutputStream getErrorStream() {\n-\t\treturn new OutputStream() {\n-\n-\t\t\tStringBuilder all = new StringBuilder();\n-\n-\t\t\tStringBuilder sb = new StringBuilder();\n-\n-\t\t\tpublic String toString() {\n-\t\t\t\tString r = all.toString();\n-\t\t\t\twhile (r.endsWith(\"\\n\"))\n-\t\t\t\t\tr = r.substring(0, r.length() - 1);\n-\t\t\t\treturn r;\n-\t\t\t}\n-\n-\t\t\t@Override\n-\t\t\tpublic void write(int b) throws IOException {\n-\t\t\t\tif (b == '\\r')\n-\t\t\t\t\treturn;\n-\t\t\t\tsb.append((char) b);\n-\t\t\t\tif (b == '\\n') {\n-\t\t\t\t\tString s = sb.toString();\n-\t\t\t\t\tall.append(s);\n-\t\t\t\t\tsb = new StringBuilder();\n-\t\t\t\t\tActivator.logError(s, new Throwable());\n-\t\t\t\t}\n-\t\t\t}\n-\t\t};\n+\tprotected JSch getUserJSch() throws JSchException {\n+\t\treturn provider.getJSch();\n \t}\n-\n }\ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/SshConfigSessionFactory.java b/org.spearce.jgit/src/org/spearce/jgit/transport/SshConfigSessionFactory.java\nindex ec91494..f929e6b 100644\n--- a/org.spearce.jgit/src/org/spearce/jgit/transport/SshConfigSessionFactory.java\n+++ b/org.spearce.jgit/src/org/spearce/jgit/transport/SshConfigSessionFactory.java\n@@ -66,7 +66,7 @@\n  * to supply appropriate {@link UserInfo} to the session.\n  */\n public abstract class SshConfigSessionFactory extends SshSessionFactory {\n-\tprivate Set<String> loadedIdentities;\n+\tprivate final Set<String> loadedIdentities = new HashSet<String>();\n \n \tprivate JSch userJSch;\n \n@@ -82,7 +82,7 @@ public synchronized Session getSession(String user, String pass,\n \t\tif (user == null)\n \t\t\tuser = hc.getUser();\n \n-\t\tfinal Session session = getUserJSch().getSession(user, host, port);\n+\t\tfinal Session session = createSession(user, host, port);\n \t\tif (hc.getIdentityFile() != null)\n \t\t\taddIdentity(hc.getIdentityFile());\n \t\tif (pass != null)\n@@ -100,6 +100,24 @@ public synchronized Session getSession(String user, String pass,\n \t}\n \n \t/**\n+\t * Create a new JSch session for the requested address.\n+\t *\n+\t * @param user\n+\t *            login to authenticate as.\n+\t * @param host\n+\t *            server name to connect to.\n+\t * @param port\n+\t *            port number of the SSH daemon (typically 22).\n+\t * @return new session instance, but otherwise unconfigured.\n+\t * @throws JSchException\n+\t *             the session could not be created.\n+\t */\n+\tprotected Session createSession(String user, String host, int port)\n+\t\t\tthrows JSchException {\n+\t\treturn getUserJSch().getSession(user, host, port);\n+\t}\n+\n+\t/**\n \t * Provide additional configuration for the session based on the host\n \t * information. This method could be used to supply {@link UserInfo}.\n \t *\n@@ -110,9 +128,15 @@ public synchronized Session getSession(String user, String pass,\n \t */\n \tprotected abstract void configure(OpenSshConfig.Host hc, Session session);\n \n-\tprivate JSch getUserJSch() throws JSchException {\n+\t/**\n+\t * Obtain the JSch used to create new sessions.\n+\t *\n+\t * @return the JSch instance to use.\n+\t * @throws JSchException\n+\t *             the user configuration could not be created.\n+\t */\n+\tprotected JSch getUserJSch() throws JSchException {\n \t\tif (userJSch == null) {\n-\t\t\tloadedIdentities = new HashSet<String>();\n \t\t\tuserJSch = new JSch();\n \t\t\tknownHosts(userJSch);\n \t\t\tidentities();\n@@ -173,7 +197,7 @@ private void identities() {\n \tprivate void addIdentity(final File identityFile) throws JSchException {\n \t\tfinal String path = identityFile.getAbsolutePath();\n \t\tif (!loadedIdentities.contains(path)) {\n-\t\t\tuserJSch.addIdentity(path);\n+\t\t\tgetUserJSch().addIdentity(path);\n \t\t\tloadedIdentities.add(path);\n \t\t}\n \t}\n-- \n1.6.3.rc0.200.g4086\n"}]}