diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginRepository.java b/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginRepository.java index 953bc4e..f662bc4 100644 --- a/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginRepository.java +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginRepository.java @@ -68,7 +68,7 @@ public class DefaultPluginRepository extends BasePluginRepository { @Override public boolean deletePluginPath(Path pluginPath) { - // TODO remove plugin zip ? + FileUtils.optimisticDelete(FileUtils.findWithEnding(pluginPath, ".zip", ".ZIP", ".Zip")); return super.deletePluginPath(pluginPath); } 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 24f67fc..70b4e01 100644 --- a/pf4j/src/main/java/ro/fortsoft/pf4j/util/FileUtils.java +++ b/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) { } + } } diff --git a/pf4j/src/test/java/ro/fortsoft/pf4j/DefaultPluginRepositoryTest.java b/pf4j/src/test/java/ro/fortsoft/pf4j/DefaultPluginRepositoryTest.java index 0e85bdc..95c153b 100644 --- a/pf4j/src/test/java/ro/fortsoft/pf4j/DefaultPluginRepositoryTest.java +++ b/pf4j/src/test/java/ro/fortsoft/pf4j/DefaultPluginRepositoryTest.java @@ -44,6 +44,8 @@ public class DefaultPluginRepositoryTest { testFolder.newFolder("plugin-1"); // Prove that we can delete a folder with a file inside 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-3"); } @@ -74,7 +76,9 @@ public class DefaultPluginRepositoryTest { PluginRepository instance = new DefaultPluginRepository(pluginsRoot, false); + assertTrue(Files.exists(pluginsRoot.resolve("plugin-1.zip"))); assertTrue(instance.deletePluginPath(pluginsRoot.resolve("plugin-1"))); + assertFalse(Files.exists(pluginsRoot.resolve("plugin-1.zip"))); assertTrue(instance.deletePluginPath(pluginsRoot.resolve("plugin-3"))); assertFalse(instance.deletePluginPath(pluginsRoot.resolve("plugin-4")));