/*
 * 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.lang.Object;
import java.rmi.RemoteException;
import javax.ejb.EJBException;
import javax.ejb.EJBLocalHome;
import javax.ejb.EJBLocalObject;
import javax.ejb.RemoveException;

/**
 * Generic part of the EJBLocalObject implementation
 */
public abstract class JSessionLocal extends JLocal {

    protected JSessionFactory bf;
    protected JSessionSwitch bs;

    /**
     * constructor
     */
    public JSessionLocal(JSessionFactory bf) {
	super(bf);
	TraceEjb.debugIc("JSessionLocal constructor");
	this.bf = bf;
    }

    // --------------------------------------------------------------------------
    // EJBLocalObject implementation
    // remove() is implemented in the generated part.
    // --------------------------------------------------------------------------

    public abstract void remove() throws RemoveException;

    /**
     * Returns the enterprise Bean's local home interface.
     */
    public EJBLocalHome getEJBLocalHome() {
	return bf.getLocalHome();
    }

    /**
     * returns the Primary Key for this EJBObject
     */
    public Object getPrimaryKey() throws EJBException {
	throw new EJBException("Session bean has no primary key");
    }

    /**
     * Tests if a given EJB is identical to the invoked EJB object.
     * This is different whether the bean is stateless or stateful.
     *
     * @param     EJBLocalObject obj - An object to test for identity with the invoked object. 
     * @return    True if the given EJB object is identical to the invoked object. 
     * @throws EJBException: Thrown when the method failed due to a system-level failure.
     */
    public boolean isIdentical(EJBLocalObject obj) {
	TraceEjb.debugIc("JSessionLocal isIdentical");
	boolean ret = false;
	JSessionFactory sf = (JSessionFactory) bf;
	if (sf.isStateful()) {
	    // For stateful sessions, just compare both objects.
	    if (obj != null) {
		ret = obj.equals(this);
	    }
	} else {
	    // In case of Stateless session bean, we must compare the 2 factories
	    if (obj != null) {
		try {
		    //a simple cast should work on a local object
		    JSessionLocal ejb2 = (JSessionLocal) obj;
		    JSessionSwitch bs2 = ejb2.getSessionSwitch();
		    JSessionFactory bf2 = bs2.getBeanFactory();
		    ret = bf2.equals(sf);
		} catch (Exception e) {
		    TraceEjb.verbose("isIdentical raised exception:"+e);
		    throw new EJBException("isIdentical failed", e);
		}
	    }
	}
	return ret;
    }

    // ---------------------------------------------------------------
    // other public methods, for internal use.
    // ---------------------------------------------------------------

    /**
     * finish initialization
     */
    public void setSessionSwitch(JSessionSwitch bs) {
	TraceEjb.debugIc("JSessionLocal setSessionSwitch");
	this.bs = bs;
    }

    /**
     * Return the JSessionSwitch for this Session
     */
    public JSessionSwitch getSessionSwitch() {
	return bs;
    }

    public RequestCtx preInvoke(int txa, String secu) {
	TraceEjb.debugIc("JSessionLocal preInvoke");
	RequestCtx rctx = bf.preInvoke(txa, secu);
	bs.setMustCommit(rctx.mustCommit); // for remove stateful session
	return rctx;
    }

    public void postInvoke(RequestCtx rctx) {
	TraceEjb.debugIc("JSessionLocal postInvoke");
	try {
	    bf.postInvoke(rctx);
	} finally {
	    bs.releaseICtx(rctx.currTx);
	}
    }

}
