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

64 lines
1.9 KiB
Java

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);
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:///"+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();
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.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;
}
}