From 935a72415569fda0688d9321053b2d8a806fa971 Mon Sep 17 00:00:00 2001 From: Decebal Suiu Date: Fri, 23 Nov 2018 08:51:47 +0200 Subject: [PATCH] Fix #248 --- .../java/org/pf4j/BasePluginRepository.java | 30 ++++++++++++++++++- .../test/java/org/pf4j/LoadPluginsTest.java | 17 ++++++----- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/pf4j/src/main/java/org/pf4j/BasePluginRepository.java b/pf4j/src/main/java/org/pf4j/BasePluginRepository.java index 0745b57..a353756 100644 --- a/pf4j/src/main/java/org/pf4j/BasePluginRepository.java +++ b/pf4j/src/main/java/org/pf4j/BasePluginRepository.java @@ -23,7 +23,9 @@ import java.io.IOException; import java.nio.file.NoSuchFileException; import java.nio.file.Path; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; +import java.util.Comparator; import java.util.List; /** @@ -33,21 +35,43 @@ import java.util.List; public class BasePluginRepository implements PluginRepository { protected final Path pluginsRoot; + protected FileFilter filter; + protected Comparator comparator; public BasePluginRepository(Path pluginsRoot) { - this.pluginsRoot = pluginsRoot; + this(pluginsRoot, null); } public BasePluginRepository(Path pluginsRoot, FileFilter filter) { this.pluginsRoot = pluginsRoot; this.filter = filter; + + // last modified file is first + this.comparator = new Comparator() { + + @Override + public int compare(File o1, File o2) { + return (int) (o2.lastModified() - o1.lastModified()); + } + + }; } public void setFilter(FileFilter filter) { this.filter = filter; } + /** + * Set a {@link File} {@link Comparator} used to sort the listed files from {@code pluginsRoot}. + * This comparator is used in {@link #getPluginPaths()} method. + * By default is used a file comparator that returns the last modified files first. + * If you don't want a file comparator, then call this method with {@code null}. + */ + public void setComparator(Comparator comparator) { + this.comparator = comparator; + } + @Override public List getPluginPaths() { File[] files = pluginsRoot.toFile().listFiles(filter); @@ -56,6 +80,10 @@ public class BasePluginRepository implements PluginRepository { return Collections.emptyList(); } + if (comparator != null) { + Arrays.sort(files, comparator); + } + List paths = new ArrayList<>(files.length); for (File file : files) { paths.add(file.toPath()); diff --git a/pf4j/src/test/java/org/pf4j/LoadPluginsTest.java b/pf4j/src/test/java/org/pf4j/LoadPluginsTest.java index cf7280f..8b6af11 100644 --- a/pf4j/src/test/java/org/pf4j/LoadPluginsTest.java +++ b/pf4j/src/test/java/org/pf4j/LoadPluginsTest.java @@ -92,7 +92,9 @@ public class LoadPluginsTest { @Test public void upgrade() throws Exception { - new PluginZip.Builder(pluginsFolder.newFile("my-plugin-1.2.3.zip"), "myPlugin") + String pluginId = "myPlugin"; + + new PluginZip.Builder(pluginsFolder.newFile("my-plugin-1.2.3.zip"), pluginId) .pluginVersion("1.2.3") .build(); @@ -102,18 +104,19 @@ public class LoadPluginsTest { assertEquals(1, pluginManager.getPlugins().size()); assertEquals(1, pluginManager.getStartedPlugins().size()); - PluginZip pluginZip2 = new PluginZip.Builder(pluginsFolder.newFile("my-plugin-2.0.0.ZIP"), "myPlugin") + PluginZip pluginZip2 = new PluginZip.Builder(pluginsFolder.newFile("my-plugin-2.0.0.ZIP"), pluginId) .pluginVersion("2.0.0") .build(); - assertEquals("1.2.3", pluginManager.getPlugin(pluginZip2.pluginId()).getDescriptor().getVersion()); + assertEquals("1.2.3", pluginManager.getPlugin(pluginId).getDescriptor().getVersion()); - pluginManager.loadPlugins(); - pluginManager.startPlugin(pluginZip2.pluginId()); + pluginManager.unloadPlugin(pluginId); + pluginManager.loadPlugin(pluginZip2.path()); // or `pluginManager.loadPlugins();` + pluginManager.startPlugin(pluginId); assertEquals(1, pluginManager.getPlugins().size()); - assertEquals("2.0.0", pluginManager.getPlugin(pluginZip2.pluginId()).getDescriptor().getVersion()); - assertEquals("2.0.0", pluginManager.getStartedPlugins().get(1).getDescriptor().getVersion()); + assertEquals("2.0.0", pluginManager.getPlugin(pluginId).getDescriptor().getVersion()); + assertEquals("2.0.0", pluginManager.getStartedPlugins().get(0).getDescriptor().getVersion()); } @Test -- 2.39.5