diff --git a/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/client/IConfigClient.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/client/IConfigClient.java index cf4d0e50..5370b221 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/client/IConfigClient.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/client/IConfigClient.java @@ -23,6 +23,6 @@ public interface IConfigClient { ConfigItemDTO getConfigurationItems(ConfigItemTypeEnum type); - Object getProps(ConfigItemTypeEnum type, String props, Object previous); + String getProps(ConfigItemTypeEnum type, String props, String previous); } diff --git a/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/client/impl/ConfigClient.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/client/impl/ConfigClient.java index ba271fcb..d575cb4d 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/client/impl/ConfigClient.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/client/impl/ConfigClient.java @@ -97,39 +97,17 @@ private boolean isReachable() { } return out; } - - - @Override - public Object getProps(ConfigItemTypeEnum type, String props, Object previous) { - Object out = previous; - - String endpoint = routes.getConfigItem(type, props); - if (isReachable()) { - Object response = client.getForObject(endpoint, Object.class); - out = convertResponse(response, previous); - } - - return out; - } - @SuppressWarnings("unchecked") - private T convertResponse(Object response, Object previous) { - try { - Class targetType = (Class) previous.getClass(); - - if (targetType == Integer.class) { - return (T) Integer.valueOf(response.toString()); - } else if (targetType == Boolean.class) { - return (T) Boolean.valueOf(response.toString()); - } else if (targetType == String.class) { - return (T) response.toString(); - } else { - return (T) response; - } - } catch (Exception e) { - return null; - } + @Override + public String getProps(ConfigItemTypeEnum type, String props, String previous) { + String out = previous; + String endpoint = routes.getConfigItem(type, props); + if (isReachable()) out = client.getForObject(endpoint, String.class); + if(out == null || !out.equals(previous)) { + log.info("[GTW-CFG] Property {} is set as {} (previously: {})", props, out, previous); + } + return out; } } diff --git a/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/dto/ConfigItemDTO.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/dto/ConfigItemDTO.java index 0b93eeca..6457eab9 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/dto/ConfigItemDTO.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/dto/ConfigItemDTO.java @@ -3,11 +3,23 @@ import lombok.Getter; import lombok.Setter; +import java.util.List; import java.util.Map; @Getter @Setter public class ConfigItemDTO { - private String key; - private Map items; + + private String traceId; + private String spanId; + + private List configurationItems; + + @Getter + @Setter + public static class ConfigDataItemDTO { + private String key; + private Map items; + } + } diff --git a/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/service/impl/ConfigSRV.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/service/impl/ConfigSRV.java index f2b005c7..a09b0413 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/service/impl/ConfigSRV.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/service/impl/ConfigSRV.java @@ -1,25 +1,27 @@ package it.finanze.sanita.fse2.ms.gtw.dispatcher.service.impl; -import static it.finanze.sanita.fse2.ms.gtw.dispatcher.client.routes.base.ClientRoutes.Config.PROPS_NAME_AUDIT_ENABLED; -import static it.finanze.sanita.fse2.ms.gtw.dispatcher.client.routes.base.ClientRoutes.Config.PROPS_NAME_CONTROL_LOG_ENABLED; -import static it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.ConfigItemTypeEnum.DISPATCHER; -import static it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.ConfigItemTypeEnum.GENERIC; +import it.finanze.sanita.fse2.ms.gtw.dispatcher.client.IConfigClient; +import it.finanze.sanita.fse2.ms.gtw.dispatcher.dto.ConfigItemDTO; +import it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.ConfigItemTypeEnum; +import it.finanze.sanita.fse2.ms.gtw.dispatcher.service.IConfigSRV; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.tuple.Pair; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import javax.annotation.PostConstruct; import java.util.Date; import java.util.HashMap; +import java.util.List; import java.util.Map; -import java.util.Map.Entry; - -import javax.annotation.PostConstruct; - -import org.apache.commons.lang3.tuple.Pair; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import it.finanze.sanita.fse2.ms.gtw.dispatcher.client.IConfigClient; -import it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.ConfigItemTypeEnum; -import it.finanze.sanita.fse2.ms.gtw.dispatcher.service.IConfigSRV; +import static it.finanze.sanita.fse2.ms.gtw.dispatcher.client.routes.base.ClientRoutes.Config.PROPS_NAME_AUDIT_ENABLED; +import static it.finanze.sanita.fse2.ms.gtw.dispatcher.client.routes.base.ClientRoutes.Config.PROPS_NAME_CONTROL_LOG_ENABLED; +import static it.finanze.sanita.fse2.ms.gtw.dispatcher.dto.ConfigItemDTO.*; +import static it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.ConfigItemTypeEnum.DISPATCHER; +import static it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.ConfigItemTypeEnum.GENERIC; +@Slf4j @Service public class ConfigSRV implements IConfigSRV { @@ -28,9 +30,7 @@ public class ConfigSRV implements IConfigSRV { @Autowired private IConfigClient client; - private long lastUpdate; - - private final Map> props; + private final Map> props; public ConfigSRV() { this.props = new HashMap<>(); @@ -39,51 +39,53 @@ public ConfigSRV() { @PostConstruct public void postConstruct() { - for(ConfigItemTypeEnum en : ConfigItemTypeEnum.values()) { - for(Entry el : client.getConfigurationItems(en).getItems().entrySet()) { - props.put(el.getKey(), Pair.of(new Date().getTime(), el.getValue())); - } - } + for(ConfigItemTypeEnum en : ConfigItemTypeEnum.values()) { + log.info("[GTW-CFG] Retrieving {} properties ...", en.name()); + ConfigItemDTO items = client.getConfigurationItems(en); + List opts = items.getConfigurationItems(); + for(ConfigDataItemDTO opt : opts) { + opt.getItems().forEach((key, value) -> { + log.info("[GTW-CFG] Property {} is set as {}", key, value); + props.put(key, Pair.of(new Date().getTime(), value)); + }); + } + } } - private void refreshAuditEnable() { - Boolean previous = props.get(PROPS_NAME_AUDIT_ENABLED)!=null ? (Boolean)props.get(PROPS_NAME_AUDIT_ENABLED).getValue() : null; - Boolean audit = (Boolean)client.getProps(DISPATCHER, PROPS_NAME_AUDIT_ENABLED,previous); - props.put(PROPS_NAME_AUDIT_ENABLED, Pair.of(new Date().getTime(), audit)); - - } - @Override public Boolean isAuditEnable() { + long lastUpdate = props.get(PROPS_NAME_AUDIT_ENABLED).getKey(); if (new Date().getTime() - lastUpdate >= DELTA_MS) { synchronized(ConfigSRV.class) { - if (new Date().getTime() - lastUpdate >= DELTA_MS) { - refreshAuditEnable(); - } + if (new Date().getTime() - lastUpdate >= DELTA_MS) { + refresh(DISPATCHER, PROPS_NAME_AUDIT_ENABLED); + } } } - return (Boolean)props.get(PROPS_NAME_AUDIT_ENABLED).getValue(); - } - - private void refreshControlLogPersistenceEnable() { - Boolean previous = props.get(PROPS_NAME_CONTROL_LOG_ENABLED)!=null ? (Boolean)props.get(PROPS_NAME_CONTROL_LOG_ENABLED).getValue() : null; - Boolean controlLogEnabled = (Boolean)client.getProps(GENERIC, PROPS_NAME_CONTROL_LOG_ENABLED,previous); - props.put(PROPS_NAME_CONTROL_LOG_ENABLED, Pair.of(new Date().getTime(), controlLogEnabled)); - + return Boolean.parseBoolean( + props.get(PROPS_NAME_AUDIT_ENABLED).getValue() + ); } - @Override public Boolean isControlLogPersistenceEnable() { + long lastUpdate = props.get(PROPS_NAME_CONTROL_LOG_ENABLED).getKey(); if (new Date().getTime() - lastUpdate >= DELTA_MS) { synchronized(ConfigSRV.class) { - if (new Date().getTime() - lastUpdate >= DELTA_MS) { - refreshControlLogPersistenceEnable(); - } + if (new Date().getTime() - lastUpdate >= DELTA_MS) { + refresh(GENERIC, PROPS_NAME_CONTROL_LOG_ENABLED); + } } } - return (Boolean)props.get(PROPS_NAME_CONTROL_LOG_ENABLED).getValue(); + return Boolean.parseBoolean( + props.get(PROPS_NAME_CONTROL_LOG_ENABLED).getValue() + ); + } + + private void refresh(ConfigItemTypeEnum type, String name) { + String previous = props.getOrDefault(name, Pair.of(0L, null)).getValue(); + String prop = client.getProps(type, name, previous); + props.put(name, Pair.of(new Date().getTime(), prop)); } - }