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

/**
 * JStatelessSwitch is the implementation of JSessionSwitch dedicated to the
 * Stateless Session Bean.
 */
public class JStatelessSwitch extends JSessionSwitch {

    private JStatelessContext bctx = null;

    /**
     * constructor. 
     */
    public JStatelessSwitch(JStatelessFactory bf) throws RemoteException {
	super(bf);
	TraceEjb.debugIc("JStatelessSwitch constructor");
    }

    // ===============================================================
    // TimerEventListener implementation
    // ===============================================================

    /**
     * The session timeout has expired
     */
    public synchronized void timeoutExpired(Object arg) {
	TraceEjb.verbose("stateless session timeout expired");
	mytimer = null;
	if (bctx != null) {
	    try {
		bctx.setRemoved();
	    } catch (RemoteException e) {
		TraceEjb.verbose("timeout expired:"+e);
	    } catch (RemoveException e) {
		TraceEjb.verbose("timeout expired:"+e);
	    }
	}
	noLongerUsed();
    }

    // ===============================================================
    // other public methods
    // ===============================================================

    /**
     * get a Bean Context
     */
    public JSessionContext getICtx(Transaction tx) throws RemoteException {
	TraceEjb.debugIc("JStatelessSwitch getICtx");
	bctx = (JStatelessContext) ((JStatelessFactory)bf).getJContext();
	bctx.initSessionContext(this);
	return bctx;
    }

    /**
     * release the bean context
     * Contexts are release at each request, in case of stateless session.
     */
    public void releaseICtx(Transaction tx) {
	TraceEjb.debugIc("JStatelessSwitch releaseICtx");
	if (bctx.isMarkedRemoved()) {
	    stopTimer();
	    noLongerUsed();
	}
	((JStatelessFactory)bf).releaseJContext(bctx);
	bctx = null;
    }

    public void noLongerUsed() {
	TraceEjb.debugIc("JStatelessSwitch noLongerUsed");

	// Unexport the EJBObject from the Orb
	if (remote != null) {
	    try {
		remote.unexportObject();
	    } catch (NoSuchObjectException e) {
		TraceEjb.error("Unexport Stateless Session failed: "+e);
	    }
	}

	// return the SessionSwitch in the pool.
	// will be reused for another Session.
	bf.removeEJB(this);
    }

    /**
     * This is not used for stateless
     */
    public void setMustCommit(boolean mc) {
    }
}
