You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by gn...@apache.org on 2017/06/02 13:39:31 UTC
karaf git commit: [KARAF-5143] Command cannot be executed via SSH
when property "karaf.shell.init.script" (etc/system.properties) has its
default value
Repository: karaf
Updated Branches:
refs/heads/karaf-4.1.x d1be682c2 -> 98ba39e48
[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/98ba39e4
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/98ba39e4
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/98ba39e4
Branch: refs/heads/karaf-4.1.x
Commit: 98ba39e4801ad6598af9b5011c5df72955663455
Parents: d1be682
Author: Guillaume Nodet <gn...@apache.org>
Authored: Fri Jun 2 15:17:09 2017 +0200
Committer: Guillaume Nodet <gn...@apache.org>
Committed: Fri Jun 2 15:33:32 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/98ba39e4/shell/core/pom.xml
----------------------------------------------------------------------
diff --git a/shell/core/pom.xml b/shell/core/pom.xml
index 548f0bb..4d7a1f3 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/98ba39e4/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/98ba39e4/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/98ba39e4/shell/ssh/pom.xml
----------------------------------------------------------------------
diff --git a/shell/ssh/pom.xml b/shell/ssh/pom.xml
index aa9830e..044b1d3 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/98ba39e4/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/98ba39e4/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();
+ }
+}