Browse Source

Fix #248

pull/255/head
Decebal Suiu 6 years ago
parent
commit
935a724155
  1. 30
      pf4j/src/main/java/org/pf4j/BasePluginRepository.java
  2. 17
      pf4j/src/test/java/org/pf4j/LoadPluginsTest.java

30
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.NoSuchFileException;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator;
import java.util.List; import java.util.List;
/** /**
@ -33,21 +35,43 @@ import java.util.List;
public class BasePluginRepository implements PluginRepository { public class BasePluginRepository implements PluginRepository {
protected final Path pluginsRoot; protected final Path pluginsRoot;
protected FileFilter filter; protected FileFilter filter;
protected Comparator<File> comparator;
public BasePluginRepository(Path pluginsRoot) { public BasePluginRepository(Path pluginsRoot) {
this.pluginsRoot = pluginsRoot; this(pluginsRoot, null);
} }
public BasePluginRepository(Path pluginsRoot, FileFilter filter) { public BasePluginRepository(Path pluginsRoot, FileFilter filter) {
this.pluginsRoot = pluginsRoot; this.pluginsRoot = pluginsRoot;
this.filter = filter; this.filter = filter;
// last modified file is first
this.comparator = new Comparator<File>() {
@Override
public int compare(File o1, File o2) {
return (int) (o2.lastModified() - o1.lastModified());
}
};
} }
public void setFilter(FileFilter filter) { public void setFilter(FileFilter filter) {
this.filter = 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<File> comparator) {
this.comparator = comparator;
}
@Override @Override
public List<Path> getPluginPaths() { public List<Path> getPluginPaths() {
File[] files = pluginsRoot.toFile().listFiles(filter); File[] files = pluginsRoot.toFile().listFiles(filter);
@ -56,6 +80,10 @@ public class BasePluginRepository implements PluginRepository {
return Collections.emptyList(); return Collections.emptyList();
} }
if (comparator != null) {
Arrays.sort(files, comparator);
}
List<Path> paths = new ArrayList<>(files.length); List<Path> paths = new ArrayList<>(files.length);
for (File file : files) { for (File file : files) {
paths.add(file.toPath()); paths.add(file.toPath());

17
pf4j/src/test/java/org/pf4j/LoadPluginsTest.java

@ -92,7 +92,9 @@ public class LoadPluginsTest {
@Test @Test
public void upgrade() throws Exception { 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") .pluginVersion("1.2.3")
.build(); .build();
@ -102,18 +104,19 @@ public class LoadPluginsTest {
assertEquals(1, pluginManager.getPlugins().size()); assertEquals(1, pluginManager.getPlugins().size());
assertEquals(1, pluginManager.getStartedPlugins().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") .pluginVersion("2.0.0")
.build(); .build();
assertEquals("1.2.3", pluginManager.getPlugin(pluginZip2.pluginId()).getDescriptor().getVersion()); assertEquals("1.2.3", pluginManager.getPlugin(pluginId).getDescriptor().getVersion());
pluginManager.loadPlugins(); pluginManager.unloadPlugin(pluginId);
pluginManager.startPlugin(pluginZip2.pluginId()); pluginManager.loadPlugin(pluginZip2.path()); // or `pluginManager.loadPlugins();`
pluginManager.startPlugin(pluginId);
assertEquals(1, pluginManager.getPlugins().size()); assertEquals(1, pluginManager.getPlugins().size());
assertEquals("2.0.0", pluginManager.getPlugin(pluginZip2.pluginId()).getDescriptor().getVersion()); assertEquals("2.0.0", pluginManager.getPlugin(pluginId).getDescriptor().getVersion());
assertEquals("2.0.0", pluginManager.getStartedPlugins().get(1).getDescriptor().getVersion()); assertEquals("2.0.0", pluginManager.getStartedPlugins().get(0).getDescriptor().getVersion());
} }
@Test @Test

Loading…
Cancel
Save