Browse Source

Add method to unload a plugin and it's dependencies

pull/3/head
James Moger 11 years ago
parent
commit
4fefdcf76c
  1. 39
      pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java
  2. 8
      pf4j/src/main/java/ro/fortsoft/pf4j/PluginManager.java
  3. 4
      pf4j/src/main/java/ro/fortsoft/pf4j/util/CompoundClassLoader.java

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

@ -294,6 +294,45 @@ public class DefaultPluginManager implements PluginManager {
}
}
@Override
public boolean unloadPlugin(String pluginId) {
try {
PluginState state = stopPlugin(pluginId);
if (!PluginState.STOPPED.equals(state)) {
return false;
}
PluginWrapper pluginWrapper = plugins.get(pluginId);
PluginDescriptor descriptor = pluginWrapper.getDescriptor();
List<PluginDependency> dependencies = descriptor.getDependencies();
for (PluginDependency dependency : dependencies) {
if (!unloadPlugin(dependency.getPluginId())) {
return false;
}
}
// remove the plugin
plugins.remove(pluginId);
resolvedPlugins.remove(pluginWrapper);
pathToIdMap.remove(pluginWrapper.getPluginPath());
// remove the classloader
if (pluginClassLoaders.containsKey(pluginId)) {
PluginClassLoader classLoader = pluginClassLoaders.remove(pluginId);
compoundClassLoader.removeLoader(classLoader);
try {
classLoader.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
return true;
} catch (IllegalArgumentException e) {
// ignore not found exceptions because this method is recursive
}
return false;
}
/**
* Get plugin class loader for this path.
*/

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

@ -71,6 +71,14 @@ public interface PluginManager {
*/
public PluginState stopPlugin(String pluginId);
/**
* Unload a plugin.
*
* @param pluginId
* @return true if the plugin was unloaded
*/
public boolean unloadPlugin(String pluginId);
public PluginClassLoader getPluginClassLoader(String pluginId);
public <T> List<T> getExtensions(Class<T> type);

4
pf4j/src/main/java/ro/fortsoft/pf4j/util/CompoundClassLoader.java

@ -34,6 +34,10 @@ public class CompoundClassLoader extends ClassLoader {
loaders.add(loader);
}
public void removeLoader(ClassLoader loader) {
loaders.remove(loader);
}
@Override
public Class<?> findClass(String name) throws ClassNotFoundException {
for (ClassLoader loader : loaders) {

Loading…
Cancel
Save