From aef9d3a48699a978650fefa902ad3520aa1b7592 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20H=C3=B8ydahl?= Date: Sat, 1 Apr 2017 07:43:53 +0200 Subject: [PATCH] Fix bug in loadJars() (#131) Fix bug in load-jars which would not detect jars from lib/ if Path is not absolute Add slf4j-simple logger in test scope for some logging from tests Convert some File usage to Path --- .gitignore | 1 + pf4j/pom.xml | 6 +++++ .../pf4j/DefaultPluginDescriptorFinder.java | 3 +++ .../ro/fortsoft/pf4j/DefaultPluginLoader.java | 2 +- .../java/ro/fortsoft/pf4j/util/FileUtils.java | 22 ++++++++----------- 5 files changed, 20 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 0679f2f..0dce733 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ target/ .settings .idea/ *.iml +demo-dist/ diff --git a/pf4j/pom.xml b/pf4j/pom.xml index fe448a5..aeefe49 100644 --- a/pf4j/pom.xml +++ b/pf4j/pom.xml @@ -32,6 +32,12 @@ slf4j-api 1.7.5 + + org.slf4j + slf4j-simple + 1.7.5 + test + com.github.zafarkhaja java-semver diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginDescriptorFinder.java b/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginDescriptorFinder.java index e58aa05..8b7eb37 100644 --- a/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginDescriptorFinder.java +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginDescriptorFinder.java @@ -44,6 +44,9 @@ public class DefaultPluginDescriptorFinder extends ManifestPluginDescriptorFinde @Override public Manifest readManifest(Path pluginPath) throws PluginException { // TODO it's ok with first classes root? Another idea is to specify in PluginClasspath the folder. + if (pluginClasspath.getClassesDirectories().size() == 0) { + throw new PluginException("Failed to read manifest, no classes folder in classpath"); + } String classes = pluginClasspath.getClassesDirectories().get(0); Path manifestPath = pluginPath.resolve(Paths.get(classes,"/META-INF/MANIFEST.MF")); log.debug("Lookup plugin descriptor in '{}'", manifestPath); diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginLoader.java b/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginLoader.java index 88bbbef..e4c6b9f 100644 --- a/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginLoader.java +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginLoader.java @@ -69,7 +69,7 @@ public class DefaultPluginLoader implements PluginLoader { */ protected void loadJars(Path pluginPath, PluginClassLoader pluginClassLoader) { for (String libDirectory : pluginClasspath.getLibDirectories()) { - File file = pluginPath.resolve(libDirectory).toFile(); + Path file = pluginPath.resolve(libDirectory); List jars = FileUtils.getJars(file); for (File jar : jars) { pluginClassLoader.addFile(jar); diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/util/FileUtils.java b/pf4j/src/main/java/ro/fortsoft/pf4j/util/FileUtils.java index 9e2ca10..8cf29e4 100644 --- a/pf4j/src/main/java/ro/fortsoft/pf4j/util/FileUtils.java +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/util/FileUtils.java @@ -15,13 +15,9 @@ */ package ro.fortsoft.pf4j.util; -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileFilter; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; +import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -96,27 +92,27 @@ public class FileUtils { return success; } - public static List getJars(File folder) { + public static List getJars(Path folder) { List bucket = new ArrayList<>(); getJars(bucket, folder); return bucket; } - private static void getJars(List bucket, File folder) { + private static void getJars(final List bucket, Path folder) { FileFilter jarFilter = new JarFileFilter(); FileFilter directoryFilter = new DirectoryFileFilter(); - if (folder.exists() && folder.isDirectory() && folder.isAbsolute()) { - File[] jars = folder.listFiles(jarFilter); + if (Files.exists(folder) && Files.isDirectory(folder)) { + File[] jars = folder.toFile().listFiles(jarFilter); for (int i = 0; (jars != null) && (i < jars.length); ++i) { bucket.add(jars[i]); } - File[] directories = folder.listFiles(directoryFilter); + File[] directories = folder.toFile().listFiles(directoryFilter); for (int i = 0; (directories != null) && (i < directories.length); ++i) { File directory = directories[i]; - getJars(bucket, directory); + getJars(bucket, directory.toPath()); } } }