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
67 changes: 28 additions & 39 deletions src/main/java/com/github/switcherapi/client/ContextBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@

import java.util.Optional;

import static com.github.switcherapi.client.SwitcherProperties.DEFAULT_TIMEOUT_MS;
import static com.github.switcherapi.client.remote.Constants.DEFAULT_POOL_SIZE;
import static com.github.switcherapi.client.remote.Constants.*;

public class ContextBuilder {

private static ContextBuilder context;

private SwitcherProperties properties;
private SwitcherProperties switcherProperties;

private ContextBuilder() {
properties = new SwitcherProperties();
private ContextBuilder(SwitcherProperties switcherProperties) {
this.switcherProperties = switcherProperties;
}

public static void preConfigure(SwitcherProperties switcherProperties) {
Expand All @@ -40,25 +39,25 @@ public static ContextBuilder builder() {
*/
public static ContextBuilder builder(boolean init) {
if (context == null || init)
context = new ContextBuilder();
context = new ContextBuilder(new SwitcherPropertiesImpl());

return context;
}

void preBuild(SwitcherProperties properties) {
this.properties = properties;
this.switcherProperties = properties;
}

SwitcherProperties build() {
return this.properties;
return this.switcherProperties;
}

/**
* @param contextLocation Feature class that extends SwitcherContext
* @return ContextBuilder
*/
public ContextBuilder contextLocation(String contextLocation) {
properties.setValue(ContextKey.CONTEXT_LOCATION, contextLocation);
switcherProperties.setValue(ContextKey.CONTEXT_LOCATION, contextLocation);
return this;
}

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

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

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

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

Expand All @@ -103,7 +102,8 @@ public ContextBuilder component(String component) {
* @return ContextBuilder
*/
public ContextBuilder environment(String environment) {
properties.setValue(ContextKey.ENVIRONMENT, properties.getValueDefault(environment, SwitcherProperties.DEFAULT_ENV));
switcherProperties.setValue(ContextKey.ENVIRONMENT,
Optional.ofNullable(environment).orElse(DEFAULT_ENV));
return this;
}

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

Expand All @@ -121,22 +121,11 @@ public ContextBuilder snapshotLocation(String snapshotLocation) {
* @return ContextBuilder
*/
public ContextBuilder snapshotAutoUpdateInterval(String snapshotAutoUpdateInterval) {
properties.setValue(ContextKey.SNAPSHOT_AUTO_UPDATE_INTERVAL, snapshotAutoUpdateInterval);
switcherProperties.setValue(ContextKey.SNAPSHOT_AUTO_UPDATE_INTERVAL, snapshotAutoUpdateInterval);

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

return this;
}
switcherProperties.setValue(ContextKey.SNAPSHOT_AUTO_LOAD, true);

/**
* Java 8 only
*
* @param regexTimeout Time in ms given to Timed Match Worker used for local Regex (ReDoS safety mechanism) - 3000 default value
* @return ContextBuilder
*/
public ContextBuilder regexTimeout(int regexTimeout) {
properties.setValue(ContextKey.REGEX_TIMEOUT, regexTimeout);
return this;
}

Expand All @@ -145,7 +134,7 @@ public ContextBuilder regexTimeout(int regexTimeout) {
* @return ContextBuilder
*/
public ContextBuilder snapshotAutoLoad(boolean snapshotAutoLoad) {
properties.setValue(ContextKey.SNAPSHOT_AUTO_LOAD, snapshotAutoLoad);
switcherProperties.setValue(ContextKey.SNAPSHOT_AUTO_LOAD, snapshotAutoLoad);
return this;
}

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

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

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

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

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

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

Expand All @@ -204,8 +193,8 @@ public ContextBuilder truststorePassword(String truststorePassword) {
* @return ContextBuilder
*/
public ContextBuilder timeoutMs(Integer timeoutMs) {
properties.setValue(ContextKey.TIMEOUT_MS,
Optional.ofNullable(timeoutMs).orElse(DEFAULT_TIMEOUT_MS));
switcherProperties.setValue(ContextKey.TIMEOUT_MS,
Optional.ofNullable(timeoutMs).orElse(DEFAULT_TIMEOUT));
return this;
}

Expand All @@ -214,7 +203,7 @@ public ContextBuilder timeoutMs(Integer timeoutMs) {
* @return ContextBuilder
*/
public ContextBuilder poolConnectionSize(Integer poolSize) {
properties.setValue(ContextKey.POOL_CONNECTION_SIZE,
switcherProperties.setValue(ContextKey.POOL_CONNECTION_SIZE,
Optional.ofNullable(poolSize).orElse(DEFAULT_POOL_SIZE));
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public abstract class SwitcherContextBase extends SwitcherConfig {
private static SnapshotWatcher watcherSnapshot;

static {
switcherProperties = new SwitcherProperties();
switcherProperties = new SwitcherPropertiesImpl();
}

@Override
Expand Down Expand Up @@ -175,13 +175,13 @@ public static void initializeClient() {
private static SwitcherExecutor buildInstance() {
final ClientWS clientWS = initRemotePoolExecutorService();
final SwitcherValidator validatorService = new ValidatorService();
final ClientRemote clientRemote = new ClientRemoteService(clientWS);
final ClientRemote clientRemote = new ClientRemoteService(clientWS, switcherProperties);
final ClientLocal clientLocal = new ClientLocalService(validatorService);

if (contextBol(ContextKey.LOCAL_MODE)) {
return new SwitcherLocalService(clientRemote, clientLocal);
return new SwitcherLocalService(clientRemote, clientLocal, switcherProperties);
} else {
return new SwitcherRemoteService(clientRemote, new SwitcherLocalService(clientRemote, clientLocal));
return new SwitcherRemoteService(clientRemote, new SwitcherLocalService(clientRemote, clientLocal, switcherProperties));
}
}

Expand Down Expand Up @@ -309,7 +309,7 @@ private static ClientWS initRemotePoolExecutorService() {
return thread;
});

return ClientWSImpl.build(remotePoolExecutorService, timeoutMs);
return ClientWSImpl.build(switcherProperties, remotePoolExecutorService, timeoutMs);
}

/**
Expand Down Expand Up @@ -447,6 +447,15 @@ public static Integer contextInt(ContextKey contextKey) {
public static boolean contextBol(ContextKey contextKey) {
return switcherProperties.getBoolean(contextKey);
}

/**
* Retrieve the Switcher Properties
*
* @return SwitcherProperties instance
*/
public static SwitcherProperties getSwitcherProperties() {
return switcherProperties;
}

/**
* Fluent builder to configure the Switcher Context
Expand Down
18 changes: 14 additions & 4 deletions src/main/java/com/github/switcherapi/client/SwitcherExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ public abstract class SwitcherExecutor {

private static final Map<String, CriteriaResponse> bypass = new HashMap<>();

protected final SwitcherProperties switcherProperties;

protected Domain domain;

protected SwitcherExecutor(final SwitcherProperties switcherProperties) {
this.switcherProperties = switcherProperties;
}

/**
* Execute criteria based on the Switcher configuration
Expand Down Expand Up @@ -69,19 +75,19 @@ public abstract class SwitcherExecutor {
public abstract long getSnapshotVersion();

protected boolean checkSnapshotVersion(ClientRemote clientRemote, final Domain domain) {
final String environment = SwitcherContextBase.contextStr(ContextKey.ENVIRONMENT);
final String environment = switcherProperties.getValue(ContextKey.ENVIRONMENT);
SwitcherUtils.debug(logger, "verifying snapshot version - environment: {}", environment);

return clientRemote.checkSnapshotVersion(domain.getVersion());
}

protected Domain initializeSnapshotFromAPI(ClientRemote clientRemote)
throws SwitcherRemoteException, SwitcherSnapshotWriteException {
final String environment = SwitcherContextBase.contextStr(ContextKey.ENVIRONMENT);
final String environment = switcherProperties.getValue(ContextKey.ENVIRONMENT);
SwitcherUtils.debug(logger, "initializing snapshot from API - environment: {}", environment);

final Snapshot snapshot = clientRemote.resolveSnapshot();
final String snapshotLocation = SwitcherContextBase.contextStr(ContextKey.SNAPSHOT_LOCATION);
final String snapshotLocation = switcherProperties.getValue(ContextKey.SNAPSHOT_LOCATION);

if (snapshotLocation != null) {
SnapshotLoader.saveSnapshot(snapshot, snapshotLocation, environment);
Expand All @@ -91,7 +97,7 @@ protected Domain initializeSnapshotFromAPI(ClientRemote clientRemote)
}

public boolean isLocalEnabled() {
return SwitcherContextBase.contextBol(ContextKey.LOCAL_MODE);
return switcherProperties.getBoolean(ContextKey.LOCAL_MODE);
}

/**
Expand Down Expand Up @@ -140,6 +146,10 @@ public static Map<String, CriteriaResponse> getBypass() {
return bypass;
}

public SwitcherProperties getSwitcherProperties() {
return switcherProperties;
}

public Domain getDomain() {
return domain;
}
Expand Down
Loading