Browse Source

Merge pull request #54 from lightglitch/issue_42

Fix #42 and it's a help for #32
pull/55/head
Decebal Suiu 9 years ago
parent
commit
49d9974dd1
  1. 18
      pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java
  2. 26
      pf4j/src/main/java/ro/fortsoft/pf4j/DependencyResolver.java
  3. 7
      pf4j/src/main/java/ro/fortsoft/pf4j/util/DirectedGraph.java

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

@ -93,6 +93,7 @@ public class DefaultPluginManager implements PluginManager {
private PluginFactory pluginFactory;
private ExtensionFactory extensionFactory;
private PluginStatusProvider pluginStatusProvider;
private DependencyResolver dependencyResolver;
/**
* The plugins repository.
@ -297,6 +298,10 @@ public class DefaultPluginManager implements PluginManager {
*/
@Override
public PluginState stopPlugin(String pluginId) {
return stopPlugin(pluginId, true);
}
private PluginState stopPlugin(String pluginId, boolean stopDependents) {
if (!plugins.containsKey(pluginId)) {
throw new IllegalArgumentException(String.format("Unknown pluginId %s", pluginId));
}
@ -315,9 +320,14 @@ public class DefaultPluginManager implements PluginManager {
return pluginState;
}
for (PluginDependency dependency : pluginDescriptor.getDependencies()) {
stopPlugin(dependency.getPluginId());
}
if (stopDependents) {
List<String> dependents = dependencyResolver.getDependents(pluginId);
while (!dependents.isEmpty()) {
String dependent = dependents.remove(0);
stopPlugin(dependent, false);
dependents.addAll(dependencyResolver.getDependents(dependent));
}
}
try {
log.info("Stop plugin '{}:{}'", pluginDescriptor.getPluginId(), pluginDescriptor.getVersion());
@ -813,7 +823,7 @@ public class DefaultPluginManager implements PluginManager {
}
private void resolveDependencies() throws PluginException {
DependencyResolver dependencyResolver = new DependencyResolver(unresolvedPlugins);
dependencyResolver = new DependencyResolver(unresolvedPlugins);
resolvedPlugins = dependencyResolver.getSortedPlugins();
for (PluginWrapper pluginWrapper : resolvedPlugins) {
unresolvedPlugins.remove(pluginWrapper);

26
pf4j/src/main/java/ro/fortsoft/pf4j/DependencyResolver.java

@ -28,7 +28,8 @@ class DependencyResolver {
private static final Logger log = LoggerFactory.getLogger(DependencyResolver.class);
private List<PluginWrapper> plugins;
private DirectedGraph<String> graph;
private DirectedGraph<String> dependenciesGraph;
private DirectedGraph<String> dependentsGraph;
public DependencyResolver(List<PluginWrapper> plugins) {
this.plugins = plugins;
@ -36,15 +37,23 @@ class DependencyResolver {
initGraph();
}
public List<String> getDependecies(String pluginsId) {
return dependenciesGraph.getNeighbors(pluginsId);
}
public List<String> getDependents(String pluginsId) {
return dependentsGraph.getNeighbors(pluginsId);
}
/**
* Get the list of plugins in dependency sorted order.
*/
public List<PluginWrapper> getSortedPlugins() throws PluginException {
log.debug("Graph: {}", graph);
List<String> pluginsId = graph.reverseTopologicalSort();
log.debug("Graph: {}", dependenciesGraph);
List<String> pluginsId = dependenciesGraph.reverseTopologicalSort();
if (pluginsId == null) {
throw new CyclicDependencyException("Cyclic dependencies !!!" + graph.toString());
throw new CyclicDependencyException("Cyclic dependencies !!!" + dependenciesGraph.toString());
}
log.debug("Plugins order: {}", pluginsId);
@ -58,7 +67,8 @@ class DependencyResolver {
private void initGraph() {
// create graph
graph = new DirectedGraph<>();
dependenciesGraph = new DirectedGraph<>();
dependentsGraph = new DirectedGraph<>();
// populate graph
for (PluginWrapper pluginWrapper : plugins) {
@ -67,10 +77,12 @@ class DependencyResolver {
List<PluginDependency> dependencies = descriptor.getDependencies();
if (!dependencies.isEmpty()) {
for (PluginDependency dependency : dependencies) {
graph.addEdge(pluginId, dependency.getPluginId());
dependenciesGraph.addEdge(pluginId, dependency.getPluginId());
dependentsGraph.addEdge(dependency.getPluginId(), pluginId);
}
} else {
graph.addVertex(pluginId);
dependenciesGraph.addVertex(pluginId);
dependentsGraph.addVertex(pluginId);
}
}
}

7
pf4j/src/main/java/ro/fortsoft/pf4j/util/DirectedGraph.java

@ -69,6 +69,13 @@ public class DirectedGraph<V> {
neighbors.get(from).remove(to);
}
public List<V> getNeighbors(V vertex) {
if (neighbors.containsKey(vertex)) {
return new ArrayList<V>();
}
return neighbors.get(vertex);
}
/**
* Report (as a Map) the out-degree of each vertex.
*/

Loading…
Cancel
Save