/*
 * Created on 2004-9-5
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package org.huihoo.willow.core;

import java.util.Enumeration;
import java.util.Hashtable;

import org.huihoo.willow.util.StringManager;

import workflow.script.core.ScriptConfig;
import workflow.script.core.ScriptContext;

/**
 * @author reic
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class ApplicationConfig implements ScriptConfig
{
	public ApplicationConfig(ApplicationContext scriptContext)
	{
		this.scriptContext=scriptContext;
	}
	
	/**
	 * The ScriptContext instance with which we are associated.
	 */	
	private ApplicationContext scriptContext;
	
	/**
	 * The  initialization parameters for this ScriptConfig.
	 */
	private Hashtable parameters = new Hashtable();
	
	/**
	 * The string manager for this package.
	 */
	private static final StringManager sm = StringManager.getManager(Constants.PACKAGE);
		
	public ScriptContext getScriptContext()
	{
		return scriptContext;
	}

	/**
	 * Return the value for the specified config initialization
	 * parameter name, if any; otherwise return <code>null</code>.
	 *
	 * @param name Name of the parameter to return
	 */
	public String getInitParameter(String name)
	{
		return (String)parameters.get(name);
	}

	/**
	 * Return the names of all defined context initialization parameters
	 * for this config. 
	 */
	public Enumeration getInitParameterNames()
	{
		return parameters.keys();
	}
	
	/**
	 * Add a new context initialization parameter.
	 *
	 * @param name Name of the new parameter
	 * @param value Value of the new  parameter
	 *
	 * @exception IllegalArgumentException if the name or value is missing,
	 *  or if this context initialization parameter has already been
	 *  registered
	 */
	public void addInitParameter(String name, String value)
	{	
		// Validate the proposed context initialization parameter
		if ((name == null) || (value == null))
		{
			throw new IllegalArgumentException(sm.getString("scriptConfig.parameter.required"));
		}
		if (parameters.get(name) != null)
		{
			throw new IllegalArgumentException(sm.getString("scriptConfig.parameter.duplicate", name));
		}

		// Add this parameter to our defined set
		synchronized (parameters)
		{
			parameters.put(name, value);
		}
	}
}
