Skip to content

Commit bb3eb02

Browse files
authored
Fixed project dependencies (#321)
* Closes #319 - removed jersey dependencies (#320) * Closes #319 - removed jersey dependencies * Moved HttpClient from constructor to ClientWS dependency * fixes code smell * Reverted jersey dependency for the v1
1 parent 80e7127 commit bb3eb02

35 files changed

+341
-195
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ switcher.silent -> Enable contigency given the time for the client to retry - e.
7878
switcher.truststore.path -> Path to the truststore file
7979
switcher.truststore.password -> Truststore password
8080
switcher.timeout -> Time in ms given to the API to respond - 3000 default value
81+
switcher.poolsize -> Number of threads used to execute the API - 10 default value
8182
8283
(Java 8 applications only)
8384
switcher.regextimeout -> Time in ms given to Timed Match Worker used for local Regex (ReDoS safety mechanism) - 3000 default value

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22

3-
<project xmlns="http://maven.apache.org/POM/4.0.0"
4-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<project xmlns="http://maven.apache.org/POM/4.0.0"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
66
<modelVersion>4.0.0</modelVersion>
77

88
<groupId>com.github.switcherapi</groupId>

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public ContextBuilder snapshotSkipValidation(boolean snapshotSkipValidation) {
154154
}
155155

156156
/**
157-
* @param retryAfter Enable contigency given the time for the client to retry - e.g. 5s (s: seconds - m: minutes - h: hours)
157+
* @param retryAfter Enable contingency given the time for the client to retry - e.g. 5s (s: seconds - m: minutes - h: hours)
158158
* @return ContextBuilder
159159
*/
160160
public ContextBuilder silentMode(String retryAfter) {
@@ -202,4 +202,13 @@ public ContextBuilder timeoutMs(String timeoutMs) {
202202
properties.setValue(ContextKey.TIMEOUT_MS, properties.getTimeoutMsOrDefault(timeoutMs));
203203
return this;
204204
}
205+
206+
/**
207+
* @param poolSize Number of threads for the pool connection - 10 default value
208+
* @return ContextBuilder
209+
*/
210+
public ContextBuilder poolConnectionSize(String poolSize) {
211+
properties.setValue(ContextKey.POOL_CONNECTION_SIZE, poolSize);
212+
return this;
213+
}
205214
}

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

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@
66
import com.github.switcherapi.client.exception.SwitchersValidationException;
77
import com.github.switcherapi.client.model.ContextKey;
88
import com.github.switcherapi.client.model.Switcher;
9+
import com.github.switcherapi.client.remote.ClientWSImpl;
10+
import com.github.switcherapi.client.service.SwitcherValidator;
11+
import com.github.switcherapi.client.service.ValidatorService;
912
import com.github.switcherapi.client.service.WorkerName;
13+
import com.github.switcherapi.client.service.local.ClientLocalService;
1014
import com.github.switcherapi.client.service.local.SwitcherLocalService;
15+
import com.github.switcherapi.client.service.remote.ClientRemote;
16+
import com.github.switcherapi.client.service.remote.ClientRemoteService;
1117
import com.github.switcherapi.client.service.remote.SwitcherRemoteService;
1218
import com.github.switcherapi.client.utils.SnapshotEventHandler;
1319
import com.github.switcherapi.client.utils.SnapshotWatcher;
@@ -70,7 +76,7 @@ public abstract class SwitcherContextBase {
7076
protected static SwitcherExecutor instance;
7177
private static ScheduledExecutorService scheduledExecutorService;
7278
private static ExecutorService watcherExecutorService;
73-
private static SnapshotWatcher watcher;
79+
private static SnapshotWatcher watcherSnapshot;
7480

7581
protected SwitcherContextBase() {
7682
throw new IllegalStateException("Context class cannot be instantiated");
@@ -112,11 +118,15 @@ public static void loadProperties(String contextFilename) {
112118
public static void initializeClient() {
113119
validateContext();
114120
validateSwitcherKeys();
115-
121+
122+
final SwitcherValidator validatorService = new ValidatorService();
123+
final ClientRemote clientRemote = new ClientRemoteService(ClientWSImpl.build());
124+
final ClientLocalService clientLocal = new ClientLocalService(validatorService);
125+
116126
if (contextBol(ContextKey.LOCAL_MODE)) {
117-
instance = new SwitcherLocalService();
127+
instance = new SwitcherLocalService(clientRemote, clientLocal);
118128
} else {
119-
instance = new SwitcherRemoteService();
129+
instance = new SwitcherRemoteService(clientRemote, new SwitcherLocalService(clientRemote, clientLocal));
120130
}
121131

122132
loadSwitchers();
@@ -309,13 +319,13 @@ public static void watchSnapshot(SnapshotEventHandler handler) {
309319
throw new SwitcherException("Cannot watch snapshot when using remote", new UnsupportedOperationException());
310320
}
311321

312-
if (watcher == null) {
313-
watcher = new SnapshotWatcher((SwitcherLocalService) instance, handler,
322+
if (watcherSnapshot == null) {
323+
watcherSnapshot = new SnapshotWatcher((SwitcherLocalService) instance, handler,
314324
contextStr(ContextKey.SNAPSHOT_LOCATION));
315325
}
316326

317327
initWatcherExecutorService();
318-
watcherExecutorService.submit(watcher);
328+
watcherExecutorService.submit(watcherSnapshot);
319329
}
320330

321331
/**
@@ -324,10 +334,10 @@ public static void watchSnapshot(SnapshotEventHandler handler) {
324334
* @throws SwitcherException if watch thread never started
325335
*/
326336
public static void stopWatchingSnapshot() {
327-
if (watcher != null) {
337+
if (watcherSnapshot != null) {
328338
watcherExecutorService.shutdownNow();
329-
watcher.terminate();
330-
watcher = null;
339+
watcherSnapshot.terminate();
340+
watcherSnapshot = null;
331341
}
332342
}
333343

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class SwitcherContextValidator {
1818
public static final String ERR_DOMAIN = "Domain not defined [add: switcher.domain]";
1919
public static final String ERR_COMPONENT = "Component not defined [add: switcher.component]";
2020
public static final String ERR_CONTEXT = "Context class location not defined [add: switcher.context]";
21+
public static final String ERR_LOCAL = "Snapshot location not defined [add: switcher.snapshot.location] " +
22+
"or enable auto-load [add: switcher.snapshot.auto]";
2123

2224
private SwitcherContextValidator() {}
2325

@@ -34,6 +36,8 @@ public static void validate(final SwitcherProperties prop) {
3436

3537
if (!prop.getBoolean(ContextKey.LOCAL_MODE)) {
3638
validateRemote(prop);
39+
} else {
40+
validateLocal(prop);
3741
}
3842

3943
validateOptionals(prop);
@@ -75,4 +79,16 @@ public static void validateRemote(final SwitcherProperties prop) {
7579
throw new SwitcherContextException(ERR_COMPONENT);
7680
}
7781
}
82+
83+
/**
84+
* Validate context properties required to run local
85+
*
86+
* @param prop Configured properties
87+
*/
88+
public static void validateLocal(final SwitcherProperties prop) {
89+
if (StringUtils.isBlank(prop.getValue(ContextKey.SNAPSHOT_LOCATION)) &&
90+
!prop.getBoolean(ContextKey.SNAPSHOT_AUTO_LOAD)) {
91+
throw new SwitcherContextException(ERR_LOCAL);
92+
}
93+
}
7894
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public abstract class SwitcherExecutor {
3131
private static final Logger logger = LoggerFactory.getLogger(SwitcherExecutor.class);
3232

3333
private static final Map<String, CriteriaResponse> bypass = new HashMap<>();
34+
35+
protected Domain domain;
3436

3537
/**
3638
* Execute criteria based on the Switcher configuration
@@ -137,4 +139,12 @@ public static void forget(final String key) {
137139
public static Map<String, CriteriaResponse> getBypass() {
138140
return bypass;
139141
}
142+
143+
public Domain getDomain() {
144+
return domain;
145+
}
146+
147+
public void setDomain(Domain domain) {
148+
this.domain = domain;
149+
}
140150
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public void loadFromProperties(Properties prop) {
5555
setValue(ContextKey.TRUSTSTORE_PATH, SwitcherUtils.resolveProperties(ContextKey.TRUSTSTORE_PATH.getParam(), prop));
5656
setValue(ContextKey.TRUSTSTORE_PASSWORD, SwitcherUtils.resolveProperties(ContextKey.TRUSTSTORE_PASSWORD.getParam(), prop));
5757
setValue(ContextKey.TIMEOUT_MS, getTimeoutMsOrDefault(SwitcherUtils.resolveProperties(ContextKey.TIMEOUT_MS.getParam(), prop)));
58+
setValue(ContextKey.POOL_CONNECTION_SIZE, SwitcherUtils.resolveProperties(ContextKey.POOL_CONNECTION_SIZE.getParam(), prop));
5859
}
5960

6061
public String getValue(ContextKey contextKey) {

src/main/java/com/github/switcherapi/client/model/AsyncSwitcher.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package com.github.switcherapi.client.model;
22

3-
import com.github.switcherapi.client.utils.SwitcherUtils;
4-
53
import com.github.switcherapi.client.exception.SwitcherException;
64
import com.github.switcherapi.client.model.response.CriteriaResponse;
5+
import com.github.switcherapi.client.utils.SwitcherUtils;
76
import org.slf4j.Logger;
87
import org.slf4j.LoggerFactory;
98

src/main/java/com/github/switcherapi/client/model/ContextKey.java

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,98 +11,97 @@ public enum ContextKey {
1111
/**
1212
* (String) Switcher API URL.
1313
*/
14-
URL("switcher.url", "url"),
14+
URL("switcher.url"),
1515

1616
/**
1717
* (String) API Key generated by your domain.
1818
*/
19-
APIKEY("switcher.apikey", "apiKey"),
19+
APIKEY("switcher.apikey"),
2020

2121
/**
2222
* (String) Registered domain name.
2323
*/
24-
DOMAIN("switcher.domain", "domain"),
24+
DOMAIN("switcher.domain"),
2525

2626
/**
2727
* (String) Name of this application. This value is used to track which applications are using switchers.
2828
*/
29-
COMPONENT("switcher.component", "component"),
29+
COMPONENT("switcher.component"),
3030

3131
/**
3232
* (String) Name of the environment where this application is running ('default' is a production environment).
3333
*/
34-
ENVIRONMENT("switcher.environment", "environment"),
34+
ENVIRONMENT("switcher.environment"),
3535

3636
/**
3737
* (String) Folder path where all snapshot files are located.
3838
*/
39-
SNAPSHOT_LOCATION("switcher.snapshot.location", "snapshotLocation"),
39+
SNAPSHOT_LOCATION("switcher.snapshot.location"),
4040

4141
/**
4242
* (String) Defines the package and class where a context wrapper is located.
4343
* It is only necessary to use with {@link com.github.switcherapi.client.SwitcherContext}
4444
*/
45-
CONTEXT_LOCATION("switcher.context", "context"),
45+
CONTEXT_LOCATION("switcher.context"),
4646

4747
/**
4848
* (boolean) Activate snapshot autoload which will try to retrieve the snapshot from the API if the file does not exist.
4949
*/
50-
SNAPSHOT_AUTO_LOAD("switcher.snapshot.auto", "snapshotAutoLoad"),
50+
SNAPSHOT_AUTO_LOAD("switcher.snapshot.auto"),
5151

5252
/**
5353
* (boolean) When true it will skip validateSnapshot() (default is false)
5454
*/
55-
SNAPSHOT_SKIP_VALIDATION("switcher.snapshot.skipvalidation", "snapshotSkipValidation"),
55+
SNAPSHOT_SKIP_VALIDATION("switcher.snapshot.skipvalidation"),
5656

5757
/**
5858
* (String) Interval given to the library to update the snapshot
5959
*/
60-
SNAPSHOT_AUTO_UPDATE_INTERVAL("switcher.snapshot.updateinterval", "snapshotAutoUpdateInterval"),
60+
SNAPSHOT_AUTO_UPDATE_INTERVAL("switcher.snapshot.updateinterval"),
6161

6262
/**
6363
* (String) Defines if client will work in silent mode by specifying the time interval to retry
6464
*/
65-
SILENT_MODE("switcher.silent", "silentMode"),
65+
SILENT_MODE("switcher.silent"),
6666

6767
/**
6868
* (boolean) Defines if client will work locally.
6969
*/
70-
LOCAL_MODE("switcher.local", "local"),
70+
LOCAL_MODE("switcher.local"),
7171

7272
/**
7373
* (Number) Defines the Timed Match regex time out.
7474
*/
75-
REGEX_TIMEOUT("switcher.regextimeout", "regexTimeout"),
75+
REGEX_TIMEOUT("switcher.regextimeout"),
7676

7777
/**
78-
* (Path) Defines the path for the trustsore file.
78+
* (Path) Defines the path for the truststore file.
7979
*/
80-
TRUSTSTORE_PATH("switcher.truststore.path", "truststorePath"),
80+
TRUSTSTORE_PATH("switcher.truststore.path"),
8181

8282
/**
8383
* (String) Defines the password for the truststore file.
8484
*/
85-
TRUSTSTORE_PASSWORD("switcher.truststore.password", "truststorePassword"),
85+
TRUSTSTORE_PASSWORD("switcher.truststore.password"),
8686

8787
/**
8888
* (Number) Defines the timeout in ms for the Remote client.
8989
*/
90-
TIMEOUT_MS("switcher.timeout", "timeoutMs");
90+
TIMEOUT_MS("switcher.timeout"),
91+
92+
/**
93+
* (Number) Defines a fixed number of threads for the pool connection (default is 10).
94+
*/
95+
POOL_CONNECTION_SIZE("switcher.poolsize");
9196

9297
private final String param;
93-
private final String propField;
9498

95-
ContextKey(String param, String propField) {
99+
ContextKey(String param) {
96100
this.param = param;
97-
this.propField = propField;
98101
}
99102

100103
public String getParam() {
101104
return param;
102105
}
103-
104-
public String getPropField() {
105-
return propField;
106-
}
107106

108107
}

src/main/java/com/github/switcherapi/client/model/Switcher.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,6 @@ public GsonInputRequest getInputRequest() {
136136
this.entry.toArray(new Entry[0]) : null);
137137
}
138138

139-
public long getDelay() {
140-
return super.delay;
141-
}
142-
143139
public boolean isBypassMetrics() {
144140
return bypassMetrics;
145141
}

0 commit comments

Comments
 (0)