*/
private List<PluginWrapper> startedPlugins;
- private List<String> enabledPlugins;
- private List<String> disabledPlugins;
-
/**
* The registered {@link PluginStateListener}s.
*/
private PluginFactory pluginFactory;
private ExtensionFactory extensionFactory;
+ private PluginStatusProvider pluginStatusProvider;
/**
* The plugins directory is supplied by System.getProperty("pf4j.pluginsDir", "plugins").
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;
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;
}
}
protected PluginStatusProvider createPluginStatusProvider() {
- return new PluginStatusProvider() {
-
- @Override
- public List<String> getEnabledPlugins() {
- List<String> 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<String> getDisabledPlugins() {
- List<String> 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) {
unresolvedPlugins = new ArrayList<PluginWrapper>();
resolvedPlugins = new ArrayList<PluginWrapper>();
startedPlugins = new ArrayList<PluginWrapper>();
- disabledPlugins = new ArrayList<String>();
pluginStateListeners = new ArrayList<PluginStateListener>();
extensionFactory = createExtensionFactory();
pluginDescriptorFinder = createPluginDescriptorFinder();
extensionFinder = createExtensionFinder();
-
- PluginStatusProvider statusLists = createPluginStatusProvider();
-
- enabledPlugins = statusLists.getEnabledPlugins();
- disabledPlugins = statusLists.getDisabledPlugins();
+ pluginStatusProvider = createPluginStatusProvider();
System.setProperty("pf4j.pluginsDir", pluginsDirectory.getAbsolutePath());
}
--- /dev/null
+/*
+ * 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<String> enabledPlugins = new ArrayList<String>();
+ private List<String> disabledPlugins = new ArrayList<String>();
+
+ 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;
+ }
+
+}
*/
package ro.fortsoft.pf4j;
-import java.util.List;
-
/**
* @author Decebal Suiu
* @author Mário Franco
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<String> 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<String> getDisabledPlugins();
+ public boolean enablePlugin(String pluginId);
}