/* Copyright (c) 2001 Oracle Corporation. All Rights Reserved. */
package oracle.portal.provider.v1.http;

import java.util.*;
import javax.servlet.*;

import oracle.portal.utils.v1.*;

/**
 * A <code>ServletProviderContext</code> contains a set of parameters
 * pertaining to the state of the context in which the {@link
 * oracle.portal.provider.v1.Provider} is running. The values are
 * <code>Object</code>s mapped to <code>String</code> names. These parameters
 * may be altered by use of the appropriate methods.
 */
public class ServletProviderContext implements ParameterMap
{

    private Hashtable mParameters;
    private ServletConfig mSc;

    /**
     * Constructs this <code>ServletProviderContext</code> object.
     *
     * @param sc the <code>ServletConfig</code> object.
     */
    public ServletProviderContext(ServletConfig sc)
    {
        mParameters = new Hashtable();
        mSc = sc;
        mParameters.put(HttpProvider.SERVLET_CONFIG, sc);
    }

    /**
     * Gets the parameter mapped to the supplied name. If a matching parameter
     * is not found, null is returned.
     *
     * @param parameterName the parameter name.
     */
    public Object getParameterValue(String parameterName)
    {
        if (mParameters.containsKey(parameterName))
        {
            return mParameters.get(parameterName);
        }
        else
        {
            return mSc.getInitParameter(parameterName);
        }
    }

    /**
     * Puts the specified parameter into this
     * <code>ServletProviderContext</code> mapped to the specified name.
     *
     * @param parameterName parameter name mapped to the specified parameter.
     * @param parameterValue parameter to be mapped to specified name.
     */
    public void putParameter(String parameterName, Object parameterValue)
    {
        mParameters.put(parameterName, parameterValue);
    }

    /**
     * Returns an <code>Enumeration</code> of the parameter names belonging to
     * this <code>ServletProviderContext</code> object, this may then be used
     * to fetch the parameters sequentially.
     *
     * @return an <code>Enumeration</code> of the parameter names.
     */
    public Enumeration getParameterNames()
    {
        //Store enumeration from Hashtable and ServletConfig in a vector, then
        //return enumeration of that vector.
        Vector v = new Vector();
        Enumeration e1 = mParameters.keys();
        Enumeration e2 = mSc.getInitParameterNames();
        while (e1.hasMoreElements())
        {
            v.addElement(e1.nextElement());
        }
        while (e2.hasMoreElements())
        {
            v.addElement(e2.nextElement());
        }
        return v.elements();
    }

    /**
     * Returns true if this <code>ServletProviderContext</code> contains a
     * mapping to the specified name.
     *
     * @param parameterName parameter name to search for.
     * @return true if a mapping to the specified name is found, false
     * otherwise.
     */
    public boolean containsParameter(String parameterName)
    {
        boolean contains = mParameters.containsKey(parameterName);
        if (!contains)
        {
            contains = (mSc.getInitParameter(parameterName) != null);
        }
        return contains;
    }

}

