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
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,13 @@ Switcher switcher = MyAppFeatures.getSwitcher(FEATURE01);
switcher.isItOn();
```

Or, you can submit the switcher request and get the criteria response, which contains result, reason and metadata that can be used for any additional verification.
Or, you can submit the switcher request and get the switcher result, which contains result, reason and metadata that can be used for any additional verification.

```java
CriteriaResponse response = switcher.submit();
response.isItOn(); // true/false
response.getReason(); // Descriptive response based on result value
response.getMetadata(YourMetadata.class); // Additional information
SwitcherResult result = switcher.submit();
result.isItOn(); // true/false
result.getReason(); // Descriptive response based on result value
result.getMetadata(YourMetadata.class); // Additional information
```

2. **Strategy validation - preparing input**
Expand Down
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@
<sonar.language>java</sonar.language>
<sonar.coverage.exclusions>
**/model/**/*.java,
**/remote/dto/*.java,
**/utils/Utils.java,
**/exception/**/*.java,
**/service/validators/RegexValidatorV8.java,
**/client/remote/Constants.java
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/com/github/switcherapi/client/SwitcherConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.switcherapi.client;

import com.github.switcherapi.client.model.ContextKey;

abstract class SwitcherConfig {

protected String url;
Expand All @@ -21,6 +23,35 @@ abstract class SwitcherConfig {
this.truststore = new TruststoreConfig();
}

/**
* Update Switcher Configurations state using pre-configured properties.
*
* @param properties Switcher Properties
*/
protected void updateSwitcherConfig(SwitcherProperties properties) {
setUrl(properties.getValue(ContextKey.URL));
setApikey(properties.getValue(ContextKey.APIKEY));
setDomain(properties.getValue(ContextKey.DOMAIN));
setComponent(properties.getValue(ContextKey.COMPONENT));
setEnvironment(properties.getValue(ContextKey.ENVIRONMENT));
setLocal(properties.getBoolean(ContextKey.LOCAL_MODE));
setSilent(properties.getValue(ContextKey.SILENT_MODE));
setTimeout(properties.getInt(ContextKey.TIMEOUT_MS));
setPoolSize(properties.getInt(ContextKey.POOL_CONNECTION_SIZE));

SnapshotConfig snapshotConfig = new SnapshotConfig();
snapshotConfig.setLocation(properties.getValue(ContextKey.SNAPSHOT_LOCATION));
snapshotConfig.setAuto(properties.getBoolean(ContextKey.SNAPSHOT_AUTO_LOAD));
snapshotConfig.setSkipValidation(properties.getBoolean(ContextKey.SNAPSHOT_SKIP_VALIDATION));
snapshotConfig.setUpdateInterval(properties.getValue(ContextKey.SNAPSHOT_AUTO_UPDATE_INTERVAL));
setSnapshot(snapshotConfig);

TruststoreConfig truststoreConfig = new TruststoreConfig();
truststoreConfig.setPath(properties.getValue(ContextKey.TRUSTSTORE_PATH));
truststoreConfig.setPassword(properties.getValue(ContextKey.TRUSTSTORE_PASSWORD));
setTruststore(truststoreConfig);
}

/**
* Initialize the Switcher Client.<br>
* - Build context {@link ContextBuilder}<br>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.github.switcherapi.client.exception.SwitcherKeyNotFoundException;
import com.github.switcherapi.client.exception.SwitchersValidationException;
import com.github.switcherapi.client.model.ContextKey;
import com.github.switcherapi.client.model.Switcher;
import com.github.switcherapi.client.model.SwitcherRequest;

/**
* <b>Switcher Context</b>
Expand Down Expand Up @@ -54,7 +54,7 @@ public static void initializeClient() {
* @return a ready to use Switcher
* @throws SwitcherKeyNotFoundException in case the key was not properly loaded
*/
public static Switcher getSwitcher(String key, boolean keepEntries) {
public static SwitcherRequest getSwitcher(String key, boolean keepEntries) {
return SwitcherContextBase.getSwitcher(key, keepEntries);
}

Expand All @@ -64,7 +64,7 @@ public static Switcher getSwitcher(String key, boolean keepEntries) {
* @param key name
* @return a ready to use Switcher
*/
public static Switcher getSwitcher(String key) {
public static SwitcherRequest getSwitcher(String key) {
return SwitcherContextBase.getSwitcher(key);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.github.switcherapi.client.exception.SwitcherKeyNotFoundException;
import com.github.switcherapi.client.exception.SwitchersValidationException;
import com.github.switcherapi.client.model.ContextKey;
import com.github.switcherapi.client.model.Switcher;
import com.github.switcherapi.client.model.SwitcherRequest;
import com.github.switcherapi.client.remote.ClientWS;
import com.github.switcherapi.client.remote.ClientWSImpl;
import com.github.switcherapi.client.service.SwitcherValidator;
Expand Down Expand Up @@ -73,7 +73,6 @@
*
* Features.initializeClient();
* }
*
* </pre>
*
* @see SwitcherKey
Expand All @@ -86,7 +85,7 @@ public abstract class SwitcherContextBase extends SwitcherConfig {

protected static SwitcherProperties switcherProperties;
protected static Set<String> switcherKeys;
protected static Map<String, Switcher> switchers;
protected static Map<String, SwitcherRequest> switchers;
protected static SwitcherExecutor instance;
private static ScheduledExecutorService scheduledExecutorService;
private static ExecutorService watcherExecutorService;
Expand Down Expand Up @@ -119,21 +118,28 @@ protected void configureClient() {
.truststorePath(truststore.getPath())
.truststorePassword(truststore.getPassword()));

switcherProperties.setValue(ContextKey.CONTEXT_LOCATION, contextBase.getClass().getName());
initializeClient();
}

@Override
protected void configureClient(String contextFile) {
setContextBase(this);
loadProperties(contextFile);

loadProperties(contextFile);
switcherProperties.setValue(ContextKey.CONTEXT_LOCATION, contextBase.getClass().getName());

updateSwitcherConfig(switcherProperties);
initializeClient();
}

private static synchronized void setContextBase(SwitcherContextBase contextBase) {
SwitcherContextBase.contextBase = contextBase;
/**
* Manually register Switcher Keys.<br>
* Use this method before calling {@link #configureClient()} to register the Switcher Keys.
*
* @param switcherKeys to be registered
*/
protected void registerSwitcherKeys(String... switcherKeys) {
setSwitcherKeys(new HashSet<>(Arrays.asList(switcherKeys)));
}

/**
Expand Down Expand Up @@ -173,7 +179,7 @@ public static void loadProperties(String contextFilename) {
*/
public static void initializeClient() {
validateContext();
validateSwitcherKeys();
registerSwitcherKeys();
instance = buildInstance();

loadSwitchers();
Expand All @@ -195,9 +201,10 @@ private static SwitcherExecutor buildInstance() {

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

return new SwitcherRemoteService(clientRemote, new SwitcherLocalService(clientRemote,
clientLocal, switcherProperties));
}

/**
Expand All @@ -212,10 +219,9 @@ private static void validateContext() throws SwitcherContextException {
}

/**
* Validate Switcher Keys.<br>
* It will ensure that only properly annotated Switchers can be used.
* Register Switcher Keys based on context properties
*/
private static void validateSwitcherKeys() {
private static void registerSwitcherKeys() {
if (Objects.nonNull(contextBase)) {
registerSwitcherKey(contextBase.getClass().getFields());
} else {
Expand All @@ -234,7 +240,7 @@ private static void validateSwitcherKeys() {
* @param fields to be registered
*/
private static void registerSwitcherKey(Field[] fields) {
switcherKeys = new HashSet<>();
switcherKeys = Optional.ofNullable(switcherKeys).orElse(new HashSet<>());
for (Field field : fields) {
if (field.isAnnotationPresent(SwitcherKey.class)) {
switcherKeys.add(field.getName());
Expand All @@ -246,13 +252,13 @@ private static void registerSwitcherKey(Field[] fields) {
* Load Switcher instances into a map cache
*/
private static void loadSwitchers() {
if (switchers == null) {
if (Objects.isNull(switchers)) {
switchers = new HashMap<>();
}

switchers.clear();
for (String key : switcherKeys) {
switchers.put(key, new Switcher(key, instance));
switchers.put(key, new SwitcherRequest(key, instance));
}
}

Expand Down Expand Up @@ -348,14 +354,14 @@ private static ClientWS initRemotePoolExecutorService() {
* @return a ready to use Switcher
* @throws SwitcherKeyNotFoundException in case the key was not properly loaded
*/
public static Switcher getSwitcher(String key, boolean keepEntries) {
public static SwitcherRequest getSwitcher(String key, boolean keepEntries) {
SwitcherUtils.debug(logger, "key: {} - keepEntries: {}", key, keepEntries);

if (!switchers.containsKey(key)) {
throw new SwitcherKeyNotFoundException(key);
}

final Switcher switcher = switchers.get(key);
final SwitcherRequest switcher = switchers.get(key);
if (!keepEntries) {
switcher.resetEntry();
}
Expand All @@ -369,7 +375,7 @@ public static Switcher getSwitcher(String key, boolean keepEntries) {
* @param key name
* @return a ready to use Switcher
*/
public static Switcher getSwitcher(String key) {
public static SwitcherRequest getSwitcher(String key) {
return getSwitcher(key, false);
}

Expand Down Expand Up @@ -414,7 +420,7 @@ public static void watchSnapshot(SnapshotEventHandler handler) {
throw new SwitcherException("Cannot watch snapshot when using remote", new UnsupportedOperationException());
}

if (watcherSnapshot == null) {
if (Objects.isNull(watcherSnapshot)) {
watcherSnapshot = new SnapshotWatcher((SwitcherLocalService) instance, handler,
contextStr(ContextKey.SNAPSHOT_LOCATION));
}
Expand All @@ -429,7 +435,7 @@ public static void watchSnapshot(SnapshotEventHandler handler) {
* @throws SwitcherException if watch thread never started
*/
public static void stopWatchingSnapshot() {
if (watcherSnapshot != null) {
if (Objects.nonNull(watcherSnapshot)) {
watcherExecutorService.shutdownNow();
watcherSnapshot.terminate();
watcherSnapshot = null;
Expand Down Expand Up @@ -497,10 +503,18 @@ public static void configure(ContextBuilder builder) {
* Cancel existing scheduled task for updating local Snapshot
*/
public static void terminateSnapshotAutoUpdateWorker() {
if (scheduledExecutorService != null) {
if (Objects.nonNull(scheduledExecutorService)) {
scheduledExecutorService.shutdownNow();
scheduledExecutorService = null;
}
}

private static synchronized void setContextBase(SwitcherContextBase contextBase) {
SwitcherContextBase.contextBase = contextBase;
}

private static synchronized void setSwitcherKeys(Set<String> switcherKeys) {
SwitcherContextBase.switcherKeys = switcherKeys;
}

}
Loading