cz.jalasoft.util.configuration.PropertiesLoader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of JalasoftUtils Show documentation
Show all versions of JalasoftUtils Show documentation
A collection of utility classes that might be useful.
package cz.jalasoft.util.configuration;
import cz.jalasoft.util.configuration.annotation.SourceType;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;
/**
* A loader of {@link java.util.Properties} based on
* location properties.
*
* @author Honza Lastovicka
*/
abstract class PropertiesLoader {
static PropertiesLoader of(SourceType type) {
switch(type) {
case FILESYSTEM:
return new PropertiesLoader() {
@Override
InputStream from(String name) throws IOException {
Path path = Paths.get(name);
return Files.newInputStream(path);
}
};
case CLASSPATH:
return new PropertiesLoader() {
@Override
InputStream from(String name) throws IOException {
return ClassLoader.getSystemResourceAsStream(name);
}
};
default:
throw new IllegalArgumentException("Unexpected type of source: " + type);
}
}
final Properties load(String name) throws IOException {
Properties properties = new Properties();
properties.load(from(name));
return properties;
}
abstract InputStream from(String name) throws IOException;
}