karaf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject [2/2] karaf git commit: [KARAF-5143] Command cannot be executed via SSH when property "karaf.shell.init.script" (etc/system.properties) has its default value
Date Fri, 02 Jun 2017 13:33:17 GMT
[KARAF-5143] Command cannot be executed via SSH when property "karaf.shell.init.script" (etc/system.properties)
has its default value

Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/58e552cc
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/58e552cc
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/58e552cc

Branch: refs/heads/master
Commit: 58e552cc4e1c84cbd07655057ed969fc8ce1edc9
Parents: f13b88f
Author: Guillaume Nodet <gnodet@apache.org>
Authored: Fri Jun 2 15:17:09 2017 +0200
Committer: Guillaume Nodet <gnodet@apache.org>
Committed: Fri Jun 2 15:33:08 2017 +0200

----------------------------------------------------------------------
 shell/core/pom.xml                              |   1 +
 .../shell/impl/console/ConsoleSessionImpl.java  |   1 +
 .../karaf/shell/impl/console/FilesStream.java   | 156 ------------------
 shell/ssh/pom.xml                               |   1 +
 .../apache/karaf/shell/ssh/ShellCommand.java    |  44 ++----
 .../karaf/util/filesstream/FilesStream.java     | 157 +++++++++++++++++++
 6 files changed, 175 insertions(+), 185 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/58e552cc/shell/core/pom.xml
----------------------------------------------------------------------
diff --git a/shell/core/pom.xml b/shell/core/pom.xml
index e8bdbe0..6c83e11 100644
--- a/shell/core/pom.xml
+++ b/shell/core/pom.xml
@@ -150,6 +150,7 @@
                             org.apache.karaf.service.guard.tools,
                             org.apache.karaf.shell.impl.*,
                             org.apache.karaf.util,
+                            org.apache.karaf.util.filesstream,
                             org.apache.karaf.util.jaas,
                             org.apache.karaf.util.tracker,
                             org.apache.felix.utils.properties,

http://git-wip-us.apache.org/repos/asf/karaf/blob/58e552cc/shell/core/src/main/java/org/apache/karaf/shell/impl/console/ConsoleSessionImpl.java
----------------------------------------------------------------------
diff --git a/shell/core/src/main/java/org/apache/karaf/shell/impl/console/ConsoleSessionImpl.java
b/shell/core/src/main/java/org/apache/karaf/shell/impl/console/ConsoleSessionImpl.java
index 5f1d7aa..f0a8569 100644
--- a/shell/core/src/main/java/org/apache/karaf/shell/impl/console/ConsoleSessionImpl.java
+++ b/shell/core/src/main/java/org/apache/karaf/shell/impl/console/ConsoleSessionImpl.java
@@ -57,6 +57,7 @@ import org.apache.karaf.shell.support.ShellUtil;
 import org.apache.karaf.shell.support.completers.FileCompleter;
 import org.apache.karaf.shell.support.completers.FileOrUriCompleter;
 import org.apache.karaf.shell.support.completers.UriCompleter;
+import org.apache.karaf.util.filesstream.FilesStream;
 import org.jline.builtins.Completers;
 import org.jline.reader.Completer;
 import org.jline.reader.EndOfFileException;

http://git-wip-us.apache.org/repos/asf/karaf/blob/58e552cc/shell/core/src/main/java/org/apache/karaf/shell/impl/console/FilesStream.java
----------------------------------------------------------------------
diff --git a/shell/core/src/main/java/org/apache/karaf/shell/impl/console/FilesStream.java
b/shell/core/src/main/java/org/apache/karaf/shell/impl/console/FilesStream.java
deleted file mode 100644
index 496945e..0000000
--- a/shell/core/src/main/java/org/apache/karaf/shell/impl/console/FilesStream.java
+++ /dev/null
@@ -1,156 +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.karaf.shell.impl.console;
-
-import java.io.IOException;
-import java.nio.file.FileVisitOption;
-import java.nio.file.FileVisitResult;
-import java.nio.file.FileVisitor;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.nio.file.PathMatcher;
-import java.nio.file.Paths;
-import java.nio.file.attribute.BasicFileAttributes;
-import java.util.ArrayList;
-import java.util.EnumSet;
-import java.util.List;
-import java.util.stream.Stream;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-final class FilesStream {
-	private static final Logger LOGGER = LoggerFactory.getLogger(ConsoleSessionImpl.class);
-
-    private FilesStream() {
-	}
-
-    /**
-     * Returns a stream of Paths for the given fileNames.
-     * The given names can be delimited by ",". A name can also contain
-     * {@link java.nio.file.FileSystem#getPathMatcher} syntax to refer to matching files.
 
-     * 
-     * @param fileNames list of names 
-     * @return Paths to the scripts 
-     */
-	static Stream<Path> stream(String fileNames) {
-        if (fileNames == null) {
-            return Stream.empty();
-        }
-        List<String> files = new ArrayList<>();
-        List<String> generators = new ArrayList<>();
-        StringBuilder buf = new StringBuilder(fileNames.length());
-        boolean hasUnescapedReserved = false;
-        boolean escaped = false;
-        for (int i = 0; i < fileNames.length(); i++) {
-            char c = fileNames.charAt(i);
-            if (escaped) {
-                buf.append(c);
-                escaped = false;
-            } else if (c == '\\') {
-                escaped = true;
-            } else if (c == ',') {
-                if (hasUnescapedReserved) {
-                    generators.add(buf.toString());
-                } else {
-                    files.add(buf.toString());
-                }
-                hasUnescapedReserved = false;
-                buf.setLength(0);
-            } else if ("*?{[".indexOf(c) >= 0) {
-                hasUnescapedReserved = true;
-                buf.append(c);
-            } else {
-                buf.append(c);
-            }
-        }
-        if (buf.length() > 0) {
-            if (hasUnescapedReserved) {
-                generators.add(buf.toString());
-            } else {
-                files.add(buf.toString());
-            }
-        }
-        Path cur = Paths.get(System.getProperty("karaf.base"));
-        return Stream.concat(
-                files.stream().map(cur::resolve),
-                generators.stream().flatMap(s -> files(cur, s)));
-    }
-
-    private static Stream<Path> files(Path cur, String glob) {
-        String prefix;
-        String rem;
-        int idx = glob.lastIndexOf('/');
-        if (idx >= 0) {
-            prefix = glob.substring(0, idx + 1);
-            rem = glob.substring(idx + 1);
-        } else {
-            prefix = "";
-            rem = glob;
-        }
-        Path dir = cur.resolve(prefix);
-        final PathMatcher matcher = dir.getFileSystem().getPathMatcher("glob:" + rem);
-        Stream.Builder<Path> stream = Stream.builder();
-        try {
-            Files.walkFileTree(dir,
-                    EnumSet.of(FileVisitOption.FOLLOW_LINKS),
-                    Integer.MAX_VALUE,
-                    new FileVisitor<Path>() {
-                        @Override
-                        public FileVisitResult preVisitDirectory(Path file, BasicFileAttributes
attrs) throws IOException {
-                            if (file.equals(dir)) {
-                                return FileVisitResult.CONTINUE;
-                            }
-                            if (Files.isHidden(file)) {
-                                return FileVisitResult.SKIP_SUBTREE;
-                            }
-                            Path r = dir.relativize(file);
-                            if (matcher.matches(r)) {
-                                stream.add(file);
-                            }
-                            return FileVisitResult.CONTINUE;
-                        }
-
-                        @Override
-                        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
-                            if (!Files.isHidden(file)) {
-                                Path r = dir.relativize(file);
-                                if (matcher.matches(r)) {
-                                    stream.add(file);
-                                }
-                            }
-                            return FileVisitResult.CONTINUE;
-                        }
-
-                        @Override
-                        public FileVisitResult visitFileFailed(Path file, IOException exc)
throws IOException {
-                            return FileVisitResult.CONTINUE;
-                        }
-
-                        @Override
-                        public FileVisitResult postVisitDirectory(Path dir, IOException exc)
throws IOException {
-                            return FileVisitResult.CONTINUE;
-                        }
-                    });
-        } catch (IOException e) {
-            LOGGER.warn("Error generating filenames", e);
-        }
-        return stream.build();
-    }
-}

http://git-wip-us.apache.org/repos/asf/karaf/blob/58e552cc/shell/ssh/pom.xml
----------------------------------------------------------------------
diff --git a/shell/ssh/pom.xml b/shell/ssh/pom.xml
index e7cfed1..abf5678 100644
--- a/shell/ssh/pom.xml
+++ b/shell/ssh/pom.xml
@@ -142,6 +142,7 @@
                         </Import-Package>
                         <Private-Package>
                             org.apache.karaf.util,
+                            org.apache.karaf.util.filesstream,
                             org.apache.karaf.util.jaas,
                         </Private-Package>
                     </instructions>

http://git-wip-us.apache.org/repos/asf/karaf/blob/58e552cc/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/ShellCommand.java
----------------------------------------------------------------------
diff --git a/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/ShellCommand.java b/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/ShellCommand.java
index f85ec3a..2b3d507 100644
--- a/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/ShellCommand.java
+++ b/shell/ssh/src/main/java/org/apache/karaf/shell/ssh/ShellCommand.java
@@ -18,15 +18,12 @@
  */
 package org.apache.karaf.shell.ssh;
 
-import java.io.CharArrayWriter;
-import java.io.File;
-import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.io.PrintStream;
-import java.io.Reader;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.security.PrivilegedActionException;
 import java.security.PrivilegedExceptionAction;
 import java.util.Map;
@@ -37,6 +34,7 @@ import org.apache.karaf.shell.api.console.Session;
 import org.apache.karaf.shell.api.console.SessionFactory;
 import org.apache.karaf.shell.support.ShellUtil;
 import org.apache.karaf.util.StreamUtils;
+import org.apache.karaf.util.filesstream.FilesStream;
 import org.apache.karaf.util.jaas.JaasHelper;
 import org.apache.sshd.server.Command;
 import org.apache.sshd.server.Environment;
@@ -152,30 +150,18 @@ public class ShellCommand implements Command, Runnable, SessionAware
{
     public void destroy() {
 	}
 
-    private void executeScript(String scriptFileName, Session session) {
-        if (scriptFileName != null) {
-            Reader r = null;
-            try {
-                File scriptFile = new File(scriptFileName);
-                r = new InputStreamReader(new FileInputStream(scriptFile));
-                CharArrayWriter w = new CharArrayWriter();
-                int n;
-                char[] buf = new char[8192];
-                while ((n = r.read(buf)) > 0) {
-                    w.write(buf, 0, n);
-                }
-                session.execute(new String(w.toCharArray()));
-            } catch (Exception e) {
-                LOGGER.debug("Error in initialization script", e);
-            } finally {
-                if (r != null) {
-                    try {
-                        r.close();
-                    } catch (IOException e) {
-                        // Ignore
-                    }
-                }
-            }
+    private void executeScript(String names, Session session) {
+        FilesStream.stream(names).forEach(p -> doExecuteScript(session, p));
+    }
+
+    private void doExecuteScript(Session session, Path scriptFileName) {
+        try {
+            String script = String.join("\n",
+                    Files.readAllLines(scriptFileName));
+            session.execute(script);
+        } catch (Exception e) {
+            LOGGER.debug("Error in initialization script {}", scriptFileName, e);
+            System.err.println("Error in initialization script: " + scriptFileName + ": "
+ e.getMessage());
         }
     }
 

http://git-wip-us.apache.org/repos/asf/karaf/blob/58e552cc/util/src/main/java/org/apache/karaf/util/filesstream/FilesStream.java
----------------------------------------------------------------------
diff --git a/util/src/main/java/org/apache/karaf/util/filesstream/FilesStream.java b/util/src/main/java/org/apache/karaf/util/filesstream/FilesStream.java
new file mode 100644
index 0000000..49d32a8
--- /dev/null
+++ b/util/src/main/java/org/apache/karaf/util/filesstream/FilesStream.java
@@ -0,0 +1,157 @@
+/*
+ * 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.karaf.util.filesstream;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.nio.file.FileVisitOption;
+import java.nio.file.FileVisitResult;
+import java.nio.file.FileVisitor;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.PathMatcher;
+import java.nio.file.Paths;
+import java.nio.file.attribute.BasicFileAttributes;
+import java.util.ArrayList;
+import java.util.EnumSet;
+import java.util.List;
+import java.util.stream.Stream;
+
+public final class FilesStream {
+
+	private static final Logger LOGGER = LoggerFactory.getLogger(FilesStream.class);
+
+    private FilesStream() {
+	}
+
+    /**
+     * Returns a stream of Paths for the given fileNames.
+     * The given names can be delimited by ",". A name can also contain
+     * {@link java.nio.file.FileSystem#getPathMatcher} syntax to refer to matching files.
 
+     * 
+     * @param fileNames list of names 
+     * @return Paths to the scripts 
+     */
+	public static Stream<Path> stream(String fileNames) {
+        if (fileNames == null) {
+            return Stream.empty();
+        }
+        List<String> files = new ArrayList<>();
+        List<String> generators = new ArrayList<>();
+        StringBuilder buf = new StringBuilder(fileNames.length());
+        boolean hasUnescapedReserved = false;
+        boolean escaped = false;
+        for (int i = 0; i < fileNames.length(); i++) {
+            char c = fileNames.charAt(i);
+            if (escaped) {
+                buf.append(c);
+                escaped = false;
+            } else if (c == '\\') {
+                escaped = true;
+            } else if (c == ',') {
+                if (hasUnescapedReserved) {
+                    generators.add(buf.toString());
+                } else {
+                    files.add(buf.toString());
+                }
+                hasUnescapedReserved = false;
+                buf.setLength(0);
+            } else if ("*?{[".indexOf(c) >= 0) {
+                hasUnescapedReserved = true;
+                buf.append(c);
+            } else {
+                buf.append(c);
+            }
+        }
+        if (buf.length() > 0) {
+            if (hasUnescapedReserved) {
+                generators.add(buf.toString());
+            } else {
+                files.add(buf.toString());
+            }
+        }
+        Path cur = Paths.get(System.getProperty("karaf.base"));
+        return Stream.concat(
+                files.stream().map(cur::resolve),
+                generators.stream().flatMap(s -> files(cur, s)));
+    }
+
+    private static Stream<Path> files(Path cur, String glob) {
+        String prefix;
+        String rem;
+        int idx = glob.lastIndexOf('/');
+        if (idx >= 0) {
+            prefix = glob.substring(0, idx + 1);
+            rem = glob.substring(idx + 1);
+        } else {
+            prefix = "";
+            rem = glob;
+        }
+        Path dir = cur.resolve(prefix);
+        final PathMatcher matcher = dir.getFileSystem().getPathMatcher("glob:" + rem);
+        Stream.Builder<Path> stream = Stream.builder();
+        try {
+            Files.walkFileTree(dir,
+                    EnumSet.of(FileVisitOption.FOLLOW_LINKS),
+                    Integer.MAX_VALUE,
+                    new FileVisitor<Path>() {
+                        @Override
+                        public FileVisitResult preVisitDirectory(Path file, BasicFileAttributes
attrs) throws IOException {
+                            if (file.equals(dir)) {
+                                return FileVisitResult.CONTINUE;
+                            }
+                            if (Files.isHidden(file)) {
+                                return FileVisitResult.SKIP_SUBTREE;
+                            }
+                            Path r = dir.relativize(file);
+                            if (matcher.matches(r)) {
+                                stream.add(file);
+                            }
+                            return FileVisitResult.CONTINUE;
+                        }
+
+                        @Override
+                        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
+                            if (!Files.isHidden(file)) {
+                                Path r = dir.relativize(file);
+                                if (matcher.matches(r)) {
+                                    stream.add(file);
+                                }
+                            }
+                            return FileVisitResult.CONTINUE;
+                        }
+
+                        @Override
+                        public FileVisitResult visitFileFailed(Path file, IOException exc)
throws IOException {
+                            return FileVisitResult.CONTINUE;
+                        }
+
+                        @Override
+                        public FileVisitResult postVisitDirectory(Path dir, IOException exc)
throws IOException {
+                            return FileVisitResult.CONTINUE;
+                        }
+                    });
+        } catch (IOException e) {
+            LOGGER.warn("Error generating filenames", e);
+        }
+        return stream.build();
+    }
+}


Mime
View raw message