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;