From 2d56ff06766090fdd88b8ec8a0a98bb47b807708 Mon Sep 17 00:00:00 2001 From: George Kalampokis Date: Wed, 15 Jan 2020 12:48:41 +0200 Subject: [PATCH] Devel Configuration Loader will use the classpath to get additional configuration files instead of absolute paths --- .../configloaders/DevelConfigLoader.java | 42 +++++++------------ .../resources/application-devel.properties | 8 ++-- 2 files changed, 18 insertions(+), 32 deletions(-) diff --git a/dmp-backend/web/src/main/java/eu/eudat/logic/proxy/config/configloaders/DevelConfigLoader.java b/dmp-backend/web/src/main/java/eu/eudat/logic/proxy/config/configloaders/DevelConfigLoader.java index 6f613f64f..d2b4b82cc 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/logic/proxy/config/configloaders/DevelConfigLoader.java +++ b/dmp-backend/web/src/main/java/eu/eudat/logic/proxy/config/configloaders/DevelConfigLoader.java @@ -47,42 +47,37 @@ public class DevelConfigLoader implements ConfigLoader { private void setExternalUrls() { String fileUrl = this.environment.getProperty("configuration.externalUrls"); System.out.println("Loaded also config file: " + fileUrl); - String current = null; InputStream is = null; try { - current = new java.io.File(".").getCanonicalPath(); - JAXBContext jaxbContext = JAXBContext.newInstance(ExternalUrls.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); - is = new URL("file:///" + current + fileUrl).openStream(); + is = getClass().getClassLoader().getResource(fileUrl).openStream(); externalUrls = (ExternalUrls) jaxbUnmarshaller.unmarshal(is); } catch (Exception ex) { ex.printStackTrace(); - System.out.println("Cannot find in folder" + current); + System.err.println("Cannot find resource in classpath"); } finally { try { if (is != null) is.close(); - } catch (IOException e) { - System.out.println("Warning: Could not close a stream after reading from file: " + fileUrl); + } catch (IOException | NullPointerException e) { + System.err.println("Warning: Could not close a stream after reading from file: " + fileUrl); } } } private void setRdaProperties() { String filePath = environment.getProperty("configuration.rda"); - String current; BufferedReader reader; List rdaList = new LinkedList<>(); try { - current = new java.io.File(".").getCanonicalPath(); - reader = new BufferedReader(new FileReader(current + filePath)); + reader = new BufferedReader(new FileReader(getClass().getClassLoader().getResource(filePath).getFile())); String line = reader.readLine(); while (line != null) { rdaList.add(line); line = reader.readLine(); } reader.close(); - } catch (IOException e) { + } catch (IOException | NullPointerException e) { e.printStackTrace(); } @@ -91,44 +86,35 @@ public class DevelConfigLoader implements ConfigLoader { private void setDocument() { String filePath = environment.getProperty("configuration.h2020template"); - String current; InputStream is = null; try { - current = new java.io.File(".").getCanonicalPath(); - is = new URL("file:///" + current + filePath).openStream(); + is = getClass().getClassLoader().getResource(filePath).openStream(); this.document = new XWPFDocument(is); - } catch (IOException e) { + } catch (IOException | NullPointerException e) { e.printStackTrace(); } finally { try { if (is != null) is.close(); } 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() { String filePath = environment.getProperty("configuration.configurable_login_providers"); - String current; InputStream is = null; try { - current = new java.io.File(".").getCanonicalPath(); - File tempFile = new File(current + filePath); - if (tempFile.exists()) { - is = new URL("file:///" + current + filePath).openStream(); - 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) { + is = getClass().getClassLoader().getResource(filePath).openStream(); + ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + this.configurableProviders = mapper.readValue(is, ConfigurableProviders.class); + } catch (IOException | NullPointerException e) { e.printStackTrace(); } finally { try { if (is != null) is.close(); } 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); } } } diff --git a/dmp-backend/web/src/main/resources/application-devel.properties b/dmp-backend/web/src/main/resources/application-devel.properties index 00fb05c37..e1c0f7883 100644 --- a/dmp-backend/web/src/main/resources/application-devel.properties +++ b/dmp-backend/web/src/main/resources/application-devel.properties @@ -15,10 +15,10 @@ http-logger.server-address = http://localhost:31311 pdf.converter.url=http://localhost:88/ ####################CONFIGURATION FILES OVERRIDES CONFIGURATIONS########## -configuration.externalUrls=/web/src/main/resources/ExternalUrls.xml -configuration.rda=/web/src/main/resources/RDACommonStandards.txt -configuration.h2020template=/web/src/main/resources/documents/h2020.docx -configuration.configurable_login_providers=/web/src/main/resources/configurableLoginProviders.json +configuration.externalUrls=ExternalUrls.xml +configuration.rda=RDACommonStandards.txt +configuration.h2020template=documents/h2020.docx +configuration.configurable_login_providers=configurableLoginProviders.json #############TWITTER LOGIN CONFIGURATIONS#########