package eu.eudat.logic.proxy.config.configloaders; import eu.eudat.logic.proxy.config.ExternalUrls; 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.IOException; import java.io.InputStream; import java.net.URL; import java.nio.file.Paths; /** * Created by ikalyvas on 2/9/2018. */ @Service("configLoader") @Profile({ "production", "staging" }) public class ProductionConfigLoader implements ConfigLoader { private ExternalUrls externalUrls; @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(Paths.get(fileUrl).toUri().toURL().toString()).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); } } } public ExternalUrls getExternalUrls() { this.setExternalUrls(); return externalUrls; } }