Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<groupId>com.github.switcherapi</groupId>
<artifactId>switcher-client</artifactId>
<packaging>jar</packaging>
<version>2.2.0</version>
<version>2.2.1-SNAPSHOT</version>

<name>Switcher Client</name>
<description>Switcher Client SDK for working with Switcher API</description>
Expand Down
37 changes: 19 additions & 18 deletions src/main/java/com/github/switcherapi/client/ContextBuilder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.switcherapi.client;

import com.github.switcherapi.client.model.ContextKey;
import org.apache.commons.lang3.StringUtils;

public class ContextBuilder {
Expand Down Expand Up @@ -52,7 +53,7 @@ SwitcherProperties build() {
* @return ContextBuilder
*/
public ContextBuilder contextLocation(String contextLocation) {
properties.setContextLocation(contextLocation);
properties.setValue(ContextKey.CONTEXT_LOCATION, contextLocation);
return this;
}

Expand All @@ -61,7 +62,7 @@ public ContextBuilder contextLocation(String contextLocation) {
* @return ContextBuilder
*/
public ContextBuilder url(String url) {
properties.setUrl(url);
properties.setValue(ContextKey.URL, url);
return this;
}

Expand All @@ -70,7 +71,7 @@ public ContextBuilder url(String url) {
* @return ContextBuilder
*/
public ContextBuilder apiKey(String apiKey) {
properties.setApiKey(apiKey);
properties.setValue(ContextKey.APIKEY, apiKey);
return this;
}

Expand All @@ -79,7 +80,7 @@ public ContextBuilder apiKey(String apiKey) {
* @return ContextBuilder
*/
public ContextBuilder domain(String domain) {
properties.setDomain(domain);
properties.setValue(ContextKey.DOMAIN, domain);
return this;
}

Expand All @@ -88,7 +89,7 @@ public ContextBuilder domain(String domain) {
* @return ContextBuilder
*/
public ContextBuilder component(String component) {
properties.setComponent(component);
properties.setValue(ContextKey.COMPONENT, component);
return this;
}

Expand All @@ -97,7 +98,7 @@ public ContextBuilder component(String component) {
* @return ContextBuilder
*/
public ContextBuilder environment(String environment) {
properties.setEnvironment(environment);
properties.setValue(ContextKey.ENVIRONMENT, properties.getEnvironmentOrDefault(environment));
return this;
}

Expand All @@ -106,7 +107,7 @@ public ContextBuilder environment(String environment) {
* @return ContextBuilder
*/
public ContextBuilder snapshotLocation(String snapshotLocation) {
properties.setSnapshotLocation(snapshotLocation);
properties.setValue(ContextKey.SNAPSHOT_LOCATION, snapshotLocation);
return this;
}

Expand All @@ -115,10 +116,10 @@ public ContextBuilder snapshotLocation(String snapshotLocation) {
* @return ContextBuilder
*/
public ContextBuilder snapshotAutoUpdateInterval(String snapshotAutoUpdateInterval) {
properties.setSnapshotAutoUpdateInterval(snapshotAutoUpdateInterval);
properties.setValue(ContextKey.SNAPSHOT_AUTO_UPDATE_INTERVAL, snapshotAutoUpdateInterval);

if (snapshotAutoUpdateInterval != null)
properties.setSnapshotAutoLoad(true);
properties.setValue(ContextKey.SNAPSHOT_AUTO_LOAD, true);

return this;
}
Expand All @@ -130,7 +131,7 @@ public ContextBuilder snapshotAutoUpdateInterval(String snapshotAutoUpdateInterv
* @return ContextBuilder
*/
public ContextBuilder regexTimeout(String regexTimeout) {
properties.setRegexTimeout(regexTimeout);
properties.setValue(ContextKey.REGEX_TIMEOUT, properties.getRegexTimeoutOrDefault(regexTimeout));
return this;
}

Expand All @@ -139,7 +140,7 @@ public ContextBuilder regexTimeout(String regexTimeout) {
* @return ContextBuilder
*/
public ContextBuilder snapshotAutoLoad(boolean snapshotAutoLoad) {
properties.setSnapshotAutoLoad(snapshotAutoLoad);
properties.setValue(ContextKey.SNAPSHOT_AUTO_LOAD, snapshotAutoLoad);
return this;
}

Expand All @@ -148,7 +149,7 @@ public ContextBuilder snapshotAutoLoad(boolean snapshotAutoLoad) {
* @return ContextBuilder
*/
public ContextBuilder snapshotSkipValidation(boolean snapshotSkipValidation) {
properties.setSnapshotSkipValidation(snapshotSkipValidation);
properties.setValue(ContextKey.SNAPSHOT_SKIP_VALIDATION, snapshotSkipValidation);
return this;
}

Expand All @@ -157,10 +158,10 @@ public ContextBuilder snapshotSkipValidation(boolean snapshotSkipValidation) {
* @return ContextBuilder
*/
public ContextBuilder silentMode(String retryAfter) {
properties.setSilentMode(retryAfter);
properties.setValue(ContextKey.SILENT_MODE, retryAfter);

if (StringUtils.isNotBlank(retryAfter)) {
properties.setSnapshotAutoLoad(true);
properties.setValue(ContextKey.SNAPSHOT_AUTO_LOAD, true);
}

return this;
Expand All @@ -171,7 +172,7 @@ public ContextBuilder silentMode(String retryAfter) {
* @return ContextBuilder
*/
public ContextBuilder local(boolean local) {
properties.setLocal(local);
properties.setValue(ContextKey.LOCAL_MODE, local);
return this;
}

Expand All @@ -180,7 +181,7 @@ public ContextBuilder local(boolean local) {
* @return ContextBuilder
*/
public ContextBuilder truststorePath(String truststorePath) {
properties.setTruststorePath(truststorePath);
properties.setValue(ContextKey.TRUSTSTORE_PATH, truststorePath);
return this;
}

Expand All @@ -189,7 +190,7 @@ public ContextBuilder truststorePath(String truststorePath) {
* @return ContextBuilder
*/
public ContextBuilder truststorePassword(String truststorePassword) {
properties.setTruststorePassword(truststorePassword);
properties.setValue(ContextKey.TRUSTSTORE_PASSWORD, truststorePassword);
return this;
}

Expand All @@ -198,7 +199,7 @@ public ContextBuilder truststorePassword(String truststorePassword) {
* @return ContextBuilder
*/
public ContextBuilder timeoutMs(String timeoutMs) {
properties.setTimeoutMs(timeoutMs);
properties.setValue(ContextKey.TIMEOUT_MS, properties.getTimeoutMsOrDefault(timeoutMs));
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,14 @@ public static void initializeClient() {
validateContext();
validateSwitcherKeys();

if (switcherProperties.isLocal()) {
if (contextBol(ContextKey.LOCAL_MODE)) {
instance = new SwitcherLocalService();
} else {
instance = new SwitcherRemoteService();
}

loadSwitchers();
scheduleSnapshotAutoUpdate(switcherProperties.getSnapshotAutoUpdateInterval());
scheduleSnapshotAutoUpdate(contextStr(ContextKey.SNAPSHOT_AUTO_UPDATE_INTERVAL));
ContextBuilder.preConfigure(switcherProperties);
}

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

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

Expand Down Expand Up @@ -347,7 +347,7 @@ public static void checkSwitchers() {
* @return Value configured for the context parameter
*/
public static String contextStr(ContextKey contextKey) {
return switcherProperties.getValue(contextKey, String.class);
return switcherProperties.getValue(contextKey);
}

/**
Expand All @@ -357,7 +357,7 @@ public static String contextStr(ContextKey contextKey) {
* @return Value configured for the context parameter
*/
public static boolean contextBol(ContextKey contextKey) {
return switcherProperties.getValue(contextKey, Boolean.class);
return switcherProperties.getBoolean(contextKey);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ private SwitcherContextValidator() {}
* @throws SwitcherContextException if validation fails
*/
public static void validate(final SwitcherProperties prop) {
if (StringUtils.isBlank(prop.getContextLocation())) {
if (StringUtils.isBlank(prop.getValue(ContextKey.CONTEXT_LOCATION))) {
throw new SwitcherContextException(ERR_CONTEXT);
}

if (!prop.isLocal()) {
if (!prop.getBoolean(ContextKey.LOCAL_MODE)) {
validateRemote(prop);
}

Expand All @@ -46,7 +46,7 @@ public static void validate(final SwitcherProperties prop) {
*/
public static void validateOptionals(final SwitcherProperties prop) {
try {
Integer.parseInt(prop.getRegexTimeout());
Integer.parseInt(prop.getValue(ContextKey.REGEX_TIMEOUT));
} catch (NumberFormatException e) {
throw new SwitcherContextException(
String.format(ERR_FORMAT, ContextKey.REGEX_TIMEOUT.getParam(), Integer.class));
Expand All @@ -59,19 +59,19 @@ public static void validateOptionals(final SwitcherProperties prop) {
* @param prop Configured properties
*/
public static void validateRemote(final SwitcherProperties prop) {
if (StringUtils.isBlank(prop.getUrl())) {
if (StringUtils.isBlank(prop.getValue(ContextKey.URL))) {
throw new SwitcherContextException(ERR_URL);
}

if (StringUtils.isBlank(prop.getApiKey())) {
if (StringUtils.isBlank(prop.getValue(ContextKey.APIKEY))) {
throw new SwitcherContextException(ERR_API);
}

if (StringUtils.isBlank(prop.getDomain())) {
if (StringUtils.isBlank(prop.getValue(ContextKey.DOMAIN))) {
throw new SwitcherContextException(ERR_DOMAIN);
}

if (StringUtils.isBlank(prop.getComponent())) {
if (StringUtils.isBlank(prop.getValue(ContextKey.COMPONENT))) {
throw new SwitcherContextException(ERR_COMPONENT);
}
}
Expand Down
Loading