Instantiate JSCH SSH Session Factory explicitily

The JGit's SSH loading of the default session factory may lead to a
non-deterministic instance if more than one factory is available.

See below the current logic:

private static SshSessionFactory loadSshSessionFactory() {
 ServiceLoader<SshSessionFactory> loader =
                         ServiceLoader.load(SshSessionFactory.class);
 Iterator<SshSessionFactory> iter = loader.iterator();
 if(iter.hasNext()) {
  return iter.next();
 }
 return null;
}

If the first service is the Apache Mina SSHD Session Factory, then
it would never be possible to configure the JSCH in clientImplementation
when configured in gerrit.config.

Manage expicitly the JSCH option for assuring that the desired session
manager is always instantiated when requested.

Release-Notes: Fix the ability to configure JSCH as client implementation in gerrit.config
Change-Id: Ifafd717e16b5fc1853553bd1b6e3c60bb048b57e
diff --git a/java/com/google/gerrit/sshd/BUILD b/java/com/google/gerrit/sshd/BUILD
index 0668c1e..32b3bad 100644
--- a/java/com/google/gerrit/sshd/BUILD
+++ b/java/com/google/gerrit/sshd/BUILD
@@ -30,6 +30,7 @@
         "//lib:jgit",
         "//lib:jgit-archive",
         "//lib:jgit-ssh-apache",
+        "//lib:jgit-ssh-jsch",
         "//lib:servlet-api",
         "//lib/auto:auto-value",
         "//lib/auto:auto-value-annotations",
diff --git a/java/com/google/gerrit/sshd/SshSessionFactoryInitializer.java b/java/com/google/gerrit/sshd/SshSessionFactoryInitializer.java
index 1cdf923..358815b 100644
--- a/java/com/google/gerrit/sshd/SshSessionFactoryInitializer.java
+++ b/java/com/google/gerrit/sshd/SshSessionFactoryInitializer.java
@@ -17,6 +17,7 @@
 import static com.google.gerrit.server.config.SshClientImplementation.APACHE;
 
 import org.eclipse.jgit.lib.Config;
+import org.eclipse.jgit.transport.JschConfigSessionFactory;
 import org.eclipse.jgit.transport.SshSessionFactory;
 import org.eclipse.jgit.transport.sshd.DefaultProxyDataFactory;
 import org.eclipse.jgit.transport.sshd.JGitKeyCache;
@@ -25,11 +26,16 @@
 
 public class SshSessionFactoryInitializer {
   public static void init(Config config) {
-    if (APACHE == config.getEnum("ssh", null, "clientImplementation", APACHE)) {
-      SshdSessionFactory factory =
-          new SshdSessionFactory(new JGitKeyCache(), new DefaultProxyDataFactory());
-      factory.setHomeDirectory(FS.DETECTED.userHome());
-      SshSessionFactory.setInstance(factory);
+    switch (config.getEnum("ssh", null, "clientImplementation", APACHE)) {
+      case APACHE:
+        SshdSessionFactory factory =
+            new SshdSessionFactory(new JGitKeyCache(), new DefaultProxyDataFactory());
+        factory.setHomeDirectory(FS.DETECTED.userHome());
+        SshSessionFactory.setInstance(factory);
+        break;
+
+      case JSCH:
+        SshSessionFactory.setInstance(new JschConfigSessionFactory());
     }
   }