Browse Source

a first version related to enable-disable plugin

pull/8/merge
Decebal Suiu 11 years ago
parent
commit
746ac803b8
  1. 8
      demo/app/src/main/java/ro/fortsoft/pf4j/demo/Boot.java
  2. 6
      demo/disabled.txt
  3. 6
      demo/enabled.txt
  4. 92
      pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java
  5. 20
      pf4j/src/main/java/ro/fortsoft/pf4j/PluginClassLoader.java
  6. 2
      pf4j/src/main/java/ro/fortsoft/pf4j/PluginClasspath.java
  7. 23
      pf4j/src/main/java/ro/fortsoft/pf4j/PluginManager.java
  8. 24
      pf4j/src/main/java/ro/fortsoft/pf4j/PluginState.java
  9. 22
      pf4j/src/main/java/ro/fortsoft/pf4j/util/FileUtils.java

8
demo/app/src/main/java/ro/fortsoft/pf4j/demo/Boot.java

@ -34,12 +34,18 @@ public class Boot {
// create the plugin manager
final PluginManager pluginManager = new DefaultPluginManager();
// load and start (active/resolved) the plugins
// load the plugins
pluginManager.loadPlugins();
// enable a disabled plugin
pluginManager.enablePlugin("welcome-plugin");
// start (active/resolved) the plugins
pluginManager.startPlugins();
// retrieves the extensions for Greeting extension point
List<Greeting> greetings = pluginManager.getExtensions(Greeting.class);
System.out.println(String.format("### Found %d extensions for the extension point '%s'", greetings.size(), Greeting.class.getName()));
for (Greeting greeting : greetings) {
System.out.println(">>> " + greeting.getGreeting());
}

6
demo/disabled.txt

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

6
demo/enabled.txt

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

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

@ -127,6 +127,18 @@ public class DefaultPluginManager implements PluginManager {
return new ArrayList<PluginWrapper>(plugins.values());
}
@Override
public List<PluginWrapper> getPlugins(PluginState pluginState) {
List<PluginWrapper> plugins= new ArrayList<PluginWrapper>();
for (PluginWrapper plugin : getPlugins()) {
if (pluginState.equals(plugin.getPluginState())) {
plugins.add(plugin);
}
}
return plugins;
}
@Override
public List<PluginWrapper> getResolvedPlugins() {
return resolvedPlugins;
@ -182,8 +194,10 @@ public class DefaultPluginManager implements PluginManager {
@Override
public void startPlugins() {
for (PluginWrapper pluginWrapper : resolvedPlugins) {
PluginDescriptor pluginDescriptor = pluginWrapper.getDescriptor();
if (!isPluginDisabled(pluginDescriptor.getPluginId())) {
try {
log.info("Start plugin '{}'", pluginWrapper.getDescriptor().getPluginId());
log.info("Start plugin '{}:{}'", pluginDescriptor.getPluginId(), pluginDescriptor.getVersion());
pluginWrapper.getPlugin().start();
pluginWrapper.setPluginState(PluginState.STARTED);
startedPlugins.add(pluginWrapper);
@ -192,6 +206,7 @@ public class DefaultPluginManager implements PluginManager {
}
}
}
}
/**
* Start the specified plugin and it's dependencies.
@ -201,15 +216,24 @@ public class DefaultPluginManager implements PluginManager {
if (!plugins.containsKey(pluginId)) {
throw new IllegalArgumentException(String.format("Unknown pluginId %s", pluginId));
}
PluginWrapper pluginWrapper = plugins.get(pluginId);
PluginDescriptor pluginDescriptor = pluginWrapper.getDescriptor();
if (pluginWrapper.getPluginState().equals(PluginState.STARTED)) {
log.debug("Already started plugin '{}:{}'", pluginDescriptor.getPluginId(), pluginDescriptor.getVersion());
return PluginState.STARTED;
}
// test for disabled plugin
if (isPluginDisabled(pluginDescriptor.getPluginId())) {
// do nothing
return pluginWrapper.getPluginState();
}
for (PluginDependency dependency : pluginDescriptor.getDependencies()) {
startPlugin(dependency.getPluginId());
}
try {
log.info("Start plugin '{}:{}'", pluginDescriptor.getPluginId(), pluginDescriptor.getVersion());
pluginWrapper.getPlugin().start();
@ -218,6 +242,7 @@ public class DefaultPluginManager implements PluginManager {
} catch (PluginException e) {
log.error(e.getMessage(), e);
}
return pluginWrapper.getPluginState();
}
@ -232,6 +257,7 @@ public class DefaultPluginManager implements PluginManager {
while (itr.hasNext()) {
PluginWrapper pluginWrapper = itr.next();
PluginDescriptor pluginDescriptor = pluginWrapper.getDescriptor();
if (!isPluginDisabled(pluginDescriptor.getPluginId())) {
try {
log.info("Stop plugin '{}:{}'", pluginDescriptor.getPluginId(), pluginDescriptor.getVersion());
pluginWrapper.getPlugin().stop();
@ -242,6 +268,7 @@ public class DefaultPluginManager implements PluginManager {
}
}
}
}
/**
* Stop the specified plugin and it's dependencies.
@ -251,15 +278,24 @@ public class DefaultPluginManager implements PluginManager {
if (!plugins.containsKey(pluginId)) {
throw new IllegalArgumentException(String.format("Unknown pluginId %s", pluginId));
}
PluginWrapper pluginWrapper = plugins.get(pluginId);
PluginDescriptor pluginDescriptor = pluginWrapper.getDescriptor();
if (pluginWrapper.getPluginState().equals(PluginState.STOPPED)) {
log.debug("Already stopped plugin '{}:{}'", pluginDescriptor.getPluginId(), pluginDescriptor.getVersion());
return PluginState.STOPPED;
}
// test for disabled plugin
if (isPluginDisabled(pluginDescriptor.getPluginId())) {
// do nothing
return pluginWrapper.getPluginState();
}
for (PluginDependency dependency : pluginDescriptor.getDependencies()) {
stopPlugin(dependency.getPluginId());
}
try {
log.info("Stop plugin '{}:{}'", pluginDescriptor.getPluginId(), pluginDescriptor.getVersion());
pluginWrapper.getPlugin().stop();
@ -268,6 +304,7 @@ public class DefaultPluginManager implements PluginManager {
} catch (PluginException e) {
log.error(e.getMessage(), e);
}
return pluginWrapper.getPluginState();
}
@ -365,6 +402,43 @@ public class DefaultPluginManager implements PluginManager {
} catch (IllegalArgumentException e) {
// ignore not found exceptions because this method is recursive
}
return false;
}
@Override
public boolean disablePlugin(String pluginId) {
if (plugins.containsKey(pluginId)) {
log.debug("Unloading plugin {}", pluginId);
unloadPlugin(pluginId);
}
if (disabledPlugins.add(pluginId)) {
try {
FileUtils.writeLines(disabledPlugins, new File(pluginsDirectory, "disabled.txt"));
return true;
} catch (IOException e) {
log.error("Failed to disable plugin {}", pluginId, e);
}
}
return false;
}
@Override
public boolean enablePlugin(String pluginId) {
if (!disabledPlugins.remove(pluginId)) {
log.debug("Plugin {} was not disabled", pluginId);
return true;
}
try {
FileUtils.writeLines(disabledPlugins, new File(pluginsDirectory, "disabled.txt"));
return true;
} catch (IOException e) {
log.error("Failed to enable plugin {}", pluginId, e);
}
return false;
}
@ -495,7 +569,8 @@ public class DefaultPluginManager implements PluginManager {
return new PluginClasspath();
}
protected boolean isPluginDisabled(String pluginId) {
@Override
public boolean isPluginDisabled(String pluginId) {
if (enabledPlugins.isEmpty()) {
return disabledPlugins.contains(pluginId);
}
@ -575,12 +650,6 @@ public class DefaultPluginManager implements PluginManager {
String pluginClassName = pluginDescriptor.getPluginClass();
log.debug("Class '{}' for plugin '{}'", pluginClassName, pluginPath);
// test for disabled plugin
if (isPluginDisabled(pluginDescriptor.getPluginId())) {
log.info("Plugin '{}' is disabled", pluginPath);
return null;
}
// load plugin
log.debug("Loading plugin '{}'", pluginPath);
PluginLoader pluginLoader = new PluginLoader(this, pluginDescriptor, pluginDirectory, pluginClasspath);
@ -591,6 +660,13 @@ public class DefaultPluginManager implements PluginManager {
log.debug("Creating wrapper for plugin '{}'", pluginPath);
PluginWrapper pluginWrapper = new PluginWrapper(pluginDescriptor, pluginPath, pluginLoader.getPluginClassLoader());
pluginWrapper.setRuntimeMode(getRuntimeMode());
/*
// test for disabled plugin
if (isPluginDisabled(pluginDescriptor.getPluginId())) {
log.info("Plugin '{}' is disabled", pluginPath);
pluginWrapper.setPluginState(PluginState.DISABLED);
}
*/
log.debug("Created wrapper '{}' for plugin '{}'", pluginWrapper, pluginPath);
String pluginId = pluginDescriptor.getPluginId();

20
pf4j/src/main/java/ro/fortsoft/pf4j/PluginClassLoader.java

@ -12,8 +12,11 @@
*/
package ro.fortsoft.pf4j;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
/**
@ -91,4 +94,21 @@ public class PluginClassLoader extends URLClassLoader {
return super.loadClass(className);
}
@Override
public URL getResource(String name) {
if (pluginManager.isPluginDisabled(pluginDescriptor.getPluginId())) {
return null;
}
return super.getResource(name);
}
@Override
public Enumeration<URL> getResources(String name) throws IOException {
if (pluginManager.isPluginDisabled(pluginDescriptor.getPluginId())) {
return Collections.emptyEnumeration();
}
return super.getResources(name);
}
}

2
pf4j/src/main/java/ro/fortsoft/pf4j/PluginClasspath.java

@ -18,7 +18,7 @@ import java.util.List;
/**
* The classpath of the plugin after it was unpacked.
* It contains classes directories and lib directories (directories that contains jars).
* All directories are relativ to plugin repository.
* All directories are relative to plugin repository.
* The default values are "classes" and "lib".
*
* @author Decebal Suiu

23
pf4j/src/main/java/ro/fortsoft/pf4j/PluginManager.java

@ -28,6 +28,11 @@ public interface PluginManager {
*/
public List<PluginWrapper> getPlugins();
/**
* Retrieves all plugins with this state.
*/
public List<PluginWrapper> getPlugins(PluginState pluginState);
/**
* Retrieves all resolved plugins (with resolved dependency).
*/
@ -88,6 +93,24 @@ public interface PluginManager {
*/
public boolean unloadPlugin(String pluginId);
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);
/**
* Deletes a plugin.
*

24
pf4j/src/main/java/ro/fortsoft/pf4j/PluginState.java

@ -18,11 +18,12 @@ package ro.fortsoft.pf4j;
public class PluginState {
public static final PluginState CREATED = new PluginState("CREATED");
public static final PluginState INITIALIZED = new PluginState("INITIALIZED");
// public static final PluginState INITIALIZED = new PluginState("INITIALIZED");
// public static final PluginState DISABLED = new PluginState("DISABLED");
public static final PluginState STARTED = new PluginState("STARTED");
public static final PluginState STOPPED = new PluginState("STOPPED");
public static final PluginState DESTROYED = new PluginState("DESTROYED");
public static final PluginState FAILED = new PluginState("FAILED");
// public static final PluginState DESTROYED = new PluginState("DESTROYED");
// public static final PluginState FAILED = new PluginState("FAILED");
private String status;
@ -30,6 +31,23 @@ public class PluginState {
this.status = status;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PluginState that = (PluginState) o;
if (!status.equals(that.status)) return false;
return true;
}
@Override
public int hashCode() {
return status.hashCode();
}
@Override
public String toString() {
return status;

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

@ -12,11 +12,9 @@
*/
package ro.fortsoft.pf4j.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
@ -50,6 +48,21 @@ public class FileUtils {
return lines;
}
public static void writeLines(Collection<String> lines, File file) throws IOException {
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(file));
for (String line : lines) {
writer.write(line);
writer.write('\n');
}
} finally {
if (writer != null) {
writer.close();
}
}
}
/**
* Delete a file or recursively delete a folder.
*
@ -71,6 +84,7 @@ public class FileUtils {
}
}
success |= fileOrFolder.delete();
return success;
}
}

Loading…
Cancel
Save