]> source.dussan.org Git - pf4j.git/commitdiff
Fix issue with listing files from the Jar file. Work with Jar's like with external...
authorAlexander Horuzhiy <123456>
Mon, 18 Jan 2016 15:25:39 +0000 (16:25 +0100)
committerAlexander Horuzhiy <123456>
Mon, 18 Jan 2016 15:25:39 +0000 (16:25 +0100)
pf4j/src/main/java/ro/fortsoft/pf4j/ServiceProviderExtensionFinder.java

index 05c9cb4f2b399617602b00a007ba31b6d649edff..64af94684ff0e1796c5afeab3f9c3a93c40a1b1c 100644 (file)
@@ -25,11 +25,10 @@ import java.io.IOException;
 import java.io.Reader;
 import java.net.URISyntaxException;
 import java.net.URL;
-import java.util.HashSet;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import java.nio.file.*;
+import java.nio.file.attribute.BasicFileAttributes;
+import java.util.*;
+import java.util.stream.Stream;
 
 /**
  * The ServiceLoader base implementation for ExtensionFinder.
@@ -50,18 +49,26 @@ public class ServiceProviderExtensionFinder extends AbstractExtensionFinder {
         log.debug("Reading extensions storages from classpath");
         Map<String, Set<String>> result = new LinkedHashMap<>();
 
-        Set<String> bucket = new HashSet<>();
+        final Set<String> bucket = new HashSet<>();
         try {
             URL url = getClass().getClassLoader().getResource(getExtensionsResource());
             if (url != null) {
-                File[] files = new File(url.toURI()).listFiles();
-                if (files != null) {
-                    for (File file : files) {
+                Path extensionPath;
+                if (url.toURI().getScheme().equals("jar")) {
+                    FileSystem fileSystem = FileSystems.newFileSystem(url.toURI(), Collections.<String, Object>emptyMap());
+                    extensionPath = fileSystem.getPath(getExtensionsResource());
+                } else {
+                    extensionPath = Paths.get(url.toURI());
+                }
+                Files.walkFileTree(extensionPath, Collections.<FileVisitOption>emptySet(), 1, new SimpleFileVisitor<Path>() {
+                    @Override
+                    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                         log.debug("Read '{}'", file);
-                        Reader reader = new FileReader(file);
+                        Reader reader = Files.newBufferedReader(file);
                         ServiceProviderExtensionStorage.read(reader, bucket);
+                        return FileVisitResult.CONTINUE;
                     }
-                }
+                });
             }
 
             if (bucket.isEmpty()) {