Skip to content

Commit b9473d5

Browse files
authored
Improved Switcher Context Base configuration options (#325)
* Improved Switcher Context Base configuration options (#324) * Improved Switcher Context Base configuration options * Fixes code smells * Added regexTimeout configuration
1 parent 1ecabc6 commit b9473d5

File tree

15 files changed

+386
-84
lines changed

15 files changed

+386
-84
lines changed

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,35 @@ MyAppFeatures.initializeClient();
101101
```
102102

103103
Or simply define a custom file properties to load everything from it.
104-
105104
```
106105
// Load from resources/switcherapi-test.properties
107106
MyAppFeatures.loadProperties("switcherapi-test");
108107
```
109108

109+
Or using configureClient() with @PostConstruct to handle all the configuration build boilerplate.
110+
111+
```java
112+
@ConfigurationProperties
113+
class MySwitcherClientConfig extends SwitcherContextBase {
114+
115+
@SwitcherKey
116+
public static final String MY_SWITCHER = "MY_SWITCHER";
117+
118+
@Override
119+
@PostConstruct
120+
public void configureClient() {
121+
// you can add pre-configuration here
122+
super.configureClient();
123+
// you can add post-configuration here
124+
}
125+
}
126+
```
127+
110128
### Defining your features
111129

130+
Create a class that extends SwitcherContext if you are loading the configuration from the switcherapi.properties file.
131+
Or use SwitcherContextBase to define the configuration using the ContextBuilder or SwitcherConfig.
132+
112133
```java
113134
public class MyAppFeatures extends SwitcherContext {
114135

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@
8484
<sonar.coverage.exclusions>
8585
**/model/**/*.java,
8686
**/exception/**/*.java,
87-
**/service/validators/RegexValidatorV8.java
87+
**/service/validators/RegexValidatorV8.java,
88+
**/client/remote/Constants.java
8889
</sonar.coverage.exclusions>
8990
</properties>
9091

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
import com.github.switcherapi.client.model.ContextKey;
44
import org.apache.commons.lang3.StringUtils;
55

6+
import java.util.Optional;
7+
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;
11+
612
public class ContextBuilder {
713

814
private static ContextBuilder context;
@@ -130,8 +136,9 @@ public ContextBuilder snapshotAutoUpdateInterval(String snapshotAutoUpdateInterv
130136
* @param regexTimeout Time in ms given to Timed Match Worker used for local Regex (ReDoS safety mechanism) - 3000 default value
131137
* @return ContextBuilder
132138
*/
133-
public ContextBuilder regexTimeout(int regexTimeout) {
134-
properties.setValue(ContextKey.REGEX_TIMEOUT, regexTimeout);
139+
public ContextBuilder regexTimeout(Integer regexTimeout) {
140+
properties.setValue(ContextKey.REGEX_TIMEOUT,
141+
Optional.ofNullable(regexTimeout).orElse(DEFAULT_REGEX_TIMEOUT));
135142
return this;
136143
}
137144

@@ -198,17 +205,19 @@ public ContextBuilder truststorePassword(String truststorePassword) {
198205
* @param timeoutMs Time in ms given to the API to respond - 3000 default value
199206
* @return ContextBuilder
200207
*/
201-
public ContextBuilder timeoutMs(int timeoutMs) {
202-
properties.setValue(ContextKey.TIMEOUT_MS, timeoutMs);
208+
public ContextBuilder timeoutMs(Integer timeoutMs) {
209+
properties.setValue(ContextKey.TIMEOUT_MS,
210+
Optional.ofNullable(timeoutMs).orElse(DEFAULT_TIMEOUT_MS));
203211
return this;
204212
}
205213

206214
/**
207215
* @param poolSize Number of threads for the pool connection - 10 default value
208216
* @return ContextBuilder
209217
*/
210-
public ContextBuilder poolConnectionSize(int poolSize) {
211-
properties.setValue(ContextKey.POOL_CONNECTION_SIZE, poolSize);
218+
public ContextBuilder poolConnectionSize(Integer poolSize) {
219+
properties.setValue(ContextKey.POOL_CONNECTION_SIZE,
220+
Optional.ofNullable(poolSize).orElse(DEFAULT_POOL_SIZE));
212221
return this;
213222
}
214223
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package com.github.switcherapi.client;
2+
3+
abstract class SwitcherConfig {
4+
5+
protected String contextLocation;
6+
protected String url;
7+
protected String apikey;
8+
protected String domain;
9+
protected String component;
10+
protected String environment;
11+
12+
protected boolean local;
13+
protected String silent;
14+
protected Integer timeout;
15+
protected Integer regexTimeout;
16+
protected Integer poolSize;
17+
protected SnapshotConfig snapshot;
18+
protected TruststoreConfig truststore;
19+
20+
SwitcherConfig() {
21+
this.snapshot = new SnapshotConfig();
22+
this.truststore = new TruststoreConfig();
23+
}
24+
25+
/**
26+
* Initialize the Switcher Client.<br>
27+
* - Build context {@link ContextBuilder}<br>
28+
* - Configure context {@link SwitcherContextBase#configure(ContextBuilder)}<br>
29+
* - Initialize client {@link SwitcherContextBase#initializeClient()}<br>
30+
*/
31+
protected abstract void configureClient();
32+
33+
public void setContextLocation(String contextLocation) {
34+
this.contextLocation = contextLocation;
35+
}
36+
37+
public void setUrl(String url) {
38+
this.url = url;
39+
}
40+
41+
public void setApikey(String apikey) {
42+
this.apikey = apikey;
43+
}
44+
45+
public void setDomain(String domain) {
46+
this.domain = domain;
47+
}
48+
49+
public void setComponent(String component) {
50+
this.component = component;
51+
}
52+
53+
public void setEnvironment(String environment) {
54+
this.environment = environment;
55+
}
56+
57+
public void setLocal(boolean local) {
58+
this.local = local;
59+
}
60+
61+
public void setSilent(String silent) {
62+
this.silent = silent;
63+
}
64+
65+
public void setTimeout(Integer timeout) {
66+
this.timeout = timeout;
67+
}
68+
69+
public void setRegexTimeout(Integer regexTimeout) {
70+
this.regexTimeout = regexTimeout;
71+
}
72+
73+
public void setPoolSize(Integer poolSize) {
74+
this.poolSize = poolSize;
75+
}
76+
77+
public void setSnapshot(SnapshotConfig snapshot) {
78+
this.snapshot = snapshot;
79+
}
80+
81+
public void setTruststore(TruststoreConfig truststore) {
82+
this.truststore = truststore;
83+
}
84+
85+
public static class SnapshotConfig {
86+
private String location;
87+
private boolean auto;
88+
private boolean skipValidation;
89+
private String updateInterval;
90+
91+
public String getLocation() {
92+
return location;
93+
}
94+
95+
public void setLocation(String location) {
96+
this.location = location;
97+
}
98+
99+
public boolean isAuto() {
100+
return auto;
101+
}
102+
103+
public void setAuto(boolean auto) {
104+
this.auto = auto;
105+
}
106+
107+
public boolean isSkipValidation() {
108+
return skipValidation;
109+
}
110+
111+
public void setSkipValidation(boolean skipValidation) {
112+
this.skipValidation = skipValidation;
113+
}
114+
115+
public String getUpdateInterval() {
116+
return updateInterval;
117+
}
118+
119+
public void setUpdateInterval(String updateInterval) {
120+
this.updateInterval = updateInterval;
121+
}
122+
}
123+
124+
public static class TruststoreConfig {
125+
private String path;
126+
private String password;
127+
128+
public String getPath() {
129+
return path;
130+
}
131+
132+
public void setPath(String path) {
133+
this.path = path;
134+
}
135+
136+
public String getPassword() {
137+
return password;
138+
}
139+
140+
public void setPassword(String password) {
141+
this.password = password;
142+
}
143+
}
144+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import com.github.switcherapi.client.model.Switcher;
77

88
/**
9-
* <b>Switcher Context Toolkit</b>
9+
* <b>Switcher Context</b>
1010
* <p>
1111
*
1212
* This class will load Switcher Properties internally, making it ready to use.

0 commit comments

Comments
 (0)