diff --git a/src/main/java/com/github/switcherapi/client/SnapshotCallback.java b/src/main/java/com/github/switcherapi/client/SnapshotCallback.java index f597c022..071f0e2f 100644 --- a/src/main/java/com/github/switcherapi/client/SnapshotCallback.java +++ b/src/main/java/com/github/switcherapi/client/SnapshotCallback.java @@ -2,19 +2,16 @@ import com.github.switcherapi.client.utils.SwitcherUtils; import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -public class SnapshotCallback { - - private static final Logger logger = LogManager.getLogger(SnapshotCallback.class); +public interface SnapshotCallback { /** * Callback method that will be invoked when the snapshot is updated * * @param version of the new snapshot */ - public void onSnapshotUpdate(long version) { - SwitcherUtils.debug(logger, "Snapshot updated: {}", version); + default void onSnapshotUpdate(long version) { + SwitcherUtils.debug(LogManager.getLogger(SnapshotCallback.class), "Snapshot updated: {}", version); } /** @@ -22,7 +19,7 @@ public void onSnapshotUpdate(long version) { * * @param e Exception */ - public void onSnapshotUpdateError(Exception e) { - SwitcherUtils.debug(logger, "Failed to update snapshot: {}", e.getMessage()); + default void onSnapshotUpdateError(Exception e) { + SwitcherUtils.debug(LogManager.getLogger(SnapshotCallback.class), "Failed to update snapshot: {}", e.getMessage()); } } diff --git a/src/main/java/com/github/switcherapi/client/SwitcherContextBase.java b/src/main/java/com/github/switcherapi/client/SwitcherContextBase.java index 3fc5b5c5..f8a477fe 100644 --- a/src/main/java/com/github/switcherapi/client/SwitcherContextBase.java +++ b/src/main/java/com/github/switcherapi/client/SwitcherContextBase.java @@ -178,7 +178,7 @@ public static boolean scheduleSnapshotAutoUpdate(String intervalValue, SnapshotC } final long interval = SwitcherUtils.getMillis(intervalValue); - final SnapshotCallback callbackFinal = Optional.ofNullable(callback).orElse(new SnapshotCallback()); + final SnapshotCallback callbackFinal = Optional.ofNullable(callback).orElse(new SnapshotCallback() {}); final Runnable runnableSnapshotValidate = () -> { try { if (validateSnapshot()) { @@ -270,19 +270,31 @@ public static boolean validateSnapshot() { /** * Start watching snapshot files for modifications.
* When the file is modified the in-memory snapshot will reload + * + *

+ * (*) Requires client to use local settings */ public static void watchSnapshot() { - watchSnapshot(new SnapshotEventHandler()); + watchSnapshot(new SnapshotEventHandler() {}); } /** * Start watching snapshot files for modifications.
* When the file is modified the in-memory snapshot will reload + * + *

+ * (*) Requires client to use local settings * * @param handler to notify snapshot change events + * @throws SwitcherException if using remote service */ public static void watchSnapshot(SnapshotEventHandler handler) { - SwitcherUtils.watchSnapshot(instance, handler); + if (!(instance instanceof SwitcherLocalService)) { + throw new SwitcherException("Cannot watch snapshot when using remote", new UnsupportedOperationException()); + } + + SwitcherLocalService executorInstance = (SwitcherLocalService) instance; + SwitcherUtils.watchSnapshot(executorInstance, handler); } /** diff --git a/src/main/java/com/github/switcherapi/client/SwitcherExecutor.java b/src/main/java/com/github/switcherapi/client/SwitcherExecutor.java index 3e71036f..6675dbad 100644 --- a/src/main/java/com/github/switcherapi/client/SwitcherExecutor.java +++ b/src/main/java/com/github/switcherapi/client/SwitcherExecutor.java @@ -8,7 +8,6 @@ import com.github.switcherapi.client.model.criteria.Snapshot; import com.github.switcherapi.client.model.response.CriteriaResponse; import com.github.switcherapi.client.service.remote.ClientRemote; -import com.github.switcherapi.client.utils.SnapshotEventHandler; import com.github.switcherapi.client.utils.SnapshotLoader; import com.github.switcherapi.client.utils.SwitcherUtils; import com.google.gson.Gson; @@ -59,16 +58,6 @@ public abstract class SwitcherExecutor { * @param switchers To be validated */ public abstract void checkSwitchers(final Set switchers); - - /** - * Update in-memory snapshot. - * - * @param snapshotFile Path location - * @param handler to notify snapshot change events - * - * @return true if valid change - */ - public abstract boolean notifyChange(final String snapshotFile, SnapshotEventHandler handler); /** * Retrieve local snapshot version diff --git a/src/main/java/com/github/switcherapi/client/service/local/SwitcherLocalService.java b/src/main/java/com/github/switcherapi/client/service/local/SwitcherLocalService.java index 2b547ee7..d09da897 100644 --- a/src/main/java/com/github/switcherapi/client/service/local/SwitcherLocalService.java +++ b/src/main/java/com/github/switcherapi/client/service/local/SwitcherLocalService.java @@ -62,6 +62,44 @@ public void init() { } } } + + /** + * Update in-memory snapshot. + * + * @param snapshotFile Path location + * @param handler to notify snapshot change events + * + * @return true if valid change + */ + public boolean notifyChange(final String snapshotFile, SnapshotEventHandler handler) { + final String environment = SwitcherContextBase.contextStr(ContextKey.ENVIRONMENT); + final String snapshotLocation = SwitcherContextBase.contextStr(ContextKey.SNAPSHOT_LOCATION); + + try { + if (snapshotFile.equals(String.format("%s.json", environment))) { + SwitcherUtils.debug(logger, "Updating domain"); + + this.domain = SnapshotLoader.loadSnapshot(snapshotLocation, environment); + handler.onSuccess(); + } + } catch (SwitcherSnapshotLoadException | IOException e) { + handler.onError(new SwitcherException(e.getMessage(), e)); + logger.error(e); + return false; + } + + return true; + } + + /** + * Update in-memory snapshot. + * + * @param snapshotFile Path location + * @return true if valid change + */ + public boolean notifyChange(final String snapshotFile) { + return this.notifyChange(snapshotFile, new SnapshotEventHandler() {}); + } @Override public CriteriaResponse executeCriteria(final Switcher switcher) { @@ -111,27 +149,6 @@ public void checkSwitchers(final Set switchers) { throw new SwitchersValidationException(response.toString()); } } - - @Override - public boolean notifyChange(final String snapshotFile, SnapshotEventHandler handler) { - final String environment = SwitcherContextBase.contextStr(ContextKey.ENVIRONMENT); - final String snapshotLocation = SwitcherContextBase.contextStr(ContextKey.SNAPSHOT_LOCATION); - - try { - if (snapshotFile.equals(String.format("%s.json", environment))) { - SwitcherUtils.debug(logger, "Updating domain"); - - this.domain = SnapshotLoader.loadSnapshot(snapshotLocation, environment); - handler.onSuccess(); - } - } catch (SwitcherSnapshotLoadException | IOException e) { - handler.onError(new SwitcherException(e.getMessage(), e)); - logger.error(e); - return false; - } - - return true; - } @Override public long getSnapshotVersion() { diff --git a/src/main/java/com/github/switcherapi/client/service/remote/SwitcherRemoteService.java b/src/main/java/com/github/switcherapi/client/service/remote/SwitcherRemoteService.java index 3efa9586..2e28845f 100644 --- a/src/main/java/com/github/switcherapi/client/service/remote/SwitcherRemoteService.java +++ b/src/main/java/com/github/switcherapi/client/service/remote/SwitcherRemoteService.java @@ -1,13 +1,5 @@ package com.github.switcherapi.client.service.remote; -import java.util.Arrays; -import java.util.Set; - -import com.github.switcherapi.client.utils.SwitcherUtils; -import org.apache.commons.lang3.StringUtils; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - import com.github.switcherapi.client.SwitcherContextBase; import com.github.switcherapi.client.SwitcherExecutor; import com.github.switcherapi.client.exception.SwitcherRemoteException; @@ -17,7 +9,13 @@ import com.github.switcherapi.client.model.criteria.SwitchersCheck; import com.github.switcherapi.client.model.response.CriteriaResponse; import com.github.switcherapi.client.service.local.SwitcherLocalService; -import com.github.switcherapi.client.utils.SnapshotEventHandler; +import com.github.switcherapi.client.utils.SwitcherUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.Arrays; +import java.util.Set; /** * @author Roger Floriano (petruki) @@ -95,11 +93,6 @@ public void checkSwitchers(final Set switchers) { throw new SwitchersValidationException(Arrays.toString(response.getNotFound())); } } - - @Override - public boolean notifyChange(String snapshotFile, SnapshotEventHandler handler) { - return this.switcherLocal.notifyChange(snapshotFile, handler); - } @Override public long getSnapshotVersion() { diff --git a/src/main/java/com/github/switcherapi/client/utils/SnapshotEventHandler.java b/src/main/java/com/github/switcherapi/client/utils/SnapshotEventHandler.java index 6261932c..6752642f 100644 --- a/src/main/java/com/github/switcherapi/client/utils/SnapshotEventHandler.java +++ b/src/main/java/com/github/switcherapi/client/utils/SnapshotEventHandler.java @@ -1,9 +1,7 @@ package com.github.switcherapi.client.utils; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - import com.github.switcherapi.client.exception.SwitcherException; +import org.apache.logging.log4j.LogManager; /** * Access snapshot event handler when a file is modified.
@@ -12,16 +10,22 @@ * @author Roger Floriano (petruki) * @since 2022-06-29 */ -public class SnapshotEventHandler { - - private static final Logger logger = LogManager.getLogger(SnapshotEventHandler.class); - - public void onSuccess() { - SwitcherUtils.debug(logger, "Snapshot has been changed"); +public interface SnapshotEventHandler { + + /** + * Callback method that will be invoked when the snapshot is updated + */ + default void onSuccess() { + SwitcherUtils.debug(LogManager.getLogger(SnapshotEventHandler.class), "Snapshot has been changed"); } - - public void onError(SwitcherException exception) { - logger.error(exception); + + /** + * Callback method that will be invoked when the snapshot update fails + * + * @param exception Exception + */ + default void onError(SwitcherException exception) { + LogManager.getLogger(SnapshotEventHandler.class).error(exception); } } diff --git a/src/main/java/com/github/switcherapi/client/utils/SnapshotWatcher.java b/src/main/java/com/github/switcherapi/client/utils/SnapshotWatcher.java index 1af79475..c6d70947 100644 --- a/src/main/java/com/github/switcherapi/client/utils/SnapshotWatcher.java +++ b/src/main/java/com/github/switcherapi/client/utils/SnapshotWatcher.java @@ -1,23 +1,17 @@ package com.github.switcherapi.client.utils; -import java.io.IOException; -import java.nio.file.ClosedWatchServiceException; -import java.nio.file.FileSystems; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.StandardWatchEventKinds; -import java.nio.file.WatchEvent; -import java.nio.file.WatchKey; -import java.nio.file.WatchService; - +import com.github.switcherapi.client.SwitcherContextBase; +import com.github.switcherapi.client.model.ContextKey; +import com.github.switcherapi.client.service.local.SwitcherLocalService; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import com.github.switcherapi.client.SwitcherContextBase; -import com.github.switcherapi.client.SwitcherExecutor; -import com.github.switcherapi.client.model.ContextKey; +import java.io.IOException; +import java.nio.file.*; /** + * SnapshotWatcher runs in a separate thread to watch for changes in the snapshot file. + * * @author Roger Floriano (petruki) * @since 2020-05-13 */ @@ -29,9 +23,9 @@ public class SnapshotWatcher implements Runnable { private WatchService watcher; - private SwitcherExecutor executorInstance; + private SwitcherLocalService executorInstance; - public SnapshotWatcher(final SwitcherExecutor executorInstance, SnapshotEventHandler handler) { + public SnapshotWatcher(final SwitcherLocalService executorInstance, SnapshotEventHandler handler) { this.executorInstance = executorInstance; this.handler = handler; } diff --git a/src/main/java/com/github/switcherapi/client/utils/SwitcherUtils.java b/src/main/java/com/github/switcherapi/client/utils/SwitcherUtils.java index e54b1558..61a6527a 100644 --- a/src/main/java/com/github/switcherapi/client/utils/SwitcherUtils.java +++ b/src/main/java/com/github/switcherapi/client/utils/SwitcherUtils.java @@ -1,10 +1,10 @@ package com.github.switcherapi.client.utils; -import com.github.switcherapi.client.SwitcherExecutor; import com.github.switcherapi.client.exception.SwitcherContextException; import com.github.switcherapi.client.exception.SwitcherInvalidDateTimeArgumentException; import com.github.switcherapi.client.model.ContextKey; import com.github.switcherapi.client.service.WorkerName; +import com.github.switcherapi.client.service.local.SwitcherLocalService; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; @@ -141,7 +141,7 @@ public static Set payloadReader(String jsonStr, String prevKey) { * @param executorInstance of a Remote or Local Switcher * @param handler to notify snapshot change events */ - public static void watchSnapshot(final SwitcherExecutor executorInstance, SnapshotEventHandler handler) { + public static void watchSnapshot(final SwitcherLocalService executorInstance, SnapshotEventHandler handler) { if (watcher == null) { watcher = new SnapshotWatcher(executorInstance, handler); } diff --git a/src/test/java/com/github/switcherapi/client/service/local/SwitcherLocalServiceTest.java b/src/test/java/com/github/switcherapi/client/service/local/SwitcherLocalServiceTest.java index 687e4f4b..e4cb7d0e 100644 --- a/src/test/java/com/github/switcherapi/client/service/local/SwitcherLocalServiceTest.java +++ b/src/test/java/com/github/switcherapi/client/service/local/SwitcherLocalServiceTest.java @@ -37,7 +37,7 @@ void shouldNotifyWithError() { SwitchersBase.configure(ContextBuilder.builder() .environment("defect_default")); - assertFalse(service.notifyChange("defect_default.json", new SnapshotEventHandler())); + assertFalse(service.notifyChange("defect_default.json")); } @Test @@ -45,7 +45,7 @@ void shouldNotifyWithSuccess() { SwitchersBase.configure(ContextBuilder.builder() .environment("snapshot_watcher")); - assertTrue(service.notifyChange("snapshot_watcher.json", new SnapshotEventHandler())); + assertTrue(service.notifyChange("snapshot_watcher.json")); } @Test diff --git a/src/test/java/com/github/switcherapi/client/utils/SnapshotWatcherErrorTest.java b/src/test/java/com/github/switcherapi/client/utils/SnapshotWatcherErrorTest.java new file mode 100644 index 00000000..a8c6e7ed --- /dev/null +++ b/src/test/java/com/github/switcherapi/client/utils/SnapshotWatcherErrorTest.java @@ -0,0 +1,30 @@ +package com.github.switcherapi.client.utils; + +import com.github.switcherapi.SwitchersBase; +import com.github.switcherapi.client.ContextBuilder; +import com.github.switcherapi.client.exception.SwitcherException; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class SnapshotWatcherErrorTest { + + @Test + void shouldNotWatchSnapshotWhenRemote() { + //given + SwitchersBase.configure(ContextBuilder.builder() + .contextLocation(SwitchersBase.class.getCanonicalName()) + .url("https://api.switcherapi.com") + .apiKey("[API_KEY]") + .domain("Test") + .component("switcher-test") + .local(false)); + + SwitchersBase.initializeClient(); + + //test + SwitcherException exception = assertThrows(SwitcherException.class, SwitchersBase::watchSnapshot); + assertEquals("Something went wrong: Cannot watch snapshot when using remote", exception.getMessage()); + } +}