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

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

public class SessionEventRequest extends BaseDelegatingServiceProvider implements Request
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static final String EMPTY_STRING = "";

  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private DefaultSessionInfoProvider m_session;
  private Verb m_verb;
  private Locale[] m_locales;
  private MemoryObjectStore m_attrs;
  private Request m_request;

  /*--------------------------------------------------------------------------------
   * Constructors
   *--------------------------------------------------------------------------------
   */

  public SessionEventRequest(ServiceProvider services, SessionInfo session, boolean start)
  {
    super("SessionEventRequest",services);
    m_session = new DefaultSessionInfoProvider(session);
    m_verb = (start ? Verb.PUT : Verb.DELETE);
    m_locales = new Locale[1];
    m_locales[0] = Locale.getDefault();
    m_attrs = new MemoryObjectStore(SessionEventRequest.class.getName());
    m_request = null;

  } // end constructor

  public SessionEventRequest(SessionInfo session, boolean start, Request request)
  {
    super("SessionEventRequest",request);
    m_session = null;
    m_verb = (start ? Verb.PUT : Verb.DELETE);
    m_locales = null;
    m_attrs = new MemoryObjectStore(SessionEventRequest.class.getName());
    m_request = request;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Overrides from class BaseDelegatingServiceProvider
   *--------------------------------------------------------------------------------
   */

  /**
   * Queries this object for a specified service.
   *
   * @param klass The class of the object that should be returned as a service.
   * @return A service object.  The service object is guaranteed to be of the class
   *         specified by <CODE>klass</CODE>; that is, if <CODE>queryService(klass)</CODE>
   *         yields some object <CODE>x</CODE>, then the expression <CODE>klass.isInstance(x)</CODE>
   *         is true.
   * @exception com.silverwrist.dynamo.except.NoSuchServiceException If no service is available in
   *            the specified class.
   */
  public Object queryService(Class klass)
  {
    if ((m_session!=null) && (klass==SessionInfoProvider.class))
      return m_session;
    return super.queryService(klass);

  } // end queryService

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

  /**
   * Retrieves an object from this <CODE>ObjectProvider</CODE>.
   *
   * @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)
  {
    return m_attrs.getObject(namespace,name);

  } // end getObject

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

  public Object setObject(String namespace, String name, Object value)
  {
    return m_attrs.setObject(namespace,name,value);

  } // end setObject

  public Object removeObject(String namespace, String name)
  {
    return m_attrs.removeObject(namespace,name);

  } // end removeObject

  public Collection getNamespaces()
  {
    return m_attrs.getNamespaces();

  } // end getNamespaces

  public Collection getNamesForNamespace(String namespace)
  {
    return m_attrs.getNamesForNamespace(namespace);

  } // end getNamesForNamespace

  /*--------------------------------------------------------------------------------
   * Implementations from interface Request
   *--------------------------------------------------------------------------------
   */

  public RequestType getType()
  {
    return RequestType._SESSION;

  } // end getType

  public String getServerName()
  {
    if (m_request!=null)
      return m_request.getServerName();
    else
      return EMPTY_STRING;

  } // end getEmptyString

  public int getServerPort()
  {
    if (m_request!=null)
      return m_request.getServerPort();
    else
      return -1;

  } // end getServerPort

  public String getContextPath()
  {
    return EMPTY_STRING;

  } // end getContextPath

  public String getRequestPath()
  {
    return EMPTY_STRING;

  } // end getRequestPath

  public String getExtraPath()
  {
    return EMPTY_STRING;

  } // end getExtraPath

  public String getQueryString()
  {
    return EMPTY_STRING;

  } // end getQueryString

  public Verb getVerb()
  {
    return m_verb;

  } // end getVerb

  public String getSourceAddress()
  {
    if (m_request!=null)
      return m_request.getSourceAddress();
    else
      return "127.0.0.1";

  } // end getSourceAddress

  public Map getParameters()
  {
    return Collections.EMPTY_MAP;

  } // end getParameters

  public Class getParametersEntryClass()
  {
    return Object.class;

  } // end getParametersEntryClass

  public Map getDataItems()
  {
    return Collections.EMPTY_MAP;

  } // end getDataItems

  public Locale[] getLocales()
  {
    if (m_request!=null)
      return m_request.getLocales();
    else
      return m_locales;

  } // end getLocales

} // end class SessionEventRequest
