/*
 * 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) 2002-03 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.dynamo.util;

import java.util.*;
import com.silverwrist.dynamo.RequestType;
import com.silverwrist.dynamo.except.*;
import com.silverwrist.dynamo.iface.*;

/**
 * An implementation of {@link com.silverwrist.dynamo.iface.SessionInfo SessionInfo} which is a "dummy," provided
 * for situations where no <CODE>SessionInfo</CODE> can be found for a given request.  This object also provides
 * an instance of {@link com.silverwrist.dynamo.iface.SessionInfoProvider SessionInfoProvider} that returns
 * this instance of <CODE>SessionInfo</CODE>.  It is implemented as a Singleton.
 *
 * @author Eric J. Bowersox &lt;erbo@silcom.com&gt;
 * @version X
 */
public class NullSessionInfo implements SessionInfo, SessionInfoProvider
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static NullSessionInfo _self = null;  // our singleton instance

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

  /**
   * Only one instance of <CODE>NullSessionInfo</CODE> may be created.
   */
  private NullSessionInfo()
  { // do nothing
  } // end constructor

  /*--------------------------------------------------------------------------------
   * Overrides from class Object
   *--------------------------------------------------------------------------------
   */

  /**
   * Indicates whether some other object is "equal to" this one.
   *
   * @param o The reference object with which to compare.
   * @return <CODE>true</CODE> if this object is the same as the <CODE>o</CODE> argument; <CODE>false</CODE> otherwise.
   */
  public boolean equals(Object o)
  {
    if ((o==null) || !(o instanceof NullSessionInfo))
      return false;
    NullSessionInfo other = (NullSessionInfo)o;
    return (other==_self);

  } // end equals

  /**
   * Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those
   * provided by <CODE>java.util.Hashtable</CODE>.
   *
   * @return A hash code value for this object.
   */
  public int hashCode()
  {
    return 0;

  } // end hashCode

  /**
   * Returns a string representation of the object. In general, the <CODE>toString</CODE> method returns a string
   * that "textually represents" this object. 
   *
   * @return A string representation of the object.
   */
  public String toString()
  {
    return "{NullSessionInfo}";

  } // end toString

  /*--------------------------------------------------------------------------------
   * Implementations from interface ObjectProvider
   *--------------------------------------------------------------------------------
   */

  /**
   * Retrieves an object from this session's <CODE>ObjectProvider</CODE>.  (The <CODE>NullSessionInfo</CODE>
   * contains no objects.)
   *
   * @param namespace The namespace to interpret the name relative to.
   * @param name The name of the object to be retrieved.
   * @return The object reference specified.
   */
  public Object getObject(String namespace, String name)
  {
    throw new NoSuchObjectException("NullSessionInfo",namespace,name);

  } // end getObject

  /*--------------------------------------------------------------------------------
   * Implementations from interface ObjectStore
   *--------------------------------------------------------------------------------
   */

  /**
   * Sets an object into this session's <CODE>ObjectStore</CODE>.  (The <CODE>NullSessionInfo</CODE>
   * contains no objects. and ignores all attempts to store any objects.)
   *
   * @param namespace The namespace to interpret the name relative to.
   * @param name The name of the object to be set.
   * @param value The object to set into the <CODE>ObjectStore</CODE>.
   * @return The previous object that was set into the <CODE>ObjectStore</CODE> under this namespace and name, or
   *         <CODE>null</CODE> if there was no such object.
   */
  public Object setObject(String namespace, String name, Object value)
  {
    return null;

  } // end setObject

  /**
   * Removes an object from this session's <CODE>ObjectStore</CODE>.  (The <CODE>NullSessionInfo</CODE>
   * contains no objects. and ignores all attempts to store any objects.)
   *
   * @param namespace The namespace to interpret the name relative to.
   * @param name The name of the object to be removed.
   * @return The previous object that was set into the <CODE>ObjectStore</CODE> under this namespace and name, or
   *         <CODE>null</CODE> if there was no such object.
   */
  public Object removeObject(String namespace, String name)
  {
    return null;

  } // end removeObject

  /**
   * Returns a collection of all object namespaces that have been set into this session's <CODE>ObjectStore</CODE>.
   * (The <CODE>NullSessionInfo</CODE> contains no objects.)
   *
   * @return A <CODE>java.util.Collection</CODE> containing <CODE>String</CODE> objects specifying all the
   *         object namespaces.
   */
  public Collection getNamespaces()
  {
    return Collections.EMPTY_LIST;

  } // end getNamespaces

  /**
   * Returns a collection of all object names that have been set into this session's <CODE>ObjectStore</CODE>
   * under a given namespace.  (The <CODE>NullSessionInfo</CODE> contains no objects.)
   *
   * @param namespace The namespace to look for names under.
   * @return A <CODE>java.util.Collection</CODE> containing <CODE>String</CODE> objects specifying all the
   *         object names for this namespace.
   */
  public Collection getNamesForNamespace(String namespace)
  {
    return Collections.EMPTY_LIST;

  } // end getNamesForNamespace

  /*--------------------------------------------------------------------------------
   * Implementations from interface SessionInfo
   *--------------------------------------------------------------------------------
   */

  /**
   * Returns a value indicating the type of this session.  (The <CODE>NullSessionInfo</CODE> is of type
   * {@link com.silverwrist.dynamo.RequestType#_NULL RequestType._NULL}.)
   *
   * @return The type of this request.
   * @see com.silverwrist.dynamo.RequestType
   * @see com.silverwrist.dynamo.iface.Request#getType()
   */
  public RequestType getSessionType()
  {
    return RequestType._NULL;

  } // end getSessionType

  /**
   * Returns the unique identifier for this session.
   *
   * @return The unique identifier for this session.
   */
  public String getID()
  {
    return toString();

  } // end getID

  /**
   * Returns the time at which this session was created.  This is expressed in the same units as
   * <CODE>System.currentTimeMillis()</CODE>.
   *
   * @return The time at which the session was created.
   */
  public long getCreationTime()
  {
    return 0L;

  } // end getCreationTime

  /**
   * Returns the time at which this session was last accessed.  This is expressed in the same units as
   * <CODE>System.currentTimeMillis()</CODE>.
   *
   * @return The time at which the session was last accessed.
   */
  public long getLastAccessedTime()
  {
    return 0L;

  } // end getLastAccessedTime

  /**
   * Specifies the time, in seconds, between client requests before the framework will invalidate this
   * session. A negative value indicates the session should never timeout.
   *
   * @param interval An integer specifying the number of seconds.
   */
  public void setMaxInactiveInterval(int interval)
  { // do nothing
  } // end setMaxInactiveInterval

  /**
   * Returns the maximum time interval, in seconds, that the framework will keep this session open between
   * client accesses. After this interval, the framework will invalidate the session. The maximum time
   * interval can be set with the {@link #setMaxInactiveInterval(int) setMaxInactiveInterval} method. A negative
   * value indicates the session should never timeout.
   *
   * @return An integer specifying the number of seconds this session remains open between client requests.
   */
  public int getMaxInactiveInterval()
  {
    return -1;

  } // end getMaxInactiveInterval

  /**
   * Causes the session to be invalidated, and any objects bound to it to be unbound.  Also invalidates
   * any underlying session attributes in the enclosing environment (such as the servlet container), if
   * present.
   */
  public void invalidate()
  { // do nothing
  } // end invalidate

  /**
   * Causes the session to be invalidated, and any objects bound to it to be unbound.  If there are any
   * underlying session attributes in the enclosing environment (such as the servlet container), they are
   * unaffected.
   */
  public void detach()
  { // do nothing
  } // end detach

  /*--------------------------------------------------------------------------------
   * Implementations from interface SessionInfoProvider
   *--------------------------------------------------------------------------------
   */

  /**
   * Retrieves the singleton instance of <CODE>NullSessionInfo</CODE>.
   *
   * @return The singleton instance of <CODE>NullSessionInfo</CODE>.
   */
  public SessionInfo getSessionInfo()
  {
    return (SessionInfo)this;

  } // end getSessionInfo

  /*--------------------------------------------------------------------------------
   * External static operations
   *--------------------------------------------------------------------------------
   */

  /**
   * Returns the singleton instance of {@link com.silverwrist.dynamo.iface.SessionInfo SessionInfo} for this object.
   *
   * @return The singleton instance of <CODE>SessionInfo</CODE> for this object.
   */
  public static SessionInfo get()
  {
    if (_self==null)
      _self = new NullSessionInfo();
    return (SessionInfo)_self;

  } // end get

  /**
   * Returns the singleton instance of
   * {@link com.silverwrist.dynamo.iface.SessionInfoProvider SessionInfoProvider} for this object.
   *
   * @return The singleton instance of <CODE>SessionInfoProvider</CODE> for this object.
   */
  public static SessionInfoProvider getProvider()
  {
    if (_self==null)
      _self = new NullSessionInfo();
    return (SessionInfoProvider)_self;

  } // end getProvider

} // end class NullSessionInfo
