package net.dryanhild.resources.impl;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Properties;
import net.dryanhild.resources.ResourceLoader;
import net.dryanhild.resources.ResourceNotFoundException;
import net.dryanhild.resources.ResourceService;
import net.dryanhild.resources.UserLocalSettings;
import net.dryanhild.resources.impl.loaders.ClassResourceLoader;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
*
* @author D. Ryan Hild
*/
@Service
public final class ResourceServiceImpl implements ResourceService {
private final Logger logger = LogManager.getFormatterLogger(ResourceServiceImpl.class);
@Autowired
private List<ResourceLoader> resourceLoaders;
@Autowired
private UserLocalSettings userLocalSettings;
public ResourceServiceImpl() {
announceServiceVersion();
}
private void announceServiceVersion() {
Properties props = new Properties();
ClassResourceLoader loader = new ClassResourceLoader();
String version;
try (InputStream in = loader.getResource("/META-INF/maven/net.dryanhild/resources/pom.properties")) {
props.load(in);
version = props.getProperty("version");
} catch (Exception e) {
version = "UNKNOWN";
}
logger.trace("Using resource library version %s", version);
}
/*
* (non-Javadoc)
*
* @see
* net.dryanhild.resources.impl.ResourceService#addLoader(net.dryanhild.
* resources.ResourceLoader)
*/
@Override
public void addLoader(ResourceLoader delegate) {
resourceLoaders.add(delegate);
}
/*
* (non-Javadoc)
*
* @see
* net.dryanhild.resources.impl.ResourceService#getResource(java.lang.String
* )
*/
@Override
public InputStream getResource(String path) {
InputStream in = loadFromDelegates(path);
if (in == null) {
throw new ResourceNotFoundException(path);
}
return in;
}
/*
* (non-Javadoc)
*
* @see
* net.dryanhild.resources.impl.ResourceService#getResourceReader(java.lang
* .String)
*/
@Override
public Reader getResourceReader(String path) {
return new InputStreamReader(getResource(path), StandardCharsets.UTF_8);
}
/*
* (non-Javadoc)
*
* @see
* net.dryanhild.resources.impl.ResourceService#getUserSettingsOutput(java
* .lang.String)
*/
@Override
public OutputStream getUserSettingsOutput(String path) throws ResourceNotFoundException {
try {
File file = userLocalSettings.getUserSettingsFile(path);
return new FileOutputStream(file);
} catch (FileNotFoundException e) {
throw new ResourceNotFoundException(path, e);
}
}
/*
* (non-Javadoc)
*
* @see
* net.dryanhild.resources.impl.ResourceService#getUserSettingsInput(java
* .lang.String)
*/
@Override
public InputStream getUserSettingsInput(String path) throws ResourceNotFoundException {
try {
File file = userLocalSettings.getUserSettingsFile(path);
return new FileInputStream(file);
} catch (FileNotFoundException e) {
throw new ResourceNotFoundException(path, e);
}
}
/*
* (non-Javadoc)
*
* @see
* net.dryanhild.resources.impl.ResourceService#doesUserFileExist(java.lang
* .String)
*/
@Override
public boolean doesUserFileExist(String path) {
File file = userLocalSettings.getUserSettingsFile(path);
return file.exists();
}
private InputStream loadFromDelegates(String path) {
for (ResourceLoader r : resourceLoaders) {
try {
InputStream in = r.getResource(path);
if (in != null) {
return in;
}
} catch (Exception e) {
// If this loader fails, just keep trying.
}
}
return null;
}
}