Browse Source

Delete plugin zip on uninstall (#136)

pull/137/head
Jan Høydahl 8 years ago committed by Decebal Suiu
parent
commit
f4ddddc61b
  1. 2
      pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginRepository.java
  2. 29
      pf4j/src/main/java/ro/fortsoft/pf4j/util/FileUtils.java
  3. 4
      pf4j/src/test/java/ro/fortsoft/pf4j/DefaultPluginRepositoryTest.java

2
pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginRepository.java

@ -68,7 +68,7 @@ public class DefaultPluginRepository extends BasePluginRepository {
@Override @Override
public boolean deletePluginPath(Path pluginPath) { public boolean deletePluginPath(Path pluginPath) {
// TODO remove plugin zip ? FileUtils.optimisticDelete(FileUtils.findWithEnding(pluginPath, ".zip", ".ZIP", ".Zip"));
return super.deletePluginPath(pluginPath); return super.deletePluginPath(pluginPath);
} }

29
pf4j/src/main/java/ro/fortsoft/pf4j/util/FileUtils.java

@ -118,4 +118,33 @@ public class FileUtils {
} }
} }
} }
/**
* Finds a path with various endings or null if not found
* @param basePath the base name
* @param endings a list of endings to search for
* @return new path or null if not found
*/
public static Path findWithEnding(Path basePath, String... endings) {
for (String ending : endings) {
Path newPath = basePath.resolveSibling(basePath.getFileName() + ending);
if (Files.exists(newPath)) {
return newPath;
}
}
return null;
}
/**
* Delete a file (not recursively) and ignore any errors
* @param path the path to delete
*/
public static void optimisticDelete(Path path) {
if (path == null) {
return;
}
try {
Files.delete(path);
} catch (IOException ignored) { }
}
} }

4
pf4j/src/test/java/ro/fortsoft/pf4j/DefaultPluginRepositoryTest.java

@ -44,6 +44,8 @@ public class DefaultPluginRepositoryTest {
testFolder.newFolder("plugin-1"); testFolder.newFolder("plugin-1");
// Prove that we can delete a folder with a file inside // Prove that we can delete a folder with a file inside
Files.createFile(Paths.get(testFolder.getRoot().getAbsolutePath()).resolve("plugin-1").resolve("myfile")); Files.createFile(Paths.get(testFolder.getRoot().getAbsolutePath()).resolve("plugin-1").resolve("myfile"));
// Create a zip file for plugin-1 to test that it is deleted when plugin is deleted
Files.createFile(Paths.get(testFolder.getRoot().getAbsolutePath()).resolve("plugin-1.zip"));
testFolder.newFolder("plugin-2"); testFolder.newFolder("plugin-2");
testFolder.newFolder("plugin-3"); testFolder.newFolder("plugin-3");
} }
@ -74,7 +76,9 @@ public class DefaultPluginRepositoryTest {
PluginRepository instance = new DefaultPluginRepository(pluginsRoot, false); PluginRepository instance = new DefaultPluginRepository(pluginsRoot, false);
assertTrue(Files.exists(pluginsRoot.resolve("plugin-1.zip")));
assertTrue(instance.deletePluginPath(pluginsRoot.resolve("plugin-1"))); assertTrue(instance.deletePluginPath(pluginsRoot.resolve("plugin-1")));
assertFalse(Files.exists(pluginsRoot.resolve("plugin-1.zip")));
assertTrue(instance.deletePluginPath(pluginsRoot.resolve("plugin-3"))); assertTrue(instance.deletePluginPath(pluginsRoot.resolve("plugin-3")));
assertFalse(instance.deletePluginPath(pluginsRoot.resolve("plugin-4"))); assertFalse(instance.deletePluginPath(pluginsRoot.resolve("plugin-4")));

Loading…
Cancel
Save