From e15946819670226c8d38def7950c01a641ebb288 Mon Sep 17 00:00:00 2001 From: Decebal Suiu Date: Fri, 5 Jun 2015 18:09:17 +0300 Subject: [PATCH] little preparation for #42 --- .../ro/fortsoft/pf4j/DependencyResolver.java | 38 +++++++++++-------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/DependencyResolver.java b/pf4j/src/main/java/ro/fortsoft/pf4j/DependencyResolver.java index 21ddf5d..022d46e 100644 --- a/pf4j/src/main/java/ro/fortsoft/pf4j/DependencyResolver.java +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/DependencyResolver.java @@ -28,29 +28,18 @@ class DependencyResolver { private static final Logger log = LoggerFactory.getLogger(DependencyResolver.class); private List plugins; + private DirectedGraph graph; public DependencyResolver(List plugins) { this.plugins = plugins; + + initGraph(); } /** * Get the list of plugins in dependency sorted order. */ public List getSortedPlugins() throws PluginException { - DirectedGraph graph = new DirectedGraph<>(); - for (PluginWrapper pluginWrapper : plugins) { - PluginDescriptor descriptor = pluginWrapper.getDescriptor(); - String pluginId = descriptor.getPluginId(); - List dependencies = descriptor.getDependencies(); - if (!dependencies.isEmpty()) { - for (PluginDependency dependency : dependencies) { - graph.addEdge(pluginId, dependency.getPluginId()); - } - } else { - graph.addVertex(pluginId); - } - } - log.debug("Graph: {}", graph); List 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 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; -- 2.39.5