{"thread":{"id":"18901","subject":"[JGIT PATCH 3/3] Extracted AWT-independent superclass from DefaultSshSessionFactory","startedAt":"2009-04-16T14:11:11Z","lastAt":"2009-04-17T15:16:45Z","messageCount":2,"participants":["Constantine Plotnikov","Shawn O. Pearce"],"isPatch":true,"patchVersion":1,"patchTotal":3},"messages":[{"id":"111452","messageId":"85647ef50904160711w6fbcfcedk2b9a9e1740a4ddd3@mail.gmail.com","threadId":"18901","inReplyTo":null,"subject":"[JGIT PATCH 3/3] Extracted AWT-independent superclass from DefaultSshSessionFactory","fromName":"Constantine Plotnikov","fromEmail":"constantine.plotnikov@gmail.com","sentAt":"2009-04-16T14:11:11Z","receivedAt":"2009-04-16T14:11:11Z","isPatch":true,"sender":{"key":"constantine.plotnikov@gmail.com","avatar":null},"body":"The part of DefaultSshSessionFactory that does not depend on\nAWT was refactored to the super class SshConfigSessionFactory.\nThe parts of DefaultSshSessionFactory that were used only by\nthe OpenSshConfig class were moved to that class.\n\nSigned-off-by: Constantine Plotnikov <constantine.plotnikov@gmail.com>\n---\nNote that I did not update EclipseSshSessionFactory since it handles\nJSch object a bit differently, and I do not quite understand the\nreason for these differences. But EclipseSshSessionFactory might also\nbenefit from SshConfigSessionFactory.\n\n .../jgit/transport/DefaultSshSessionFactory.java   |  157 +--------------\n .../org/spearce/jgit/transport/OpenSshConfig.java  |   17 ++-\n .../jgit/transport/SshConfigSessionFactory.java    |  215 ++++++++++++++++++++\n 3 files changed, 233 insertions(+), 156 deletions(-)\n create mode 100644\norg.spearce.jgit/src/org/spearce/jgit/transport/SshConfigSessionFactory.java\n\ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/DefaultSshSessionFactory.java\nb/org.spearce.jgit/src/org/spearce/jgit/transport/DefaultSshSessionFactory.java\nindex c9050fa..8e7e529 100644\n--- a/org.spearce.jgit/src/org/spearce/jgit/transport/DefaultSshSessionFactory.java\n+++ b/org.spearce.jgit/src/org/spearce/jgit/transport/DefaultSshSessionFactory.java\n@@ -42,15 +42,6 @@\n import java.awt.GridBagConstraints;\n import java.awt.GridBagLayout;\n import java.awt.Insets;\n-import java.io.File;\n-import java.io.FileInputStream;\n-import java.io.FileNotFoundException;\n-import java.io.IOException;\n-import java.io.OutputStream;\n-import java.security.AccessController;\n-import java.security.PrivilegedAction;\n-import java.util.HashSet;\n-import java.util.Set;\n\n import javax.swing.JLabel;\n import javax.swing.JOptionPane;\n@@ -58,10 +49,6 @@\n import javax.swing.JPasswordField;\n import javax.swing.JTextField;\n\n-import org.spearce.jgit.util.FS;\n-\n-import com.jcraft.jsch.JSch;\n-import com.jcraft.jsch.JSchException;\n import com.jcraft.jsch.Session;\n import com.jcraft.jsch.UIKeyboardInteractive;\n import com.jcraft.jsch.UserInfo;\n@@ -76,116 +63,11 @@\n  * If user interactivity is required by SSH (e.g. to obtain a password) AWT is\n  * used to display a password input field to the end-user.\n  */\n-class DefaultSshSessionFactory extends SshSessionFactory {\n-\t/** IANA assigned port number for SSH. */\n-\tstatic final int SSH_PORT = 22;\n-\n-\tprivate Set<String> loadedIdentities;\n-\n-\tprivate JSch userJSch;\n-\n-\tprivate OpenSshConfig config;\n-\n-\t@Override\n-\tpublic synchronized Session getSession(String user, String pass,\n-\t\t\tString host, int port) throws 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+class DefaultSshSessionFactory extends SshConfigSessionFactory {\n\n-\t\tfinal Session session = getUserJSch().getSession(user, host, port);\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+\tprotected void configure(final OpenSshConfig.Host hc, final Session session) {\n+\t\tif (!hc.isBatchMode())\n \t\t\tsession.setUserInfo(new AWT_UserInfo());\n-\t\tfinal String strictHostKeyCheckingPolicy = hc.getStrictHostKeyChecking();\n-\t\tif (strictHostKeyCheckingPolicy != null)\n-\t\t\tsession.setConfig(\"StrictHostKeyChecking\", strictHostKeyCheckingPolicy);\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-\tstatic String userName() {\n-\t\treturn AccessController.doPrivileged(new PrivilegedAction<String>() {\n-\t\t\tpublic String run() {\n-\t\t\t\treturn System.getProperty(\"user.name\");\n-\t\t\t}\n-\t\t});\n-\t}\n-\n-\tprivate 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-\t\t}\n-\t\treturn userJSch;\n-\t}\n-\n-\tprivate OpenSshConfig getConfig() {\n-\t\tif (config == null)\n-\t\t\tconfig = OpenSshConfig.get();\n-\t\treturn config;\n-\t}\n-\n-\tprivate void knownHosts(final JSch sch) throws JSchException {\n-\t\tfinal File home = FS.userHome();\n-\t\tif (home == null)\n-\t\t\treturn;\n-\t\tfinal File known_hosts = new File(new File(home, \".ssh\"), \"known_hosts\");\n-\t\ttry {\n-\t\t\tfinal FileInputStream in = new FileInputStream(known_hosts);\n-\t\t\ttry {\n-\t\t\t\tsch.setKnownHosts(in);\n-\t\t\t} finally {\n-\t\t\t\tin.close();\n-\t\t\t}\n-\t\t} catch (FileNotFoundException none) {\n-\t\t\t// Oh well. They don't have a known hosts in home.\n-\t\t} catch (IOException err) {\n-\t\t\t// Oh well. They don't have a known hosts in home.\n-\t\t}\n-\t}\n-\n-\tprivate void identities() {\n-\t\tfinal File home = FS.userHome();\n-\t\tif (home == null)\n-\t\t\treturn;\n-\t\tfinal File sshdir = new File(home, \".ssh\");\n-\t\tfinal File[] keys = sshdir.listFiles();\n-\t\tif (keys == null)\n-\t\t\treturn;\n-\t\tfor (int i = 0; i < keys.length; i++) {\n-\t\t\tfinal File pk = keys[i];\n-\t\t\tfinal String n = pk.getName();\n-\t\t\tif (!n.endsWith(\".pub\"))\n-\t\t\t\tcontinue;\n-\t\t\tfinal File k = new File(sshdir, n.substring(0, n.length() - 4));\n-\t\t\tif (!k.isFile())\n-\t\t\t\tcontinue;\n-\n-\t\t\ttry {\n-\t\t\t\taddIdentity(k);\n-\t\t\t} catch (JSchException e) {\n-\t\t\t\tcontinue;\n-\t\t\t}\n-\t\t}\n-\t}\n-\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\tloadedIdentities.add(path);\n-\t\t}\n \t}\n\n \tprivate static class AWT_UserInfo implements UserInfo,\n@@ -285,37 +167,4 @@ public String getPassphrase() {\n \t\t\treturn null; // cancel\n \t\t}\n \t}\n-\n-\t@Override\n-\tpublic OutputStream getErrorStream() {\n-\t\treturn new OutputStream() {\n-\t\t\tprivate StringBuilder all = new StringBuilder();\n-\n-\t\t\tprivate StringBuilder 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(final int b) throws IOException {\n-\t\t\t\tif (b == '\\r') {\n-\t\t\t\t\tSystem.err.print('\\r');\n-\t\t\t\t\treturn;\n-\t\t\t\t}\n-\n-\t\t\t\tsb.append((char) b);\n-\n-\t\t\t\tif (b == '\\n') {\n-\t\t\t\t\tfinal String line = sb.toString();\n-\t\t\t\t\tSystem.err.print(line);\n-\t\t\t\t\tall.append(line);\n-\t\t\t\t\tsb = new StringBuilder();\n-\t\t\t\t}\n-\t\t\t}\n-\t\t};\n-\t}\n }\ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/OpenSshConfig.java\nb/org.spearce.jgit/src/org/spearce/jgit/transport/OpenSshConfig.java\nindex 1c264d4..c50c228 100644\n--- a/org.spearce.jgit/src/org/spearce/jgit/transport/OpenSshConfig.java\n+++ b/org.spearce.jgit/src/org/spearce/jgit/transport/OpenSshConfig.java\n@@ -44,6 +44,8 @@\n import java.io.IOException;\n import java.io.InputStream;\n import java.io.InputStreamReader;\n+import java.security.AccessController;\n+import java.security.PrivilegedAction;\n import java.util.ArrayList;\n import java.util.Collections;\n import java.util.LinkedHashMap;\n@@ -95,6 +97,9 @@ public static OpenSshConfig get() {\n \t/** Cached entries read out of the configuration file. */\n \tprivate Map<String, Host> hosts;\n\n+\t/** IANA assigned port number for SSH. */\n+\tstatic final int SSH_PORT = 22;\n+\n \tOpenSshConfig(final File h, final File cfg) {\n \t\thome = h;\n \t\tconfigFile = cfg;\n@@ -129,9 +134,9 @@ public Host lookup(final String hostName) {\n \t\tif (h.hostName == null)\n \t\t\th.hostName = hostName;\n \t\tif (h.user == null)\n-\t\t\th.user = DefaultSshSessionFactory.userName();\n+\t\t\th.user = OpenSshConfig.userName();\n \t\tif (h.port == 0)\n-\t\t\th.port = DefaultSshSessionFactory.SSH_PORT;\n+\t\t\th.port = OpenSshConfig.SSH_PORT;\n \t\th.patternsApplied = true;\n \t\treturn h;\n \t}\n@@ -277,6 +282,14 @@ private File toFile(final String path) {\n \t\treturn new File(home, path);\n \t}\n\n+\tstatic String userName() {\n+\t\treturn AccessController.doPrivileged(new PrivilegedAction<String>() {\n+\t\t\tpublic String run() {\n+\t\t\t\treturn System.getProperty(\"user.name\");\n+\t\t\t}\n+\t\t});\n+\t}\n+\n \t/**\n \t * Configuration of one \"Host\" block in the configuration file.\n \t * <p>\ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/SshConfigSessionFactory.java\nb/org.spearce.jgit/src/org/spearce/jgit/transport/SshConfigSessionFactory.java\nnew file mode 100644\nindex 0000000..05e2346\n--- /dev/null\n+++ b/org.spearce.jgit/src/org/spearce/jgit/transport/SshConfigSessionFactory.java\n@@ -0,0 +1,215 @@\n+/*\n+ * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>\n+ * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>\n+ * Copyright (C) 2009, JetBrains s.r.o.\n+ *\n+ * All rights reserved.\n+ *\n+ * Redistribution and use in source and binary forms, with or\n+ * without modification, are permitted provided that the following\n+ * conditions are met:\n+ *\n+ * - Redistributions of source code must retain the above copyright\n+ *   notice, this list of conditions and the following disclaimer.\n+ *\n+ * - Redistributions in binary form must reproduce the above\n+ *   copyright notice, this list of conditions and the following\n+ *   disclaimer in the documentation and/or other materials provided\n+ *   with the distribution.\n+ *\n+ * - Neither the name of the Git Development Community nor the\n+ *   names of its contributors may be used to endorse or promote\n+ *   products derived from this software without specific prior\n+ *   written permission.\n+ *\n+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND\n+ * CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES,\n+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\n+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR\n+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\n+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\n+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\n+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\n+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n+ */\n+package org.spearce.jgit.transport;\n+\n+import java.io.File;\n+import java.io.FileInputStream;\n+import java.io.FileNotFoundException;\n+import java.io.IOException;\n+import java.io.OutputStream;\n+import java.util.HashSet;\n+import java.util.Set;\n+\n+import org.spearce.jgit.util.FS;\n+\n+import com.jcraft.jsch.JSch;\n+import com.jcraft.jsch.JSchException;\n+import com.jcraft.jsch.Session;\n+import com.jcraft.jsch.UserInfo;\n+\n+/**\n+ * The base session factory that loads known hosts and private keys from\n+ * <code>$HOME/.ssh</code>.\n+ * <p>\n+ * This is the default implementation used by JGit and provides most of the\n+ * compatibility necessary to match OpenSSH, a popular implementation of SSH\n+ * used by C Git.\n+ * <p>\n+ * The factory does not provides UI behavior. Override the method\n+ * {@link #configure(org.spearce.jgit.transport.OpenSshConfig.Host, Session)}\n+ * to supply appropriate {@link UserInfo} to the session.\n+ */\n+public abstract class SshConfigSessionFactory extends SshSessionFactory {\n+\n+\tprivate Set<String> loadedIdentities;\n+\n+\tprivate JSch userJSch;\n+\n+\tprivate OpenSshConfig config;\n+\n+\t@Override\n+\tpublic synchronized Session getSession(String user, String pass,\n+\t\t\tString host, int port) throws 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 = getUserJSch().getSession(user, host, port);\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\tfinal String strictHostKeyCheckingPolicy = hc\n+\t\t\t\t.getStrictHostKeyChecking();\n+\t\tif (strictHostKeyCheckingPolicy != null)\n+\t\t\tsession.setConfig(\"StrictHostKeyChecking\",\n+\t\t\t\t\tstrictHostKeyCheckingPolicy);\n+\t\tfinal String pauth = hc.getPreferredAuthentications();\n+\t\tif (pauth != null)\n+\t\t\tsession.setConfig(\"PreferredAuthentications\", pauth);\n+\t\tconfigure(hc, session);\n+\t\treturn session;\n+\t}\n+\n+\n+\t/**\n+\t * Provide additional configuration for the session basing on the host\n+\t * information. This method could be used to supply {@link UserInfo}.\n+\t *\n+\t * @param hc\n+\t *            host configuration\n+\t * @param session\n+\t *            session to configure\n+\t */\n+\tprotected abstract void configure(final OpenSshConfig.Host hc, final\nSession session);\n+\n+\tprivate 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+\t\t}\n+\t\treturn userJSch;\n+\t}\n+\n+\tprivate OpenSshConfig getConfig() {\n+\t\tif (config == null)\n+\t\t\tconfig = OpenSshConfig.get();\n+\t\treturn config;\n+\t}\n+\n+\tprivate void knownHosts(final JSch sch) throws JSchException {\n+\t\tfinal File home = FS.userHome();\n+\t\tif (home == null)\n+\t\t\treturn;\n+\t\tfinal File known_hosts = new File(new File(home, \".ssh\"), \"known_hosts\");\n+\t\ttry {\n+\t\t\tfinal FileInputStream in = new FileInputStream(known_hosts);\n+\t\t\ttry {\n+\t\t\t\tsch.setKnownHosts(in);\n+\t\t\t} finally {\n+\t\t\t\tin.close();\n+\t\t\t}\n+\t\t} catch (FileNotFoundException none) {\n+\t\t\t// Oh well. They don't have a known hosts in home.\n+\t\t} catch (IOException err) {\n+\t\t\t// Oh well. They don't have a known hosts in home.\n+\t\t}\n+\t}\n+\n+\tprivate void identities() {\n+\t\tfinal File home = FS.userHome();\n+\t\tif (home == null)\n+\t\t\treturn;\n+\t\tfinal File sshdir = new File(home, \".ssh\");\n+\t\tfinal File[] keys = sshdir.listFiles();\n+\t\tif (keys == null)\n+\t\t\treturn;\n+\t\tfor (int i = 0; i < keys.length; i++) {\n+\t\t\tfinal File pk = keys[i];\n+\t\t\tfinal String n = pk.getName();\n+\t\t\tif (!n.endsWith(\".pub\"))\n+\t\t\t\tcontinue;\n+\t\t\tfinal File k = new File(sshdir, n.substring(0, n.length() - 4));\n+\t\t\tif (!k.isFile())\n+\t\t\t\tcontinue;\n+\n+\t\t\ttry {\n+\t\t\t\taddIdentity(k);\n+\t\t\t} catch (JSchException e) {\n+\t\t\t\tcontinue;\n+\t\t\t}\n+\t\t}\n+\t}\n+\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\tloadedIdentities.add(path);\n+\t\t}\n+\t}\n+\n+\t@Override\n+\tpublic OutputStream getErrorStream() {\n+\t\treturn new OutputStream() {\n+\t\t\tprivate StringBuilder all = new StringBuilder();\n+\n+\t\t\tprivate StringBuilder 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(final int b) throws IOException {\n+\t\t\t\tif (b == '\\r') {\n+\t\t\t\t\tSystem.err.print('\\r');\n+\t\t\t\t\treturn;\n+\t\t\t\t}\n+\n+\t\t\t\tsb.append((char) b);\n+\n+\t\t\t\tif (b == '\\n') {\n+\t\t\t\t\tfinal String line = sb.toString();\n+\t\t\t\t\tSystem.err.print(line);\n+\t\t\t\t\tall.append(line);\n+\t\t\t\t\tsb = new StringBuilder();\n+\t\t\t\t}\n+\t\t\t}\n+\t\t};\n+\t}\n+}\n-- \n1.6.0.2.1172.ga5ed0\n"},{"id":"111493","messageId":"20090417151645.GQ23604@spearce.org","threadId":"18901","inReplyTo":"85647ef50904160711w6fbcfcedk2b9a9e1740a4ddd3@mail.gmail.com","subject":"Re: [JGIT PATCH 3/3] Extracted AWT-independent superclass from DefaultSshSessionFactory","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2009-04-17T15:16:45Z","receivedAt":"2009-04-17T15:16:45Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"Constantine Plotnikov <constantine.plotnikov@gmail.com> wrote:\n> The part of DefaultSshSessionFactory that does not depend on\n> AWT was refactored to the super class SshConfigSessionFactory.\n> The parts of DefaultSshSessionFactory that were used only by\n> the OpenSshConfig class were moved to that class.\n> \n> Signed-off-by: Constantine Plotnikov <constantine.plotnikov@gmail.com>\n\nThanks. I applied this, but with a couple of style nitpicks corrected.\n\n> --- a/org.spearce.jgit/src/org/spearce/jgit/transport/OpenSshConfig.java\n> +++ b/org.spearce.jgit/src/org/spearce/jgit/transport/OpenSshConfig.java\n> @@ -95,6 +97,9 @@ public static OpenSshConfig get() {\n>  \t/** Cached entries read out of the configuration file. */\n>  \tprivate Map<String, Host> hosts;\n> \n> +\t/** IANA assigned port number for SSH. */\n> +\tstatic final int SSH_PORT = 22;\n> +\n\nStatic constants should be at the top of the class, not after the\ninstance members.\n\n-- \nShawn.\n"}]}