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
14 changes: 10 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
<!-- utils -->
<commons-lang3.version>3.17.0</commons-lang3.version>
<commons-net.version>3.11.1</commons-net.version>
<log4j.version>2.24.1</log4j.version>
<slf4j-api.version>2.0.16</slf4j-api.version>

<!-- test -->
<okhttp.version>5.0.0-alpha.14</okhttp.version>
Expand Down Expand Up @@ -123,9 +123,9 @@
<version>${commons-net.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j-api.version}</version>
</dependency>

<!-- test -->
Expand Down Expand Up @@ -159,6 +159,12 @@
<version>${junit-pioneer.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j-api.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.switcherapi.client;

import com.github.switcherapi.client.utils.SwitcherUtils;
import org.apache.logging.log4j.LogManager;
import org.slf4j.LoggerFactory;

public interface SnapshotCallback {

Expand All @@ -11,7 +11,7 @@ public interface SnapshotCallback {
* @param version of the new snapshot
*/
default void onSnapshotUpdate(long version) {
SwitcherUtils.debug(LogManager.getLogger(SnapshotCallback.class), "Snapshot updated: {}", version);
SwitcherUtils.debug(LoggerFactory.getLogger(SnapshotCallback.class), "Snapshot updated: {}", version);
}

/**
Expand All @@ -20,6 +20,6 @@ default void onSnapshotUpdate(long version) {
* @param e Exception
*/
default void onSnapshotUpdateError(Exception e) {
SwitcherUtils.debug(LogManager.getLogger(SnapshotCallback.class), "Failed to update snapshot: {}", e.getMessage());
SwitcherUtils.debug(LoggerFactory.getLogger(SnapshotCallback.class), "Failed to update snapshot: {}", e.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import com.github.switcherapi.client.utils.SnapshotWatcher;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -62,7 +62,7 @@
*/
public abstract class SwitcherContextBase {

protected static final Logger logger = LogManager.getLogger(SwitcherContextBase.class);
protected static final Logger logger = LoggerFactory.getLogger(SwitcherContextBase.class);

protected static SwitcherProperties switcherProperties;
protected static Set<String> switcherKeys;
Expand Down Expand Up @@ -189,7 +189,7 @@ public static boolean scheduleSnapshotAutoUpdate(String intervalValue, SnapshotC
callbackFinal.onSnapshotUpdate(instance.getSnapshotVersion());
}
} catch (Exception e) {
logger.error(e.getMessage());
logger.error(e.getMessage(), e);
callbackFinal.onSnapshotUpdateError(e);
}
};
Expand Down
28 changes: 12 additions & 16 deletions src/main/java/com/github/switcherapi/client/SwitcherExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import com.github.switcherapi.client.utils.SwitcherUtils;
import com.google.gson.Gson;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -28,7 +28,7 @@
*/
public abstract class SwitcherExecutor {

private static final Logger logger = LogManager.getLogger(SwitcherExecutor.class);
private static final Logger logger = LoggerFactory.getLogger(SwitcherExecutor.class);

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

Expand Down Expand Up @@ -73,23 +73,19 @@ protected boolean checkSnapshotVersion(ClientRemote clientRemote, final Domain d
return clientRemote.checkSnapshotVersion(domain.getVersion());
}

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

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

if (snapshotLocation != null) {
SnapshotLoader.saveSnapshot(snapshot, snapshotLocation, environment);
}

return snapshot.getDomain();
} catch (SwitcherRemoteException | SwitcherSnapshotWriteException e) {
logger.error(e);
throw e;
final Snapshot snapshot = clientRemote.resolveSnapshot();
final String snapshotLocation = SwitcherContextBase.contextStr(ContextKey.SNAPSHOT_LOCATION);

if (snapshotLocation != null) {
SnapshotLoader.saveSnapshot(snapshot, snapshotLocation, environment);
}

return snapshot.getDomain();
}

public boolean isLocalEnabled() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.github.switcherapi.client.model;

import com.github.switcherapi.client.utils.SwitcherUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.github.switcherapi.client.exception.SwitcherException;
import com.github.switcherapi.client.model.response.CriteriaResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand All @@ -20,7 +20,7 @@
*/
public class AsyncSwitcher implements Runnable {

private static final Logger logger = LogManager.getLogger(AsyncSwitcher.class);
private static final Logger logger = LoggerFactory.getLogger(AsyncSwitcher.class);

private final ExecutorService executorService;

Expand Down Expand Up @@ -62,7 +62,7 @@ public void run() {

switcherInterface.getHistoryExecution().add(response);
} catch (SwitcherException e) {
logger.error(e);
logger.error(e.getMessage(), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
import com.github.switcherapi.client.model.response.AuthResponse;
import com.github.switcherapi.client.model.response.CriteriaResponse;
import com.github.switcherapi.client.model.response.SnapshotVersionResponse;
import com.github.switcherapi.client.utils.SwitcherUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.Entity;
Expand All @@ -29,9 +26,7 @@
*/
public class ClientWSImpl implements ClientWS {

private static final Logger logger = LogManager.getLogger(ClientWSImpl.class);

public static final String QUERY =
public static final String QUERY =
"{\"query\":\"{ domain(name: \\\"%s\\\", environment: \\\"%s\\\", _component: \\\"%s\\\") { " +
"name version description activated " +
"group { name description activated " +
Expand All @@ -51,8 +46,6 @@ public ClientWSImpl() {

@Override
public CriteriaResponse executeCriteriaService(final Switcher switcher, final String token) {
SwitcherUtils.debug(logger, "switcher: {}", switcher);

final String url = SwitcherContextBase.contextStr(ContextKey.URL);
final WebTarget myResource = client.target(String.format(CRITERIA_URL, url))
.queryParam(Switcher.KEY, switcher.getSwitcherKey())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import com.github.switcherapi.client.service.ValidatorService;
import com.github.switcherapi.client.utils.SwitcherUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -29,7 +29,7 @@
*/
public class ClientLocalService {

private static final Logger logger = LogManager.getLogger(ClientLocalService.class);
private static final Logger logger = LoggerFactory.getLogger(ClientLocalService.class);

public static final String DISABLED_DOMAIN = "Domain disabled";
public static final String DISABLED_GROUP = "Group disabled";
Expand All @@ -38,6 +38,8 @@ public class ClientLocalService {
private static final String STRATEGY_FAIL_PATTERN = "Strategy %s does not agree";
private static final String STRATEGY_FAIL_NO_INPUT_PATTERN = "Strategy %s did not receive any input";

private static final String LOG_PROCESS_OP_TEMPLATE = "processOperation: configStrategies: %s";

private final ValidatorService validatorService;

public ClientLocalService() {
Expand Down Expand Up @@ -117,8 +119,7 @@ private Config findConfigInGroup(final Group group, final String switcherKey) {
*/
private CriteriaResponse processOperation(final Strategy[] configStrategies, final List<Entry> input,
final Switcher switcher) {
SwitcherUtils.debugSupplier(logger, "configStrategies: {}", Arrays.toString(configStrategies));
SwitcherUtils.debugSupplier(logger, "input: {}", Arrays.toString(input != null ? input.toArray() : ArrayUtils.EMPTY_STRING_ARRAY));
SwitcherUtils.debug(logger, LOG_PROCESS_OP_TEMPLATE, Arrays.toString(configStrategies));

for (final Strategy strategy : configStrategies) {
if (!strategy.isActivated()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import com.github.switcherapi.client.utils.SnapshotLoader;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.List;
Expand All @@ -26,7 +26,7 @@
*/
public class SwitcherLocalService extends SwitcherExecutor {

private static final Logger logger = LogManager.getLogger(SwitcherLocalService.class);
private static final Logger logger = LoggerFactory.getLogger(SwitcherLocalService.class);

private final ClientRemote clientRemote;

Expand Down Expand Up @@ -84,7 +84,7 @@ public boolean notifyChange(final String snapshotFile, SnapshotEventHandler hand
}
} catch (SwitcherSnapshotLoadException | IOException e) {
handler.onError(new SwitcherException(e.getMessage(), e));
logger.error(e);
logger.error(e.getMessage(), e);
return false;
}

Expand All @@ -103,7 +103,7 @@ public boolean notifyChange(final String snapshotFile) {

@Override
public CriteriaResponse executeCriteria(final Switcher switcher) {
SwitcherUtils.debug(logger, "switcher: {}", switcher);
SwitcherUtils.debug(logger, "[Local] request: {}", switcher);

CriteriaResponse response;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import com.github.switcherapi.client.service.local.SwitcherLocalService;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.Set;
Expand All @@ -23,7 +23,7 @@
*/
public class SwitcherRemoteService extends SwitcherExecutor {

private static final Logger logger = LogManager.getLogger(SwitcherRemoteService.class);
private static final Logger logger = LoggerFactory.getLogger(SwitcherRemoteService.class);

private final SwitcherLocalService switcherLocal;

Expand All @@ -36,15 +36,15 @@ public SwitcherRemoteService() {

@Override
public CriteriaResponse executeCriteria(final Switcher switcher) {
SwitcherUtils.debug(logger, "switcher: {}", switcher);
SwitcherUtils.debug(logger, "[Remote] request:: {}", switcher);

try {
final CriteriaResponse response = this.clientRemote.executeCriteria(switcher);
SwitcherUtils.debug(logger, "[Remote] response: {}", response);

return response;
} catch (final SwitcherRemoteException e) {
logger.error("Failed to execute criteria - {}\nCause: {}", e.getMessage(), e.getCause());
logger.error("Failed to execute criteria - Cause: {}", e.getMessage(), e.getCause());
return tryExecuteLocalCriteria(switcher, e);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.github.switcherapi.client.service.validators;

import com.github.switcherapi.client.utils.SwitcherUtils;
import org.apache.commons.lang3.RegExUtils;
import org.apache.commons.lang3.StringUtils;

public abstract class DateTimeValidator extends Validator {

private static final String LOG_DATE = "date: {}";
private static final String LOG_DATE_TIME = "date time: {} {}";
private static final String FULL_DATE_REGEX = "([12]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\\d|3[01]))";

private static final String DATE_FORMAT = "%s 00:00:00";
private static final String HOUR_FORMAT = "%s %s:00:00";
private static final String MINUTE_FORMAT = "%s %s:00";
private static final String TIME_SPLIT = ":";
private static final String DATE_SPLIT = " ";

protected String getFullDate(final String date) {
SwitcherUtils.debug(logger, LOG_DATE, date);

final String time = RegExUtils.removePattern(date, FULL_DATE_REGEX).trim();
return getFullTime(date, time);
}

protected String getFullTime(final String date, final String time) {
SwitcherUtils.debug(logger, LOG_DATE_TIME, date, time);

if (StringUtils.isBlank(time)) {
return String.format(DATE_FORMAT, date);
}

String[] timeSplit = time.split(TIME_SPLIT);
if (timeSplit.length == 1) {
return String.format(HOUR_FORMAT, date.split(DATE_SPLIT)[0], time);
}

if (timeSplit.length == 2) {
return String.format(MINUTE_FORMAT, date.split(DATE_SPLIT)[0], time);
}

return date;
}

}
Loading