Search in sources :

Example 1 with DependencySpec

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;
}
Also used : EnunciateModule(com.webcohesion.enunciate.module.EnunciateModule) DefaultDirectedGraph(org.jgrapht.graph.DefaultDirectedGraph) DefaultEdge(org.jgrapht.graph.DefaultEdge) ApiRegistryAwareModule(com.webcohesion.enunciate.module.ApiRegistryAwareModule) CycleDetector(org.jgrapht.alg.cycle.CycleDetector) DependingModuleAwareModule(com.webcohesion.enunciate.module.DependingModuleAwareModule) TreeSet(java.util.TreeSet) DependencySpec(com.webcohesion.enunciate.module.DependencySpec)

Aggregations

ApiRegistryAwareModule (com.webcohesion.enunciate.module.ApiRegistryAwareModule)1 DependencySpec (com.webcohesion.enunciate.module.DependencySpec)1 DependingModuleAwareModule (com.webcohesion.enunciate.module.DependingModuleAwareModule)1 EnunciateModule (com.webcohesion.enunciate.module.EnunciateModule)1 TreeSet (java.util.TreeSet)1 CycleDetector (org.jgrapht.alg.cycle.CycleDetector)1 DefaultDirectedGraph (org.jgrapht.graph.DefaultDirectedGraph)1 DefaultEdge (org.jgrapht.graph.DefaultEdge)1