All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.directwebremoting.util.FakeServletConfig Maven / Gradle / Ivy

package org.directwebremoting.util;

import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;

/**
 * A fake implementation of ServletConfig for cases (Like inside Spring) when
 * you don't have a real one, or when you want to modify the initParameters
 * provided by the real {@link ServletConfig}
 * @author Joe Walker [joe at getahead dot ltd dot uk]
 */
public class FakeServletConfig implements ServletConfig
{
    /**
     * @param name The servlet name
     * @param servletContext The ServletContext
     */
    public FakeServletConfig(String name, ServletContext servletContext)
    {
        this(name, servletContext, null);
    }

    /**
     * @param name The servlet name
     * @param servletContext The ServletContext
     * @param initParameters Optional init parameters (can be null)
     */
    public FakeServletConfig(String name, ServletContext servletContext, Map initParameters)
    {
        this.name = name;
        this.servletContext = servletContext;
        this.initParameters = (initParameters != null) ? initParameters : new HashMap();
    }

    /**
     * Copy the values from another {@link ServletConfig} so we can modify them.
     * @param servletConfig ...
     */
    public FakeServletConfig(ServletConfig servletConfig)
    {
        this.name = servletConfig.getServletName();
        this.servletContext = servletConfig.getServletContext();
        this.initParameters = new HashMap();

        for (String key : LocalUtil.iterableizer(servletConfig.getInitParameterNames(), String.class))
        {
            String value = servletConfig.getInitParameter(key);
            initParameters.put(key, value);
        }
    }

    public String getServletName()
    {
        return name;
    }

    public ServletContext getServletContext()
    {
        return servletContext;
    }

    /**
     * Modify an init parameter
     * @param name ...
     * @param value ...
     */
    public void setInitParameter(String name, String value)
    {
        initParameters.put(name, value);
    }

    public String getInitParameter(String paramName)
    {
        Object obj = initParameters.get(paramName);
        if (obj instanceof String)
        {
            return (String) obj;
        }
        else
        {
            return null;
        }
    }

    public Enumeration getInitParameterNames()
    {
        return Collections.enumeration(initParameters.keySet());
    }

    /**
     * The servlet name
     */
    private final String name;

    /**
     * The servlet deployment information
     */
    private final ServletContext servletContext;

    /**
     * Initialization parameters
     */
    private final Map initParameters;
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy