package org.biomoby.registry.properties;

import java.util.Map;

/**
 * This class delegates the responisiblity for obtaining system environment
 * variables to other classes. The classes UNIX.java, Windows.java, etc, all
 * implement this factory pattern and use the Singleton pattern.
 * <p>
 * <b>Please note</b> that this class attempts to mimic the functionality that
 * was inherent in java up until java 1.4 and then brought back in java 5.0.
 * <p>
 * 
 * <pre>
 * public class MyOS implements Environment {
 * 	public static final MyOS os = new MyOS();
 * 
 * 	private MyOS() {
 * 	}
 * 
 * 	public Map getEnv() {
 * 		// perform os specific code to get environment variables as key,value pairs
 * 		// to put in a map
 * 	}
 * 
 * 	public static final void register() {
 * 		RegistryOS.register(&quot;value obtained using jvm property 'os.name'&quot;, os);
 * 	}
 * }
 * </pre>
 * 
 * <p>
 * Then just let RegistryOS know about your class, by putting a
 * 
 * <pre>
 * MyOS.register();
 * </pre>
 * 
 * in the the static block of this class.
 * <p>
 * To use this class, on any <i>supported</i> OS, do the following:
 * 
 * <pre>
 * java.util.Map map = RegistryOS.getEnv();
 * // lets retrieve the local registries url
 * String server = (String) map.get(&quot;MOBY_SERVER&quot;);
 * </pre>
 * 
 * @author Eddie created Nov 28, 2005
 */
@SuppressWarnings("unchecked")
public class RegistryOS {
	// factory pattern
	private static java.util.HashMap storage = new java.util.HashMap();

	/**
	 * 
	 * @return a java.util.Map object containing the OS environment variables as
	 *         key,value pairs
	 */
	public static Map getEnv() {
		Environment value = (Environment) storage.get(System
				.getProperty("os.name"));
		if (value == null)
			return DefaultOS.os.getEnv();
		return value.getEnv();
	}

	/**
	 * 
	 * @param key
	 *            the value obtained by the jvm using the property 'os.name' for
	 *            a specific machine
	 * @param value
	 *            a java Object that implements Environment.java
	 */
	public static void register(final String key, final Environment value) {
		storage.put(key, value);
	}

	/*
	 * register os here.
	 */
	static {
		// add the OS's here
		WindowsNT.register();
		Windows.register();
		UNIX.register();
		MacOS.register();
	}

}
