/*
 * 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.rmi.RemoteException;
import javax.ejb.EJBException;
import javax.ejb.EJBLocalObject;
import javax.ejb.EJBObject;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
import javax.transaction.Transaction;
import org.objectweb.jonas_timer.TimerEvent;
import org.objectweb.jonas_timer.TimerEventListener;
import org.objectweb.jonas_timer.TimerManager;

/**
 * JSessionSwitch holds all the code that is common to EJBObject ans
 * EJBLocalObject for session beans. It mainly keep a reference on the
 * SessionContext and is used to manage the timeout for the session.
 * This class has 2 subclasses, depending if session is stateless or stateful.
 */
public abstract class JSessionSwitch implements TimerEventListener {

    protected JSessionFactory bf;
    protected JSessionLocal local = null;
    protected JSessionRemote remote = null;
    protected TimerEvent mytimer = null;

    /**
     * constructor. 
     * a new object is build when the pool managed by JSessionFactory becomes empty.
     */
    public JSessionSwitch(JSessionFactory bf) throws RemoteException {
	TraceEjb.debugIc("JSessionSwitch constructor");
	this.bf = bf;

	// Create EJBObject if bean has a Remote Interface
	if (bf.getHome() != null) {
	    remote = ((JSessionHome)bf.getHome()).createRemoteObject();
	    remote.setSessionSwitch(this);
	}

	// Create EJBLocalObject if bean has a Local Interface
	if (bf.getLocalHome() != null) {
	    local = ((JSessionLocalHome)bf.getLocalHome()).createLocalObject();
	    local.setSessionSwitch(this);
	}
    }

    /**
     * Return the underlaying EJBLocalObject
     */
    public JSessionLocal getLocal() {
	return local;
    }

    /**
     * Return the underlaying EJBObject
     */
    public JSessionRemote getRemote() {
	return remote;
    }

    /**
     * Return the BeanFactory
     */
    public JSessionFactory getBeanFactory() {
	return bf;
    }

    public void startTimer(int timeout) {
	TraceEjb.debugIc("JSessionSwitch startTimer");
	mytimer = TimerManager.getInstance().addTimer(this, timeout, null, false);
    }

    public void stopTimer() {
	TraceEjb.debugIc("JSessionSwitch stopTimer");
	if (mytimer != null) {
	    mytimer.stop();
	}
    }

    abstract public JSessionContext getICtx(Transaction tx) throws RemoteException;
    abstract public void releaseICtx(Transaction tx);
    abstract public void setMustCommit(boolean mc);
}

