Browse Source

Add methods to start & stop a single plugin and it's dependencies

pull/3/head
James Moger 11 years ago
parent
commit
02455e68f6
  1. 6
      pf4j/pom.xml
  2. 65
      pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java
  3. 14
      pf4j/src/main/java/ro/fortsoft/pf4j/PluginManager.java

6
pf4j/pom.xml

@ -31,6 +31,12 @@
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId> <artifactId>slf4j-api</artifactId>
<version>1.6.4</version> <version>1.6.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>

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

@ -19,6 +19,7 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -163,6 +164,34 @@ public class DefaultPluginManager implements PluginManager {
} }
} }
/**
* Start the specified plugin and it's dependencies.
*/
@Override
public PluginState startPlugin(String pluginId) {
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;
}
for (PluginDependency dependency : pluginDescriptor.getDependencies()) {
startPlugin(dependency.getPluginId());
}
try {
log.info("Start plugin '{}:{}'", pluginDescriptor.getPluginId(), pluginDescriptor.getVersion());
pluginWrapper.getPlugin().start();
pluginWrapper.setPluginState(PluginState.STARTED);
startedPlugins.add(pluginWrapper);
} catch (PluginException e) {
log.error(e.getMessage(), e);
}
return pluginWrapper.getPluginState();
}
/** /**
* Stop all active plugins. * Stop all active plugins.
*/ */
@ -170,17 +199,49 @@ public class DefaultPluginManager implements PluginManager {
public void stopPlugins() { public void stopPlugins() {
// stop started plugins in reverse order // stop started plugins in reverse order
Collections.reverse(startedPlugins); Collections.reverse(startedPlugins);
for (PluginWrapper pluginWrapper : startedPlugins) { Iterator<PluginWrapper> itr = startedPlugins.iterator();
while (itr.hasNext()) {
PluginWrapper pluginWrapper = itr.next();
PluginDescriptor pluginDescriptor = pluginWrapper.getDescriptor();
try { try {
log.info("Stop plugin '{}'", pluginWrapper.getDescriptor().getPluginId()); log.info("Stop plugin '{}:{}'", pluginDescriptor.getPluginId(), pluginDescriptor.getVersion());
pluginWrapper.getPlugin().stop(); pluginWrapper.getPlugin().stop();
pluginWrapper.setPluginState(PluginState.STOPPED); pluginWrapper.setPluginState(PluginState.STOPPED);
itr.remove();
} catch (PluginException e) { } catch (PluginException e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
} }
} }
} }
/**
* Stop the specified plugin and it's dependencies.
*/
@Override
public PluginState stopPlugin(String pluginId) {
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;
}
for (PluginDependency dependency : pluginDescriptor.getDependencies()) {
stopPlugin(dependency.getPluginId());
}
try {
log.info("Stop plugin '{}:{}'", pluginDescriptor.getPluginId(), pluginDescriptor.getVersion());
pluginWrapper.getPlugin().stop();
pluginWrapper.setPluginState(PluginState.STOPPED);
startedPlugins.remove(pluginWrapper);
} catch (PluginException e) {
log.error(e.getMessage(), e);
}
return pluginWrapper.getPluginState();
}
/** /**
* Load plugins. * Load plugins.
*/ */

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

@ -52,11 +52,25 @@ public interface PluginManager {
*/ */
public void startPlugins(); public void startPlugins();
/**
* Start the specified plugin and it's dependencies.
*
* @return the plugin state
*/
public PluginState startPlugin(String pluginId);
/** /**
* Stop all active plugins. * Stop all active plugins.
*/ */
public void stopPlugins(); public void stopPlugins();
/**
* Stop the specified plugin and it's dependencies.
*
* @return the plugin state
*/
public PluginState stopPlugin(String pluginId);
public PluginClassLoader getPluginClassLoader(String pluginId); public PluginClassLoader getPluginClassLoader(String pluginId);
public <T> List<T> getExtensions(Class<T> type); public <T> List<T> getExtensions(Class<T> type);

Loading…
Cancel
Save