-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathLocalhostSplitClient.java
More file actions
325 lines (270 loc) · 11.6 KB
/
LocalhostSplitClient.java
File metadata and controls
325 lines (270 loc) · 11.6 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
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
package io.split.android.client.localhost;
import static io.split.android.client.utils.Utils.checkNotNull;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.lang.ref.WeakReference;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import io.split.android.client.EvaluatorImpl;
import io.split.android.client.FlagSetsFilter;
import io.split.android.client.SplitClient;
import io.split.android.client.SplitClientConfig;
import io.split.android.client.SplitFactory;
import io.split.android.client.SplitResult;
import io.split.android.client.api.Key;
import io.split.android.client.attributes.AttributesManager;
import io.split.android.client.attributes.AttributesMerger;
import io.split.android.client.events.SplitEvent;
import io.split.android.client.events.SplitEventTask;
import io.split.android.client.events.SplitEventsManager;
import io.split.android.client.impressions.ImpressionListener;
import io.split.android.client.shared.SplitClientContainer;
import io.split.android.client.storage.splits.SplitsStorage;
import io.split.android.client.telemetry.storage.TelemetryStorageProducer;
import io.split.android.client.utils.logger.Logger;
import io.split.android.client.validators.FlagSetsValidatorImpl;
import io.split.android.client.validators.KeyValidatorImpl;
import io.split.android.client.validators.SplitValidatorImpl;
import io.split.android.client.validators.TreatmentManager;
import io.split.android.client.validators.TreatmentManagerImpl;
import io.split.android.client.validators.ValidationMessageLoggerImpl;
import io.split.android.engine.experiments.SplitParser;
import io.split.android.grammar.Treatments;
/**
* An implementation of SplitClient that considers all partitions
* passed in the constructor to be 100% on for all users, and
* any other split to be 100% off for all users. This implementation
* is useful for using in localhost environment.
*/
public final class LocalhostSplitClient implements SplitClient {
private final WeakReference<LocalhostSplitFactory> mFactoryRef;
private final WeakReference<SplitClientContainer> mClientContainer;
private final Key mKey;
private final SplitEventsManager mEventsManager;
private final TreatmentManager mTreatmentManager;
private boolean mIsClientDestroyed = false;
private final SplitsStorage mSplitsStorage;
public LocalhostSplitClient(@NonNull LocalhostSplitFactory container,
@NonNull SplitClientContainer clientContainer,
@NonNull SplitClientConfig splitClientConfig,
@NonNull Key key,
@NonNull SplitsStorage splitsStorage,
@NonNull SplitEventsManager eventsManager,
@NonNull SplitParser splitParser,
@NonNull AttributesManager attributesManager,
@NonNull AttributesMerger attributesMerger,
@NonNull TelemetryStorageProducer telemetryStorageProducer,
@Nullable FlagSetsFilter flagSetsFilter) {
mFactoryRef = new WeakReference<>(checkNotNull(container));
mClientContainer = new WeakReference<>(checkNotNull(clientContainer));
mKey = checkNotNull(key);
mEventsManager = checkNotNull(eventsManager);
mSplitsStorage = splitsStorage;
mTreatmentManager = new TreatmentManagerImpl(mKey.matchingKey(), mKey.bucketingKey(),
new EvaluatorImpl(splitsStorage, splitParser), new KeyValidatorImpl(),
new SplitValidatorImpl(), getImpressionsListener(splitClientConfig),
splitClientConfig.labelsEnabled(), eventsManager, attributesManager, attributesMerger,
telemetryStorageProducer, flagSetsFilter, splitsStorage, new ValidationMessageLoggerImpl(), new FlagSetsValidatorImpl());
}
@Override
public String getTreatment(String featureFlagName) {
try {
return mTreatmentManager.getTreatment(featureFlagName, null, mIsClientDestroyed);
} catch (Exception exception) {
Logger.e(exception);
return Treatments.CONTROL;
}
}
@Override
public String getTreatment(String featureFlagName, Map<String, Object> attributes) {
try {
return mTreatmentManager.getTreatment(featureFlagName, attributes, mIsClientDestroyed);
} catch (Exception exception) {
Logger.e(exception);
return Treatments.CONTROL;
}
}
@Override
public SplitResult getTreatmentWithConfig(String featureFlagName, Map<String, Object> attributes) {
try {
return mTreatmentManager.getTreatmentWithConfig(featureFlagName, attributes, mIsClientDestroyed);
} catch (Exception exception) {
Logger.e(exception);
return new SplitResult(Treatments.CONTROL);
}
}
@Override
public Map<String, String> getTreatments(List<String> featureFlagNames, Map<String, Object> attributes) {
try {
return mTreatmentManager.getTreatments(featureFlagNames, attributes, mIsClientDestroyed);
} catch (Exception exception) {
Logger.e(exception);
Map<String, String> result = new HashMap<>();
for (String featureFlagName : featureFlagNames) {
result.put(featureFlagName, Treatments.CONTROL);
}
return result;
}
}
@Override
public Map<String, SplitResult> getTreatmentsWithConfig(List<String> featureFlagNames, Map<String, Object> attributes) {
try {
return mTreatmentManager.getTreatmentsWithConfig(featureFlagNames, attributes, mIsClientDestroyed);
} catch (Exception exception) {
Logger.e(exception);
Map<String, SplitResult> result = new HashMap<>();
for (String featureFlagName : featureFlagNames) {
result.put(featureFlagName, new SplitResult(Treatments.CONTROL));
}
return result;
}
}
@Override
public Map<String, String> getTreatmentsByFlagSet(@NonNull String flagSet, @Nullable Map<String, Object> attributes) {
try {
return mTreatmentManager.getTreatmentsByFlagSet(flagSet, attributes, mIsClientDestroyed);
} catch (Exception exception) {
Logger.e(exception);
return buildExceptionResult(Collections.singletonList(flagSet));
}
}
@Override
public Map<String, String> getTreatmentsByFlagSets(@NonNull List<String> flagSets, @Nullable Map<String, Object> attributes) {
try {
return mTreatmentManager.getTreatmentsByFlagSets(flagSets, attributes, mIsClientDestroyed);
} catch (Exception exception) {
Logger.e(exception);
return buildExceptionResult(flagSets);
}
}
@Override
public Map<String, SplitResult> getTreatmentsWithConfigByFlagSet(@NonNull String flagSet, @Nullable Map<String, Object> attributes) {
try {
return mTreatmentManager.getTreatmentsWithConfigByFlagSet(flagSet, attributes, mIsClientDestroyed);
} catch (Exception exception) {
Logger.e(exception);
return buildExceptionResultWithConfig(Collections.singletonList(flagSet));
}
}
@Override
public Map<String, SplitResult> getTreatmentsWithConfigByFlagSets(@NonNull List<String> flagSets, @Nullable Map<String, Object> attributes) {
try {
return mTreatmentManager.getTreatmentsWithConfigByFlagSets(flagSets, attributes, mIsClientDestroyed);
} catch (Exception exception) {
Logger.e(exception);
return buildExceptionResultWithConfig(flagSets);
}
}
@Override
public void destroy() {
mIsClientDestroyed = true;
SplitClientContainer splitClientContainer = mClientContainer.get();
if (splitClientContainer != null) {
splitClientContainer.remove(mKey);
}
SplitFactory factory = mFactoryRef.get();
if (factory != null) {
factory.destroy();
}
}
@Override
public void flush() {
}
@Override
public boolean isReady() {
return mEventsManager.eventAlreadyTriggered(SplitEvent.SDK_READY);
}
public void on(SplitEvent event, SplitEventTask task) {
checkNotNull(event);
checkNotNull(task);
if (!event.equals(SplitEvent.SDK_READY_FROM_CACHE) && mEventsManager.eventAlreadyTriggered(event)) {
Logger.w(String.format("A listener was added for %s on the SDK, which has already fired and won’t be emitted again. The callback won’t be executed.", event.toString()));
return;
}
mEventsManager.register(event, task);
}
@Override
public boolean track(String trafficType, String eventType) {
return false;
}
@Override
public boolean track(String trafficType, String eventType, double value) {
return false;
}
@Override
public boolean track(String eventType) {
return false;
}
@Override
public boolean track(String eventType, double value) {
return false;
}
@Override
public boolean track(String trafficType, String eventType, Map<String, Object> properties) {
return false;
}
@Override
public boolean track(String trafficType, String eventType, double value, Map<String, Object> properties) {
return false;
}
@Override
public boolean track(String eventType, Map<String, Object> properties) {
return false;
}
@Override
public boolean track(String eventType, double value, Map<String, Object> properties) {
return false;
}
private ImpressionListener.FederatedImpressionListener getImpressionsListener(SplitClientConfig config) {
if (config.impressionListener() != null) {
return new ImpressionListener.FederatedImpressionListener(new ImpressionListener.NoopImpressionListener(), Collections.singletonList(config.impressionListener()));
} else {
return new ImpressionListener.FederatedImpressionListener(new ImpressionListener.NoopImpressionListener(), Collections.emptyList());
}
}
@Override
public boolean setAttribute(String attributeName, Object value) {
return true;
}
@Nullable
@Override
public Object getAttribute(String attributeName) {
return null;
}
@Override
public boolean setAttributes(Map<String, Object> attributes) {
return true;
}
@NonNull
@Override
public Map<String, Object> getAllAttributes() {
return new HashMap<>();
}
@Override
public boolean removeAttribute(String attributeName) {
return true;
}
@Override
public boolean clearAttributes() {
return true;
}
private Map<String, String> buildExceptionResult(List<String> flagSets) {
Map<String, String> result = new HashMap<>();
Set<String> namesByFlagSets = mSplitsStorage.getNamesByFlagSets(flagSets);
for (String featureFlagName : namesByFlagSets) {
result.put(featureFlagName, Treatments.CONTROL);
}
return result;
}
private Map<String, SplitResult> buildExceptionResultWithConfig(List<String> flagSets) {
Map<String, SplitResult> result = new HashMap<>();
Set<String> namesByFlagSets = mSplitsStorage.getNamesByFlagSets(flagSets);
for (String featureFlagName : namesByFlagSets) {
result.put(featureFlagName, new SplitResult(Treatments.CONTROL));
}
return result;
}
}