/*
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 Bull S.A.
 * Contact: jonas-team@objectweb.org
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * Initial developer(s): Philippe Durieux
 * Contributor(s): ______________________________________.
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas_ejb.container;

import java.util.Hashtable;

/**
 * This class manages SessionContext objects for Session Stateful beans
 * It is responsible for keeping Contexts in memory and storing them
 * on disk when the swapper ask for more available memory.
 */
public class JStatefulPool {
    private Hashtable memoryList = new Hashtable();
    private Hashtable diskList = new Hashtable();
    private int sessionCount = 0;

    /**
     * Constructor
     */
    public JStatefulPool() {
	TraceEjb.debugIc("JStatefulPool Constructor");
    }

    /**
     * add a new Context in the pool
     */
    public synchronized int addContext(JStatefulContext ctx) {
	TraceEjb.debugIc("JStatefulPool: addContext");

	// sanity check
	if (ctx == null) {
	    TraceEjb.error("JStatefulPool addContext null");
	}

	// First get a unique identifier
	int ctxid = sessionCount++;

	// Put in in the memoryList
	memoryList.put(new Integer(ctxid), (Object)ctx);

	// return Id
	return ctxid;
    }

    /**
     * get a Context matching this Id
     */
    public synchronized JStatefulContext getContext(int ctxid) {
	TraceEjb.debugIc("JStatefulPool: getContext");

	JStatefulContext ctx = (JStatefulContext) memoryList.get(new Integer(ctxid));
	// TODO

	// sanity check
	if (ctx == null) {
	    TraceEjb.error("JStatefulPool getContext null");
	}

	return ctx;
    }

    /**
     * remove the Context matching this Id
     */
    public synchronized void removeContext(int ctxid) {
	TraceEjb.debugIc("JStatefulPool: removeContext");

	memoryList.remove(new Integer(ctxid));
    }
}
