Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
factory - showing dedicated error log when DI cycle is detected.
This fixes #252
  • Loading branch information
fcamblor committed Aug 28, 2017
1 parent 682fb8a commit 6ce644f
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions restx-factory/src/main/java/restx/factory/Factory.java
Expand Up @@ -1198,8 +1198,19 @@ private <T> Optional<NamedComponent<T>> buildAndStore(BuildingBox<T> buildingBox

SatisfiedBOM satisfiedBOM = buildingBox.satisfiedBOM;
if (satisfiedBOM == null) {
throw new IllegalStateException("problem with dependency resolution" +
" order " + buildingBox.engine.getBillOfMaterial() + " for " + name + " not yet satisfied");
if(buildingBox.depsToSort != null && !buildingBox.depsToSort.isEmpty()) {
StringBuilder circularDependencyLog = new StringBuilder();
for (BuildingBox<?> boxToSort : buildingBox.depsToSort) {
buildCircularDependencyLog(circularDependencyLog, boxToSort, ImmutableSet.<Name>of());
}
logger.error("Circular dependency detected : \n{}\n" +
"Please, fix this as current RestX DI can't handle cycles.", circularDependencyLog);
throw new IllegalStateException("Circular dependency detected : \n" +
circularDependencyLog + "\nPlease, fix this as current RestX DI can't handle cycles.");
} else {
throw new IllegalStateException("problem with dependency resolution" +
" order " + buildingBox.engine.getBillOfMaterial() + " for " + name + " not yet satisfied");
}
}

namedComponent = buildAndStore(name, buildingBox.engine, satisfiedBOM);
Expand All @@ -1210,6 +1221,25 @@ private <T> Optional<NamedComponent<T>> buildAndStore(BuildingBox<T> buildingBox
return namedComponent;
}

private void buildCircularDependencyLog(StringBuilder circularDependencyLog, BuildingBox buildingBox, ImmutableSet<Name> alreadyDisplayedComponents) {
StringBuilder indentation = new StringBuilder();
for(int i=0; i<alreadyDisplayedComponents.size(); i++) {
indentation.append(" ");
}

Iterator<BuildingBox<?>> buildingBoxToSortIter = buildingBox.depsToSort.iterator();
while(buildingBoxToSortIter.hasNext()) {
BuildingBox<?> buildingBoxToSort = buildingBoxToSortIter.next();
circularDependencyLog.append(indentation.toString()).append("-> ").append(buildingBox.engine.getName()).append("\n");
if(!alreadyDisplayedComponents.contains(buildingBox.engine.getName())) {
buildCircularDependencyLog(
circularDependencyLog, buildingBoxToSort,
ImmutableSet.<Name>builder().addAll(alreadyDisplayedComponents).add(buildingBox.engine.getName()).build()
);
}
}
}

private <T> Optional<NamedComponent<T>> buildAndStore(Name<T> name, MachineEngine<T> engine, SatisfiedBOM satisfiedBOM) {
logger.info("{} - building {} with {} / {}", id, name, engine, satisfiedBOM);
Timer timer = metrics.timer("<BUILD> " + name.getSimpleName());
Expand Down

0 comments on commit 6ce644f

Please sign in to comment.