use of com.webcohesion.enunciate.module.DependencySpec in project enunciate by stoicflame.
the class Enunciate method buildModuleGraph.
protected Graph<String, DefaultEdge> buildModuleGraph(Map<String, ? extends EnunciateModule> modules) {
Graph<String, DefaultEdge> graph = new DefaultDirectedGraph<String, DefaultEdge>(DefaultEdge.class);
for (String moduleName : modules.keySet()) {
graph.addVertex(moduleName);
}
for (EnunciateModule module : modules.values()) {
List<DependencySpec> dependencies = module.getDependencySpecifications();
if (dependencies != null && !dependencies.isEmpty()) {
for (DependencySpec dependency : dependencies) {
for (EnunciateModule other : modules.values()) {
if (dependency.accept(other)) {
graph.addEdge(other.getName(), module.getName());
}
}
if (!dependency.isFulfilled()) {
throw new EnunciateException(String.format("Unfulfilled dependency %s of module %s.", dependency.toString(), module.getName()));
}
}
}
}
for (EnunciateModule module : modules.values()) {
if (module instanceof DependingModuleAwareModule) {
Set<DefaultEdge> edges = graph.outgoingEdgesOf(module.getName());
Set<String> dependingModules = new TreeSet<String>();
for (DefaultEdge edge : edges) {
dependingModules.add(graph.getEdgeTarget(edge));
}
((DependingModuleAwareModule) module).acknowledgeDependingModules(dependingModules);
}
if (module instanceof ApiRegistryAwareModule) {
((ApiRegistryAwareModule) module).setApiRegistry(this.apiRegistry);
}
}
CycleDetector<String, DefaultEdge> cycleDetector = new CycleDetector<String, DefaultEdge>(graph);
Set<String> modulesInACycle = cycleDetector.findCycles();
if (!modulesInACycle.isEmpty()) {
StringBuilder errorMessage = new StringBuilder("Module cycle detected: ");
java.util.Iterator<String> subcycle = cycleDetector.findCyclesContainingVertex(modulesInACycle.iterator().next()).iterator();
while (subcycle.hasNext()) {
String next = subcycle.next();
errorMessage.append(next);
if (subcycle.hasNext()) {
errorMessage.append(" --> ");
}
}
throw new EnunciateException(errorMessage.toString());
}
return graph;
}
Aggregations