forked from open-feature/java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenFeatureAPI.java
142 lines (120 loc) · 3.86 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
package dev.openfeature.sdk;
import dev.openfeature.sdk.internal.AutoCloseableLock;
import dev.openfeature.sdk.internal.AutoCloseableReentrantReadWriteLock;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* 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 {
// package-private multi-read/single-write lock
static AutoCloseableReentrantReadWriteLock hooksLock = new AutoCloseableReentrantReadWriteLock();
static AutoCloseableReentrantReadWriteLock contextLock = new AutoCloseableReentrantReadWriteLock();
private final ProviderRepository providerRepository = new ProviderRepository();
private final List<Hook> apiHooks;
private EvaluationContext evaluationContext;
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 getProvider().getMetadata();
}
public Metadata getProviderMetadata(String clientName) {
return getProvider(clientName).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;
}
}
/**
* Set the default provider.
*/
public void setProvider(FeatureProvider provider) {
providerRepository.setProvider(provider);
}
/**
* 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) {
providerRepository.setProvider(clientName, provider);
}
/**
* 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 __ = 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();
}
}
}