diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java b/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java index 4af3947..9334315 100644 --- a/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java @@ -75,9 +75,6 @@ public class DefaultPluginManager implements PluginManager { */ private List startedPlugins; - private List enabledPlugins; - private List disabledPlugins; - /** * The registered {@link PluginStateListener}s. */ @@ -96,6 +93,7 @@ public class DefaultPluginManager implements PluginManager { private PluginFactory pluginFactory; private ExtensionFactory extensionFactory; + private PluginStatusProvider pluginStatusProvider; /** * The plugins directory is supplied by System.getProperty("pf4j.pluginsDir", "plugins"). @@ -446,14 +444,10 @@ public class DefaultPluginManager implements PluginManager { firePluginStateEvent(new PluginStateEvent(this, pluginWrapper, PluginState.STOPPED)); - if (disabledPlugins.add(pluginId)) { - try { - FileUtils.writeLines(disabledPlugins, new File(pluginsDirectory, "disabled.txt")); - } catch (IOException e) { - log.error("Failed to disable plugin {}", pluginId, e); - return false; - } + if (!pluginStatusProvider.disablePlugin(pluginId)) { + return false; } + log.info("Disabled plugin '{}:{}'", pluginDescriptor.getPluginId(), pluginDescriptor.getVersion()); return true; @@ -482,12 +476,7 @@ public class DefaultPluginManager implements PluginManager { return true; } - try { - if (disabledPlugins.remove(pluginId)) { - FileUtils.writeLines(disabledPlugins, new File(pluginsDirectory, "disabled.txt")); - } - } catch (IOException e) { - log.error("Failed to enable plugin {}", pluginId, e); + if (!pluginStatusProvider.enablePlugin(pluginId)) { return false; } @@ -659,42 +648,12 @@ public class DefaultPluginManager implements PluginManager { } protected PluginStatusProvider createPluginStatusProvider() { - return new PluginStatusProvider() { - - @Override - public List getEnabledPlugins() { - List enabledPlugins = Collections.emptyList(); - try { - // create a list with plugin identifiers that should be only accepted by this manager (whitelist from plugins/enabled.txt file) - enabledPlugins = FileUtils.readLines(new File(pluginsDirectory, "enabled.txt"), true); - log.info("Enabled plugins: {}", enabledPlugins); - } catch (IOException e) { - log.error(e.getMessage(), e); - } - return enabledPlugins; - } - - @Override - public List getDisabledPlugins() { - List disabledPlugins = Collections.emptyList(); - try { - // create a list with plugin identifiers that should not be accepted by this manager (blacklist from plugins/disabled.txt file) - disabledPlugins = FileUtils.readLines(new File(pluginsDirectory, "disabled.txt"), true); - log.info("Disabled plugins: {}", disabledPlugins); - } catch (IOException e) { - log.error(e.getMessage(), e); - } - return disabledPlugins; - } - }; + return new DefaultPluginStatusProvider(pluginsDirectory); } protected boolean isPluginDisabled(String pluginId) { - if (enabledPlugins.isEmpty()) { - return disabledPlugins.contains(pluginId); - } - return !enabledPlugins.contains(pluginId); + return pluginStatusProvider.isPluginDisabled(pluginId); } protected boolean isPluginValid(PluginWrapper pluginWrapper) { @@ -759,7 +718,6 @@ public class DefaultPluginManager implements PluginManager { unresolvedPlugins = new ArrayList(); resolvedPlugins = new ArrayList(); startedPlugins = new ArrayList(); - disabledPlugins = new ArrayList(); pluginStateListeners = new ArrayList(); @@ -770,11 +728,7 @@ public class DefaultPluginManager implements PluginManager { extensionFactory = createExtensionFactory(); pluginDescriptorFinder = createPluginDescriptorFinder(); extensionFinder = createExtensionFinder(); - - PluginStatusProvider statusLists = createPluginStatusProvider(); - - enabledPlugins = statusLists.getEnabledPlugins(); - disabledPlugins = statusLists.getDisabledPlugins(); + pluginStatusProvider = createPluginStatusProvider(); System.setProperty("pf4j.pluginsDir", pluginsDirectory.getAbsolutePath()); } diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginStatusProvider.java b/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginStatusProvider.java new file mode 100644 index 0000000..84d09cc --- /dev/null +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginStatusProvider.java @@ -0,0 +1,92 @@ +/* + * Copyright 2014 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.io.IOException; +import java.util.ArrayList; +import java.util.List; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import ro.fortsoft.pf4j.util.FileUtils; + +/** + * The default implementation for PluginStatusProvider. + * + * @author Decebal Suiu + * @author Mário Franco + */ +public class DefaultPluginStatusProvider implements PluginStatusProvider { + + private static final Logger log = LoggerFactory.getLogger(DefaultPluginStatusProvider.class); + + private final File pluginsDirectory; + + private List enabledPlugins = new ArrayList(); + private List disabledPlugins = new ArrayList(); + + public DefaultPluginStatusProvider(File pluginsDirectory) { + this.pluginsDirectory = pluginsDirectory; + initialize(); + } + + private void initialize() { + try { + // create a list with plugin identifiers that should be only accepted by this manager (whitelist from plugins/enabled.txt file) + enabledPlugins = FileUtils.readLines(new File(pluginsDirectory, "enabled.txt"), true); + log.info("Enabled plugins: {}", enabledPlugins); + + // create a list with plugin identifiers that should not be accepted by this manager (blacklist from plugins/disabled.txt file) + disabledPlugins = FileUtils.readLines(new File(pluginsDirectory, "disabled.txt"), true); + log.info("Disabled plugins: {}", disabledPlugins); + } catch (IOException e) { + log.error(e.getMessage(), e); + } + } + + @Override + public boolean isPluginDisabled(String pluginId) { + if (enabledPlugins.isEmpty()) { + return disabledPlugins.contains(pluginId); + } + + return !enabledPlugins.contains(pluginId); + } + + @Override + public boolean disablePlugin(String pluginId) { + if (disabledPlugins.add(pluginId)) { + try { + FileUtils.writeLines(disabledPlugins, new File(pluginsDirectory, "disabled.txt")); + } catch (IOException e) { + log.error("Failed to disable plugin {}", pluginId, e); + return false; + } + } + return true; + } + + @Override + public boolean enablePlugin(String pluginId) { + try { + if (disabledPlugins.remove(pluginId)) { + FileUtils.writeLines(disabledPlugins, new File(pluginsDirectory, "disabled.txt")); + } + } catch (IOException e) { + log.error("Failed to enable plugin {}", pluginId, e); + return false; + } + return true; + } + +} diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/PluginStatusProvider.java b/pf4j/src/main/java/ro/fortsoft/pf4j/PluginStatusProvider.java index e0d93dd..33c7b6f 100644 --- a/pf4j/src/main/java/ro/fortsoft/pf4j/PluginStatusProvider.java +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/PluginStatusProvider.java @@ -12,8 +12,6 @@ */ package ro.fortsoft.pf4j; -import java.util.List; - /** * @author Decebal Suiu * @author Mário Franco @@ -21,13 +19,27 @@ import java.util.List; public interface PluginStatusProvider { /** - * Retrieves a list with plugin identifiers that should be only accepted by this manager + * Checks if the plugin is disabled or not + * + * @param pluginId the plugin id + * @return if the plugin is disabled or not + */ + public boolean isPluginDisabled(String pluginId); + + /** + * Disables a plugin from being loaded. + * + * @param pluginId + * @return true if plugin is disabled */ - public List getEnabledPlugins(); + public boolean disablePlugin(String pluginId); /** - * Retrieves a list with plugin identifiers that should not be accepted by this manager + * Enables a plugin that has previously been disabled. + * + * @param pluginId + * @return true if plugin is enabled */ - public List getDisabledPlugins(); + public boolean enablePlugin(String pluginId); }