/*
 * 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.venice.ui.rpc;

import java.util.*;
import com.silverwrist.venice.core.*;
import com.silverwrist.venice.except.*;
import com.silverwrist.venice.ui.*;

class RpcVeniceUISession implements VeniceUISession
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private RpcSessionBroker broker;        // session broker
  private long create_time;               // session creation time
  private long access_time;               // session last access time
  private String id;                      // the session ID
  private String remote_addr;             // the remote address
  private int max_inactive;               // the maximum inactive interval
  private HashMap attrs = new HashMap();  // storage for attributes
  private UserContext user;               // user context in this session

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

  RpcVeniceUISession(String id, String remote_addr, RpcSessionBroker broker) throws DataException
  {
    this.broker = broker;
    this.id = id;
    this.remote_addr = remote_addr;
    this.max_inactive = broker.getTimeout() * 60;
    this.user = broker.getEngine().createUserContext(remote_addr);
    this.create_time = this.access_time = System.currentTimeMillis();

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface VeniceUISession
   *--------------------------------------------------------------------------------
   */

  public long getCreationTime()
  {
    return create_time;

  } // end getCreationTime

  public String getID()
  {
    return id;

  } // end getID

  public long getLastAccessedTime()
  {
    return access_time;

  } // end getLastAccessedTime

  public synchronized void setMaxInactiveInterval(int interval)
  {
    max_inactive = interval;
    touch();

  } // end setMaxInactiveInterval

  public int getMaxInactiveInterval()
  {
    return max_inactive;

  } // end getMaxInactiveInterval

  public Object getAttribute(String name)
  {
    return attrs.get(name);

  } // end getAttribute

  public Enumeration getAttributeNames()
  {
    return Collections.enumeration(attrs.keySet());

  } // end getAttributeNames

  public synchronized void setAttribute(String name, Object o)
  {
    attrs.put(name,o);
    touch();

  } // end setAttribute

  public synchronized void removeAttribute(String name)
  {
    attrs.remove(name);
    touch();

  } // end removeAttribute

  public synchronized void invalidate()
  {
    broker.detachSession(id);
    broker = null;
    attrs.clear();

  } // end invalidate

  public UserContext getUser()
  {
    return user;

  } // end getUser

  public synchronized void setUser(UserContext user)
  {
    this.user = user;
    touch();

  } // end setUser

  public void preprocess(RequestInput ri)
  { // do nothing
  } // end preprocess

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

  final synchronized boolean canUnloadNow()
  {
    return ((System.currentTimeMillis() - access_time) > ((long)1000 * max_inactive));

  } // end canUnloadNow

  final synchronized void touch()
  {
    access_time = System.currentTimeMillis();

  } // end touch

} // end class RpcVeniceUISession
