argos/dmp-backend/src/main/java/eu/eudat/proxy/config/ConfigLoader.java

64 lines
1.9 KiB
Java
Raw Normal View History

2017-12-15 00:01:26 +01:00
package eu.eudat.proxy.config;
2017-12-15 13:25:21 +01:00
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
2017-12-15 00:01:26 +01:00
import org.springframework.stereotype.Service;
2017-11-20 18:01:19 +01:00
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
2017-12-15 00:01:26 +01:00
@Service
public class ConfigLoader {
2017-11-20 18:01:19 +01:00
private ExternalUrls externalUrls;
2017-12-15 13:25:21 +01:00
@Autowired
private Environment environment;
2017-11-20 18:01:19 +01:00
// public static void main(String [] args) {
// ConfigLoader l = new ConfigLoader("file:///home/nikolas/git/OpenAIRE-EUDAT-DMP/dmp-backend/src/main/resources/ExternalUrls.xml");
2017-11-20 18:01:19 +01:00
// }
2017-12-15 00:01:26 +01:00
public ConfigLoader() {
}
2017-12-15 13:25:21 +01:00
private void setExternalUrls() {
String fileUrl = this.environment.getProperty("configuration.externalUrls");
System.out.println("Loaded also config file: " + fileUrl);
2018-01-17 13:03:51 +01:00
String current=null;
InputStream is = null;
2017-11-20 18:01:19 +01:00
try {
2018-01-17 13:03:51 +01:00
current = new java.io.File( "." ).getCanonicalPath();
2017-12-15 13:25:21 +01:00
JAXBContext jaxbContext = JAXBContext.newInstance(ExternalUrls.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
2018-01-19 15:19:14 +01:00
//is = new URL("file:///"+System.getenv("CATALINA_HOME")+fileUrl).openStream();
is = new URL("file:///C:/Users/ikalyvas/Documents/Projects/OpenAIRE-EUDAT-DMP-service-pilot/dmp-backend/src/main/resources/ExternalUrls.xml").openStream();
2017-12-15 13:25:21 +01:00
externalUrls = (ExternalUrls) jaxbUnmarshaller.unmarshal(is);
2018-01-18 11:27:30 +01:00
// System.out.println(new ObjectMapper().writeValueAsString(externalUrls));
2017-12-15 13:25:21 +01:00
} catch (Exception ex) {
2018-01-18 11:27:30 +01:00
//log the error and shutdown the system (that's a critical error)
2017-12-15 13:25:21 +01:00
ex.printStackTrace();
2018-01-17 13:03:51 +01:00
System.out.println("Cannot find in folder"+current);
2017-12-15 13:25:21 +01:00
} finally {
try {
if (is != null) is.close();
} catch (IOException e) {
System.out.println("Warning: Could not close a stream after reading from file: " + fileUrl);
}
}
2017-11-20 18:01:19 +01:00
}
public ExternalUrls getExternalUrls() {
2017-12-15 13:25:21 +01:00
this.setExternalUrls();
return externalUrls;
}
2017-11-20 18:01:19 +01:00
}