All Downloads are FREE. Search and download functionalities are using the official Maven repository.

scray.common.properties.PropertyFileStorage Maven / Gradle / Ivy

There is a newer version: 1.1.2
Show newest version
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 entry : properties.entrySet()) {

			// verify that all registered properties have the right type
			// (others might have not been registered because some module is not
			// in use)

			String key = (String) entry.getKey();
			String value = (String) entry.getValue();

			for (Property prop : (Collection>) (Object) ScrayProperties
					.getRegisteredProperties()) {

				if (prop.getName().equals(key)) {

					if (!prop.checkConstraints(prop.fromString(value))) {

						throw new RuntimeException(
								new PropertyConstraintViolatedException(key));
					}
				}
			}
		}
	}

	public static String DEFAULT_PROPERTY_FILE = "scray.properties";
	public static String DEFAULT_JVM_ARGUMENT = "scray-properties";
	
	public String toString() {
		return "PropertyFileStorage, location='"+ location + "', size=" + props.size() + ", type=" + fileLocationType;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy