scray.common.properties.PropertyFileStorage Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scray-common Show documentation
Show all versions of scray-common Show documentation
scray artefacts shared between scala and java components
package scray.common.properties;
import java.io.FileInputStream;
import java.util.Collection;
import java.util.Map.Entry;
import java.util.Properties;
/**
* Storage abstraction for property files
*
* @author andreas
*
*/
public class PropertyFileStorage implements PropertyStorage {
private static org.slf4j.Logger log = org.slf4j.LoggerFactory
.getLogger(PropertyFileStorage.class);
private String location = null;
public enum FileLocationTypes {
JarPropertiesFile, LocalPropertiesFile
}
private FileLocationTypes fileLocationType = null;
public PropertyFileStorage(String location,
FileLocationTypes fileLocationType) {
this.location = location;
this.fileLocationType = fileLocationType;
}
private Properties props;
/**
* get the property value out of the
*/
public T get(Property name) {
String value = (String) props.get(name.getName());
if (value != null) {
return name.fromString(value);
} else {
return null;
}
}
/**
* Initializes system with default property file
*/
public void init() {
props = new Properties();
try {
if (fileLocationType.equals(FileLocationTypes.JarPropertiesFile)) {
props.load(ScrayProperties.class.getClassLoader()
.getResourceAsStream(location));
} else if (fileLocationType
.equals(FileLocationTypes.LocalPropertiesFile)) {
if(System.getProperty(location) != null) {
props.load(new FileInputStream(System.getProperty(location)));
} else {
props.load(new FileInputStream(System.getenv(location)));
}
}
checkProperties(props);
} catch (Exception e) {
log.warn("Configuration file '" + location + "' not loaded.");
}
}
@SuppressWarnings("unchecked")
public static void checkProperties(Properties properties) {
for (Entry
© 2015 - 2024 Weber Informatics LLC | Privacy Policy