//----------------------------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.workflow.selector;
import java.util.Enumeration;
import java.util.HashMap;

import org.huihoo.commons.util.Enumerator;
import org.huihoo.workflow.util.StringManager;
import org.huihoo.workflow.xpdl.WorkflowParameter;

public class SelectorContext
{
	/**
	 * The string manager for this package.
	 */
	private static final StringManager sm = StringManager.getManager(Constants.PACKAGE);


	/**
	 * The merged context initialization parameters for this Context.
	 */
	private HashMap parameters = new HashMap();
	
	/**
	 * The context attributes for this context.
	 */
	private HashMap attributes = new HashMap();

	/**
	 * List of read only attributes for this context.
	 */
	private HashMap readOnlyAttributes = new HashMap();
	
	public SelectorContext()
	{
	}
	
	public WorkflowParameter getParameter(String name)
	{
		return (WorkflowParameter)parameters.get(name);	
	}
	
	public void addParameter(WorkflowParameter param)
	{
		parameters.put(param.getName(),param);	
	}
	
	public Enumeration getParameterNames()
	{
		return (new Enumerator(parameters.keySet()));
	}
	

	public Object getAttribute(String name)
	{
		synchronized (attributes)
		{
			return (attributes.get(name));
		}
	}

	public Enumeration getAttributeNames()
	{
		synchronized (attributes)
		{
			return new Enumerator(attributes.keySet(), true);
		}
	}

	public void setAttribute(String name, Object value)
	{

		// Name cannot be null
		if (name == null)
		{
			throw new IllegalArgumentException(sm.getString("selectorContext.setAttribute.namenull"));
		}

		// Null value is the same as removeAttribute()
		if (value == null)
		{
			removeAttribute(name);
			return;
		}

		// Add or replace the specified attribute
		synchronized (attributes)
		{
			// Check for read only attribute
			if (readOnlyAttributes.containsKey(name))
			{
				return;
			}
			attributes.put(name, value);
		}
	}

	/**
	 * Set an attribute as read only.
	 */
	void setAttributeReadOnly(String name)
	{

		synchronized (attributes)
		{
			if (attributes.containsKey(name))
			{
				readOnlyAttributes.put(name, name);
			}
		}

	}
	
	public void removeAttribute(String name)
	{
		boolean found = false;

		// Remove the specified attribute
		synchronized (attributes)
		{
			// Check for read only attribute
			if (readOnlyAttributes.containsKey(name))
			{
				return;
			}
			found = attributes.containsKey(name);
			if (found)
			{
				attributes.remove(name);
			}
			else
			{
				return;
			}
		}
	}
}
