Improve configuration loader

This commit is contained in:
George Kalampokis 2021-09-28 14:15:50 +03:00
parent 99e4d231c2
commit fc7c39081a
2 changed files with 68 additions and 17 deletions

View File

@ -8,7 +8,6 @@ import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.w3c.dom.Document;
@ -26,16 +25,13 @@ import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.*;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
@Service("configLoader")
@Profile("devel")
public class DevelConfigLoader implements ConfigLoader {
private static final Logger logger = LoggerFactory.getLogger(DevelConfigLoader.class);
//@Profile("devel")
public class DefaultConfigLoader implements ConfigLoader {
private static final Logger logger = LoggerFactory.getLogger(DefaultConfigLoader.class);
private ExternalUrls externalUrls;
private List<String> rdaProperties;
@ -53,10 +49,10 @@ public class DevelConfigLoader implements ConfigLoader {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(ExternalUrls.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
is = getClass().getClassLoader().getResource(fileUrl).openStream();
is = getStreamFromPath(fileUrl);
externalUrls = (ExternalUrls) jaxbUnmarshaller.unmarshal(is);
} catch (Exception ex) {
logger.error("Cannot find resource in classpath", ex);
logger.error("Cannot find resource", ex);
} finally {
try {
if (is != null) is.close();
@ -89,7 +85,7 @@ public class DevelConfigLoader implements ConfigLoader {
String filePath = environment.getProperty("configuration.h2020template");
InputStream is = null;
try {
is = getClass().getClassLoader().getResource(filePath).openStream();
is = getStreamFromPath(filePath);
this.document = new XWPFDocument(is);
} catch (IOException | NullPointerException e) {
logger.error(e.getMessage(), e);
@ -106,7 +102,7 @@ public class DevelConfigLoader implements ConfigLoader {
String filePath = environment.getProperty("configuration.configurable_login_providers");
InputStream is = null;
try {
is = getClass().getClassLoader().getResource(filePath).openStream();
is = getStreamFromPath(filePath);
ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
this.configurableProviders = mapper.readValue(is, ConfigurableProviders.class);
} catch (IOException | NullPointerException e) {
@ -145,12 +141,34 @@ public class DevelConfigLoader implements ConfigLoader {
public ExternalUrls getExternalUrls() {
this.setExternalUrls();
if (externalUrls == null) {
externalUrls = new ExternalUrls();
this.setExternalUrls();
} else {
while (externalUrls.getMaxresults() == null) {
try {
Thread.sleep(100L);
} catch (InterruptedException e) {
logger.error(e.getMessage(), e);
}
}
}
return externalUrls;
}
public List<String> getRdaProperties() {
this.setRdaProperties();
if (rdaProperties == null) {
rdaProperties = new ArrayList<>();
this.setRdaProperties();
} else {
while (rdaProperties.size() == 0) {
try {
Thread.sleep(100L);
} catch (InterruptedException e) {
logger.error(e.getMessage(), e);
}
}
}
return rdaProperties;
}
@ -160,11 +178,34 @@ public class DevelConfigLoader implements ConfigLoader {
}
public ConfigurableProviders getConfigurableProviders() {
this.setConfigurableProviders();
if (configurableProviders == null) {
configurableProviders = new ConfigurableProviders();
this.setConfigurableProviders();
} else {
while (configurableProviders.getProviders().size() == 0) {
try {
Thread.sleep(100L);
} catch (InterruptedException e) {
logger.error(e.getMessage(), e);
}
}
}
return configurableProviders;
}
public Map<String, String> getKeyToSourceMap() {
if (keyToSourceMap == null) {
keyToSourceMap = new HashMap<>();
this.setKeyToSourceMap();
} else {
while (keyToSourceMap.size() == 0) {
try {
Thread.sleep(100L);
} catch (InterruptedException e) {
logger.error(e.getMessage(), e);
}
}
}
this.setKeyToSourceMap();
return keyToSourceMap;
}
@ -173,7 +214,7 @@ public class DevelConfigLoader implements ConfigLoader {
InputStream is = null;
Document doc;
try {
is = getClass().getClassLoader().getResource(filePath).openStream();
is = getStreamFromPath(filePath);
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
doc = documentBuilder.parse(is);
@ -211,4 +252,13 @@ public class DevelConfigLoader implements ConfigLoader {
}
return values;
}
private InputStream getStreamFromPath(String filePath) {
try {
return new FileInputStream(filePath);
} catch (FileNotFoundException e) {
logger.info("loading from classpath");
return getClass().getClassLoader().getResourceAsStream(filePath);
}
}
}

View File

@ -33,6 +33,7 @@ import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/*
@Service("configLoader")
@Profile({ "production", "staging" })
public class ProductionConfigLoader implements ConfigLoader {
@ -236,4 +237,4 @@ public class ProductionConfigLoader implements ConfigLoader {
}
return values;
}
}
}*/