Devel Configuration Loader will use the classpath to get additional configuration files instead of absolute paths

This commit is contained in:
George Kalampokis 2020-01-15 12:48:41 +02:00
parent df07884cbe
commit 2d56ff0676
2 changed files with 18 additions and 32 deletions

View File

@ -47,42 +47,37 @@ public class DevelConfigLoader implements ConfigLoader {
private void setExternalUrls() { private void setExternalUrls() {
String fileUrl = this.environment.getProperty("configuration.externalUrls"); String fileUrl = this.environment.getProperty("configuration.externalUrls");
System.out.println("Loaded also config file: " + fileUrl); System.out.println("Loaded also config file: " + fileUrl);
String current = null;
InputStream is = null; InputStream is = null;
try { try {
current = new java.io.File(".").getCanonicalPath();
JAXBContext jaxbContext = JAXBContext.newInstance(ExternalUrls.class); JAXBContext jaxbContext = JAXBContext.newInstance(ExternalUrls.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
is = new URL("file:///" + current + fileUrl).openStream(); is = getClass().getClassLoader().getResource(fileUrl).openStream();
externalUrls = (ExternalUrls) jaxbUnmarshaller.unmarshal(is); externalUrls = (ExternalUrls) jaxbUnmarshaller.unmarshal(is);
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
System.out.println("Cannot find in folder" + current); System.err.println("Cannot find resource in classpath");
} finally { } finally {
try { try {
if (is != null) is.close(); if (is != null) is.close();
} catch (IOException e) { } catch (IOException | NullPointerException e) {
System.out.println("Warning: Could not close a stream after reading from file: " + fileUrl); System.err.println("Warning: Could not close a stream after reading from file: " + fileUrl);
} }
} }
} }
private void setRdaProperties() { private void setRdaProperties() {
String filePath = environment.getProperty("configuration.rda"); String filePath = environment.getProperty("configuration.rda");
String current;
BufferedReader reader; BufferedReader reader;
List<String> rdaList = new LinkedList<>(); List<String> rdaList = new LinkedList<>();
try { try {
current = new java.io.File(".").getCanonicalPath(); reader = new BufferedReader(new FileReader(getClass().getClassLoader().getResource(filePath).getFile()));
reader = new BufferedReader(new FileReader(current + filePath));
String line = reader.readLine(); String line = reader.readLine();
while (line != null) { while (line != null) {
rdaList.add(line); rdaList.add(line);
line = reader.readLine(); line = reader.readLine();
} }
reader.close(); reader.close();
} catch (IOException e) { } catch (IOException | NullPointerException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -91,44 +86,35 @@ public class DevelConfigLoader implements ConfigLoader {
private void setDocument() { private void setDocument() {
String filePath = environment.getProperty("configuration.h2020template"); String filePath = environment.getProperty("configuration.h2020template");
String current;
InputStream is = null; InputStream is = null;
try { try {
current = new java.io.File(".").getCanonicalPath(); is = getClass().getClassLoader().getResource(filePath).openStream();
is = new URL("file:///" + current + filePath).openStream();
this.document = new XWPFDocument(is); this.document = new XWPFDocument(is);
} catch (IOException e) { } catch (IOException | NullPointerException e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
try { try {
if (is != null) is.close(); if (is != null) is.close();
} catch (IOException e) { } catch (IOException e) {
System.out.println("Warning: Could not close a stream after reading from file: " + filePath); System.err.println("Warning: Could not close a stream after reading from file: " + filePath);
} }
} }
} }
private void setConfigurableProviders() { private void setConfigurableProviders() {
String filePath = environment.getProperty("configuration.configurable_login_providers"); String filePath = environment.getProperty("configuration.configurable_login_providers");
String current;
InputStream is = null; InputStream is = null;
try { try {
current = new java.io.File(".").getCanonicalPath(); is = getClass().getClassLoader().getResource(filePath).openStream();
File tempFile = new File(current + filePath); ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
if (tempFile.exists()) { this.configurableProviders = mapper.readValue(is, ConfigurableProviders.class);
is = new URL("file:///" + current + filePath).openStream(); } catch (IOException | NullPointerException e) {
ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
this.configurableProviders = mapper.readValue(is, ConfigurableProviders.class);
} else {
this.configurableProviders = new ConfigurableProviders();
}
} catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
try { try {
if (is != null) is.close(); if (is != null) is.close();
} catch (IOException e) { } catch (IOException e) {
System.out.println("Warning: Could not close a stream after reading from file: " + filePath); System.err.println("Warning: Could not close a stream after reading from file: " + filePath);
} }
} }
} }

View File

@ -15,10 +15,10 @@ http-logger.server-address = http://localhost:31311
pdf.converter.url=http://localhost:88/ pdf.converter.url=http://localhost:88/
####################CONFIGURATION FILES OVERRIDES CONFIGURATIONS########## ####################CONFIGURATION FILES OVERRIDES CONFIGURATIONS##########
configuration.externalUrls=/web/src/main/resources/ExternalUrls.xml configuration.externalUrls=ExternalUrls.xml
configuration.rda=/web/src/main/resources/RDACommonStandards.txt configuration.rda=RDACommonStandards.txt
configuration.h2020template=/web/src/main/resources/documents/h2020.docx configuration.h2020template=documents/h2020.docx
configuration.configurable_login_providers=/web/src/main/resources/configurableLoginProviders.json configuration.configurable_login_providers=configurableLoginProviders.json
#############TWITTER LOGIN CONFIGURATIONS######### #############TWITTER LOGIN CONFIGURATIONS#########