karaf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cschnei...@apache.org
Subject karaf git commit: Extract and document FilesStream for init scripts
Date Wed, 22 Mar 2017 11:13:28 GMT
Repository: karaf
Updated Branches:
  refs/heads/master cba7000ab -> 4cad3de3b


Extract and document FilesStream for init scripts

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

Branch: refs/heads/master
Commit: 4cad3de3b62dc14c0183b17293dfe036a98ee2b0
Parents: cba7000
Author: Christian Schneider <chris@die-schneider.net>
Authored: Wed Mar 22 11:08:45 2017 +0100
Committer: Christian Schneider <chris@die-schneider.net>
Committed: Wed Mar 22 12:13:22 2017 +0100

----------------------------------------------------------------------
 .../shell/impl/console/ConsoleSessionImpl.java  | 113 +-------------
 .../karaf/shell/impl/console/FilesStream.java   | 156 +++++++++++++++++++
 2 files changed, 157 insertions(+), 112 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/4cad3de3/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 4a80b1e..10f8929 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
@@ -24,16 +24,9 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.PrintStream;
 import java.lang.management.ManagementFactory;
-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.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -42,7 +35,6 @@ import java.util.Set;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
-import java.util.stream.Stream;
 
 import org.apache.felix.gogo.jline.Shell;
 import org.apache.felix.gogo.runtime.CommandSessionImpl;
@@ -464,7 +456,7 @@ public class ConsoleSessionImpl implements Session {
     }
 
     private void executeScript(String names) {
-        generateFiles(names).forEach(this::doExecuteScript);
+        FilesStream.stream(names).forEach(this::doExecuteScript);
     }
 
     private void doExecuteScript(Path scriptFileName) {
@@ -478,110 +470,7 @@ public class ConsoleSessionImpl implements Session {
         }
     }
 
-    private Stream<Path> generateFiles(String scriptFileName) {
-        if (scriptFileName == null) {
-            return Stream.empty();
-        }
-        List<String> files = new ArrayList<>();
-        List<String> generators = new ArrayList<>();
-        StringBuilder buf = new StringBuilder(scriptFileName.length());
-        boolean hasUnescapedReserved = false;
-        boolean escaped = false;
-        for (int i = 0; i < scriptFileName.length(); i++) {
-            char c = scriptFileName.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 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();
-    }
 
     protected void welcome(Properties brandingProps) {
         String welcome = brandingProps.getProperty("welcome");

http://git-wip-us.apache.org/repos/asf/karaf/blob/4cad3de3/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
new file mode 100644
index 0000000..496945e
--- /dev/null
+++ b/shell/core/src/main/java/org/apache/karaf/shell/impl/console/FilesStream.java
@@ -0,0 +1,156 @@
+/*
+ * 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();
+    }
+}


Mime
View raw message