From 4382806fb7ee675ba7134eeaab2d1ab6dffe1a58 Mon Sep 17 00:00:00 2001 From: Giuseppe Baittiner Date: Wed, 13 Dec 2023 11:30:44 +0100 Subject: [PATCH] feat: update get-props function to support override --- .../statusmanager/client/IConfigClient.java | 2 +- .../client/impl/ConfigClient.java | 21 +++++++++++++++---- .../enums/ConfigItemTypeEnum.java | 16 +++++++++++++- .../statusmanager/service/impl/ConfigSRV.java | 14 +++++++------ 4 files changed, 41 insertions(+), 12 deletions(-) diff --git a/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/client/IConfigClient.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/client/IConfigClient.java index eb91561..5634483 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/client/IConfigClient.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/client/IConfigClient.java @@ -16,5 +16,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/gtw/statusmanager/client/impl/ConfigClient.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/client/impl/ConfigClient.java index 8de7377..c0220e1 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/client/impl/ConfigClient.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/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.gtw.statusmanager.enums.ConfigItemTypeEnum.GENERIC; + @Slf4j @Component public class ConfigClient implements IConfigClient { @@ -48,12 +50,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/gtw/statusmanager/enums/ConfigItemTypeEnum.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/enums/ConfigItemTypeEnum.java index 57da3f5..eb202c2 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/enums/ConfigItemTypeEnum.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/enums/ConfigItemTypeEnum.java @@ -1,6 +1,20 @@ package it.finanze.sanita.fse2.ms.gtw.statusmanager.enums; +import java.util.Arrays; +import java.util.List; + public enum ConfigItemTypeEnum { GENERIC, - STATUS_MANAGER + STATUS_MANAGER; + + /** + * 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; + } } diff --git a/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/service/impl/ConfigSRV.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/service/impl/ConfigSRV.java index 2d0bbf0..8326e66 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/service/impl/ConfigSRV.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/statusmanager/service/impl/ConfigSRV.java @@ -29,6 +29,7 @@ import static it.finanze.sanita.fse2.ms.gtw.statusmanager.client.routes.base.ClientRoutes.Config.*; import static it.finanze.sanita.fse2.ms.gtw.statusmanager.dto.ConfigItemDTO.ConfigDataItemDTO; +import static it.finanze.sanita.fse2.ms.gtw.statusmanager.enums.ConfigItemTypeEnum.GENERIC; import static it.finanze.sanita.fse2.ms.gtw.statusmanager.enums.ConfigItemTypeEnum.STATUS_MANAGER; @Slf4j @@ -48,7 +49,7 @@ public ConfigSRV() { @EventListener(ApplicationStartedEvent.class) void initialize() { - for(ConfigItemTypeEnum en : ConfigItemTypeEnum.values()) { + for(ConfigItemTypeEnum en : ConfigItemTypeEnum.priority()) { log.info("[GTW-CFG] Retrieving {} properties ...", en.name()); ConfigItemDTO items = client.getConfigurationItems(en); List opts = items.getConfigurationItems(); @@ -57,6 +58,7 @@ void initialize() { 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(); @@ -68,7 +70,7 @@ public Integer getExpirationDate() { if (new Date().getTime() - lastUpdate >= DELTA_MS) { synchronized(PROPS_NAME_EXP_DAYS) { if (new Date().getTime() - lastUpdate >= DELTA_MS) { - refresh(STATUS_MANAGER, PROPS_NAME_EXP_DAYS); + refresh(PROPS_NAME_EXP_DAYS); } } } @@ -83,7 +85,7 @@ public Boolean isSubjectNotAllowed() { if (new Date().getTime() - lastUpdate >= DELTA_MS) { synchronized (PROPS_NAME_SUBJECT) { if (new Date().getTime() - lastUpdate >= DELTA_MS) { - refresh(STATUS_MANAGER, PROPS_NAME_SUBJECT); + refresh(PROPS_NAME_SUBJECT); } } } @@ -98,7 +100,7 @@ public Boolean isCfOnIssuerNotAllowed() { if (new Date().getTime() - lastUpdate >= DELTA_MS) { synchronized(PROPS_NAME_ISSUER_CF) { if (new Date().getTime() - lastUpdate >= DELTA_MS) { - refresh(STATUS_MANAGER, PROPS_NAME_ISSUER_CF); + refresh(PROPS_NAME_ISSUER_CF); } } } @@ -107,9 +109,9 @@ public Boolean isCfOnIssuerNotAllowed() { ); } - 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, STATUS_MANAGER); props.put(name, Pair.of(new Date().getTime(), prop)); }