//----------------------------BEGIN LICENSE----------------------------
/* 
 * Willow : the Open Source WorkFlow Project
 * Distributable under GNU LGPL license by gun.org
 *
 * Copyright (C) 2004-2010  huihoo.org 
 * Copyright (C) 2004-2010  ZosaTapo <dertyang@hotmail.com> 
 * 
 * ====================================================================
 * Project Homepage : http://www.huihoo.org/willow
 * Source Forge     : http://sourceforge.net/projects/huihoo
 * Mailing list     : willow@lists.sourceforge.net 
 */
//----------------------------END  LICENSE-----------------------------
package org.huihoo.willow.bootstrap;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.Properties;

public class WillowProperties
{
	// ------------------------------------------------------- Static Variables

	private static Properties properties = null;

	static {

		loadProperties();

	}

	// --------------------------------------------------------- Public Methods

	/**
	 * Return specified property value.
	 */
	public static String getProperty(String name)
	{

		return properties.getProperty(name);

	}

	/**
	 * Return specified property value.
	 */
	public static String getProperty(String name, String defaultValue)
	{

		return properties.getProperty(name, defaultValue);

	}

	// --------------------------------------------------------- Public Methods

	/**
	 * Load properties.
	 */
	private static void loadProperties()
	{

		InputStream is = null;
		Throwable error = null;

		try
		{
			String configUrl = getConfigUrl();
			if (configUrl != null)
			{
				is = (new URL(configUrl)).openStream();
			}
		}
		catch (Throwable t)
		{
			// Ignore
		}

		if (is == null)
		{
			try
			{
				File home = new File(getWillowHome());
				File conf = new File(home, "conf");
				File properties = new File(conf, "willow.properties");
				is = new FileInputStream(properties);
			}
			catch (Throwable t)
			{
				// Ignore
			}
		}

		if (is == null)
		{
			try
			{
				is =
					WillowProperties.class.getResourceAsStream(
						"/org/huihoo/willow/startup/willow.properties");
			}
			catch (Throwable t)
			{
				// Ignore
			}
		}

		if (is != null)
		{
			try
			{
				properties = new Properties();
				properties.load(is);
				is.close();
			}
			catch (Throwable t)
			{
				error = t;
			}
		}

		if ((is == null) || (error != null))
		{
			// Do something
			System.out.println("Error");
		}

		// Register the properties as system properties
		Enumeration enum = properties.propertyNames();
		while (enum.hasMoreElements())
		{
			String name = (String) enum.nextElement();
			String value = properties.getProperty(name);
			if (value != null)
			{
				System.setProperty(name, value);
			}
		}

	}

	/**
	 * Get the value of the willow.home environment variable.
	 */
	private static String getWillowHome()
	{
		return System.getProperty("willow.home", System.getProperty("user.dir"));
	}

	/**
	 * Get the value of the configuration URL.
	 */
	private static String getConfigUrl()
	{
		return System.getProperty("willow.config");
	}

}
