]> source.dussan.org Git - pf4j.git/commitdiff
Fix #42 and it's a help for #32 54/head
authorMário Franco <mario.ffranco@gmail.com>
Mon, 20 Jul 2015 11:00:03 +0000 (12:00 +0100)
committerMário Franco <mario.ffranco@gmail.com>
Mon, 20 Jul 2015 11:00:03 +0000 (12:00 +0100)
pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginManager.java
pf4j/src/main/java/ro/fortsoft/pf4j/DependencyResolver.java
pf4j/src/main/java/ro/fortsoft/pf4j/util/DirectedGraph.java

index 49c87f1f3fe33f660a5a7726a3722e4d19fadd5e..d73a5ea4906279a7aee49c879146eecd1756ca1f 100644 (file)
@@ -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);
index 022d46e4fdf75f2a5bef066f769a86d7d662653e..fb6cee4e466046c328a8510058348d3526e40878 100644 (file)
@@ -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);
             }
         }
     }
index 58b8d6c1057468af215c39ee72c4b8a4276c58cb..08a90bfaa8512cc279b546a699a4c6205ca74a77 100644 (file)
@@ -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.
      */