You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by lg...@apache.org on 2018/04/23 10:08:14 UTC

[2/2] mina-sshd git commit: [SSHD-817] Using NIO2 I/O service factory by default in CLI code unless overwritten by command line argument

[SSHD-817] Using NIO2 I/O service factory by default in CLI code unless overwritten by command line argument


Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/bc756775
Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/bc756775
Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/bc756775

Branch: refs/heads/master
Commit: bc756775e5a6df670b3618073eddcff25197e504
Parents: 1bbe415
Author: Goldstein Lyor <ly...@c-b4.com>
Authored: Mon Apr 23 11:59:38 2018 +0300
Committer: Goldstein Lyor <ly...@c-b4.com>
Committed: Mon Apr 23 13:08:02 2018 +0300

----------------------------------------------------------------------
 .../java/org/apache/sshd/cli/CliSupport.java    | 62 ++++++++++++++++++++
 .../apache/sshd/cli/client/ScpCommandMain.java  |  6 +-
 .../apache/sshd/cli/client/SftpCommandMain.java | 41 ++++++++++++-
 .../sshd/cli/client/SshClientCliSupport.java    | 19 ++++--
 .../apache/sshd/cli/server/SshServerMain.java   | 27 +++------
 .../org/apache/sshd/common/NamedResource.java   |  3 +-
 .../io/BuiltinIoServiceFactoryFactories.java    |  3 +-
 .../server/subsystem/sftp/SftpSubsystem.java    |  4 +-
 8 files changed, 134 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/bc756775/sshd-cli/src/main/java/org/apache/sshd/cli/CliSupport.java
----------------------------------------------------------------------
diff --git a/sshd-cli/src/main/java/org/apache/sshd/cli/CliSupport.java b/sshd-cli/src/main/java/org/apache/sshd/cli/CliSupport.java
index c0067be..942fcf3 100644
--- a/sshd-cli/src/main/java/org/apache/sshd/cli/CliSupport.java
+++ b/sshd-cli/src/main/java/org/apache/sshd/cli/CliSupport.java
@@ -20,12 +20,19 @@ package org.apache.sshd.cli;
 
 import java.io.PrintStream;
 
+import org.apache.sshd.common.helpers.AbstractFactoryManager;
+import org.apache.sshd.common.io.BuiltinIoServiceFactoryFactories;
+import org.apache.sshd.common.io.IoServiceFactoryFactory;
+import org.apache.sshd.common.util.GenericUtils;
+
 /**
  * TODO Add javadoc
  *
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
 public abstract class CliSupport {
+    public static final BuiltinIoServiceFactoryFactories DEFAULT_IO_SERVICE_FACTORY = BuiltinIoServiceFactoryFactories.NIO2;
+
     protected CliSupport() {
         super();
     }
@@ -34,4 +41,59 @@ public abstract class CliSupport {
         stderr.println(message);
         return true;
     }
+
+    /**
+     * Scans the arguments for the &quot;-io&quot; command line option and sets the I/O
+     * service accordingly. If no specific option specified then {@link #DEFAULT_IO_SERVICE_FACTORY}
+     * is used.
+     *
+     * @param stderr Error stream for output of error messages
+     * @param args The arguments to scan
+     * @return The resolved I/O service factory - {@code null} if errors encountered
+     */
+    public static BuiltinIoServiceFactoryFactories resolveIoServiceFactory(PrintStream stderr, String... args) {
+        int numArgs = GenericUtils.length(args);
+        BuiltinIoServiceFactoryFactories factory = null;
+        for (int index = 0; index < numArgs; index++) {
+            String argName = args[index];
+            if (!"-io".equals(argName)) {
+                continue;
+            }
+
+            if (factory != null) {
+                stderr.println("I/O factory re-specified - already set as " + factory);
+                return null;
+            }
+
+            index++;
+            if (index >= numArgs) {
+                stderr.println("option requires an argument: " + argName);
+                return null;
+            }
+
+            String provider = args[index];
+            factory = BuiltinIoServiceFactoryFactories.fromFactoryName(provider);
+            if (factory == null) {
+                System.err.println("provider (" + argName + ") should be one of " + BuiltinIoServiceFactoryFactories.VALUES);
+                return null;
+            }
+        }
+
+        if (factory == null) {
+            factory = DEFAULT_IO_SERVICE_FACTORY;
+        }
+
+        System.setProperty(IoServiceFactoryFactory.class.getName(), factory.getFactoryClassName());
+        return factory;
+    }
+
+    public static <M extends AbstractFactoryManager> M setupIoServiceFactory(M manager, PrintStream stderr, String... args) {
+        BuiltinIoServiceFactoryFactories factory = resolveIoServiceFactory(stderr, args);
+        if (factory == null) {
+            return null;
+        }
+
+        manager.setIoServiceFactoryFactory(factory.create());
+        return manager;
+    }
 }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/bc756775/sshd-cli/src/main/java/org/apache/sshd/cli/client/ScpCommandMain.java
----------------------------------------------------------------------
diff --git a/sshd-cli/src/main/java/org/apache/sshd/cli/client/ScpCommandMain.java b/sshd-cli/src/main/java/org/apache/sshd/cli/client/ScpCommandMain.java
index 770ab32..8a9e4a9 100644
--- a/sshd-cli/src/main/java/org/apache/sshd/cli/client/ScpCommandMain.java
+++ b/sshd-cli/src/main/java/org/apache/sshd/cli/client/ScpCommandMain.java
@@ -118,8 +118,8 @@ public class ScpCommandMain extends SshClientCliSupport {
     }
 
     public static void main(String[] args) throws Exception {
-        final PrintStream stdout = System.out;
-        final PrintStream stderr = System.err;
+        PrintStream stdout = System.out;
+        PrintStream stderr = System.err;
         OutputStream logStream = stdout;
         try (BufferedReader stdin = new BufferedReader(
                 new InputStreamReader(new NoCloseInputStream(System.in), Charset.defaultCharset()))) {
@@ -137,7 +137,7 @@ public class ScpCommandMain extends SshClientCliSupport {
             ClientSession session = (logStream == null) || GenericUtils.isEmpty(args)
                 ? null : setupClientSession(SCP_PORT_OPTION, stdin, stdout, stderr, args);
             if (session == null) {
-                stderr.println("usage: scp [" + SCP_PORT_OPTION + " port] [-i identity]"
+                stderr.println("usage: scp [" + SCP_PORT_OPTION + " port] [-i identity] [-io nio2|mina|netty]"
                          + " [-v[v][v]] [-E logoutput] [-r] [-p] [-q] [-o option=value]"
                          + " [-c cipherlist] [-m maclist] [-w password] [-C] <source> <target>");
                 stderr.println();

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/bc756775/sshd-cli/src/main/java/org/apache/sshd/cli/client/SftpCommandMain.java
----------------------------------------------------------------------
diff --git a/sshd-cli/src/main/java/org/apache/sshd/cli/client/SftpCommandMain.java b/sshd-cli/src/main/java/org/apache/sshd/cli/client/SftpCommandMain.java
index 09b440d..21b308c 100644
--- a/sshd-cli/src/main/java/org/apache/sshd/cli/client/SftpCommandMain.java
+++ b/sshd-cli/src/main/java/org/apache/sshd/cli/client/SftpCommandMain.java
@@ -41,6 +41,7 @@ import java.util.ServiceLoader;
 import java.util.TreeMap;
 import java.util.logging.Level;
 
+import org.apache.sshd.client.ClientFactoryManager;
 import org.apache.sshd.client.session.ClientSession;
 import org.apache.sshd.client.subsystem.sftp.SftpClient;
 import org.apache.sshd.client.subsystem.sftp.SftpClient.Attributes;
@@ -49,9 +50,18 @@ import org.apache.sshd.client.subsystem.sftp.SftpClientFactory;
 import org.apache.sshd.client.subsystem.sftp.SftpFileSystemProvider;
 import org.apache.sshd.client.subsystem.sftp.extensions.openssh.OpenSSHStatExtensionInfo;
 import org.apache.sshd.client.subsystem.sftp.extensions.openssh.OpenSSHStatPathExtension;
+import org.apache.sshd.common.NamedResource;
+import org.apache.sshd.common.ServiceFactory;
+import org.apache.sshd.common.channel.ChannelFactory;
+import org.apache.sshd.common.cipher.CipherFactory;
+import org.apache.sshd.common.compression.CompressionFactory;
+import org.apache.sshd.common.io.IoServiceFactory;
 import org.apache.sshd.common.io.IoSession;
 import org.apache.sshd.common.kex.KexProposalOption;
+import org.apache.sshd.common.kex.KeyExchange;
+import org.apache.sshd.common.mac.MacFactory;
 import org.apache.sshd.common.session.Session;
+import org.apache.sshd.common.signature.SignatureFactory;
 import org.apache.sshd.common.subsystem.sftp.SftpConstants;
 import org.apache.sshd.common.subsystem.sftp.SftpException;
 import org.apache.sshd.common.subsystem.sftp.extensions.ParserUtils;
@@ -89,6 +99,7 @@ public class SftpCommandMain extends SshClientCliSupport implements Channel {
                 new PwdCommandExecutor(),
                 new InfoCommandExecutor(),
                 new SessionCommandExecutor(),
+                new ClientCommandExecutor(),
                 new VersionCommandExecutor(),
                 new CdCommandExecutor(),
                 new LcdCommandExecutor(),
@@ -300,7 +311,7 @@ public class SftpCommandMain extends SshClientCliSupport implements Channel {
 
             ClientSession session = (logStream == null) ? null : setupClientSession(SFTP_PORT_OPTION, stdin, stdout, stderr, args);
             if (session == null) {
-                System.err.println("usage: sftp [-v[v][v]] [-E logoutput] [-i identity]"
+                System.err.println("usage: sftp [-v[v][v]] [-E logoutput] [-i identity] [-io nio2|mina|netty]"
                         + " [-l login] [" + SFTP_PORT_OPTION + " port] [-o option=value]"
                         + " [-w password] [-c cipherlist]  [-m maclist] [-C] hostname/user@host");
                 System.exit(-1);
@@ -392,6 +403,34 @@ public class SftpCommandMain extends SshClientCliSupport implements Channel {
         }
     }
 
+    private class ClientCommandExecutor implements SftpCommandExecutor {
+        ClientCommandExecutor() {
+            super();
+        }
+
+        @Override
+        public String getName() {
+            return "client";
+        }
+
+        @Override
+        public boolean executeCommand(String args, BufferedReader stdin, PrintStream stdout, PrintStream stderr) throws Exception {
+            ValidateUtils.checkTrue(GenericUtils.isEmpty(args), "Unexpected arguments: %s", args);
+            SftpClient sftp = getClient();
+            ClientSession session = sftp.getSession();
+            ClientFactoryManager manager = session.getFactoryManager();
+            appendInfoValue(stdout, IoServiceFactory.class.getSimpleName(), manager.getIoServiceFactory().getClass().getSimpleName()).println();
+            appendInfoValue(stdout, CipherFactory.class.getSimpleName(), NamedResource.getNames(manager.getCipherFactories())).println();
+            appendInfoValue(stdout, KeyExchange.class.getSimpleName(), NamedResource.getNames(manager.getKeyExchangeFactories())).println();
+            appendInfoValue(stdout, SignatureFactory.class.getSimpleName(), NamedResource.getNames(manager.getSignatureFactories())).println();
+            appendInfoValue(stdout, MacFactory.class.getSimpleName(), NamedResource.getNames(manager.getMacFactories())).println();
+            appendInfoValue(stdout, CompressionFactory.class.getSimpleName(), NamedResource.getNames(manager.getCompressionFactories())).println();
+            appendInfoValue(stdout, ChannelFactory.class.getSimpleName(), NamedResource.getNames(manager.getChannelFactories())).println();
+            appendInfoValue(stdout, ServiceFactory.class.getSimpleName(), NamedResource.getNames(manager.getServiceFactories())).println();
+            return false;
+        }
+    }
+
     private class InfoCommandExecutor implements SftpCommandExecutor {
         InfoCommandExecutor() {
             super();

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/bc756775/sshd-cli/src/main/java/org/apache/sshd/cli/client/SshClientCliSupport.java
----------------------------------------------------------------------
diff --git a/sshd-cli/src/main/java/org/apache/sshd/cli/client/SshClientCliSupport.java b/sshd-cli/src/main/java/org/apache/sshd/cli/client/SshClientCliSupport.java
index 47021e1..5278ba9 100644
--- a/sshd-cli/src/main/java/org/apache/sshd/cli/client/SshClientCliSupport.java
+++ b/sshd-cli/src/main/java/org/apache/sshd/cli/client/SshClientCliSupport.java
@@ -159,7 +159,8 @@ public abstract class SshClientCliSupport extends CliSupport {
                     break;
                 }
             } else if ("-i".equals(argName)) {
-                identities.add(resolveIdentityFile(argVal));
+                Path idFile = resolveIdentityFile(argVal);
+                identities.add(idFile);
             } else if ("-C".equals(argName)) {
                 compressions = setupCompressions(argName,
                     GenericUtils.join(
@@ -220,7 +221,7 @@ public abstract class SshClientCliSupport extends CliSupport {
             return null;
         }
 
-        SshClient client = setupClient(options, ciphers, macs, compressions, identities, stdin, stdout, stderr);
+        SshClient client = setupClient(options, ciphers, macs, compressions, identities, stdin, stdout, stderr, args);
         if (client == null) {
             return null;
         }
@@ -265,6 +266,10 @@ public abstract class SshClientCliSupport extends CliSupport {
         }
     }
 
+    public static SshClient setupDefaultClient(PrintStream stderr, String... args) {
+        return setupIoServiceFactory(SshClient.setUpDefaultClient(), System.err, args);
+    }
+
     // returns null if error encountered
     public static SshClient setupClient(
             Map<String, Object> options,
@@ -272,7 +277,9 @@ public abstract class SshClientCliSupport extends CliSupport {
             List<NamedFactory<Mac>> macs,
             List<NamedFactory<Compression>> compressions,
             Collection<? extends Path> identities,
-            BufferedReader stdin, PrintStream stdout, PrintStream stderr) throws Exception {
+            BufferedReader stdin, PrintStream stdout, PrintStream stderr,
+            String[] args)
+                throws Exception {
         PropertyResolver resolver = PropertyResolverUtils.toPropertyResolver(options);
         if (GenericUtils.isEmpty(ciphers)) {
             ciphers = setupCiphers(resolver, stderr);
@@ -295,7 +302,11 @@ public abstract class SshClientCliSupport extends CliSupport {
             }
         }
 
-        SshClient client = SshClient.setUpDefaultClient();
+        SshClient client = setupDefaultClient(stderr, args);
+        if (client == null) {
+            return null;
+        }
+
         try {
             if (GenericUtils.size(ciphers) > 0) {
                 client.setCipherFactories(ciphers);

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/bc756775/sshd-cli/src/main/java/org/apache/sshd/cli/server/SshServerMain.java
----------------------------------------------------------------------
diff --git a/sshd-cli/src/main/java/org/apache/sshd/cli/server/SshServerMain.java b/sshd-cli/src/main/java/org/apache/sshd/cli/server/SshServerMain.java
index 78e43a9..cba9d24 100644
--- a/sshd-cli/src/main/java/org/apache/sshd/cli/server/SshServerMain.java
+++ b/sshd-cli/src/main/java/org/apache/sshd/cli/server/SshServerMain.java
@@ -31,8 +31,6 @@ import org.apache.sshd.common.NamedResource;
 import org.apache.sshd.common.PropertyResolver;
 import org.apache.sshd.common.PropertyResolverUtils;
 import org.apache.sshd.common.config.SshConfigFileReader;
-import org.apache.sshd.common.io.BuiltinIoServiceFactoryFactories;
-import org.apache.sshd.common.io.IoServiceFactory;
 import org.apache.sshd.common.keyprovider.KeyPairProvider;
 import org.apache.sshd.common.util.GenericUtils;
 import org.apache.sshd.server.Command;
@@ -114,22 +112,6 @@ public class SshServerMain extends SshServerCliSupport {
                     keyFiles = new LinkedList<>();
                 }
                 keyFiles.add(keyFilePath);
-            } else if ("-io".equals(argName)) {
-                if ((i + 1) >= numArgs) {
-                    System.err.println("option requires an argument: " + argName);
-                    error = true;
-                    break;
-                }
-                provider = args[++i];
-                if ("mina".equals(provider)) {
-                    System.setProperty(IoServiceFactory.class.getName(), BuiltinIoServiceFactoryFactories.MINA.getFactoryClassName());
-                } else if ("nio2".endsWith(provider)) {
-                    System.setProperty(IoServiceFactory.class.getName(), BuiltinIoServiceFactoryFactories.NIO2.getFactoryClassName());
-                } else {
-                    System.err.println("provider should be mina or nio2: " + argName);
-                    error = true;
-                    break;
-                }
             } else if ("-o".equals(argName)) {
                 if ((i + 1) >= numArgs) {
                     System.err.println("option requires and argument: " + argName);
@@ -167,12 +149,17 @@ public class SshServerMain extends SshServerCliSupport {
                 break;
             }
         }
+
+        SshServer sshd = setupIoServiceFactory(SshServer.setUpDefaultServer(), System.err, args);
+        if (sshd == null) {
+            error = true;
+        }
+
         if (error) {
-            System.err.println("usage: sshd [-p port] [-io mina|nio2] [-key-type RSA|DSA|EC] [-key-size NNNN] [-key-file <path>] [-o option=value]");
+            System.err.println("usage: sshd [-p port] [-io mina|nio2|netty] [-key-type RSA|DSA|EC] [-key-size NNNN] [-key-file <path>] [-o option=value]");
             System.exit(-1);
         }
 
-        SshServer sshd = SshServer.setUpDefaultServer();
         Map<String, Object> props = sshd.getProperties();
         props.putAll(options);
 

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/bc756775/sshd-core/src/main/java/org/apache/sshd/common/NamedResource.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/NamedResource.java b/sshd-core/src/main/java/org/apache/sshd/common/NamedResource.java
index a0d13f7..813f53d 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/NamedResource.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/NamedResource.java
@@ -62,7 +62,8 @@ public interface NamedResource {
      * @return A comma separated list of factory names
      */
     static String getNames(Collection<? extends NamedResource> resources) {
-        return GenericUtils.join(getNameList(resources), ',');
+        Collection<String> nameList = getNameList(resources);
+        return GenericUtils.join(nameList, ',');
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/bc756775/sshd-core/src/main/java/org/apache/sshd/common/io/BuiltinIoServiceFactoryFactories.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/io/BuiltinIoServiceFactoryFactories.java b/sshd-core/src/main/java/org/apache/sshd/common/io/BuiltinIoServiceFactoryFactories.java
index 662bb47..4c9e05d 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/io/BuiltinIoServiceFactoryFactories.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/io/BuiltinIoServiceFactoryFactories.java
@@ -33,7 +33,8 @@ import org.apache.sshd.common.io.nio2.Nio2ServiceFactoryFactory;
  */
 public enum BuiltinIoServiceFactoryFactories implements NamedFactory<IoServiceFactoryFactory>, OptionalFeature {
     NIO2(Nio2ServiceFactoryFactory.class),
-    MINA("org.apache.sshd.common.io.mina.MinaServiceFactoryFactory");
+    MINA("org.apache.sshd.common.io.mina.MinaServiceFactoryFactory"),
+    NETTY("org.apache.sshd.netty.NettyIoServiceFactoryFactory");
 
     public static final Set<BuiltinIoServiceFactoryFactories> VALUES =
             Collections.unmodifiableSet(EnumSet.allOf(BuiltinIoServiceFactoryFactories.class));

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/bc756775/sshd-sftp/src/main/java/org/apache/sshd/server/subsystem/sftp/SftpSubsystem.java
----------------------------------------------------------------------
diff --git a/sshd-sftp/src/main/java/org/apache/sshd/server/subsystem/sftp/SftpSubsystem.java b/sshd-sftp/src/main/java/org/apache/sshd/server/subsystem/sftp/SftpSubsystem.java
index 9989b7e..6b5d08b 100644
--- a/sshd-sftp/src/main/java/org/apache/sshd/server/subsystem/sftp/SftpSubsystem.java
+++ b/sshd-sftp/src/main/java/org/apache/sshd/server/subsystem/sftp/SftpSubsystem.java
@@ -859,7 +859,9 @@ public class SftpSubsystem
             log.debug("doClose({})[id={}] SSH_FXP_CLOSE (handle={}[{}])",
                       session, id, handle, h);
         }
-        validateHandle(handle, h, Handle.class).close();
+
+        Handle nodeHandle = validateHandle(handle, h, Handle.class);
+        nodeHandle.close();
 
         SftpEventListener listener = getSftpEventListenerProxy();
         listener.close(session, handle, h);