Skip to content

Commit

Permalink
feat: update get-props function to support override
Browse files Browse the repository at this point in the history
  • Loading branch information
gb-cic committed Dec 13, 2023
1 parent 034fa6b commit 5ce480a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ public interface IConfigClient {

ConfigItemDTO getConfigurationItems(ConfigItemTypeEnum type);

String getProps(ConfigItemTypeEnum type, String props, String previous);
String getProps(String props, String previous, ConfigItemTypeEnum ms);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.client.RestTemplate;

import static it.finanze.sanita.fse2.ms.gtw.rulesmanager.enums.ConfigItemTypeEnum.GENERIC;

/**
* Implementation of gtw-config Client.
*/
Expand Down Expand Up @@ -81,15 +83,26 @@ 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;
}
}

private boolean isReachable() {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
package it.finanze.sanita.fse2.ms.gtw.rulesmanager.enums;

import java.util.Arrays;
import java.util.List;

public enum ConfigItemTypeEnum {
GENERIC,
RULES_MANAGER,
RULES_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<ConfigItemTypeEnum> priority() {
List<ConfigItemTypeEnum> items = Arrays.asList(ConfigItemTypeEnum.values());
items.sort((a, b) -> a == GENERIC ? -1 : b == GENERIC ? 1 : 0);
return items;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import java.util.Map;

import static it.finanze.sanita.fse2.ms.gtw.rulesmanager.client.routes.base.ClientRoutes.Config.PROPS_NAME_CONTROL_LOG_ENABLED;
import static it.finanze.sanita.fse2.ms.gtw.rulesmanager.enums.ConfigItemTypeEnum.GENERIC;
import static it.finanze.sanita.fse2.ms.gtw.rulesmanager.enums.ConfigItemTypeEnum.RULES_MANAGER;

@Service
@Slf4j
Expand All @@ -37,7 +37,7 @@ public ConfigSRV() {

@PostConstruct
public void postConstruct() {
for(ConfigItemTypeEnum en : ConfigItemTypeEnum.values()) {
for(ConfigItemTypeEnum en : ConfigItemTypeEnum.priority()) {
log.info("[GTW-CFG] Retrieving {} properties ...", en.name());
ConfigItemDTO items = client.getConfigurationItems(en);
List<ConfigDataItemDTO> opts = items.getConfigurationItems();
Expand All @@ -46,6 +46,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();
Expand All @@ -57,7 +58,7 @@ public Boolean isControlLogPersistenceEnable() {
if (new Date().getTime() - lastUpdate >= DELTA_MS) {
synchronized(PROPS_NAME_CONTROL_LOG_ENABLED) {
if (new Date().getTime() - lastUpdate >= DELTA_MS) {
refresh(GENERIC, PROPS_NAME_CONTROL_LOG_ENABLED);
refresh(PROPS_NAME_CONTROL_LOG_ENABLED);
}
}
}
Expand All @@ -66,9 +67,9 @@ public Boolean isControlLogPersistenceEnable() {
);
}

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, RULES_MANAGER);
props.put(name, Pair.of(new Date().getTime(), prop));
}

Expand Down

0 comments on commit 5ce480a

Please sign in to comment.