Browse Source

Merge pull request #40 from lightglitch/plugin-status-provider

Add Plugin status provider
pull/41/merge
Decebal Suiu 10 years ago
parent
commit
56ac163f91
  1. 42
      pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java
  2. 92
      pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginStatusProvider.java
  3. 45
      pf4j/src/main/java/ro/fortsoft/pf4j/PluginStatusProvider.java

42
pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java

@ -75,9 +75,6 @@ public class DefaultPluginManager implements PluginManager {
*/
private List<PluginWrapper> startedPlugins;
private List<String> enabledPlugins;
private List<String> 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);
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;
}
@ -658,12 +647,13 @@ public class DefaultPluginManager implements PluginManager {
return new PluginClasspath();
}
protected boolean isPluginDisabled(String pluginId) {
if (enabledPlugins.isEmpty()) {
return disabledPlugins.contains(pluginId);
protected PluginStatusProvider createPluginStatusProvider() {
return new DefaultPluginStatusProvider(pluginsDirectory);
}
return !enabledPlugins.contains(pluginId);
protected boolean isPluginDisabled(String pluginId) {
return pluginStatusProvider.isPluginDisabled(pluginId);
}
protected boolean isPluginValid(PluginWrapper pluginWrapper) {
@ -728,7 +718,6 @@ public class DefaultPluginManager implements PluginManager {
unresolvedPlugins = new ArrayList<PluginWrapper>();
resolvedPlugins = new ArrayList<PluginWrapper>();
startedPlugins = new ArrayList<PluginWrapper>();
disabledPlugins = new ArrayList<String>();
pluginStateListeners = new ArrayList<PluginStateListener>();
@ -739,18 +728,7 @@ public class DefaultPluginManager implements PluginManager {
extensionFactory = createExtensionFactory();
pluginDescriptorFinder = createPluginDescriptorFinder();
extensionFinder = createExtensionFinder();
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);
}
pluginStatusProvider = createPluginStatusProvider();
System.setProperty("pf4j.pluginsDir", pluginsDirectory.getAbsolutePath());
}

92
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<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;
}
}

45
pf4j/src/main/java/ro/fortsoft/pf4j/PluginStatusProvider.java

@ -0,0 +1,45 @@
/*
* 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;
/**
* @author Decebal Suiu
* @author Mário Franco
*/
public interface PluginStatusProvider {
/**
* 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 boolean disablePlugin(String pluginId);
/**
* Enables a plugin that has previously been disabled.
*
* @param pluginId
* @return true if plugin is enabled
*/
public boolean enablePlugin(String pluginId);
}
Loading…
Cancel
Save