package eu.eudat.logic.proxy.config.configloaders; import eu.eudat.logic.proxy.config.ExternalUrls; import org.apache.poi.xwpf.usermodel.XWPFDocument; 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 javax.xml.bind.JAXBContext; import javax.xml.bind.Unmarshaller; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.LinkedList; import java.util.List; @Service("configLoader") @Profile("devel") public class DevelConfigLoader implements ConfigLoader { private ExternalUrls externalUrls; private List rdaProperties; private XWPFDocument document; @Autowired private Environment environment; 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(); externalUrls = (ExternalUrls) jaxbUnmarshaller.unmarshal(is); } catch (Exception ex) { ex.printStackTrace(); System.out.println("Cannot find in folder" + current); } finally { try { if (is != null) is.close(); } catch (IOException e) { System.out.println("Warning: Could not close a stream after reading from file: " + fileUrl); } } } private void setRdaProperties() { String filePath = environment.getProperty("configuration.rda"); String current = null; BufferedReader reader; List rdaList = new LinkedList<>(); try { current = new java.io.File(".").getCanonicalPath(); reader = new BufferedReader(new FileReader(current + filePath)); String line = reader.readLine(); while (line != null) { rdaList.add(line); line = reader.readLine(); } reader.close(); } catch (IOException e) { e.printStackTrace(); } rdaProperties = rdaList; } private void setDocument() { String filePath = environment.getProperty("configuration.h2020template"); String current = null; try { current = new java.io.File(".").getCanonicalPath(); InputStream is = new URL("file:///" + current + filePath).openStream(); this.document = new XWPFDocument(is); } catch (IOException e) { e.printStackTrace(); } } public ExternalUrls getExternalUrls() { this.setExternalUrls(); return externalUrls; } public List getRdaProperties() { this.setRdaProperties(); return rdaProperties; } public XWPFDocument getDocument() { this.setDocument(); return document; } }