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;
/**
public class BasePluginRepository implements PluginRepository {
protected final Path pluginsRoot;
+
protected FileFilter filter;
+ protected Comparator<File> 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<File>() {
+
+ @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<File> comparator) {
+ this.comparator = comparator;
+ }
+
@Override
public List<Path> getPluginPaths() {
File[] files = pluginsRoot.toFile().listFiles(filter);
return Collections.emptyList();
}
+ if (comparator != null) {
+ Arrays.sort(files, comparator);
+ }
+
List<Path> paths = new ArrayList<>(files.length);
for (File file : files) {
paths.add(file.toPath());
@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();
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