Browse Source

add support for enabled (whitelist) plugins and disabled (blacklist) plugins

pull/3/head
Decebal Suiu 12 years ago
parent
commit
1ebca8c91a
  1. 6
      demo/disabled.txt
  2. 6
      demo/enabled.txt
  3. 78
      pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java
  4. 53
      pf4j/src/main/java/ro/fortsoft/pf4j/util/FileUtils.java

6
demo/disabled.txt

@ -0,0 +1,6 @@
########################################
# - load all plugins except these
# - add one plugin id on each line
# - put this file in plugins folder
########################################
welcome-plugin

6
demo/enabled.txt

@ -0,0 +1,6 @@
########################################
# - load only these plugins
# - add one plugin id on each line
# - put this file in plugins folder
########################################
welcome-plugin

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

@ -26,6 +26,7 @@ import org.slf4j.LoggerFactory;
import ro.fortsoft.pf4j.util.CompoundClassLoader; import ro.fortsoft.pf4j.util.CompoundClassLoader;
import ro.fortsoft.pf4j.util.DirectoryFilter; import ro.fortsoft.pf4j.util.DirectoryFilter;
import ro.fortsoft.pf4j.util.FileUtils;
import ro.fortsoft.pf4j.util.Unzip; import ro.fortsoft.pf4j.util.Unzip;
import ro.fortsoft.pf4j.util.ZipFilter; import ro.fortsoft.pf4j.util.ZipFilter;
@ -72,16 +73,14 @@ public class DefaultPluginManager implements PluginManager {
*/ */
private List<PluginWrapper> resolvedPlugins; private List<PluginWrapper> resolvedPlugins;
/**
* A list with disabled plugins.
*/
private List<PluginWrapper> disabledPlugins;
/** /**
* A list with started plugins. * A list with started plugins.
*/ */
private List<PluginWrapper> startedPlugins; private List<PluginWrapper> startedPlugins;
private List<String> enabledPlugins;
private List<String> disabledPlugins;
/** /**
* A compound class loader of resolved plugins. * A compound class loader of resolved plugins.
*/ */
@ -108,22 +107,26 @@ public class DefaultPluginManager implements PluginManager {
pathToIdMap = new HashMap<String, String>(); pathToIdMap = new HashMap<String, String>();
unresolvedPlugins = new ArrayList<PluginWrapper>(); unresolvedPlugins = new ArrayList<PluginWrapper>();
resolvedPlugins = new ArrayList<PluginWrapper>(); resolvedPlugins = new ArrayList<PluginWrapper>();
disabledPlugins = new ArrayList<PluginWrapper>();
startedPlugins = new ArrayList<PluginWrapper>(); startedPlugins = new ArrayList<PluginWrapper>();
disabledPlugins = new ArrayList<String>();
compoundClassLoader = new CompoundClassLoader(); compoundClassLoader = new CompoundClassLoader();
pluginDescriptorFinder = createPluginDescriptorFinder(); pluginDescriptorFinder = createPluginDescriptorFinder();
extensionFinder = createExtensionFinder(); extensionFinder = createExtensionFinder();
System.setProperty("pf4j.pluginsDir", pluginsDirectory.getAbsolutePath()); 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);
protected PluginDescriptorFinder createPluginDescriptorFinder() { // create a list with plugin identifiers that should not be accepted by this manager (blacklist from plugins/disabled.txt file)
return new DefaultPluginDescriptorFinder(); disabledPlugins = FileUtils.readLines(new File(pluginsDirectory, "disabled.txt"), true);
log.info("Disabled plugins: " + disabledPlugins);
} catch (IOException e) {
log.error(e.getMessage(), e);
} }
protected ExtensionFinder createExtensionFinder() { System.setProperty("pf4j.pluginsDir", pluginsDirectory.getAbsolutePath());
return new DefaultExtensionFinder(compoundClassLoader);
} }
@Override @Override
@ -145,10 +148,6 @@ public class DefaultPluginManager implements PluginManager {
return unresolvedPlugins; return unresolvedPlugins;
} }
public List<PluginWrapper> getDisabledPlugins() {
return disabledPlugins;
}
@Override @Override
public List<PluginWrapper> getStartedPlugins() { public List<PluginWrapper> getStartedPlugins() {
return startedPlugins; return startedPlugins;
@ -211,9 +210,15 @@ public class DefaultPluginManager implements PluginManager {
} }
} }
// load any plugin from plugins directory // check for no plugins
FilenameFilter directoryFilter = new DirectoryFilter(); FilenameFilter directoryFilter = new DirectoryFilter();
String[] directories = pluginsDirectory.list(directoryFilter); String[] directories = pluginsDirectory.list(directoryFilter);
if (directories.length == 0) {
log.info("No plugins");
return;
}
// load any plugin from plugins directory
for (String directory : directories) { for (String directory : directories) {
try { try {
loadPlugin(directory); loadPlugin(directory);
@ -222,12 +227,6 @@ public class DefaultPluginManager implements PluginManager {
} }
} }
// check for no plugins
if (directories.length == 0) {
log.info("No plugins");
return;
}
// resolve 'unresolvedPlugins' // resolve 'unresolvedPlugins'
try { try {
resolvePlugins(); resolvePlugins();
@ -279,11 +278,6 @@ public class DefaultPluginManager implements PluginManager {
// try to load the plugin // try to load the plugin
String pluginPath = "/".concat(fileName); String pluginPath = "/".concat(fileName);
// test for disabled plugin
if (disabledPlugins.contains(pluginPath)) {
return;
}
// test for plugin duplication // test for plugin duplication
if (plugins.get(pathToIdMap.get(pluginPath)) != null) { if (plugins.get(pathToIdMap.get(pluginPath)) != null) {
return; return;
@ -296,6 +290,12 @@ public class DefaultPluginManager implements PluginManager {
String pluginClassName = pluginDescriptor.getPluginClass(); String pluginClassName = pluginDescriptor.getPluginClass();
log.debug("Class '" + pluginClassName + "'" + " for plugin '" + pluginPath + "'"); log.debug("Class '" + pluginClassName + "'" + " for plugin '" + pluginPath + "'");
// test for disabled plugin
if (isPluginDisabled(pluginDescriptor.getPluginId())) {
log.info("Plugin '" + pluginPath + "' is disabled");
return;
}
// load plugin // load plugin
log.debug("Loading plugin '" + pluginPath + "'"); log.debug("Loading plugin '" + pluginPath + "'");
PluginLoader pluginLoader = new PluginLoader(this, pluginDescriptor, pluginDirectory); PluginLoader pluginLoader = new PluginLoader(this, pluginDescriptor, pluginDirectory);
@ -318,6 +318,28 @@ public class DefaultPluginManager implements PluginManager {
pluginClassLoaders.put(pluginId, pluginClassLoader); pluginClassLoaders.put(pluginId, pluginClassLoader);
} }
/**
* Add the possibility to override the PluginDescriptorFinder.
*/
protected PluginDescriptorFinder createPluginDescriptorFinder() {
return new DefaultPluginDescriptorFinder();
}
/**
* Add the possibility to override the ExtensionFinder.
*/
protected ExtensionFinder createExtensionFinder() {
return new DefaultExtensionFinder(compoundClassLoader);
}
protected boolean isPluginDisabled(String pluginId) {
if (enabledPlugins.isEmpty()) {
return disabledPlugins.contains(pluginId);
}
return !enabledPlugins.contains(pluginId);
}
private void expandPluginArchive(String fileName) throws IOException { private void expandPluginArchive(String fileName) throws IOException {
File pluginArchiveFile = new File(pluginsDirectory, fileName); File pluginArchiveFile = new File(pluginsDirectory, fileName);
long pluginArchiveDate = pluginArchiveFile.lastModified(); long pluginArchiveDate = pluginArchiveFile.lastModified();

53
pf4j/src/main/java/ro/fortsoft/pf4j/util/FileUtils.java

@ -0,0 +1,53 @@
/*
* 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.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* @author Decebal Suiu
*/
public class FileUtils {
public static List<String> readLines(File file, boolean ignoreComments) throws IOException {
if (!file.exists() || !file.isFile()) {
return Collections.emptyList();
}
List<String> lines = new ArrayList<String>();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
if (ignoreComments && !line.startsWith("#") && !lines.contains(line)) {
lines.add(line);
}
}
} finally {
if (reader != null) {
reader.close();
}
}
return lines;
}
}
Loading…
Cancel
Save