//----------------------------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.startup;

import java.security.Security;

import org.huihoo.willow.bootstrap.WillowProperties;


public final class SecurityConfig
{
	private static SecurityConfig singleton = null;

	private static org.apache.commons.logging.Log log =
		org.apache.commons.logging.LogFactory.getLog(SecurityConfig.class);

	private final static String PACKAGE_ACCESS =
		"sun.,"+"org.huihoo.willow.";

	private final static String PACKAGE_DEFINITION =
		"java.,sun." + "org.huihoo.workflow." + ",workflow.store.std." + ",workflow.tools." + ",org.huihoo.willow.";
	
	/**
	 * List of protected package from conf/willow.properties
	 */
	private String packageDefinition;

	/**
	 * List of protected package from conf/willow.properties
	 */
	private String packageAccess;

	/**
	 * Create a single instance of this class.
	 */
	private SecurityConfig()
	{
		try
		{
			packageDefinition = WillowProperties.getProperty("package.definition");
			packageAccess = WillowProperties.getProperty("package.access");
		}
		catch (java.lang.Exception ex)
		{
			if (log.isDebugEnabled())
			{
				log.debug("Unable to load properties using WillowProperties", ex);
			}
		}
	}

	/**
	 * Returns the singleton instance of that class.
	 * @return an instance of that class.
	 */
	public static SecurityConfig newInstance()
	{
		if (singleton == null)
		{
			singleton = new SecurityConfig();
		}
		return singleton;
	}

	/**
	 * Set the security package.access value.
	 */
	public void setPackageAccess()
	{
		// If willow.properties is missing, protect all by default.
		if (packageAccess == null)
		{
			setSecurityProperty("package.access", PACKAGE_ACCESS);
		}
		else
		{
			setSecurityProperty("package.access", packageAccess);
		}
	}

	/**
	 * Set the security package.definition value.
	 */
	public void setPackageDefinition()
	{
		// If willow.properties is missing, protect all by default.
		if (packageDefinition == null)
		{
			setSecurityProperty("package.definition", PACKAGE_DEFINITION);
		}
		else
		{
			setSecurityProperty("package.definition", packageDefinition);
		}
	}

	/**
	 * Set the proper security property
	 * @param properties the package.* property.
	 */
	private final void setSecurityProperty(String properties, String packageList)
	{
		if (System.getSecurityManager() != null)
		{
			String definition = Security.getProperty(properties);
			if (definition != null && definition.length() > 0)
			{
				definition += ",";
			}
			
			// FIX ME package "javax." was removed to prevent HotSpot fatal internal errors
			Security.setProperty(properties,definition + packageList);
		}
	}

}