-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathOpenFeatureAPI.java
402 lines (354 loc) · 13.1 KB
/
OpenFeatureAPI.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package dev.openfeature.sdk;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import javax.annotation.Nullable;
import dev.openfeature.sdk.exceptions.OpenFeatureError;
import dev.openfeature.sdk.internal.AutoCloseableLock;
import dev.openfeature.sdk.internal.AutoCloseableReentrantReadWriteLock;
import lombok.extern.slf4j.Slf4j;
/**
* A global singleton which holds base configuration for the OpenFeature
* library.
* Configuration here will be shared across all {@link Client}s.
*/
@Slf4j
public class OpenFeatureAPI implements EventBus<OpenFeatureAPI> {
// package-private multi-read/single-write lock
static AutoCloseableReentrantReadWriteLock lock = new AutoCloseableReentrantReadWriteLock();
private final List<Hook> apiHooks;
private ProviderRepository providerRepository;
private EventSupport eventSupport;
private EvaluationContext evaluationContext;
private TransactionContextPropagator transactionContextPropagator;
protected OpenFeatureAPI() {
apiHooks = new ArrayList<>();
providerRepository = new ProviderRepository();
eventSupport = new EventSupport();
transactionContextPropagator = new NoOpTransactionContextPropagator();
}
private static class SingletonHolder {
private static final OpenFeatureAPI INSTANCE = new OpenFeatureAPI();
}
/**
* Provisions the {@link OpenFeatureAPI} singleton (if needed) and returns it.
*
* @return The singleton instance.
*/
public static OpenFeatureAPI getInstance() {
return SingletonHolder.INSTANCE;
}
public Metadata getProviderMetadata() {
return getProvider().getMetadata();
}
public Metadata getProviderMetadata(String clientName) {
return getProvider(clientName).getMetadata();
}
/**
* {@inheritDoc}
*/
public Client getClient() {
return getClient(null, null);
}
/**
* {@inheritDoc}
*/
public Client getClient(@Nullable String name) {
return getClient(name, null);
}
/**
* {@inheritDoc}
*/
public Client getClient(@Nullable String name, @Nullable String version) {
return new OpenFeatureClient(this,
name,
version);
}
/**
* {@inheritDoc}
*/
public void setEvaluationContext(EvaluationContext evaluationContext) {
try (AutoCloseableLock __ = lock.writeLockAutoCloseable()) {
this.evaluationContext = evaluationContext;
}
}
/**
* {@inheritDoc}
*/
public EvaluationContext getEvaluationContext() {
try (AutoCloseableLock __ = lock.readLockAutoCloseable()) {
return this.evaluationContext;
}
}
/**
* Return the transaction context propagator.
*/
public TransactionContextPropagator getTransactionContextPropagator() {
try (AutoCloseableLock __ = lock.readLockAutoCloseable()) {
return this.transactionContextPropagator;
}
}
/**
* Sets the transaction context propagator.
*
* @throws IllegalArgumentException if {@code transactionContextPropagator} is null
*/
public void setTransactionContextPropagator(TransactionContextPropagator transactionContextPropagator) {
if (transactionContextPropagator == null) {
throw new IllegalArgumentException("Transaction context propagator cannot be null");
}
try (AutoCloseableLock __ = lock.writeLockAutoCloseable()) {
this.transactionContextPropagator = transactionContextPropagator;
}
}
/**
* Returns the currently defined transaction context using the registered transaction
* context propagator.
*
* @return {@link EvaluationContext} The current transaction context
*/
EvaluationContext getTransactionContext() {
return this.transactionContextPropagator.getTransactionContext();
}
/**
* Sets the transaction context using the registered transaction context propagator.
*/
public void setTransactionContext(EvaluationContext evaluationContext) {
this.transactionContextPropagator.setTransactionContext(evaluationContext);
}
/**
* Set the default provider.
*/
public void setProvider(FeatureProvider provider) {
try (AutoCloseableLock __ = lock.writeLockAutoCloseable()) {
providerRepository.setProvider(
provider,
this::attachEventProvider,
this::emitReady,
this::detachEventProvider,
this::emitError,
false);
}
}
/**
* Add a provider for a named client.
*
* @param clientName The name of the client.
* @param provider The provider to set.
*/
public void setProvider(String clientName, FeatureProvider provider) {
try (AutoCloseableLock __ = lock.writeLockAutoCloseable()) {
providerRepository.setProvider(clientName,
provider,
this::attachEventProvider,
this::emitReady,
this::detachEventProvider,
this::emitError,
false);
}
}
/**
* Set the default provider and wait for initialization to finish.
*/
public void setProviderAndWait(FeatureProvider provider) throws OpenFeatureError {
try (AutoCloseableLock __ = lock.writeLockAutoCloseable()) {
providerRepository.setProvider(
provider,
this::attachEventProvider,
this::emitReady,
this::detachEventProvider,
this::emitErrorAndThrow,
true);
}
}
/**
* Add a provider for a named client and wait for initialization to finish.
*
* @param clientName The name of the client.
* @param provider The provider to set.
*/
public void setProviderAndWait(String clientName, FeatureProvider provider) throws OpenFeatureError {
try (AutoCloseableLock __ = lock.writeLockAutoCloseable()) {
providerRepository.setProvider(clientName,
provider,
this::attachEventProvider,
this::emitReady,
this::detachEventProvider,
this::emitErrorAndThrow,
true);
}
}
private void attachEventProvider(FeatureProvider provider) {
if (provider instanceof EventProvider) {
((EventProvider) provider).attach((p, event, details) -> {
runHandlersForProvider(p, event, details);
});
}
}
private void emitReady(FeatureProvider provider) {
runHandlersForProvider(provider, ProviderEvent.PROVIDER_READY, ProviderEventDetails.builder().build());
}
private void detachEventProvider(FeatureProvider provider) {
if (provider instanceof EventProvider) {
((EventProvider) provider).detach();
}
}
private void emitError(FeatureProvider provider, OpenFeatureError exception) {
runHandlersForProvider(provider, ProviderEvent.PROVIDER_ERROR,
ProviderEventDetails.builder().message(exception.getMessage()).build());
}
private void emitErrorAndThrow(FeatureProvider provider, OpenFeatureError exception) throws OpenFeatureError {
this.emitError(provider, exception);
throw exception;
}
/**
* Return the default provider.
*/
public FeatureProvider getProvider() {
return providerRepository.getProvider();
}
/**
* Fetch a provider for a named client. If not found, return the default.
*
* @param name The client name to look for.
* @return A named {@link FeatureProvider}
*/
public FeatureProvider getProvider(String name) {
return providerRepository.getProvider(name);
}
/**
* {@inheritDoc}
*/
public void addHooks(Hook... hooks) {
try (AutoCloseableLock __ = lock.writeLockAutoCloseable()) {
this.apiHooks.addAll(Arrays.asList(hooks));
}
}
/**
* {@inheritDoc}
*/
public List<Hook> getHooks() {
try (AutoCloseableLock __ = lock.readLockAutoCloseable()) {
return this.apiHooks;
}
}
/**
* {@inheritDoc}
*/
public void clearHooks() {
try (AutoCloseableLock __ = lock.writeLockAutoCloseable()) {
this.apiHooks.clear();
}
}
/**
* Shut down and reset the current status of OpenFeature API.
* This call cleans up all active providers and attempts to shut down internal
* event handling mechanisms.
* Once shut down is complete, API is reset and ready to use again.
*/
public void shutdown() {
try (AutoCloseableLock __ = lock.writeLockAutoCloseable()) {
providerRepository.shutdown();
eventSupport.shutdown();
providerRepository = new ProviderRepository();
eventSupport = new EventSupport();
}
}
/**
* {@inheritDoc}
*/
@Override
public OpenFeatureAPI onProviderReady(Consumer<EventDetails> handler) {
return this.on(ProviderEvent.PROVIDER_READY, handler);
}
/**
* {@inheritDoc}
*/
@Override
public OpenFeatureAPI onProviderConfigurationChanged(Consumer<EventDetails> handler) {
return this.on(ProviderEvent.PROVIDER_CONFIGURATION_CHANGED, handler);
}
/**
* {@inheritDoc}
*/
@Override
public OpenFeatureAPI onProviderStale(Consumer<EventDetails> handler) {
return this.on(ProviderEvent.PROVIDER_STALE, handler);
}
/**
* {@inheritDoc}
*/
@Override
public OpenFeatureAPI onProviderError(Consumer<EventDetails> handler) {
return this.on(ProviderEvent.PROVIDER_ERROR, handler);
}
/**
* {@inheritDoc}
*/
@Override
public OpenFeatureAPI on(ProviderEvent event, Consumer<EventDetails> handler) {
try (AutoCloseableLock __ = lock.writeLockAutoCloseable()) {
this.eventSupport.addGlobalHandler(event, handler);
return this;
}
}
/**
* {@inheritDoc}
*/
@Override
public OpenFeatureAPI removeHandler(ProviderEvent event, Consumer<EventDetails> handler) {
this.eventSupport.removeGlobalHandler(event, handler);
return this;
}
void removeHandler(String clientName, ProviderEvent event, Consumer<EventDetails> handler) {
try (AutoCloseableLock __ = lock.writeLockAutoCloseable()) {
eventSupport.removeClientHandler(clientName, event, handler);
}
}
void addHandler(String clientName, ProviderEvent event, Consumer<EventDetails> handler) {
try (AutoCloseableLock __ = lock.writeLockAutoCloseable()) {
// if the provider is in the state associated with event, run immediately
if (Optional.ofNullable(this.providerRepository.getProvider(clientName).getState())
.orElse(ProviderState.READY).matchesEvent(event)) {
eventSupport.runHandler(handler, EventDetails.builder().clientName(clientName).build());
}
eventSupport.addClientHandler(clientName, event, handler);
}
}
/**
* Runs the handlers associated with a particular provider.
*
* @param provider the provider from where this event originated
* @param event the event type
* @param details the event details
*/
private void runHandlersForProvider(FeatureProvider provider, ProviderEvent event, ProviderEventDetails details) {
try (AutoCloseableLock __ = lock.readLockAutoCloseable()) {
List<String> clientNamesForProvider = providerRepository
.getClientNamesForProvider(provider);
final String providerName = Optional.ofNullable(provider.getMetadata())
.map(metadata -> metadata.getName())
.orElse(null);
// run the global handlers
eventSupport.runGlobalHandlers(event, EventDetails.fromProviderEventDetails(details, providerName));
// run the handlers associated with named clients for this provider
clientNamesForProvider.forEach(name -> {
eventSupport.runClientHandlers(name, event,
EventDetails.fromProviderEventDetails(details, providerName, name));
});
if (providerRepository.isDefaultProvider(provider)) {
// run handlers for clients that have no bound providers (since this is the default)
Set<String> allClientNames = eventSupport.getAllClientNames();
Set<String> boundClientNames = providerRepository.getAllBoundClientNames();
allClientNames.removeAll(boundClientNames);
allClientNames.forEach(name -> {
eventSupport.runClientHandlers(name, event,
EventDetails.fromProviderEventDetails(details, providerName, name));
});
}
}
}
}