-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwitcherExecutor.java
More file actions
132 lines (109 loc) · 4.01 KB
/
SwitcherExecutor.java
File metadata and controls
132 lines (109 loc) · 4.01 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
package com.github.switcherapi.client;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.github.switcherapi.client.exception.SwitcherRemoteException;
import com.github.switcherapi.client.exception.SwitcherSnapshotWriteException;
import com.github.switcherapi.client.model.ContextKey;
import com.github.switcherapi.client.model.Switcher;
import com.github.switcherapi.client.model.criteria.Domain;
import com.github.switcherapi.client.model.criteria.Snapshot;
import com.github.switcherapi.client.model.response.CriteriaResponse;
import com.github.switcherapi.client.service.remote.ClientRemoteService;
import com.github.switcherapi.client.utils.SnapshotEventHandler;
import com.github.switcherapi.client.utils.SnapshotLoader;
/**
* The Executor provides an API to handle Remote and Local functionalities that
* should be available for both Services implementations.
*
* @author Roger Floriano (petruki)
* @since 2019-12-24
*/
public abstract class SwitcherExecutor {
private static final Logger logger = LogManager.getLogger(SwitcherExecutor.class);
private static final Map<String, Boolean> bypass = new HashMap<>();
/**
* Execute criteria based on the Switcher configuration
*
* @param switcher to be evaluated
* @return Criteria response containing the evaluation details
*/
public abstract CriteriaResponse executeCriteria(final Switcher switcher);
/**
* Check the snapshot versions against the Remote configuration.
*
* @return True if snapshot is up-to-date
*/
public abstract boolean checkSnapshotVersion();
/**
* Retrieve updated snapshot from the remote API
*/
public abstract void updateSnapshot();
/**
* Check set of Switchers if they are properly configured.
*
* @param switchers To be validated
*/
public abstract void checkSwitchers(final Set<String> switchers);
/**
* Update in-memory snapshot.
*
* @param snapshotFile Path location
* @param handler to notify snapshot change events
*
* @return true if valid change
*/
public abstract boolean notifyChange(final String snapshotFile, SnapshotEventHandler handler);
/**
* Retrieve local snapshot version
*
* @return snapshot version
*/
public abstract long getSnapshotVersion();
protected boolean checkSnapshotVersion(final Domain domain) {
final String environment = SwitcherContextBase.contextStr(ContextKey.ENVIRONMENT);
if (logger.isDebugEnabled()) {
logger.debug(String.format("verifying snapshot version - environment: %s", environment));
}
return ClientRemoteService.getInstance().checkSnapshotVersion(domain.getVersion());
}
protected Domain initializeSnapshotFromAPI() {
final String environment = SwitcherContextBase.contextStr(ContextKey.ENVIRONMENT);
if (logger.isDebugEnabled()) {
logger.debug(String.format("initializing snapshot from API - environment: %s", environment));
}
try {
final Snapshot snapshot = ClientRemoteService.getInstance().resolveSnapshot();
final String snapshotLocation = SwitcherContextBase.contextStr(ContextKey.SNAPSHOT_LOCATION);
if (snapshotLocation != null) {
SnapshotLoader.saveSnapshot(snapshot, snapshotLocation, environment);
}
return snapshot.getDomain();
} catch (SwitcherRemoteException | SwitcherSnapshotWriteException e) {
logger.error(e);
throw e;
}
}
/**
* It manipulates the result of a given Switcher key.
*
* @param key name of the key that you want to change the result
* @param expectedResult that will be returned when performing isItOn
*/
public static void assume(final String key, boolean expectedResult) {
bypass.put(key, expectedResult);
}
/**
* It will clean up any result manipulation added before by invoking {@link SwitcherExecutor#assume(String, boolean)}
*
* @param key name of the key you want to remove
*/
public static void forget(final String key) {
bypass.remove(key);
}
public static Map<String, Boolean> getBypass() {
return bypass;
}
}