/*
 * 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 javax.servlet.http.*;
import com.silverwrist.dynamo.WebConstants;
import com.silverwrist.dynamo.iface.*;

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

  private HttpSession m_session;            // wrapped HTTP session object
  private Collection m_names = null;        // cache of attribute names

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

  /**
   * Constructs a new instance of <CODE>ServletExternalSessionAttributes</CODE>.
   *
   * @param session The <CODE>HttpSession<CODE> wrapped by this object.
   */
  ServletExternalSessionAttributes(HttpSession session)
  {
    m_session = session;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface ExternalSessionAttributes
   *--------------------------------------------------------------------------------
   */

  /**
   * Returns a <CODE>java.util.Collection</CODE> containing all names of external session attributes.
   * Session 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_session.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.SESSION_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 session attribute.  Attempts to get the value of a session
   * 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.SESSION_PREFIX))
      return null;
    else
      return m_session.getAttribute(name);

  } // end getAttribute

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

    } // end removeAttribute

  } // end removeAttribute

  /**
   * Sets the value of an external session attribute.  Attempts to set the value of a session
   * 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 synchronized void setAttribute(String name, Object value)
  {
    if (!(name.startsWith(WebConstants.SESSION_PREFIX)))
    { // invalidate the "names" cache when we add
      m_session.setAttribute(name,value);
      m_names = null;

    } // end setAttribute

  } // end setAttribute

} // end class ServletExternalSessionAttributes
