Elffuss BraidHub

kikocis/EDC public

main · 7.1 KiBRenderedRawPermalink
1# Advanced dependency resolution: default provider methods
2
3This feature (and this document) is aimed at platform developers who intend to provide additional platform features. It
4is **not** intended for people who simply want to contribute a technology extension, such as a database implementation
5or a storage backend!
6
7In this document we will use the term "default provider" and "default provider method" synonymously to refer to a method
8annotated with `@Provider(isDefault=true)`. Similarly, "provider", "provider method" or "factory method"
9refer to methods annotated with just `@Provider`.
10
11## Fallbacks versus extensibility
12
13Default provider methods are intended to provide fallback implementations for services rather than to achieve
14extensibility - that is what extensions are for. There is a subtle but important semantic difference between _fallback
15implementations_ and _extensibility_:
16
17### Fallback implementations
18
19Fallbacks are meant as safety net, in case developers forget or don't want to add a specific implementation for a
20service. It is there so as not to end up _without_ an implementation for a service interface. A good example for this
21are in-memory store implementations. It is expected that an actual persistence implementation is contributed by another
22extension. In-mem stores get you up and running quickly, but we wouldn't recommend using them in production
23environments. Typically, fallbacks should not have any dependencies onto other services.
24
25> Default-provided services, even though they are on the classpath, only get instantiated if there is no other
26> implementation.
27
28### Extensibility
29
30In contrast, _extensibility_ refers to the possibility of swapping out one implementation of a service for another by
31choosing the respective module at compile time. Each implementation must therefore be contained in its own java module,
32and the choice between one or the other is made by referencing one or the other in the build file. The service
33implementation is typically instantiated and provided by its own extension. In this case, the `@Provider`-annotation **
34must not** have the `isDefault` attribute. This is also the case if there will likely only ever be one implementation
35for a service.
36
37One example for extensibility is the `IdentityService`: there could be several implementations for it (OAuth,
38DecentralizedIdentity, Keycloak etc.), but providing either one as default would make little sense, because all of them
39require external services to work. Each implementation would be in its own module and get instantiated by its own
40extension.
41
42> Provided services get instantiated only if they are on the classpath, but always get instantiated.
43
44## Deep-dive into extension lifecycle management
45
46Generally speaking every extension goes through these lifecycle stages during loading:
47
48- `inject`: all fields annotated with `@Inject` are resolved
49- `initialize`: the `initialize()` method is invoked. All required collaborators are expected to be resolved after this.
50- `provide`: all `@Provider` methods are invoked, the object they return is registered in the context.
51
52Due to the fact that default provider methods act a safety net, they only get invoked if no other provider method offers
53the same service type. However, what may be a bit misleading is the fact that they typically get invoked _during the
54`inject` phase_. The following section will demonstrate this.
55
56### Example 1 - provider method
57
58Recall that `@Provider` methods get invoked regardless, and after the `initialze` phase. That means, assuming both
59extensions are on the classpath, the extension that declares the provider method (= `ExtensionA`) will get fully
60instantiated before another extension (= `ExtensionB`) can use the provided object:
61
62```java
63public class ExtensionA { // gets loaded first
64 @Inject
65 private SomeStore store; // provided by some other extension
66
67 @Provider
68 public SomeService getSomeService() {
69 return new SomeServiceImpl(store);
70 }
71}
72
73public class ExtensionB { // gets loaded second
74 @Inject
75 private SomeService service;
76}
77```
78
79After building the dependency graph, the loader mechanism would first fully construct `ExtensionA`, i.e.
80`getSomeService()` is invoked, and the instance of `SomeServiceImpl` is registered in the context. Note that this is
81done regardless whether another extension _actually injects a `SomeService`_. After that, `ExtensionB` gets constructed,
82and by the time it goes through its `inject` phase, the injected `SomeService` is already in the context, so the
83`SomeService` field gets resolved properly.
84
85### Example 2 - default provider method
86
87Methods annotated with `@Provider(isDefault=true)` only get invoked if there is no other provider method for that
88service, and at the time when the corresponding `@Inject` is resolved. Modifying example 1 slightly we get:
89
90```java
91public class ExtensionA {
92
93 @Inject
94 private SomeStore store;
95
96 @Provider(isDefault = true)
97 public SomeService getSomeService() {
98 return new SomeServiceImpl(store);
99 }
100}
101
102public class ExtensionB {
103 @Inject
104 private SomeService service;
105}
106```
107
108The biggest difference here is the point in time at which `getSomeService` is invoked. Default provider methods get
109invoked _when the `@Inject` dependency is resolved_, because that is the "latest" point in time that that decision can
110be made. That means, they get invoked during `ExtensionB`'s inject phase, and _not_ during `ExtensionA`'s provide phase.
111There is no guarantee that `ExtensionA` is already initialized by that time, because the extension loader does not know
112whether it needs to invoke `getSomeService` at all, until the very last moment, i.e. when resolving `ExtensionB`'s
113`service` field. By that time, the dependency graph is already built.
114
115Consequently, default provider methods could (and likely would) get invoked before the defining extension's `provide`
116phase has completed. They even could get invoked before the `initialize` phase has completed: consider the following
117situation the previous example:
118
1191. all implementors of `ServiceExtension` get constructed by the Java `ServiceLoader`
1202. `ExtensionB` gets loaded, runs through its inject phase
1213. no provider for `SomeService`, thus the default provider kicks in
1224. `ExtensionA.getSomeService()` is invoked, but `ExtensionA` is not yet loaded -> `store` is null
1235. -> potential NPE
124
125Because there is no explicit ordering in how the `@Inject` fields are resolved, the order may depend on several factors,
126like the Java version or specific JVM used, the classloader and/or implementation of reflection used, etc.
127
128## Usage guidelines when using default providers
129
130From the previous sections and the examples demonstrated above we can derive a few important guidelines:
131
132- do not use them to achieve extensibility. That is what extensions are for.
133- use them only to provide a _fallback implementation_
134- they should not depend on other injected fields (as those may still be null)
135- they should be in their own dedicated extension (cf. `DefaultServicesExtension`) and Java module
136- do not provide and inject the same service in one extension
137- rule of thumb: unless you know exactly what you're doing and why you need them - don't use them!