Skip to content

Commit

Permalink
fix: various fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gb-cic committed Dec 7, 2023
1 parent f7201a7 commit e0563b2
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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> T convertResponse(Object response, Object previous) {
try {
Class<T> targetType = (Class<T>) 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> items;

private String traceId;
private String spanId;

private List<ConfigDataItemDTO> configurationItems;

@Getter
@Setter
public static class ConfigDataItemDTO {
private String key;
private Map<String, String> items;
}

}
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -28,9 +30,7 @@ public class ConfigSRV implements IConfigSRV {
@Autowired
private IConfigClient client;

private long lastUpdate;

private final Map<String, Pair<Long, Object>> props;
private final Map<String, Pair<Long, String>> props;

public ConfigSRV() {
this.props = new HashMap<>();
Expand All @@ -39,51 +39,53 @@ public ConfigSRV() {

@PostConstruct
public void postConstruct() {
for(ConfigItemTypeEnum en : ConfigItemTypeEnum.values()) {
for(Entry<String, String> 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<ConfigDataItemDTO> 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));
}


}

0 comments on commit e0563b2

Please sign in to comment.