package eu.eudat.proxy.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.stereotype.Service; import java.io.IOException; import java.io.InputStream; import java.net.URL; import javax.xml.bind.JAXBContext; import javax.xml.bind.Unmarshaller; @Service public class ConfigLoader { private ExternalUrls externalUrls; @Autowired private Environment environment; // public static void main(String [] args) { // ConfigLoader l = new ConfigLoader("file:///home/nikolas/git/OpenAIRE-EUDAT-DMP/dmp-backend/src/main/resources/ExternalUrls.xml"); // } public ConfigLoader() { } private void setExternalUrls() { String fileUrl = this.environment.getProperty("configuration.externalUrls"); System.out.println("Loaded also config file: " + fileUrl); InputStream is = null; try { JAXBContext jaxbContext = JAXBContext.newInstance(ExternalUrls.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); is = new URL(fileUrl).openStream(); externalUrls = (ExternalUrls) jaxbUnmarshaller.unmarshal(is); // System.out.println(new ObjectMapper().writeValueAsString(externalUrls)); } catch (Exception ex) { //log the error and shutdown the system (that's a critical error) ex.printStackTrace(); System.exit(0); } finally { try { if (is != null) is.close(); } catch (IOException e) { System.out.println("Warning: Could not close a stream after reading from file: " + fileUrl); } } } public ExternalUrls getExternalUrls() { this.setExternalUrls(); return externalUrls; } }