Skip to content

Commit 80e7127

Browse files
authored
Replaced reflection-based impl for validators, impproved SwitcherProperties (#318)
* Replaced reflection-based impl for validators, impproved SwitcherProperties (#317) * Replaced reflection-based impl for validators, impproved SwitcherProperties * chore: replaced with primitive * Updated RegexValidatorV8
1 parent dae2916 commit 80e7127

21 files changed

+172
-329
lines changed

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<groupId>com.github.switcherapi</groupId>
99
<artifactId>switcher-client</artifactId>
1010
<packaging>jar</packaging>
11-
<version>1.6.0</version>
11+
<version>1.6.1-SNAPSHOT</version>
1212

1313
<name>Switcher Client</name>
1414
<description>Switcher Client SDK for working with Switcher API</description>
@@ -49,6 +49,7 @@
4949
<java.version>1.8</java.version>
5050
<maven.compiler.source>${java.version}</maven.compiler.source>
5151
<maven.compiler.target>${java.version}</maven.compiler.target>
52+
<argLine />
5253

5354
<!-- rest/json libs -->
5455
<jersey-client.version>2.45</jersey-client.version>

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

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.switcherapi.client;
22

3+
import com.github.switcherapi.client.model.ContextKey;
34
import org.apache.commons.lang3.StringUtils;
45

56
public class ContextBuilder {
@@ -52,7 +53,7 @@ SwitcherProperties build() {
5253
* @return ContextBuilder
5354
*/
5455
public ContextBuilder contextLocation(String contextLocation) {
55-
properties.setContextLocation(contextLocation);
56+
properties.setValue(ContextKey.CONTEXT_LOCATION, contextLocation);
5657
return this;
5758
}
5859

@@ -61,7 +62,7 @@ public ContextBuilder contextLocation(String contextLocation) {
6162
* @return ContextBuilder
6263
*/
6364
public ContextBuilder url(String url) {
64-
properties.setUrl(url);
65+
properties.setValue(ContextKey.URL, url);
6566
return this;
6667
}
6768

@@ -70,7 +71,7 @@ public ContextBuilder url(String url) {
7071
* @return ContextBuilder
7172
*/
7273
public ContextBuilder apiKey(String apiKey) {
73-
properties.setApiKey(apiKey);
74+
properties.setValue(ContextKey.APIKEY, apiKey);
7475
return this;
7576
}
7677

@@ -79,7 +80,7 @@ public ContextBuilder apiKey(String apiKey) {
7980
* @return ContextBuilder
8081
*/
8182
public ContextBuilder domain(String domain) {
82-
properties.setDomain(domain);
83+
properties.setValue(ContextKey.DOMAIN, domain);
8384
return this;
8485
}
8586

@@ -88,7 +89,7 @@ public ContextBuilder domain(String domain) {
8889
* @return ContextBuilder
8990
*/
9091
public ContextBuilder component(String component) {
91-
properties.setComponent(component);
92+
properties.setValue(ContextKey.COMPONENT, component);
9293
return this;
9394
}
9495

@@ -97,7 +98,7 @@ public ContextBuilder component(String component) {
9798
* @return ContextBuilder
9899
*/
99100
public ContextBuilder environment(String environment) {
100-
properties.setEnvironment(environment);
101+
properties.setValue(ContextKey.ENVIRONMENT, properties.getEnvironmentOrDefault(environment));
101102
return this;
102103
}
103104

@@ -106,7 +107,7 @@ public ContextBuilder environment(String environment) {
106107
* @return ContextBuilder
107108
*/
108109
public ContextBuilder snapshotLocation(String snapshotLocation) {
109-
properties.setSnapshotLocation(snapshotLocation);
110+
properties.setValue(ContextKey.SNAPSHOT_LOCATION, snapshotLocation);
110111
return this;
111112
}
112113

@@ -115,10 +116,10 @@ public ContextBuilder snapshotLocation(String snapshotLocation) {
115116
* @return ContextBuilder
116117
*/
117118
public ContextBuilder snapshotAutoUpdateInterval(String snapshotAutoUpdateInterval) {
118-
properties.setSnapshotAutoUpdateInterval(snapshotAutoUpdateInterval);
119+
properties.setValue(ContextKey.SNAPSHOT_AUTO_UPDATE_INTERVAL, snapshotAutoUpdateInterval);
119120

120121
if (snapshotAutoUpdateInterval != null)
121-
properties.setSnapshotAutoLoad(true);
122+
properties.setValue(ContextKey.SNAPSHOT_AUTO_LOAD, true);
122123

123124
return this;
124125
}
@@ -130,7 +131,7 @@ public ContextBuilder snapshotAutoUpdateInterval(String snapshotAutoUpdateInterv
130131
* @return ContextBuilder
131132
*/
132133
public ContextBuilder regexTimeout(String regexTimeout) {
133-
properties.setRegexTimeout(regexTimeout);
134+
properties.setValue(ContextKey.REGEX_TIMEOUT, properties.getRegexTimeoutOrDefault(regexTimeout));
134135
return this;
135136
}
136137

@@ -139,7 +140,7 @@ public ContextBuilder regexTimeout(String regexTimeout) {
139140
* @return ContextBuilder
140141
*/
141142
public ContextBuilder snapshotAutoLoad(boolean snapshotAutoLoad) {
142-
properties.setSnapshotAutoLoad(snapshotAutoLoad);
143+
properties.setValue(ContextKey.SNAPSHOT_AUTO_LOAD, snapshotAutoLoad);
143144
return this;
144145
}
145146

@@ -148,7 +149,7 @@ public ContextBuilder snapshotAutoLoad(boolean snapshotAutoLoad) {
148149
* @return ContextBuilder
149150
*/
150151
public ContextBuilder snapshotSkipValidation(boolean snapshotSkipValidation) {
151-
properties.setSnapshotSkipValidation(snapshotSkipValidation);
152+
properties.setValue(ContextKey.SNAPSHOT_SKIP_VALIDATION, snapshotSkipValidation);
152153
return this;
153154
}
154155

@@ -157,10 +158,10 @@ public ContextBuilder snapshotSkipValidation(boolean snapshotSkipValidation) {
157158
* @return ContextBuilder
158159
*/
159160
public ContextBuilder silentMode(String retryAfter) {
160-
properties.setSilentMode(retryAfter);
161+
properties.setValue(ContextKey.SILENT_MODE, retryAfter);
161162

162163
if (StringUtils.isNotBlank(retryAfter)) {
163-
properties.setSnapshotAutoLoad(true);
164+
properties.setValue(ContextKey.SNAPSHOT_AUTO_LOAD, true);
164165
}
165166

166167
return this;
@@ -171,7 +172,7 @@ public ContextBuilder silentMode(String retryAfter) {
171172
* @return ContextBuilder
172173
*/
173174
public ContextBuilder local(boolean local) {
174-
properties.setLocal(local);
175+
properties.setValue(ContextKey.LOCAL_MODE, local);
175176
return this;
176177
}
177178

@@ -180,7 +181,7 @@ public ContextBuilder local(boolean local) {
180181
* @return ContextBuilder
181182
*/
182183
public ContextBuilder truststorePath(String truststorePath) {
183-
properties.setTruststorePath(truststorePath);
184+
properties.setValue(ContextKey.TRUSTSTORE_PATH, truststorePath);
184185
return this;
185186
}
186187

@@ -189,7 +190,7 @@ public ContextBuilder truststorePath(String truststorePath) {
189190
* @return ContextBuilder
190191
*/
191192
public ContextBuilder truststorePassword(String truststorePassword) {
192-
properties.setTruststorePassword(truststorePassword);
193+
properties.setValue(ContextKey.TRUSTSTORE_PASSWORD, truststorePassword);
193194
return this;
194195
}
195196

@@ -198,7 +199,7 @@ public ContextBuilder truststorePassword(String truststorePassword) {
198199
* @return ContextBuilder
199200
*/
200201
public ContextBuilder timeoutMs(String timeoutMs) {
201-
properties.setTimeoutMs(timeoutMs);
202+
properties.setValue(ContextKey.TIMEOUT_MS, properties.getTimeoutMsOrDefault(timeoutMs));
202203
return this;
203204
}
204205
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,14 @@ public static void initializeClient() {
113113
validateContext();
114114
validateSwitcherKeys();
115115

116-
if (switcherProperties.isLocal()) {
116+
if (contextBol(ContextKey.LOCAL_MODE)) {
117117
instance = new SwitcherLocalService();
118118
} else {
119119
instance = new SwitcherRemoteService();
120120
}
121121

122122
loadSwitchers();
123-
scheduleSnapshotAutoUpdate(switcherProperties.getSnapshotAutoUpdateInterval());
123+
scheduleSnapshotAutoUpdate(contextStr(ContextKey.SNAPSHOT_AUTO_UPDATE_INTERVAL));
124124
ContextBuilder.preConfigure(switcherProperties);
125125
}
126126

@@ -143,7 +143,7 @@ private static void validateSwitcherKeys() {
143143
try {
144144
switcherKeys = new HashSet<>();
145145

146-
final Class<?> clazz = Class.forName(switcherProperties.getContextLocation());
146+
final Class<?> clazz = Class.forName(contextStr(ContextKey.CONTEXT_LOCATION));
147147
for (Field field : clazz.getFields()) {
148148
if (field.isAnnotationPresent(SwitcherKey.class)) {
149149
switcherKeys.add(field.getName());
@@ -275,7 +275,7 @@ public static Switcher getSwitcher(String key) {
275275
* @return true if snapshot was updated
276276
*/
277277
public static boolean validateSnapshot() {
278-
if (switcherProperties.isSnapshotSkipValidation() || instance.checkSnapshotVersion()) {
278+
if (contextBol(ContextKey.SNAPSHOT_SKIP_VALIDATION) || instance.checkSnapshotVersion()) {
279279
return false;
280280
}
281281

@@ -347,7 +347,7 @@ public static void checkSwitchers() {
347347
* @return Value configured for the context parameter
348348
*/
349349
public static String contextStr(ContextKey contextKey) {
350-
return switcherProperties.getValue(contextKey, String.class);
350+
return switcherProperties.getValue(contextKey);
351351
}
352352

353353
/**
@@ -357,7 +357,7 @@ public static String contextStr(ContextKey contextKey) {
357357
* @return Value configured for the context parameter
358358
*/
359359
public static boolean contextBol(ContextKey contextKey) {
360-
return switcherProperties.getValue(contextKey, Boolean.class);
360+
return switcherProperties.getBoolean(contextKey);
361361
}
362362

363363
/**

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ private SwitcherContextValidator() {}
2828
* @throws SwitcherContextException if validation fails
2929
*/
3030
public static void validate(final SwitcherProperties prop) {
31-
if (StringUtils.isBlank(prop.getContextLocation())) {
31+
if (StringUtils.isBlank(prop.getValue(ContextKey.CONTEXT_LOCATION))) {
3232
throw new SwitcherContextException(ERR_CONTEXT);
3333
}
3434

35-
if (!prop.isLocal()) {
35+
if (!prop.getBoolean(ContextKey.LOCAL_MODE)) {
3636
validateRemote(prop);
3737
}
3838

@@ -46,7 +46,7 @@ public static void validate(final SwitcherProperties prop) {
4646
*/
4747
public static void validateOptionals(final SwitcherProperties prop) {
4848
try {
49-
Integer.parseInt(prop.getRegexTimeout());
49+
Integer.parseInt(prop.getValue(ContextKey.REGEX_TIMEOUT));
5050
} catch (NumberFormatException e) {
5151
throw new SwitcherContextException(
5252
String.format(ERR_FORMAT, ContextKey.REGEX_TIMEOUT.getParam(), Integer.class));
@@ -59,19 +59,19 @@ public static void validateOptionals(final SwitcherProperties prop) {
5959
* @param prop Configured properties
6060
*/
6161
public static void validateRemote(final SwitcherProperties prop) {
62-
if (StringUtils.isBlank(prop.getUrl())) {
62+
if (StringUtils.isBlank(prop.getValue(ContextKey.URL))) {
6363
throw new SwitcherContextException(ERR_URL);
6464
}
6565

66-
if (StringUtils.isBlank(prop.getApiKey())) {
66+
if (StringUtils.isBlank(prop.getValue(ContextKey.APIKEY))) {
6767
throw new SwitcherContextException(ERR_API);
6868
}
6969

70-
if (StringUtils.isBlank(prop.getDomain())) {
70+
if (StringUtils.isBlank(prop.getValue(ContextKey.DOMAIN))) {
7171
throw new SwitcherContextException(ERR_DOMAIN);
7272
}
7373

74-
if (StringUtils.isBlank(prop.getComponent())) {
74+
if (StringUtils.isBlank(prop.getValue(ContextKey.COMPONENT))) {
7575
throw new SwitcherContextException(ERR_COMPONENT);
7676
}
7777
}

0 commit comments

Comments
 (0)