Skip to content

Commit f66d5e4

Browse files
authored
Fixed circular dependency introduced by SwitcherContextBase (#327)
* Fixed circular dependency introduced by SwitcherContextBase (#326) * Fixed circular dependency introduced by SwitcherContextBase * Fixes code smell * Removed Java 8 feature exclusive * Reverted ClientWSImpl for Java 8, based on Jersey
1 parent b9473d5 commit f66d5e4

17 files changed

+287
-196
lines changed

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

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,16 @@
55

66
import java.util.Optional;
77

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

1210
public class ContextBuilder {
1311

1412
private static ContextBuilder context;
1513

16-
private SwitcherProperties properties;
14+
private SwitcherProperties switcherProperties;
1715

18-
private ContextBuilder() {
19-
properties = new SwitcherProperties();
16+
private ContextBuilder(SwitcherProperties switcherProperties) {
17+
this.switcherProperties = switcherProperties;
2018
}
2119

2220
public static void preConfigure(SwitcherProperties switcherProperties) {
@@ -41,25 +39,25 @@ public static ContextBuilder builder() {
4139
*/
4240
public static ContextBuilder builder(boolean init) {
4341
if (context == null || init)
44-
context = new ContextBuilder();
42+
context = new ContextBuilder(new SwitcherPropertiesImpl());
4543

4644
return context;
4745
}
4846

4947
void preBuild(SwitcherProperties properties) {
50-
this.properties = properties;
48+
this.switcherProperties = properties;
5149
}
52-
50+
5351
SwitcherProperties build() {
54-
return this.properties;
52+
return this.switcherProperties;
5553
}
5654

5755
/**
5856
* @param contextLocation Feature class that extends SwitcherContext
5957
* @return ContextBuilder
6058
*/
6159
public ContextBuilder contextLocation(String contextLocation) {
62-
properties.setValue(ContextKey.CONTEXT_LOCATION, contextLocation);
60+
switcherProperties.setValue(ContextKey.CONTEXT_LOCATION, contextLocation);
6361
return this;
6462
}
6563

@@ -68,7 +66,7 @@ public ContextBuilder contextLocation(String contextLocation) {
6866
* @return ContextBuilder
6967
*/
7068
public ContextBuilder url(String url) {
71-
properties.setValue(ContextKey.URL, url);
69+
switcherProperties.setValue(ContextKey.URL, url);
7270
return this;
7371
}
7472

@@ -77,7 +75,7 @@ public ContextBuilder url(String url) {
7775
* @return ContextBuilder
7876
*/
7977
public ContextBuilder apiKey(String apiKey) {
80-
properties.setValue(ContextKey.APIKEY, apiKey);
78+
switcherProperties.setValue(ContextKey.APIKEY, apiKey);
8179
return this;
8280
}
8381

@@ -86,7 +84,7 @@ public ContextBuilder apiKey(String apiKey) {
8684
* @return ContextBuilder
8785
*/
8886
public ContextBuilder domain(String domain) {
89-
properties.setValue(ContextKey.DOMAIN, domain);
87+
switcherProperties.setValue(ContextKey.DOMAIN, domain);
9088
return this;
9189
}
9290

@@ -95,7 +93,7 @@ public ContextBuilder domain(String domain) {
9593
* @return ContextBuilder
9694
*/
9795
public ContextBuilder component(String component) {
98-
properties.setValue(ContextKey.COMPONENT, component);
96+
switcherProperties.setValue(ContextKey.COMPONENT, component);
9997
return this;
10098
}
10199

@@ -104,7 +102,8 @@ public ContextBuilder component(String component) {
104102
* @return ContextBuilder
105103
*/
106104
public ContextBuilder environment(String environment) {
107-
properties.setValue(ContextKey.ENVIRONMENT, properties.getValueDefault(environment, SwitcherProperties.DEFAULT_ENV));
105+
switcherProperties.setValue(ContextKey.ENVIRONMENT,
106+
Optional.ofNullable(environment).orElse(DEFAULT_ENV));
108107
return this;
109108
}
110109

@@ -113,7 +112,7 @@ public ContextBuilder environment(String environment) {
113112
* @return ContextBuilder
114113
*/
115114
public ContextBuilder snapshotLocation(String snapshotLocation) {
116-
properties.setValue(ContextKey.SNAPSHOT_LOCATION, snapshotLocation);
115+
switcherProperties.setValue(ContextKey.SNAPSHOT_LOCATION, snapshotLocation);
117116
return this;
118117
}
119118

@@ -122,10 +121,10 @@ public ContextBuilder snapshotLocation(String snapshotLocation) {
122121
* @return ContextBuilder
123122
*/
124123
public ContextBuilder snapshotAutoUpdateInterval(String snapshotAutoUpdateInterval) {
125-
properties.setValue(ContextKey.SNAPSHOT_AUTO_UPDATE_INTERVAL, snapshotAutoUpdateInterval);
124+
switcherProperties.setValue(ContextKey.SNAPSHOT_AUTO_UPDATE_INTERVAL, snapshotAutoUpdateInterval);
126125

127126
if (snapshotAutoUpdateInterval != null)
128-
properties.setValue(ContextKey.SNAPSHOT_AUTO_LOAD, true);
127+
switcherProperties.setValue(ContextKey.SNAPSHOT_AUTO_LOAD, true);
129128

130129
return this;
131130
}
@@ -137,7 +136,7 @@ public ContextBuilder snapshotAutoUpdateInterval(String snapshotAutoUpdateInterv
137136
* @return ContextBuilder
138137
*/
139138
public ContextBuilder regexTimeout(Integer regexTimeout) {
140-
properties.setValue(ContextKey.REGEX_TIMEOUT,
139+
switcherProperties.setValue(ContextKey.REGEX_TIMEOUT,
141140
Optional.ofNullable(regexTimeout).orElse(DEFAULT_REGEX_TIMEOUT));
142141
return this;
143142
}
@@ -147,7 +146,7 @@ public ContextBuilder regexTimeout(Integer regexTimeout) {
147146
* @return ContextBuilder
148147
*/
149148
public ContextBuilder snapshotAutoLoad(boolean snapshotAutoLoad) {
150-
properties.setValue(ContextKey.SNAPSHOT_AUTO_LOAD, snapshotAutoLoad);
149+
switcherProperties.setValue(ContextKey.SNAPSHOT_AUTO_LOAD, snapshotAutoLoad);
151150
return this;
152151
}
153152

@@ -156,7 +155,7 @@ public ContextBuilder snapshotAutoLoad(boolean snapshotAutoLoad) {
156155
* @return ContextBuilder
157156
*/
158157
public ContextBuilder snapshotSkipValidation(boolean snapshotSkipValidation) {
159-
properties.setValue(ContextKey.SNAPSHOT_SKIP_VALIDATION, snapshotSkipValidation);
158+
switcherProperties.setValue(ContextKey.SNAPSHOT_SKIP_VALIDATION, snapshotSkipValidation);
160159
return this;
161160
}
162161

@@ -165,10 +164,10 @@ public ContextBuilder snapshotSkipValidation(boolean snapshotSkipValidation) {
165164
* @return ContextBuilder
166165
*/
167166
public ContextBuilder silentMode(String retryAfter) {
168-
properties.setValue(ContextKey.SILENT_MODE, retryAfter);
167+
switcherProperties.setValue(ContextKey.SILENT_MODE, retryAfter);
169168

170169
if (StringUtils.isNotBlank(retryAfter)) {
171-
properties.setValue(ContextKey.SNAPSHOT_AUTO_LOAD, true);
170+
switcherProperties.setValue(ContextKey.SNAPSHOT_AUTO_LOAD, true);
172171
}
173172

174173
return this;
@@ -179,7 +178,7 @@ public ContextBuilder silentMode(String retryAfter) {
179178
* @return ContextBuilder
180179
*/
181180
public ContextBuilder local(boolean local) {
182-
properties.setValue(ContextKey.LOCAL_MODE, local);
181+
switcherProperties.setValue(ContextKey.LOCAL_MODE, local);
183182
return this;
184183
}
185184

@@ -188,7 +187,7 @@ public ContextBuilder local(boolean local) {
188187
* @return ContextBuilder
189188
*/
190189
public ContextBuilder truststorePath(String truststorePath) {
191-
properties.setValue(ContextKey.TRUSTSTORE_PATH, truststorePath);
190+
switcherProperties.setValue(ContextKey.TRUSTSTORE_PATH, truststorePath);
192191
return this;
193192
}
194193

@@ -197,7 +196,7 @@ public ContextBuilder truststorePath(String truststorePath) {
197196
* @return ContextBuilder
198197
*/
199198
public ContextBuilder truststorePassword(String truststorePassword) {
200-
properties.setValue(ContextKey.TRUSTSTORE_PASSWORD, truststorePassword);
199+
switcherProperties.setValue(ContextKey.TRUSTSTORE_PASSWORD, truststorePassword);
201200
return this;
202201
}
203202

@@ -206,8 +205,8 @@ public ContextBuilder truststorePassword(String truststorePassword) {
206205
* @return ContextBuilder
207206
*/
208207
public ContextBuilder timeoutMs(Integer timeoutMs) {
209-
properties.setValue(ContextKey.TIMEOUT_MS,
210-
Optional.ofNullable(timeoutMs).orElse(DEFAULT_TIMEOUT_MS));
208+
switcherProperties.setValue(ContextKey.TIMEOUT_MS,
209+
Optional.ofNullable(timeoutMs).orElse(DEFAULT_TIMEOUT));
211210
return this;
212211
}
213212

@@ -216,7 +215,7 @@ public ContextBuilder timeoutMs(Integer timeoutMs) {
216215
* @return ContextBuilder
217216
*/
218217
public ContextBuilder poolConnectionSize(Integer poolSize) {
219-
properties.setValue(ContextKey.POOL_CONNECTION_SIZE,
218+
switcherProperties.setValue(ContextKey.POOL_CONNECTION_SIZE,
220219
Optional.ofNullable(poolSize).orElse(DEFAULT_POOL_SIZE));
221220
return this;
222221
}

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
@@ -176,13 +176,13 @@ public static void initializeClient() {
176176
private static SwitcherExecutor buildInstance() {
177177
final ClientWS clientWS = initRemotePoolExecutorService();
178178
final SwitcherValidator validatorService = new ValidatorService();
179-
final ClientRemote clientRemote = new ClientRemoteService(clientWS);
179+
final ClientRemote clientRemote = new ClientRemoteService(clientWS, switcherProperties);
180180
final ClientLocal clientLocal = new ClientLocalService(validatorService);
181181

182182
if (contextBol(ContextKey.LOCAL_MODE)) {
183-
return new SwitcherLocalService(clientRemote, clientLocal);
183+
return new SwitcherLocalService(clientRemote, clientLocal, switcherProperties);
184184
} else {
185-
return new SwitcherRemoteService(clientRemote, new SwitcherLocalService(clientRemote, clientLocal));
185+
return new SwitcherRemoteService(clientRemote, new SwitcherLocalService(clientRemote, clientLocal, switcherProperties));
186186
}
187187
}
188188

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

313-
return ClientWSImpl.build(remotePoolExecutorService, timeoutMs);
313+
return ClientWSImpl.build(switcherProperties, remotePoolExecutorService, timeoutMs);
314314
}
315315

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

452461
/**
453462
* 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)