From: Decebal Suiu Date: Fri, 5 Jun 2015 12:15:25 +0000 (+0300) Subject: review #41 X-Git-Tag: release-0.10.0~21^2 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=56a7ee47ac9758357ce6d5d8eb807da3a7910198;p=pf4j.git review #41 --- diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/CompoundPluginRepository.java b/pf4j/src/main/java/ro/fortsoft/pf4j/CompoundPluginRepository.java new file mode 100644 index 0000000..28c5d80 --- /dev/null +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/CompoundPluginRepository.java @@ -0,0 +1,52 @@ +/* + * Copyright 2012 Decebal Suiu + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with + * the License. You may obtain a copy of the License in the LICENSE file, or at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package ro.fortsoft.pf4j; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +/** + * @author Decebal Suiu + * @author Mário Franco + */ +public class CompoundPluginRepository implements PluginRepository { + + private final PluginRepository[] repositories; + + public CompoundPluginRepository(PluginRepository... repositories) { + this.repositories = repositories; + } + + @Override + public List getPluginArchives() { + List file = new ArrayList<>(); + for (PluginRepository repository : repositories) { + file.addAll(repository.getPluginArchives()); + } + + return file; + } + + @Override + public boolean deletePluginArchive(String pluginPath) { + for (PluginRepository repository : repositories) { + if (repository.deletePluginArchive(pluginPath)) { + return true; + } + } + + return false; + } + +} diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java b/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java index 9334315..9ee3e7a 100644 --- a/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java @@ -34,9 +34,6 @@ public class DefaultPluginManager implements PluginManager { public static final String DEFAULT_PLUGINS_DIRECTORY = "plugins"; public static final String DEVELOPMENT_PLUGINS_DIRECTORY = "../plugins"; - /** - * The plugins repository. - */ private File pluginsDirectory; private ExtensionFinder extensionFinder; @@ -95,20 +92,24 @@ public class DefaultPluginManager implements PluginManager { private ExtensionFactory extensionFactory; private PluginStatusProvider pluginStatusProvider; + /** + * The plugins repository. + */ + private PluginRepository pluginRepository; + /** * The plugins directory is supplied by System.getProperty("pf4j.pluginsDir", "plugins"). */ public DefaultPluginManager() { - this.pluginsDirectory = createPluginsDirectory(); + this.pluginsDirectory = createPluginsDirectory(); - initialize(); + initialize(); } /** * Constructs DefaultPluginManager which the given plugins directory. * - * @param pluginsDirectory - * the directory to search for plugins + * @param pluginsDirectory the directory to search for plugins */ public DefaultPluginManager(File pluginsDirectory) { this.pluginsDirectory = pluginsDirectory; @@ -343,16 +344,13 @@ public class DefaultPluginManager implements PluginManager { } // expand all plugin archives - FileFilter zipFilter = new ZipFileFilter(); - File[] zipFiles = pluginsDirectory.listFiles(zipFilter); - if (zipFiles != null) { - for (File zipFile : zipFiles) { - try { - expandPluginArchive(zipFile); - } catch (IOException e) { - log.error(e.getMessage(), e); - } - } + List pluginArchives = pluginRepository.getPluginArchives(); + for (File archiveFile : pluginArchives) { + try { + expandPluginArchive(archiveFile); + } catch (IOException e) { + log.error(e.getMessage(), e); + } } // check for no plugins @@ -447,7 +445,7 @@ public class DefaultPluginManager implements PluginManager { if (!pluginStatusProvider.disablePlugin(pluginId)) { return false; } - + log.info("Disabled plugin '{}:{}'", pluginDescriptor.getPluginId(), pluginDescriptor.getVersion()); return true; @@ -508,29 +506,12 @@ public class DefaultPluginManager implements PluginManager { } File pluginFolder = new File(pluginsDirectory, pluginWrapper.getPluginPath()); - File pluginZip = null; - - FileFilter zipFilter = new ZipFileFilter(); - File[] zipFiles = pluginsDirectory.listFiles(zipFilter); - if (zipFiles != null) { - // strip prepended / from the plugin path - String dirName = pluginWrapper.getPluginPath().substring(1); - // find the zip file that matches the plugin path - for (File zipFile : zipFiles) { - String name = zipFile.getName().substring(0, zipFile.getName().lastIndexOf('.')); - if (name.equals(dirName)) { - pluginZip = zipFile; - break; - } - } - } if (pluginFolder.exists()) { FileUtils.delete(pluginFolder); } - if (pluginZip != null && pluginZip.exists()) { - FileUtils.delete(pluginZip); - } + + pluginRepository.deletePluginArchive(pluginWrapper.getPluginPath()); return true; } @@ -651,8 +632,11 @@ public class DefaultPluginManager implements PluginManager { return new DefaultPluginStatusProvider(pluginsDirectory); } - protected boolean isPluginDisabled(String pluginId) { + protected PluginRepository createPluginRepository() { + return new DefaultPluginRepository(pluginsDirectory, new ZipFileFilter()); + } + protected boolean isPluginDisabled(String pluginId) { return pluginStatusProvider.isPluginDisabled(pluginId); } @@ -729,11 +713,12 @@ public class DefaultPluginManager implements PluginManager { pluginDescriptorFinder = createPluginDescriptorFinder(); extensionFinder = createExtensionFinder(); pluginStatusProvider = createPluginStatusProvider(); + pluginRepository = createPluginRepository(); System.setProperty("pf4j.pluginsDir", pluginsDirectory.getAbsolutePath()); } - private PluginWrapper loadPluginDirectory(File pluginDirectory) throws PluginException { + private PluginWrapper loadPluginDirectory(File pluginDirectory) throws PluginException { // try to load the plugin String pluginName = pluginDirectory.getName(); String pluginPath = "/".concat(pluginName); diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginRepository.java b/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginRepository.java new file mode 100644 index 0000000..15f6ca3 --- /dev/null +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginRepository.java @@ -0,0 +1,67 @@ +/* + * Copyright 2012 Decebal Suiu + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with + * the License. You may obtain a copy of the License in the LICENSE file, or at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package ro.fortsoft.pf4j; + +import ro.fortsoft.pf4j.util.FileUtils; + +import java.io.File; +import java.io.FileFilter; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +/** + * @author Decebal Suiu + * @author Mário Franco + */ +public class DefaultPluginRepository implements PluginRepository { + + private final File directory; + private final FileFilter filter; + + public DefaultPluginRepository(File directory, FileFilter filter) { + this.directory = directory; + this.filter = filter; + } + + @Override + public List getPluginArchives() { + File[] files = directory.listFiles(filter); + + return (files != null) ? Arrays.asList(files) : Collections.emptyList(); + } + + @Override + public boolean deletePluginArchive(String pluginPath) { + File[] files = directory.listFiles(filter); + if (files != null) { + File pluginArchive = null; + // strip prepended "/" from the plugin path + String dirName = pluginPath.substring(1); + // find the zip file that matches the plugin path + for (File archive : files) { + String name = archive.getName().substring(0, archive.getName().lastIndexOf('.')); + if (name.equals(dirName)) { + pluginArchive = archive; + break; + } + } + if (pluginArchive != null && pluginArchive.exists()) { + return FileUtils.delete(pluginArchive); + } + } + + return false; + } + +} diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/PluginRepository.java b/pf4j/src/main/java/ro/fortsoft/pf4j/PluginRepository.java new file mode 100644 index 0000000..dcdcc42 --- /dev/null +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/PluginRepository.java @@ -0,0 +1,41 @@ +/* + * Copyright 2012 Decebal Suiu + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with + * the License. You may obtain a copy of the License in the LICENSE file, or at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package ro.fortsoft.pf4j; + +import java.io.File; +import java.util.List; + +/** + * Directory whose contents are .zip files used as plugins. + * + * @author Decebal Suiu + * @author Mário Franco + */ +public interface PluginRepository { + + /** + * List all plugin archive filed. + * + * @return a list of files + */ + public List getPluginArchives(); + + /** + * Removes a plugin from the repository. + * + * @param pluginPath the plugin path + * @return true if deleted + */ + public boolean deletePluginArchive(String pluginPath); + +}