Skip to content

Commit 51d8d58

Browse files
committed
#13 - Support priority of @primary and @secondary across modules
1 parent 57820ee commit 51d8d58

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

src/main/java/io/dinject/BootContext.java

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ public class BootContext {
5555

5656
private final Set<String> includeModules = new LinkedHashSet<>();
5757

58+
private boolean ignoreMissingModuleDependencies;
59+
5860
/**
5961
* Create a BootContext to ultimately load and return a new BeanContext.
6062
*
@@ -115,6 +117,7 @@ public BootContext withNoShutdownHook() {
115117
* try (BeanContext context = new BootContext()
116118
* .withBeans(mockEmailService)
117119
* .withModules("coffee")
120+
* .withIgnoreMissingModuleDependencies()
118121
* .load()) {
119122
*
120123
* // built with test doubles injected ...
@@ -127,7 +130,6 @@ public BootContext withNoShutdownHook() {
127130
*
128131
* }</pre>
129132
*
130-
*
131133
* @param modules The names of modules that we want to include in dependency injection.
132134
* @return This BootContext
133135
*/
@@ -136,6 +138,15 @@ public BootContext withModules(String... modules) {
136138
return this;
137139
}
138140

141+
/**
142+
* Set this when building a BeanContext (typically for testing) and supplied beans replace module dependencies.
143+
* This means we don't need the usual module dependencies as supplied beans are used instead.
144+
*/
145+
public BootContext withIgnoreMissingModuleDependencies() {
146+
this.ignoreMissingModuleDependencies = true;
147+
return this;
148+
}
149+
139150
/**
140151
* Supply a bean to the context that will be used instead of any similar bean in the context.
141152
* <p>
@@ -180,7 +191,7 @@ public BootContext withBeans(Object... beans) {
180191
public BeanContext load() {
181192

182193
// sort factories by dependsOn
183-
FactoryOrder factoryOrder = new FactoryOrder(includeModules, !suppliedBeans.isEmpty());
194+
FactoryOrder factoryOrder = new FactoryOrder(includeModules, !suppliedBeans.isEmpty(), ignoreMissingModuleDependencies);
184195
ServiceLoader.load(BeanContextFactory.class).forEach(factoryOrder::add);
185196

186197
Set<String> moduleNames = factoryOrder.orderFactories();
@@ -308,16 +319,18 @@ static class FactoryOrder {
308319

309320
private final Set<String> includeModules;
310321
private final boolean suppliedBeans;
322+
private final boolean ignoreMissingModuleDependencies;
311323

312324
private final Set<String> moduleNames = new LinkedHashSet<>();
313325
private final List<BeanContextFactory> factories = new ArrayList<>();
314326
private final List<FactoryState> queue = new ArrayList<>();
315327

316-
private final Map<String,FactoryList> providesMap = new HashMap<>();
328+
private final Map<String, FactoryList> providesMap = new HashMap<>();
317329

318-
FactoryOrder(Set<String> includeModules, boolean suppliedBeans) {
330+
FactoryOrder(Set<String> includeModules, boolean suppliedBeans, boolean ignoreMissingModuleDependencies) {
319331
this.includeModules = includeModules;
320332
this.suppliedBeans = suppliedBeans;
333+
this.ignoreMissingModuleDependencies = ignoreMissingModuleDependencies;
321334
}
322335

323336
void add(BeanContextFactory factory) {
@@ -384,25 +397,26 @@ private void processQueue() {
384397
count = processQueuedFactories();
385398
} while (count > 0);
386399

387-
if (suppliedBeans) {
400+
if (suppliedBeans || ignoreMissingModuleDependencies) {
388401
// just push everything left assuming supplied beans
389402
// will satisfy the required dependencies
390403
for (FactoryState factoryState : queue) {
391404
push(factoryState);
392405
}
393406

394-
} else if (!queue.isEmpty()) {
407+
} else if (!queue.isEmpty()) {
395408
StringBuilder sb = new StringBuilder();
396409
for (FactoryState factory : queue) {
397-
sb.append("module ").append(factory.getName()).append(" has unsatisfied dependencies - ");
410+
sb.append("Module [").append(factory.getName()).append("] has unsatisfied dependencies on modules:");
398411
for (String depModuleName : factory.getDependsOn()) {
399-
boolean ok = moduleNames.contains(depModuleName);
400-
String result = (ok) ? "ok" : "UNSATISFIED";
401-
sb.append(String.format("depends on %s - %s", depModuleName, result));
412+
if (!moduleNames.contains(depModuleName)) {
413+
sb.append(String.format(" [%s]", depModuleName));
414+
}
402415
}
403416
}
404417

405-
sb.append("- Modules loaded ok ").append(moduleNames);
418+
sb.append(". Modules that were loaded ok are:").append(moduleNames);
419+
sb.append(". Consider using BootContext.withIgnoreMissingModuleDependencies() or BootContext.withSuppliedBeans(...)");
406420
throw new IllegalStateException(sb.toString());
407421
}
408422
}

src/test/java/io/dinject/BootContextTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class BootContextTest {
1515
@Test
1616
public void noDepends() {
1717

18-
BootContext.FactoryOrder factoryOrder = new BootContext.FactoryOrder(Collections.emptySet(), true);
18+
BootContext.FactoryOrder factoryOrder = new BootContext.FactoryOrder(Collections.emptySet(), true, true);
1919
factoryOrder.add(bc("1", null, null));
2020
factoryOrder.add(bc("2", null, null));
2121
factoryOrder.add(bc("3", null, null));
@@ -27,7 +27,7 @@ public void noDepends() {
2727
@Test
2828
public void name_depends() {
2929

30-
BootContext.FactoryOrder factoryOrder = new BootContext.FactoryOrder(Collections.emptySet(), true);
30+
BootContext.FactoryOrder factoryOrder = new BootContext.FactoryOrder(Collections.emptySet(), true, true);
3131
factoryOrder.add(bc("two", null, "one"));
3232
factoryOrder.add(bc("one", null, null));
3333
factoryOrder.orderFactories();
@@ -38,7 +38,7 @@ public void name_depends() {
3838
@Test
3939
public void name_depends4() {
4040

41-
BootContext.FactoryOrder factoryOrder = new BootContext.FactoryOrder(Collections.emptySet(), true);
41+
BootContext.FactoryOrder factoryOrder = new BootContext.FactoryOrder(Collections.emptySet(), true, true);
4242
factoryOrder.add(bc("1", null, "3"));
4343
factoryOrder.add(bc("2", null, "4"));
4444
factoryOrder.add(bc("3", null, "4"));
@@ -52,7 +52,7 @@ public void name_depends4() {
5252
@Test
5353
public void nameFeature_depends() {
5454

55-
BootContext.FactoryOrder factoryOrder = new BootContext.FactoryOrder(Collections.emptySet(), true);
55+
BootContext.FactoryOrder factoryOrder = new BootContext.FactoryOrder(Collections.emptySet(), true, true);
5656
factoryOrder.add(bc("1", "a", "3"));
5757
factoryOrder.add(bc("2", null, "4,a"));
5858
factoryOrder.add(bc("3", null, "4"));
@@ -66,7 +66,7 @@ public void nameFeature_depends() {
6666
@Test
6767
public void feature_depends() {
6868

69-
BootContext.FactoryOrder factoryOrder = new BootContext.FactoryOrder(Collections.emptySet(), true);
69+
BootContext.FactoryOrder factoryOrder = new BootContext.FactoryOrder(Collections.emptySet(), true, true);
7070
factoryOrder.add(bc("two", null, "myfeature"));
7171
factoryOrder.add(bc("one", "myfeature", null));
7272
factoryOrder.orderFactories();
@@ -77,7 +77,7 @@ public void feature_depends() {
7777
@Test
7878
public void feature_depends2() {
7979

80-
BootContext.FactoryOrder factoryOrder = new BootContext.FactoryOrder(Collections.emptySet(), true);
80+
BootContext.FactoryOrder factoryOrder = new BootContext.FactoryOrder(Collections.emptySet(), true, true);
8181
factoryOrder.add(bc("two", null, "myfeature"));
8282
factoryOrder.add(bc("one", "myfeature", null));
8383
factoryOrder.add(bc("three", "myfeature", null));

0 commit comments

Comments
 (0)