@@ -1198,8 +1198,19 @@ private <T> Optional<NamedComponent<T>> buildAndStore(BuildingBox<T> buildingBox
11981198
11991199 SatisfiedBOM satisfiedBOM = buildingBox .satisfiedBOM ;
12001200 if (satisfiedBOM == null ) {
1201- throw new IllegalStateException ("problem with dependency resolution" +
1202- " order " + buildingBox .engine .getBillOfMaterial () + " for " + name + " not yet satisfied" );
1201+ if (buildingBox .depsToSort != null && !buildingBox .depsToSort .isEmpty ()) {
1202+ StringBuilder circularDependencyLog = new StringBuilder ();
1203+ for (BuildingBox <?> boxToSort : buildingBox .depsToSort ) {
1204+ buildCircularDependencyLog (circularDependencyLog , boxToSort , ImmutableSet .<Name >of ());
1205+ }
1206+ logger .error ("Circular dependency detected : \n {}\n " +
1207+ "Please, fix this as current RestX DI can't handle cycles." , circularDependencyLog );
1208+ throw new IllegalStateException ("Circular dependency detected : \n " +
1209+ circularDependencyLog + "\n Please, fix this as current RestX DI can't handle cycles." );
1210+ } else {
1211+ throw new IllegalStateException ("problem with dependency resolution" +
1212+ " order " + buildingBox .engine .getBillOfMaterial () + " for " + name + " not yet satisfied" );
1213+ }
12031214 }
12041215
12051216 namedComponent = buildAndStore (name , buildingBox .engine , satisfiedBOM );
@@ -1210,6 +1221,25 @@ private <T> Optional<NamedComponent<T>> buildAndStore(BuildingBox<T> buildingBox
12101221 return namedComponent ;
12111222 }
12121223
1224+ private void buildCircularDependencyLog (StringBuilder circularDependencyLog , BuildingBox buildingBox , ImmutableSet <Name > alreadyDisplayedComponents ) {
1225+ StringBuilder indentation = new StringBuilder ();
1226+ for (int i =0 ; i <alreadyDisplayedComponents .size (); i ++) {
1227+ indentation .append (" " );
1228+ }
1229+
1230+ Iterator <BuildingBox <?>> buildingBoxToSortIter = buildingBox .depsToSort .iterator ();
1231+ while (buildingBoxToSortIter .hasNext ()) {
1232+ BuildingBox <?> buildingBoxToSort = buildingBoxToSortIter .next ();
1233+ circularDependencyLog .append (indentation .toString ()).append ("-> " ).append (buildingBox .engine .getName ()).append ("\n " );
1234+ if (!alreadyDisplayedComponents .contains (buildingBox .engine .getName ())) {
1235+ buildCircularDependencyLog (
1236+ circularDependencyLog , buildingBoxToSort ,
1237+ ImmutableSet .<Name >builder ().addAll (alreadyDisplayedComponents ).add (buildingBox .engine .getName ()).build ()
1238+ );
1239+ }
1240+ }
1241+ }
1242+
12131243 private <T > Optional <NamedComponent <T >> buildAndStore (Name <T > name , MachineEngine <T > engine , SatisfiedBOM satisfiedBOM ) {
12141244 logger .info ("{} - building {} with {} / {}" , id , name , engine , satisfiedBOM );
12151245 Timer timer = metrics .timer ("<BUILD> " + name .getSimpleName ());
0 commit comments