62 lines
1.9 KiB
Java
62 lines
1.9 KiB
Java
package eu.eudat.depositinterface.ckanrepository.config;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.core.env.Environment;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.io.FileInputStream;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
|
|
@Service("ckanConfigLoader")
|
|
public class ConfigLoaderImpl implements ConfigLoader{
|
|
private static final Logger logger = LoggerFactory.getLogger(ConfigLoaderImpl.class);
|
|
private static final ObjectMapper mapper = new ObjectMapper();
|
|
|
|
private CkanConfig ckanConfig;
|
|
|
|
@Autowired
|
|
private Environment environment;
|
|
|
|
@Override
|
|
public CkanConfig getCkanConfig() {
|
|
if(ckanConfig == null){
|
|
try{
|
|
ckanConfig = mapper.readValue(getStreamFromPath(environment.getProperty("configuration.ckan")), CkanConfig.class);
|
|
} catch (IOException e) {
|
|
logger.error(e.getLocalizedMessage(), e);
|
|
}
|
|
}
|
|
return ckanConfig;
|
|
}
|
|
|
|
@Override
|
|
public byte[] getLogo() {
|
|
String logo = environment.getProperty("configuration.logo");
|
|
if(logo != null && !logo.isEmpty()){
|
|
InputStream logoStream = getStreamFromPath(logo);
|
|
try {
|
|
return logoStream.readAllBytes();
|
|
}
|
|
catch (IOException e){
|
|
logger.error(e.getMessage(), e);
|
|
return null;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private InputStream getStreamFromPath(String filePath) {
|
|
try {
|
|
return new FileInputStream(filePath);
|
|
} catch (FileNotFoundException e) {
|
|
logger.info("loading from classpath");
|
|
return getClass().getClassLoader().getResourceAsStream(filePath);
|
|
}
|
|
}
|
|
}
|