/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2003 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.dynamo.servlet;

import java.util.*;
import javax.servlet.*;
import com.silverwrist.dynamo.WebConstants;
import com.silverwrist.dynamo.iface.*;

/**
 * The concrete implementation of {@link com.silverwrist.dynamo.iface.ExternalAppAttributes ExternalAppAttributes}
 * for HTTP requests.
 *
 * @author Eric J. Bowersox &lt;erbo@silcom.com&gt;
 * @version X
 */
class ServletExternalAppAttributes implements ExternalAppAttributes
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private ServletContext m_ctxt;         // the servlet context
  private Collection m_names = null;     // internal cache of the "names" collection

  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  /**
   * Constructs a new instance of <CODE>ServletExternalAppAttributes</CODE>.
   *
   * @param ctxt The <CODE>ServletContext</CODE> to be wrapped by this object.
   */
  ServletExternalAppAttributes(ServletContext ctxt)
  {
    m_ctxt = ctxt;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface ExternalAppAttributes
   *--------------------------------------------------------------------------------
   */

  /**
   * Returns a <CODE>java.util.Collection</CODE> containing all names of external application attributes.
   * Application attributes used internally by Dynamo itself will <EM>not</EM> be included in this list.
   *
   * @return The collection of attribute names.
   */
  public synchronized Collection getAttributeNames()
  {
    if (m_names==null)
    { // probe the application attributes
      Enumeration names = m_ctxt.getAttributeNames();
      if (names==null)
	m_names = Collections.EMPTY_LIST;
      else
      { // go through the attributes and strip out the ones with a Dynamo internal prefix
	ArrayList tmp = new ArrayList();
	while (names.hasMoreElements())
	{ // move over the names in turn
	  String n = (String)(names.nextElement());
	  if (!(n.startsWith(WebConstants.APPLICATION_PREFIX)))
	    tmp.add(n);

	} // end while

	if (tmp.isEmpty())
	  m_names = Collections.EMPTY_LIST;
	else
	{ // save off the list
	  tmp.trimToSize();
	  m_names = Collections.unmodifiableList(tmp);

	} // end else

      } // end else

    } // end if

    return m_names;

  } // end getAttributeNames

  /**
   * Returns the value of an external application attribute.  Attempts to get the value of an application
   * attribute used internally by Dynamo itself will return <CODE>null</CODE>.
   *
   * @param name The name of the attribute to retrieve.
   * @return The attribute's value, or <CODE>null</CODE> if the attribute did not exist.
   */
  public Object getAttribute(String name)
  {
    if (name.startsWith(WebConstants.APPLICATION_PREFIX))
      return null;
    else
      return m_ctxt.getAttribute(name);

  } // end getAttribute

  /**
   * Deletes the value of an external application attribute.  Attempts to delete the value of an application
   * attribute used internally by Dynamo itself will be silently ignored.
   *
   * @param name The name of the attribute to remove.
   */
  public void removeAttribute(String name)
  {
    if (!(name.startsWith(WebConstants.APPLICATION_PREFIX)))
    { // invalidate names cache when we remove
      m_ctxt.removeAttribute(name);
      m_names = null;

    } // end if

  } // end removeAttribute

  /**
   * Sets the value of an external application attribute.  Attempts to set the value of an application
   * attribute used internally by Dynamo will be silently ignored.
   *
   * @param name The name of the attribute to set.
   * @param value The new value of the attribute.
   */
  public void setAttribute(String name, Object value)
  {
    if (!(name.startsWith(WebConstants.APPLICATION_PREFIX)))
    { // invalidate names cache when we add
      m_ctxt.setAttribute(name,value);
      m_names = null;

    } // end if

  } // end setAttribute

} // end class ServletExternalAppAttributes
