package org.biomoby.registry.properties;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

/**
 * This class incorporates the logic needed to obtain environment variables from
 * Windows NT like machines (XP, 2000, etc).
 * 
 * @author Eddie created Nov 28, 2005
 */
@SuppressWarnings("unchecked")
public class WindowsNT implements Environment {

	/**
	 * the singleton
	 */
	public static final WindowsNT os = new WindowsNT();

	// empty
	private WindowsNT() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.biomoby.registry.properties.Environment#getEnv()
	 */
	public Map getEnv() {
		HashMap map = new HashMap();
		Process p;
		try {
			p = Runtime.getRuntime().exec("cmd /c set");
			BufferedReader br = new BufferedReader(new InputStreamReader(p
					.getInputStream()));
			String line = "";
			while ((line = br.readLine()) != null) {
				String[] contents = line.split("=");
				if (contents.length != 2)
					continue; // continue or break?
				String key = contents[0];
				String value = contents[1];
				map.put(key, value);
			}
			p.waitFor();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
		}
		return map;
	}

	/**
	 * Register the terms that System.getProperty("os.name") returns with this
	 * os
	 * 
	 */
	public static final void register() {
		// register the different names that java returns when
		// java.lang.System.getProperty("os.name") is called
		RegistryOS.register("Windows NT", os);
		RegistryOS.register("Windows 2000", os);
		RegistryOS.register("Windows XP", os);
		RegistryOS.register("Windows 2003", os);
	}

}
