You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DependencyResolver.java 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright 2012 Decebal Suiu
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with
  5. * the License. You may obtain a copy of the License in the LICENSE file, or at:
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  10. * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  11. * specific language governing permissions and limitations under the License.
  12. */
  13. package ro.fortsoft.pf4j;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. import org.slf4j.Logger;
  17. import org.slf4j.LoggerFactory;
  18. import ro.fortsoft.pf4j.util.DirectedGraph;
  19. /**
  20. * @author Decebal Suiu
  21. */
  22. class DependencyResolver {
  23. private static final Logger LOG = LoggerFactory.getLogger(DependencyResolver.class);
  24. private List<PluginWrapper> plugins;
  25. public DependencyResolver(List<PluginWrapper> plugins) {
  26. this.plugins = plugins;
  27. }
  28. /**
  29. * Get the list of plugins in dependency sorted order.
  30. */
  31. public List<PluginWrapper> getSortedPlugins() throws PluginException {
  32. DirectedGraph<String> graph = new DirectedGraph<String>();
  33. for (PluginWrapper pluginWrapper : plugins) {
  34. PluginDescriptor descriptor = pluginWrapper.getDescriptor();
  35. String pluginId = descriptor.getPluginId();
  36. List<PluginDependency> dependencies = descriptor.getDependencies();
  37. if (!dependencies.isEmpty()) {
  38. for (PluginDependency dependency : dependencies) {
  39. graph.addEdge(pluginId, dependency.getPluginId());
  40. }
  41. } else {
  42. graph.addVertex(pluginId);
  43. }
  44. }
  45. LOG.debug("Graph: " + graph);
  46. List<String> pluginsId = graph.reverseTopologicalSort();
  47. if (pluginsId == null) {
  48. throw new CyclicDependencyException("Cyclic dependences !!!" + graph.toString());
  49. }
  50. LOG.debug("Plugins order: " + pluginsId);
  51. List<PluginWrapper> sortedPlugins = new ArrayList<PluginWrapper>();
  52. for (String pluginId : pluginsId) {
  53. sortedPlugins.add(getPlugin(pluginId));
  54. }
  55. return sortedPlugins;
  56. }
  57. private PluginWrapper getPlugin(String pluginId) throws PluginNotFoundException {
  58. for (PluginWrapper pluginWrapper : plugins) {
  59. if (pluginId.equals(pluginWrapper.getDescriptor().getPluginId())) {
  60. return pluginWrapper;
  61. }
  62. }
  63. throw new PluginNotFoundException(pluginId);
  64. }
  65. }