-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathOpenFeatureAPI.java
More file actions
119 lines (101 loc) · 3.25 KB
/
Copy pathOpenFeatureAPI.java
File metadata and controls
119 lines (101 loc) · 3.25 KB
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
package dev.openfeature.sdk;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Nullable;
import dev.openfeature.sdk.internal.AutoCloseableLock;
import dev.openfeature.sdk.internal.AutoCloseableReentrantReadWriteLock;
/**
* A global singleton which holds base configuration for the OpenFeature library.
* Configuration here will be shared across all {@link Client}s.
*/
public class OpenFeatureAPI {
// package-private multi-read/single-write lock
static AutoCloseableReentrantReadWriteLock hooksLock = new AutoCloseableReentrantReadWriteLock();
static AutoCloseableReentrantReadWriteLock providerLock = new AutoCloseableReentrantReadWriteLock();
static AutoCloseableReentrantReadWriteLock contextLock = new AutoCloseableReentrantReadWriteLock();
private FeatureProvider provider;
private EvaluationContext evaluationContext;
private List<Hook> apiHooks;
private OpenFeatureAPI() {
this.apiHooks = new ArrayList<>();
}
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 provider.getMetadata();
}
public Client getClient() {
return getClient(null, null);
}
public Client getClient(@Nullable String name) {
return getClient(name, null);
}
public Client getClient(@Nullable String name, @Nullable String version) {
return new OpenFeatureClient(this, name, version);
}
/**
* {@inheritDoc}
*/
public void setEvaluationContext(EvaluationContext evaluationContext) {
try (AutoCloseableLock __ = contextLock.writeLockAutoCloseable()) {
this.evaluationContext = evaluationContext;
}
}
/**
* {@inheritDoc}
*/
public EvaluationContext getEvaluationContext() {
try (AutoCloseableLock __ = contextLock.readLockAutoCloseable()) {
return this.evaluationContext;
}
}
/**
* {@inheritDoc}
*/
public void setProvider(FeatureProvider provider) {
try (AutoCloseableLock __ = providerLock.writeLockAutoCloseable()) {
this.provider = provider;
}
}
/**
* {@inheritDoc}
*/
public FeatureProvider getProvider() {
try (AutoCloseableLock __ = providerLock.readLockAutoCloseable()) {
return this.provider;
}
}
/**
* {@inheritDoc}
*/
public void addHooks(Hook... hooks) {
try (AutoCloseableLock __ = hooksLock.writeLockAutoCloseable()) {
this.apiHooks.addAll(Arrays.asList(hooks));
}
}
/**
* {@inheritDoc}
*/
public List<Hook> getHooks() {
try (AutoCloseableLock __ = hooksLock.readLockAutoCloseable()) {
return this.apiHooks;
}
}
/**
* {@inheritDoc}
*/
public void clearHooks() {
try (AutoCloseableLock __ = hooksLock.writeLockAutoCloseable()) {
this.apiHooks.clear();
}
}
}