]> source.dussan.org Git - pf4j.git/commitdiff
little preparation for #42
authorDecebal Suiu <decebal.suiu@gmail.com>
Fri, 5 Jun 2015 15:09:17 +0000 (18:09 +0300)
committerDecebal Suiu <decebal.suiu@gmail.com>
Fri, 5 Jun 2015 15:09:17 +0000 (18:09 +0300)
pf4j/src/main/java/ro/fortsoft/pf4j/DependencyResolver.java

index 21ddf5d841219ba1db5fc4df0b4c63046cb23ed9..022d46e4fdf75f2a5bef066f769a86d7d662653e 100644 (file)
@@ -28,29 +28,18 @@ class DependencyResolver {
        private static final Logger log = LoggerFactory.getLogger(DependencyResolver.class);
 
     private List<PluginWrapper> plugins;
+    private DirectedGraph<String> graph;
 
        public DependencyResolver(List<PluginWrapper> plugins) {
                this.plugins = plugins;
+
+        initGraph();
        }
 
        /**
         * Get the list of plugins in dependency sorted order.
         */
        public List<PluginWrapper> getSortedPlugins() throws PluginException {
-               DirectedGraph<String> graph = new DirectedGraph<>();
-               for (PluginWrapper pluginWrapper : plugins) {
-                       PluginDescriptor descriptor = pluginWrapper.getDescriptor();
-                       String pluginId = descriptor.getPluginId();
-                       List<PluginDependency> dependencies = descriptor.getDependencies();
-                       if (!dependencies.isEmpty()) {
-                               for (PluginDependency dependency : dependencies) {
-                                       graph.addEdge(pluginId, dependency.getPluginId());
-                               }
-                       } else {
-                               graph.addVertex(pluginId);
-                       }
-               }
-
                log.debug("Graph: {}", graph);
                List<String> pluginsId = graph.reverseTopologicalSort();
 
@@ -67,7 +56,26 @@ class DependencyResolver {
                return sortedPlugins;
        }
 
-       private PluginWrapper getPlugin(String pluginId) throws PluginNotFoundException {
+    private void initGraph() {
+        // create graph
+        graph = new DirectedGraph<>();
+
+        // populate graph
+        for (PluginWrapper pluginWrapper : plugins) {
+            PluginDescriptor descriptor = pluginWrapper.getDescriptor();
+            String pluginId = descriptor.getPluginId();
+            List<PluginDependency> dependencies = descriptor.getDependencies();
+            if (!dependencies.isEmpty()) {
+                for (PluginDependency dependency : dependencies) {
+                    graph.addEdge(pluginId, dependency.getPluginId());
+                }
+            } else {
+                graph.addVertex(pluginId);
+            }
+        }
+    }
+
+    private PluginWrapper getPlugin(String pluginId) throws PluginNotFoundException {
                for (PluginWrapper pluginWrapper : plugins) {
                        if (pluginId.equals(pluginWrapper.getDescriptor().getPluginId())) {
                                return pluginWrapper;