From 8cfc9edede5d715e24e19e16aedcd840755ec80f Mon Sep 17 00:00:00 2001 From: Giuseppe Baittiner Date: Wed, 13 Dec 2023 11:55:36 +0100 Subject: [PATCH] feat: update get-props function to support override --- .../ms/edsclient/client/IConfigClient.java | 2 +- .../edsclient/client/impl/ConfigClient.java | 21 +++++++++++++++---- .../edsclient/enums/ConfigItemTypeEnum.java | 16 +++++++++++++- .../ms/edsclient/service/impl/ConfigSRV.java | 10 ++++----- 4 files changed, 38 insertions(+), 11 deletions(-) diff --git a/src/main/java/it/finanze/sanita/fse2/ms/edsclient/client/IConfigClient.java b/src/main/java/it/finanze/sanita/fse2/ms/edsclient/client/IConfigClient.java index 91c2879..b49acde 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/edsclient/client/IConfigClient.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/edsclient/client/IConfigClient.java @@ -22,5 +22,5 @@ public interface IConfigClient { ConfigItemDTO getConfigurationItems(ConfigItemTypeEnum type); - String getProps(ConfigItemTypeEnum type, String props, String previous); + String getProps(String props, String previous, ConfigItemTypeEnum ms); } diff --git a/src/main/java/it/finanze/sanita/fse2/ms/edsclient/client/impl/ConfigClient.java b/src/main/java/it/finanze/sanita/fse2/ms/edsclient/client/impl/ConfigClient.java index 334fdb4..2c0cb8f 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/edsclient/client/impl/ConfigClient.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/edsclient/client/impl/ConfigClient.java @@ -21,6 +21,8 @@ import org.springframework.web.client.ResourceAccessException; import org.springframework.web.client.RestTemplate; +import static it.finanze.sanita.fse2.ms.edsclient.enums.ConfigItemTypeEnum.GENERIC; + /** * Implementation of gtw-config Client. */ @@ -43,12 +45,23 @@ public ConfigItemDTO getConfigurationItems(ConfigItemTypeEnum type) { } @Override - public String getProps(ConfigItemTypeEnum type, String props, String previous) { + public String getProps(String props, String previous, ConfigItemTypeEnum ms) { String out = previous; - String endpoint = routes.getConfigItem(type, props); - if (isReachable()) out = client.getForObject(endpoint, String.class); + ConfigItemTypeEnum src = ms; + // Check if gtw-config is available and get props + if (isReachable()) { + // Try to get the specific one + out = client.getForObject(routes.getConfigItem(ms, props), String.class); + // If the props don't exist + if (out == null) { + // Retrieve the generic one + out = client.getForObject(routes.getConfigItem(GENERIC, props), String.class); + // Set where has been retrieved from + src = GENERIC; + } + } if(out == null || !out.equals(previous)) { - log.info("[GTW-CFG] Property {} is set as {} (previously: {})", props, out, previous); + log.info("[GTW-CFG] {} set as {} (previously: {}) from {}", props, out, previous, src); } return out; } diff --git a/src/main/java/it/finanze/sanita/fse2/ms/edsclient/enums/ConfigItemTypeEnum.java b/src/main/java/it/finanze/sanita/fse2/ms/edsclient/enums/ConfigItemTypeEnum.java index 1735486..dd48274 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/edsclient/enums/ConfigItemTypeEnum.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/edsclient/enums/ConfigItemTypeEnum.java @@ -1,6 +1,20 @@ package it.finanze.sanita.fse2.ms.edsclient.enums; +import java.util.Arrays; +import java.util.List; + public enum ConfigItemTypeEnum { GENERIC, - EDS_CLIENT, + EDS_CLIENT; + + /** + * This method may seem useless, but it has been made + * to prevent relying on enum declaration order {@link ConfigItemTypeEnum#values()} + * @return The config item types sort by priority + */ + public static List priority() { + List items = Arrays.asList(ConfigItemTypeEnum.values()); + items.sort((a, b) -> a == GENERIC ? -1 : b == GENERIC ? 1 : 0); + return items; + } } \ No newline at end of file diff --git a/src/main/java/it/finanze/sanita/fse2/ms/edsclient/service/impl/ConfigSRV.java b/src/main/java/it/finanze/sanita/fse2/ms/edsclient/service/impl/ConfigSRV.java index f77041a..9ee833f 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/edsclient/service/impl/ConfigSRV.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/edsclient/service/impl/ConfigSRV.java @@ -18,7 +18,6 @@ import static it.finanze.sanita.fse2.ms.edsclient.client.routes.base.ClientRoutes.Config.PROPS_NAME_REMOVE_METADATA_ENABLE; import static it.finanze.sanita.fse2.ms.edsclient.enums.ConfigItemTypeEnum.EDS_CLIENT; -import static it.finanze.sanita.fse2.ms.edsclient.enums.ConfigItemTypeEnum.values; @Service @Slf4j @@ -37,7 +36,7 @@ public ConfigSRV() { @PostConstruct public void postConstruct() { - for(ConfigItemTypeEnum en : values()) { + for(ConfigItemTypeEnum en : ConfigItemTypeEnum.priority()) { log.info("[GTW-CFG] Retrieving {} properties ...", en.name()); ConfigItemDTO items = client.getConfigurationItems(en); List opts = items.getConfigurationItems(); @@ -46,6 +45,7 @@ public void postConstruct() { log.info("[GTW-CFG] Property {} is set as {}", key, value); props.put(key, Pair.of(new Date().getTime(), value)); }); + if(opt.getItems().isEmpty()) log.info("[GTW-CFG] No props were found"); } } integrity(); @@ -57,7 +57,7 @@ public Boolean isRemoveMetadataEnable() { if (new Date().getTime() - lastUpdate >= DELTA_MS) { synchronized(ConfigSRV.class) { if (new Date().getTime() - lastUpdate >= DELTA_MS) { - refresh(EDS_CLIENT, PROPS_NAME_REMOVE_METADATA_ENABLE); + refresh(PROPS_NAME_REMOVE_METADATA_ENABLE); } } } @@ -66,9 +66,9 @@ public Boolean isRemoveMetadataEnable() { ); } - private void refresh(ConfigItemTypeEnum type, String name) { + private void refresh(String name) { String previous = props.getOrDefault(name, Pair.of(0L, null)).getValue(); - String prop = client.getProps(type, name, previous); + String prop = client.getProps(name, previous, EDS_CLIENT); props.put(name, Pair.of(new Date().getTime(), prop)); }