Improve configuration loader
This commit is contained in:
parent
99e4d231c2
commit
fc7c39081a
|
@ -8,7 +8,6 @@ import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.annotation.Profile;
|
|
||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.w3c.dom.Document;
|
import org.w3c.dom.Document;
|
||||||
|
@ -26,16 +25,13 @@ import javax.xml.xpath.XPathConstants;
|
||||||
import javax.xml.xpath.XPathExpressionException;
|
import javax.xml.xpath.XPathExpressionException;
|
||||||
import javax.xml.xpath.XPathFactory;
|
import javax.xml.xpath.XPathFactory;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.util.HashMap;
|
import java.util.*;
|
||||||
import java.util.LinkedList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Service("configLoader")
|
@Service("configLoader")
|
||||||
@Profile("devel")
|
//@Profile("devel")
|
||||||
public class DevelConfigLoader implements ConfigLoader {
|
public class DefaultConfigLoader implements ConfigLoader {
|
||||||
private static final Logger logger = LoggerFactory.getLogger(DevelConfigLoader.class);
|
private static final Logger logger = LoggerFactory.getLogger(DefaultConfigLoader.class);
|
||||||
|
|
||||||
private ExternalUrls externalUrls;
|
private ExternalUrls externalUrls;
|
||||||
private List<String> rdaProperties;
|
private List<String> rdaProperties;
|
||||||
|
@ -53,10 +49,10 @@ public class DevelConfigLoader implements ConfigLoader {
|
||||||
try {
|
try {
|
||||||
JAXBContext jaxbContext = JAXBContext.newInstance(ExternalUrls.class);
|
JAXBContext jaxbContext = JAXBContext.newInstance(ExternalUrls.class);
|
||||||
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
|
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
|
||||||
is = getClass().getClassLoader().getResource(fileUrl).openStream();
|
is = getStreamFromPath(fileUrl);
|
||||||
externalUrls = (ExternalUrls) jaxbUnmarshaller.unmarshal(is);
|
externalUrls = (ExternalUrls) jaxbUnmarshaller.unmarshal(is);
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
logger.error("Cannot find resource in classpath", ex);
|
logger.error("Cannot find resource", ex);
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
if (is != null) is.close();
|
if (is != null) is.close();
|
||||||
|
@ -89,7 +85,7 @@ public class DevelConfigLoader implements ConfigLoader {
|
||||||
String filePath = environment.getProperty("configuration.h2020template");
|
String filePath = environment.getProperty("configuration.h2020template");
|
||||||
InputStream is = null;
|
InputStream is = null;
|
||||||
try {
|
try {
|
||||||
is = getClass().getClassLoader().getResource(filePath).openStream();
|
is = getStreamFromPath(filePath);
|
||||||
this.document = new XWPFDocument(is);
|
this.document = new XWPFDocument(is);
|
||||||
} catch (IOException | NullPointerException e) {
|
} catch (IOException | NullPointerException e) {
|
||||||
logger.error(e.getMessage(), e);
|
logger.error(e.getMessage(), e);
|
||||||
|
@ -106,7 +102,7 @@ public class DevelConfigLoader implements ConfigLoader {
|
||||||
String filePath = environment.getProperty("configuration.configurable_login_providers");
|
String filePath = environment.getProperty("configuration.configurable_login_providers");
|
||||||
InputStream is = null;
|
InputStream is = null;
|
||||||
try {
|
try {
|
||||||
is = getClass().getClassLoader().getResource(filePath).openStream();
|
is = getStreamFromPath(filePath);
|
||||||
ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||||
this.configurableProviders = mapper.readValue(is, ConfigurableProviders.class);
|
this.configurableProviders = mapper.readValue(is, ConfigurableProviders.class);
|
||||||
} catch (IOException | NullPointerException e) {
|
} catch (IOException | NullPointerException e) {
|
||||||
|
@ -145,12 +141,34 @@ public class DevelConfigLoader implements ConfigLoader {
|
||||||
|
|
||||||
|
|
||||||
public ExternalUrls getExternalUrls() {
|
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;
|
return externalUrls;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> getRdaProperties() {
|
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;
|
return rdaProperties;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -160,11 +178,34 @@ public class DevelConfigLoader implements ConfigLoader {
|
||||||
}
|
}
|
||||||
|
|
||||||
public ConfigurableProviders getConfigurableProviders() {
|
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;
|
return configurableProviders;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, String> getKeyToSourceMap() {
|
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();
|
this.setKeyToSourceMap();
|
||||||
return keyToSourceMap;
|
return keyToSourceMap;
|
||||||
}
|
}
|
||||||
|
@ -173,7 +214,7 @@ public class DevelConfigLoader implements ConfigLoader {
|
||||||
InputStream is = null;
|
InputStream is = null;
|
||||||
Document doc;
|
Document doc;
|
||||||
try {
|
try {
|
||||||
is = getClass().getClassLoader().getResource(filePath).openStream();
|
is = getStreamFromPath(filePath);
|
||||||
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||||
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
|
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
|
||||||
doc = documentBuilder.parse(is);
|
doc = documentBuilder.parse(is);
|
||||||
|
@ -211,4 +252,13 @@ public class DevelConfigLoader implements ConfigLoader {
|
||||||
}
|
}
|
||||||
return values;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -33,6 +33,7 @@ import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
/*
|
||||||
@Service("configLoader")
|
@Service("configLoader")
|
||||||
@Profile({ "production", "staging" })
|
@Profile({ "production", "staging" })
|
||||||
public class ProductionConfigLoader implements ConfigLoader {
|
public class ProductionConfigLoader implements ConfigLoader {
|
||||||
|
@ -236,4 +237,4 @@ public class ProductionConfigLoader implements ConfigLoader {
|
||||||
}
|
}
|
||||||
return values;
|
return values;
|
||||||
}
|
}
|
||||||
}
|
}*/
|
||||||
|
|
Loading…
Reference in New Issue