Skip to content

Commit 919b2e7

Browse files
authored
Fixed circular dependency introduced by SwitcherContextBase (#326)
* Fixed circular dependency introduced by SwitcherContextBase * Fixes code smell * Removed Java 8 feature exclusive
1 parent f7e7559 commit 919b2e7

17 files changed

+288
-207
lines changed

src/main/java/com/github/switcherapi/client/ContextBuilder.java

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@
55

66
import java.util.Optional;
77

8-
import static com.github.switcherapi.client.SwitcherProperties.DEFAULT_TIMEOUT_MS;
9-
import static com.github.switcherapi.client.remote.Constants.DEFAULT_POOL_SIZE;
8+
import static com.github.switcherapi.client.remote.Constants.*;
109

1110
public class ContextBuilder {
1211

1312
private static ContextBuilder context;
1413

15-
private SwitcherProperties properties;
14+
private SwitcherProperties switcherProperties;
1615

17-
private ContextBuilder() {
18-
properties = new SwitcherProperties();
16+
private ContextBuilder(SwitcherProperties switcherProperties) {
17+
this.switcherProperties = switcherProperties;
1918
}
2019

2120
public static void preConfigure(SwitcherProperties switcherProperties) {
@@ -40,25 +39,25 @@ public static ContextBuilder builder() {
4039
*/
4140
public static ContextBuilder builder(boolean init) {
4241
if (context == null || init)
43-
context = new ContextBuilder();
42+
context = new ContextBuilder(new SwitcherPropertiesImpl());
4443

4544
return context;
4645
}
4746

4847
void preBuild(SwitcherProperties properties) {
49-
this.properties = properties;
48+
this.switcherProperties = properties;
5049
}
51-
50+
5251
SwitcherProperties build() {
53-
return this.properties;
52+
return this.switcherProperties;
5453
}
5554

5655
/**
5756
* @param contextLocation Feature class that extends SwitcherContext
5857
* @return ContextBuilder
5958
*/
6059
public ContextBuilder contextLocation(String contextLocation) {
61-
properties.setValue(ContextKey.CONTEXT_LOCATION, contextLocation);
60+
switcherProperties.setValue(ContextKey.CONTEXT_LOCATION, contextLocation);
6261
return this;
6362
}
6463

@@ -67,7 +66,7 @@ public ContextBuilder contextLocation(String contextLocation) {
6766
* @return ContextBuilder
6867
*/
6968
public ContextBuilder url(String url) {
70-
properties.setValue(ContextKey.URL, url);
69+
switcherProperties.setValue(ContextKey.URL, url);
7170
return this;
7271
}
7372

@@ -76,7 +75,7 @@ public ContextBuilder url(String url) {
7675
* @return ContextBuilder
7776
*/
7877
public ContextBuilder apiKey(String apiKey) {
79-
properties.setValue(ContextKey.APIKEY, apiKey);
78+
switcherProperties.setValue(ContextKey.APIKEY, apiKey);
8079
return this;
8180
}
8281

@@ -85,7 +84,7 @@ public ContextBuilder apiKey(String apiKey) {
8584
* @return ContextBuilder
8685
*/
8786
public ContextBuilder domain(String domain) {
88-
properties.setValue(ContextKey.DOMAIN, domain);
87+
switcherProperties.setValue(ContextKey.DOMAIN, domain);
8988
return this;
9089
}
9190

@@ -94,7 +93,7 @@ public ContextBuilder domain(String domain) {
9493
* @return ContextBuilder
9594
*/
9695
public ContextBuilder component(String component) {
97-
properties.setValue(ContextKey.COMPONENT, component);
96+
switcherProperties.setValue(ContextKey.COMPONENT, component);
9897
return this;
9998
}
10099

@@ -103,7 +102,8 @@ public ContextBuilder component(String component) {
103102
* @return ContextBuilder
104103
*/
105104
public ContextBuilder environment(String environment) {
106-
properties.setValue(ContextKey.ENVIRONMENT, properties.getValueDefault(environment, SwitcherProperties.DEFAULT_ENV));
105+
switcherProperties.setValue(ContextKey.ENVIRONMENT,
106+
Optional.ofNullable(environment).orElse(DEFAULT_ENV));
107107
return this;
108108
}
109109

@@ -112,7 +112,7 @@ public ContextBuilder environment(String environment) {
112112
* @return ContextBuilder
113113
*/
114114
public ContextBuilder snapshotLocation(String snapshotLocation) {
115-
properties.setValue(ContextKey.SNAPSHOT_LOCATION, snapshotLocation);
115+
switcherProperties.setValue(ContextKey.SNAPSHOT_LOCATION, snapshotLocation);
116116
return this;
117117
}
118118

@@ -121,22 +121,11 @@ public ContextBuilder snapshotLocation(String snapshotLocation) {
121121
* @return ContextBuilder
122122
*/
123123
public ContextBuilder snapshotAutoUpdateInterval(String snapshotAutoUpdateInterval) {
124-
properties.setValue(ContextKey.SNAPSHOT_AUTO_UPDATE_INTERVAL, snapshotAutoUpdateInterval);
124+
switcherProperties.setValue(ContextKey.SNAPSHOT_AUTO_UPDATE_INTERVAL, snapshotAutoUpdateInterval);
125125

126126
if (snapshotAutoUpdateInterval != null)
127-
properties.setValue(ContextKey.SNAPSHOT_AUTO_LOAD, true);
128-
129-
return this;
130-
}
127+
switcherProperties.setValue(ContextKey.SNAPSHOT_AUTO_LOAD, true);
131128

132-
/**
133-
* Java 8 only
134-
*
135-
* @param regexTimeout Time in ms given to Timed Match Worker used for local Regex (ReDoS safety mechanism) - 3000 default value
136-
* @return ContextBuilder
137-
*/
138-
public ContextBuilder regexTimeout(int regexTimeout) {
139-
properties.setValue(ContextKey.REGEX_TIMEOUT, regexTimeout);
140129
return this;
141130
}
142131

@@ -145,7 +134,7 @@ public ContextBuilder regexTimeout(int regexTimeout) {
145134
* @return ContextBuilder
146135
*/
147136
public ContextBuilder snapshotAutoLoad(boolean snapshotAutoLoad) {
148-
properties.setValue(ContextKey.SNAPSHOT_AUTO_LOAD, snapshotAutoLoad);
137+
switcherProperties.setValue(ContextKey.SNAPSHOT_AUTO_LOAD, snapshotAutoLoad);
149138
return this;
150139
}
151140

@@ -154,7 +143,7 @@ public ContextBuilder snapshotAutoLoad(boolean snapshotAutoLoad) {
154143
* @return ContextBuilder
155144
*/
156145
public ContextBuilder snapshotSkipValidation(boolean snapshotSkipValidation) {
157-
properties.setValue(ContextKey.SNAPSHOT_SKIP_VALIDATION, snapshotSkipValidation);
146+
switcherProperties.setValue(ContextKey.SNAPSHOT_SKIP_VALIDATION, snapshotSkipValidation);
158147
return this;
159148
}
160149

@@ -163,10 +152,10 @@ public ContextBuilder snapshotSkipValidation(boolean snapshotSkipValidation) {
163152
* @return ContextBuilder
164153
*/
165154
public ContextBuilder silentMode(String retryAfter) {
166-
properties.setValue(ContextKey.SILENT_MODE, retryAfter);
155+
switcherProperties.setValue(ContextKey.SILENT_MODE, retryAfter);
167156

168157
if (StringUtils.isNotBlank(retryAfter)) {
169-
properties.setValue(ContextKey.SNAPSHOT_AUTO_LOAD, true);
158+
switcherProperties.setValue(ContextKey.SNAPSHOT_AUTO_LOAD, true);
170159
}
171160

172161
return this;
@@ -177,7 +166,7 @@ public ContextBuilder silentMode(String retryAfter) {
177166
* @return ContextBuilder
178167
*/
179168
public ContextBuilder local(boolean local) {
180-
properties.setValue(ContextKey.LOCAL_MODE, local);
169+
switcherProperties.setValue(ContextKey.LOCAL_MODE, local);
181170
return this;
182171
}
183172

@@ -186,7 +175,7 @@ public ContextBuilder local(boolean local) {
186175
* @return ContextBuilder
187176
*/
188177
public ContextBuilder truststorePath(String truststorePath) {
189-
properties.setValue(ContextKey.TRUSTSTORE_PATH, truststorePath);
178+
switcherProperties.setValue(ContextKey.TRUSTSTORE_PATH, truststorePath);
190179
return this;
191180
}
192181

@@ -195,7 +184,7 @@ public ContextBuilder truststorePath(String truststorePath) {
195184
* @return ContextBuilder
196185
*/
197186
public ContextBuilder truststorePassword(String truststorePassword) {
198-
properties.setValue(ContextKey.TRUSTSTORE_PASSWORD, truststorePassword);
187+
switcherProperties.setValue(ContextKey.TRUSTSTORE_PASSWORD, truststorePassword);
199188
return this;
200189
}
201190

@@ -204,8 +193,8 @@ public ContextBuilder truststorePassword(String truststorePassword) {
204193
* @return ContextBuilder
205194
*/
206195
public ContextBuilder timeoutMs(Integer timeoutMs) {
207-
properties.setValue(ContextKey.TIMEOUT_MS,
208-
Optional.ofNullable(timeoutMs).orElse(DEFAULT_TIMEOUT_MS));
196+
switcherProperties.setValue(ContextKey.TIMEOUT_MS,
197+
Optional.ofNullable(timeoutMs).orElse(DEFAULT_TIMEOUT));
209198
return this;
210199
}
211200

@@ -214,7 +203,7 @@ public ContextBuilder timeoutMs(Integer timeoutMs) {
214203
* @return ContextBuilder
215204
*/
216205
public ContextBuilder poolConnectionSize(Integer poolSize) {
217-
properties.setValue(ContextKey.POOL_CONNECTION_SIZE,
206+
switcherProperties.setValue(ContextKey.POOL_CONNECTION_SIZE,
218207
Optional.ofNullable(poolSize).orElse(DEFAULT_POOL_SIZE));
219208
return this;
220209
}

src/main/java/com/github/switcherapi/client/SwitcherContextBase.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public abstract class SwitcherContextBase extends SwitcherConfig {
9393
private static SnapshotWatcher watcherSnapshot;
9494

9595
static {
96-
switcherProperties = new SwitcherProperties();
96+
switcherProperties = new SwitcherPropertiesImpl();
9797
}
9898

9999
@Override
@@ -175,13 +175,13 @@ public static void initializeClient() {
175175
private static SwitcherExecutor buildInstance() {
176176
final ClientWS clientWS = initRemotePoolExecutorService();
177177
final SwitcherValidator validatorService = new ValidatorService();
178-
final ClientRemote clientRemote = new ClientRemoteService(clientWS);
178+
final ClientRemote clientRemote = new ClientRemoteService(clientWS, switcherProperties);
179179
final ClientLocal clientLocal = new ClientLocalService(validatorService);
180180

181181
if (contextBol(ContextKey.LOCAL_MODE)) {
182-
return new SwitcherLocalService(clientRemote, clientLocal);
182+
return new SwitcherLocalService(clientRemote, clientLocal, switcherProperties);
183183
} else {
184-
return new SwitcherRemoteService(clientRemote, new SwitcherLocalService(clientRemote, clientLocal));
184+
return new SwitcherRemoteService(clientRemote, new SwitcherLocalService(clientRemote, clientLocal, switcherProperties));
185185
}
186186
}
187187

@@ -309,7 +309,7 @@ private static ClientWS initRemotePoolExecutorService() {
309309
return thread;
310310
});
311311

312-
return ClientWSImpl.build(remotePoolExecutorService, timeoutMs);
312+
return ClientWSImpl.build(switcherProperties, remotePoolExecutorService, timeoutMs);
313313
}
314314

315315
/**
@@ -447,6 +447,15 @@ public static Integer contextInt(ContextKey contextKey) {
447447
public static boolean contextBol(ContextKey contextKey) {
448448
return switcherProperties.getBoolean(contextKey);
449449
}
450+
451+
/**
452+
* Retrieve the Switcher Properties
453+
*
454+
* @return SwitcherProperties instance
455+
*/
456+
public static SwitcherProperties getSwitcherProperties() {
457+
return switcherProperties;
458+
}
450459

451460
/**
452461
* Fluent builder to configure the Switcher Context

src/main/java/com/github/switcherapi/client/SwitcherExecutor.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@ public abstract class SwitcherExecutor {
3232

3333
private static final Map<String, CriteriaResponse> bypass = new HashMap<>();
3434

35+
protected final SwitcherProperties switcherProperties;
36+
3537
protected Domain domain;
38+
39+
protected SwitcherExecutor(final SwitcherProperties switcherProperties) {
40+
this.switcherProperties = switcherProperties;
41+
}
3642

3743
/**
3844
* Execute criteria based on the Switcher configuration
@@ -69,19 +75,19 @@ public abstract class SwitcherExecutor {
6975
public abstract long getSnapshotVersion();
7076

7177
protected boolean checkSnapshotVersion(ClientRemote clientRemote, final Domain domain) {
72-
final String environment = SwitcherContextBase.contextStr(ContextKey.ENVIRONMENT);
78+
final String environment = switcherProperties.getValue(ContextKey.ENVIRONMENT);
7379
SwitcherUtils.debug(logger, "verifying snapshot version - environment: {}", environment);
7480

7581
return clientRemote.checkSnapshotVersion(domain.getVersion());
7682
}
7783

7884
protected Domain initializeSnapshotFromAPI(ClientRemote clientRemote)
7985
throws SwitcherRemoteException, SwitcherSnapshotWriteException {
80-
final String environment = SwitcherContextBase.contextStr(ContextKey.ENVIRONMENT);
86+
final String environment = switcherProperties.getValue(ContextKey.ENVIRONMENT);
8187
SwitcherUtils.debug(logger, "initializing snapshot from API - environment: {}", environment);
8288

8389
final Snapshot snapshot = clientRemote.resolveSnapshot();
84-
final String snapshotLocation = SwitcherContextBase.contextStr(ContextKey.SNAPSHOT_LOCATION);
90+
final String snapshotLocation = switcherProperties.getValue(ContextKey.SNAPSHOT_LOCATION);
8591

8692
if (snapshotLocation != null) {
8793
SnapshotLoader.saveSnapshot(snapshot, snapshotLocation, environment);
@@ -91,7 +97,7 @@ protected Domain initializeSnapshotFromAPI(ClientRemote clientRemote)
9197
}
9298

9399
public boolean isLocalEnabled() {
94-
return SwitcherContextBase.contextBol(ContextKey.LOCAL_MODE);
100+
return switcherProperties.getBoolean(ContextKey.LOCAL_MODE);
95101
}
96102

97103
/**
@@ -140,6 +146,10 @@ public static Map<String, CriteriaResponse> getBypass() {
140146
return bypass;
141147
}
142148

149+
public SwitcherProperties getSwitcherProperties() {
150+
return switcherProperties;
151+
}
152+
143153
public Domain getDomain() {
144154
return domain;
145155
}

0 commit comments

Comments
 (0)