From ebcae3361515f6fee4763b59584a0cb423c59161 Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Fri, 15 Aug 2008 10:35:04 -0700 Subject: [PATCH] Refactor SSH key loading so we don't duplicate keys We only want to read each private key once, so we cache the names of the keys we have processed before, adding keys which we have not yet seen. This allows us to alter add keys on the fly and avoid duplication. Signed-off-by: Shawn O. Pearce Signed-off-by: Robin Rosenberg --- .../jgit/transport/DefaultSshSessionFactory.java | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/DefaultSshSessionFactory.java b/org.spearce.jgit/src/org/spearce/jgit/transport/DefaultSshSessionFactory.java index b4578d40..0484fc01 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/transport/DefaultSshSessionFactory.java +++ b/org.spearce.jgit/src/org/spearce/jgit/transport/DefaultSshSessionFactory.java @@ -78,6 +78,8 @@ class DefaultSshSessionFactory extends SshSessionFactory { /** IANA assigned port number for SSH. */ private static final int SSH_PORT = 22; + private Set loadedIdentities; + private JSch userJSch; @Override @@ -106,10 +108,10 @@ class DefaultSshSessionFactory extends SshSessionFactory { private JSch getUserJSch() throws JSchException { if (userJSch == null) { - final JSch sch = new JSch(); - knownHosts(sch); - identities(sch); - userJSch = sch; + loadedIdentities = new HashSet(); + userJSch = new JSch(); + knownHosts(userJSch); + identities(); } return userJSch; } @@ -133,7 +135,7 @@ class DefaultSshSessionFactory extends SshSessionFactory { } } - private void identities(final JSch sch) throws JSchException { + private void identities() throws JSchException { final File home = FS.userHome(); if (home == null) return; @@ -149,10 +151,16 @@ class DefaultSshSessionFactory extends SshSessionFactory { final File k = new File(sshdir, n.substring(0, n.length() - 4)); if (!k.isFile()) continue; - sch.addIdentity(k.getAbsolutePath()); + addIdentity(k); } } + private void addIdentity(final File identityFile) throws JSchException { + final String path = identityFile.getAbsolutePath(); + if (loadedIdentities.add(path)) + userJSch.addIdentity(path); + } + private static class AWT_UserInfo implements UserInfo, UIKeyboardInteractive { private String passwd; -- 2.11.4.GIT