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 2015/12/16 06:32:47 UTC

mina-sshd git commit: Moved 'scp' command 'main' to a more natural location

Repository: mina-sshd
Updated Branches:
  refs/heads/master 155407b86 -> e329167e3


Moved 'scp' command 'main' to a more natural location


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

Branch: refs/heads/master
Commit: e329167e3886598d1599e827abe4fcbae178a556
Parents: 155407b
Author: Lyor Goldstein <lg...@vmware.com>
Authored: Wed Dec 16 07:32:28 2015 +0200
Committer: Lyor Goldstein <lg...@vmware.com>
Committed: Wed Dec 16 07:32:28 2015 +0200

----------------------------------------------------------------------
 assembly/src/main/distribution/bin/scp.bat      |   2 +-
 assembly/src/main/distribution/bin/scp.sh       |   2 +-
 .../sshd/client/scp/DefaultScpClient.java       | 159 +++++++++++++++++-
 .../org/apache/sshd/server/scp/ScpCommand.java  | 167 +------------------
 .../apache/sshd/client/scp/ScpCommandMain.java  |  35 ++++
 .../apache/sshd/server/scp/ScpCommandMain.java  |  33 ----
 6 files changed, 196 insertions(+), 202 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/e329167e/assembly/src/main/distribution/bin/scp.bat
----------------------------------------------------------------------
diff --git a/assembly/src/main/distribution/bin/scp.bat b/assembly/src/main/distribution/bin/scp.bat
index bab69df..6a30289 100644
--- a/assembly/src/main/distribution/bin/scp.bat
+++ b/assembly/src/main/distribution/bin/scp.bat
@@ -91,7 +91,7 @@ goto :EOF
 SET ARGS=%1 %2 %3 %4 %5 %6 %7 %8
 rem Execute the Java Virtual Machine
 cd %SSHD_HOME%
-"%JAVA%" %JAVA_OPTS% %OPTS% -classpath "%CLASSPATH%" -Dsshd.home="%SSHD_HOME%" org.apache.sshd.server.scp.ScpCommand %ARGS%
+"%JAVA%" %JAVA_OPTS% %OPTS% -classpath "%CLASSPATH%" -Dsshd.home="%SSHD_HOME%" org.apache.sshd.client.scp.DefaultScpClient %ARGS%
 
 rem # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
 

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/e329167e/assembly/src/main/distribution/bin/scp.sh
----------------------------------------------------------------------
diff --git a/assembly/src/main/distribution/bin/scp.sh b/assembly/src/main/distribution/bin/scp.sh
index d7d7b27..fa894d5 100644
--- a/assembly/src/main/distribution/bin/scp.sh
+++ b/assembly/src/main/distribution/bin/scp.sh
@@ -255,7 +255,7 @@ run() {
         CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
     fi
     cd $SSHD_BASE
-    exec $JAVA $JAVA_OPTS -Dsshd.home="$SSHD_HOME" $OPTS -classpath "$CLASSPATH" org.apache.sshd.server.scp.ScpCommand "$@"
+    exec $JAVA $JAVA_OPTS -Dsshd.home="$SSHD_HOME" $OPTS -classpath "$CLASSPATH" org.apache.sshd.client.scp.DefaultScpClient "$@"
 }
 
 main() {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/e329167e/sshd-core/src/main/java/org/apache/sshd/client/scp/DefaultScpClient.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/scp/DefaultScpClient.java b/sshd-core/src/main/java/org/apache/sshd/client/scp/DefaultScpClient.java
index b86df2f..7913ac9 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/scp/DefaultScpClient.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/scp/DefaultScpClient.java
@@ -18,16 +18,22 @@
  */
 package org.apache.sshd.client.scp;
 
+import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.io.OutputStream;
+import java.io.PrintStream;
 import java.nio.file.FileSystem;
 import java.nio.file.Path;
 import java.nio.file.attribute.PosixFilePermission;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.EnumSet;
-
+import java.util.List;
+import java.util.Set;
+import org.apache.sshd.client.SshClient;
 import org.apache.sshd.client.channel.ChannelExec;
 import org.apache.sshd.client.session.ClientSession;
 import org.apache.sshd.common.FactoryManager;
@@ -35,9 +41,12 @@ import org.apache.sshd.common.file.FileSystemFactory;
 import org.apache.sshd.common.file.util.MockFileSystem;
 import org.apache.sshd.common.file.util.MockPath;
 import org.apache.sshd.common.scp.ScpHelper;
+import org.apache.sshd.common.scp.ScpLocation;
 import org.apache.sshd.common.scp.ScpTimestamp;
 import org.apache.sshd.common.scp.ScpTransferEventListener;
+import org.apache.sshd.common.util.GenericUtils;
 import org.apache.sshd.common.util.ValidateUtils;
+import org.apache.sshd.common.util.io.NoCloseInputStream;
 
 /**
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
@@ -137,4 +146,152 @@ public class DefaultScpClient extends AbstractScpClient {
             channel.close(false);
         }
     }
+
+    //////////////////////////////////////////////////////////////////////////
+
+    private static boolean showError(PrintStream stderr, String message) {
+        stderr.println(message);
+        return true;
+    }
+
+    private static String[] normalizeCommandArguments(PrintStream stdout, PrintStream stderr, String ... args) {
+        int numArgs = GenericUtils.length(args);
+        if (numArgs <= 0) {
+            return args;
+        }
+
+        List<String> effective = new ArrayList<String>(numArgs);
+        boolean error = false;
+        for (int index = 0; (index < numArgs) && (!error); index++) {
+            String argName = args[index];
+            if ("-i".equals(argName) || "-P".equals(argName) || "-o".equals(argName)) {
+                if ((index + 1) >= numArgs) {
+                    error = showError(stderr, "option requires an argument: " + argName);
+                    break;
+                }
+
+                effective.add(argName);
+                effective.add(args[++index]);
+            } else if ("-r".equals(argName) || "-p".equals(argName) || "-q".equals(argName)) {
+                effective.add(argName);
+            } else if (argName.charAt(0) == '-') {
+                error = showError(stderr, "Unknown option: " + argName);
+                break;
+            } else {
+                if ((index + 1) >= numArgs) {
+                    error = showError(stderr, "Not enough arguments");
+                    break;
+                }
+
+                ScpLocation source = new ScpLocation(argName);
+                ScpLocation target = new ScpLocation(args[++index]);
+                if (index < (numArgs - 1)) {
+                    error = showError(stderr, "Unexpected extra arguments");
+                    break;
+                }
+
+                if (source.isLocal() == target.isLocal()) {
+                    error = showError(stderr, "Both targets are either remote or local");
+                    break;
+                }
+
+                ScpLocation remote = source.isLocal() ? target : source;
+                effective.add(remote.resolveUsername() + "@" + remote.getHost());
+                effective.add(source.toString());
+                effective.add(target.toString());
+                break;
+            }
+        }
+
+        if (error) {
+            return null;
+        }
+
+        return effective.toArray(new String[effective.size()]);
+    }
+
+    public static void main(String[] args) throws Exception {
+        final PrintStream stdout = System.out;
+        final PrintStream stderr = System.err;
+        try (BufferedReader stdin = new BufferedReader(new InputStreamReader(new NoCloseInputStream(System.in)))) {
+            args = normalizeCommandArguments(stdout, stderr, args);
+
+            ClientSession session = GenericUtils.isEmpty(args) ? null : SshClient.setupClientSession("-P", stdin, stdout, stderr, args);
+            if (session == null) {
+                stderr.println("usage: scp [-P port] [-i identity] [-r] [-p] [-q] [-o option=value] <source> <target>");
+                stderr.println();
+                stderr.println("Where <source> or <target> are either 'user@host:file' or a local file path");
+                stderr.println("NOTE: exactly ONE of the source or target must be remote and the other one local");
+                System.exit(-1);
+                return; // not that we really need it...
+            }
+
+            try {
+                // see the way normalizeCommandArguments works...
+                int numArgs = GenericUtils.length(args);
+                Collection<Option> options = EnumSet.noneOf(Option.class);
+                boolean quiet = false;
+                for (int index = 0; index < numArgs; index++) {
+                    String argName = args[index];
+                    if ("-r".equals(argName)) {
+                        options.add(Option.Recursive);
+                    } else if ("-p".equals(argName)) {
+                        options.add(Option.PreserveAttributes);
+                    } else if ("-q".equals(argName)) {
+                        quiet = true;
+                    }
+                }
+
+                if (!quiet) {
+                    session.setScpTransferEventListener(new ScpTransferEventListener() {
+                        @Override
+                        public void startFolderEvent(FileOperation op, Path file, Set<PosixFilePermission> perms) {
+                            logEvent("startFolderEvent", op, file, -1L, perms, null);
+                        }
+
+                        @Override
+                        public void endFolderEvent(FileOperation op, Path file, Set<PosixFilePermission> perms, Throwable thrown) {
+                            logEvent("endFolderEvent", op, file, -1L, perms, thrown);
+                        }
+
+                        @Override
+                        public void startFileEvent(FileOperation op, Path file, long length, Set<PosixFilePermission> perms) {
+                            logEvent("startFileEvent", op, file, length, perms, null);
+                        }
+
+                        @Override
+                        public void endFileEvent(FileOperation op, Path file, long length, Set<PosixFilePermission> perms, Throwable thrown) {
+                            logEvent("endFileEvent", op, file, length, perms, thrown);
+                        }
+
+                        private void logEvent(String name, FileOperation op, Path file, long length, Collection<PosixFilePermission> perms, Throwable thrown) {
+                            PrintStream ps = (thrown == null) ? stdout : stderr;
+                            ps.append('\t').append(name).append('[').append(op.name()).append(']').append(' ').append(file.toString());
+                            if (length > 0L) {
+                                ps.append(' ').append("length=").append(Long.toString(length));
+                            }
+                            ps.append(' ').append(String.valueOf(perms));
+
+                            if (thrown != null) {
+                                ps.append(" - ").append(thrown.getClass().getSimpleName()).append(": ").append(thrown.getMessage());
+                            }
+                            ps.println();
+                        }
+                    });
+                }
+
+                ScpClient client = session.createScpClient();
+                ScpLocation source = new ScpLocation(args[numArgs - 2]);
+                ScpLocation target = new ScpLocation(args[numArgs - 1]);
+                if (source.isLocal()) {
+                    client.upload(source.getPath(), target.getPath(), options);
+                } else {
+                    client.download(source.getPath(), target.getPath(), options);
+                }
+            } finally {
+                session.close();
+            }
+        }
+    }
 }
+

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/e329167e/sshd-core/src/main/java/org/apache/sshd/server/scp/ScpCommand.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/scp/ScpCommand.java b/sshd-core/src/main/java/org/apache/sshd/server/scp/ScpCommand.java
index bb26cdf..78458a1 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/scp/ScpCommand.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/scp/ScpCommand.java
@@ -18,36 +18,20 @@
  */
 package org.apache.sshd.server.scp;
 
-import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.InputStreamReader;
 import java.io.OutputStream;
-import java.io.PrintStream;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.FileSystem;
-import java.nio.file.Path;
-import java.nio.file.attribute.PosixFilePermission;
-import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.EnumSet;
-import java.util.List;
-import java.util.Set;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Future;
-import java.util.concurrent.atomic.AtomicBoolean;
 
-import org.apache.sshd.client.SshClient;
-import org.apache.sshd.client.scp.ScpClient;
-import org.apache.sshd.client.scp.ScpClient.Option;
-import org.apache.sshd.client.session.ClientSession;
 import org.apache.sshd.common.file.FileSystemAware;
 import org.apache.sshd.common.scp.ScpHelper;
-import org.apache.sshd.common.scp.ScpLocation;
 import org.apache.sshd.common.scp.ScpTransferEventListener;
 import org.apache.sshd.common.util.GenericUtils;
-import org.apache.sshd.common.util.io.NoCloseInputStream;
 import org.apache.sshd.common.util.logging.AbstractLoggingBean;
 import org.apache.sshd.common.util.threads.ThreadUtils;
 import org.apache.sshd.server.Command;
@@ -278,153 +262,4 @@ public class ScpCommand extends AbstractLoggingBean implements Command, Runnable
     public String toString() {
         return name;
     }
-
-    //////////////////////////////////////////////////////////////////////////
-
-    private static boolean showError(PrintStream stderr, String message) {
-        stderr.println(message);
-        return true;
-    }
-
-    private static String[] normalizeCommandArguments(PrintStream stdout, PrintStream stderr, String ... args) {
-        int numArgs = GenericUtils.length(args);
-        if (numArgs <= 0) {
-            return args;
-        }
-
-        List<String> effective = new ArrayList<String>(numArgs);
-        boolean error = false;
-        for (int index = 0; (index < numArgs) && (!error); index++) {
-            String argName = args[index];
-            if ("-i".equals(argName) || "-P".equals(argName) || "-o".equals(argName)) {
-                if ((index + 1) >= numArgs) {
-                    error = showError(stderr, "option requires an argument: " + argName);
-                    break;
-                }
-
-                effective.add(argName);
-                effective.add(args[++index]);
-            } else if ("-r".equals(argName) || "-p".equals(argName) || "-q".equals(argName)) {
-                effective.add(argName);
-            } else if (argName.charAt(0) == '-') {
-                error = showError(stderr, "Unknown option: " + argName);
-                break;
-            } else {
-                if ((index + 1) >= numArgs) {
-                    error = showError(stderr, "Not enough arguments");
-                    break;
-                }
-
-                ScpLocation source = new ScpLocation(argName);
-                ScpLocation target = new ScpLocation(args[++index]);
-                if (index < (numArgs - 1)) {
-                    error = showError(stderr, "Unexpected extra arguments");
-                    break;
-                }
-
-                if (source.isLocal() == target.isLocal()) {
-                    error = showError(stderr, "Both targets are either remote or local");
-                    break;
-                }
-
-                ScpLocation remote = source.isLocal() ? target : source;
-                effective.add(remote.resolveUsername() + "@" + remote.getHost());
-                effective.add(source.toString());
-                effective.add(target.toString());
-                break;
-            }
-        }
-
-        if (error) {
-            return null;
-        }
-
-        return effective.toArray(new String[effective.size()]);
-    }
-
-    public static void main(String[] args) throws Exception {
-        final PrintStream stdout = System.out;
-        final PrintStream stderr = System.err;
-        try (BufferedReader stdin = new BufferedReader(new InputStreamReader(new NoCloseInputStream(System.in)))) {
-            args = normalizeCommandArguments(stdout, stderr, args);
-
-            ClientSession session = GenericUtils.isEmpty(args) ? null : SshClient.setupClientSession("-P", stdin, stdout, stderr, args);
-            if (session == null) {
-                stderr.println("usage: scp [-P port] [-i identity] [-r] [-p] [-q] [-o option=value] <source> <target>");
-                stderr.println();
-                stderr.println("Where <source> or <target> are either 'user@host:file' or a local file path");
-                stderr.println("NOTE: exactly ONE of the source or target must be remote and the other one local");
-                System.exit(-1);
-                return; // not that we really need it...
-            }
-
-            try {
-                // see the way normalizeCommandArguments works...
-                int numArgs = GenericUtils.length(args);
-                Collection<Option> options = EnumSet.noneOf(Option.class);
-                final AtomicBoolean quietHolder = new AtomicBoolean(false);
-                for (int index = 0; index < numArgs; index++) {
-                    String argName = args[index];
-                    if ("-r".equals(argName)) {
-                        options.add(Option.Recursive);
-                    } else if ("-p".equals(argName)) {
-                        options.add(Option.PreserveAttributes);
-                    } else if ("-q".equals(argName)) {
-                        quietHolder.set(true);
-                    }
-                }
-
-                ScpLocation source = new ScpLocation(args[numArgs - 2]);
-                ScpLocation target = new ScpLocation(args[numArgs - 1]);
-
-                ScpClient client = session.createScpClient(new ScpTransferEventListener() {
-                    @Override
-                    public void startFolderEvent(FileOperation op, Path file, Set<PosixFilePermission> perms) {
-                        logEvent("startFolderEvent", op, file, -1L, perms, null);
-                    }
-
-                    @Override
-                    public void endFolderEvent(FileOperation op, Path file, Set<PosixFilePermission> perms, Throwable thrown) {
-                        logEvent("endFolderEvent", op, file, -1L, perms, thrown);
-                    }
-
-                    @Override
-                    public void startFileEvent(FileOperation op, Path file, long length, Set<PosixFilePermission> perms) {
-                        logEvent("startFileEvent", op, file, length, perms, null);
-                    }
-
-                    @Override
-                    public void endFileEvent(FileOperation op, Path file, long length, Set<PosixFilePermission> perms, Throwable thrown) {
-                        logEvent("endFileEvent", op, file, length, perms, thrown);
-                    }
-
-                    private void logEvent(String name, FileOperation op, Path file, long length, Collection<PosixFilePermission> perms, Throwable thrown) {
-                        if (quietHolder.get()) {
-                            return;
-                        }
-
-                        PrintStream ps = (thrown == null) ? stdout : stderr;
-                        ps.append('\t').append(name).append('[').append(op.name()).append(']').append(' ').append(file.toString());
-                        if (length > 0L) {
-                            ps.append(' ').append("length=").append(Long.toString(length));
-                        }
-                        ps.append(' ').append(String.valueOf(perms));
-
-                        if (thrown != null) {
-                            ps.append(" - ").append(thrown.getClass().getSimpleName()).append(": ").append(thrown.getMessage());
-                        }
-                        ps.println();
-                    }
-                });
-
-                if (source.isLocal()) {
-                    client.upload(source.getPath(), target.getPath(), options);
-                } else {
-                    client.download(source.getPath(), target.getPath(), options);
-                }
-            } finally {
-                session.close();
-            }
-        }
-    }
-}
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/e329167e/sshd-core/src/test/java/org/apache/sshd/client/scp/ScpCommandMain.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/client/scp/ScpCommandMain.java b/sshd-core/src/test/java/org/apache/sshd/client/scp/ScpCommandMain.java
new file mode 100644
index 0000000..a9a8082
--- /dev/null
+++ b/sshd-core/src/test/java/org/apache/sshd/client/scp/ScpCommandMain.java
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.sshd.client.scp;
+
+import org.apache.sshd.client.scp.DefaultScpClient;
+
+/**
+ * Just a test class used to invoke {@link DefaultScpClient#main(String[])} in
+ * order to have logging - which is in {@code test} scope
+ *
+ * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
+ */
+public class ScpCommandMain {
+    public static void main(String[] args) throws Exception {
+        DefaultScpClient.main(args);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/e329167e/sshd-core/src/test/java/org/apache/sshd/server/scp/ScpCommandMain.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/server/scp/ScpCommandMain.java b/sshd-core/src/test/java/org/apache/sshd/server/scp/ScpCommandMain.java
deleted file mode 100644
index aa6b42f..0000000
--- a/sshd-core/src/test/java/org/apache/sshd/server/scp/ScpCommandMain.java
+++ /dev/null
@@ -1,33 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.apache.sshd.server.scp;
-
-/**
- * Just a test class used to invoke {@link ScpCommand#main(String[])} in
- * order to have logging - which is in {@code test} scope
- *
- * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
- */
-public class ScpCommandMain {
-    public static void main(String[] args) throws Exception {
-        ScpCommand.main(args);
-    }
-
-}