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, U> T get(Property<T, U> 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 {
System.out.println("0" + location + "\n\n\n");
			if (fileLocationType.equals(FileLocationTypes.JarPropertiesFile)) {
				props.load(ScrayProperties.class.getClassLoader()
						.getResourceAsStream(location));
			} else if (fileLocationType
					.equals(FileLocationTypes.LocalPropertiesFile)) {
				System.out.println(location + "\t" + System.getProperty(location));
				if(System.getProperty(location) != null) {
					System.out.println("1" + location + "\n\n\n");
					props.load(new FileInputStream(location));
				} else {
					System.out.println("2" + location + "\t" + System.getenv(location) + "\n\n\n");
					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<Object, Object> 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<Object, Object> prop : (Collection<Property<Object, Object>>) (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;
	}
}

@@ -1108,55 +1108,8 @@
 y %7B%0A
-System.out.println(%220%22 + location + %22%5Cn%5Cn%5Cn%22);%0A
 %09%09%09i
@@ -1362,320 +1362,131 @@
 %09%09%09%09
-System.out.println(location + %22%5Ct%22 + System.getProperty(location));%0A%09%09%09%09if(System.getProperty(location) != null) %7B%0A%09%09%09%09%09System.out.println(%221%22 + location + %22%5Cn%5Cn%5Cn%22);%0A%09%09%09%09%09props.load(new FileInputStream(location));%0A%09%09%09%09%7D else %7B%0A%09%09%09%09%09System.out.println(%222%22 + location + %22%5Ct%22 + System.getenv(location) + %22%5Cn%5Cn%5Cn%22);
+if(System.getProperty(location) != null) %7B%0A%09%09%09%09%09props.load(new FileInputStream(System.getProperty(location)));%0A%09%09%09%09%7D else %7B
 %0A%09%09%09

fix loading of scray-properties and remove some debug output