Permalink
Browse files

factory - showing dedicated error log when DI cycle is detected.

This fixes #252
  • Loading branch information...
fcamblor committed Aug 22, 2017
1 parent 682fb8a commit 6ce644f680b91855d82702b8f067a3b15cd59e97
Showing with 32 additions and 2 deletions.
  1. +32 −2 restx-factory/src/main/java/restx/factory/Factory.java
@@ -1198,8 +1198,19 @@ private void satisfyBoms(Deque<BuildingBox<?>> dependencies) {
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);
@@ -1210,6 +1221,25 @@ private void satisfyBoms(Deque<BuildingBox<?>> dependencies) {
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());

0 comments on commit 6ce644f

Please sign in to comment.