/*
 * 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 Coq, 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.EJBHome;
import javax.ejb.EJBObject;
import javax.ejb.Handle;
import javax.ejb.RemoveException;

/**
 * Generic part of the EJBObject implementation
 */
public abstract class JSessionRemote extends JRemote {

    protected JSessionSwitch bs;
    protected Handle myHandle = null;

    /**
     * constructor
     */
    public JSessionRemote(JSessionFactory bf) throws RemoteException {
	super((JFactory)bf);
	TraceEjb.debugIc("JSessionRemote constructor");
    }

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

    public abstract void remove() throws RemoteException, RemoveException;

    /**
     * Returns the enterprise Bean's home interface.
     */
    public EJBHome getEJBHome() {
	return bf.getHome();
    }

    /**
     * returns the Primary Key for this EJBObject
     */
    public Object getPrimaryKey() throws RemoteException {
	throw new RemoteException("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     EJBObject 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 RemoteException: Thrown when the method failed due to a system-level failure.
     */
    public boolean isIdentical(EJBObject obj) throws RemoteException {
	TraceEjb.debugIc("JSessionRemote 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 Home
	    // We cannot cast the remote EJBObject cbecause it's a stub.
	    String myhome = getEJBHome().getEJBMetaData().getHomeInterfaceClass().getName();
	    if (obj != null) {
		ret = obj.getEJBHome().getEJBMetaData().getHomeInterfaceClass().getName().equals(myhome);
	    }
	}
	return ret;
    }

    /**
     * Obtains a handle for the EJB object. The handle can be used at later time to re-obtain
     * a reference to the EJB object, possibly in a different JVM.
     *
     * @return    A handle for the EJB object. 
     * @exception RemoteException: Thrown when the method failed due to a system-level failure.
     */
    public Handle getHandle() throws RemoteException {
	TraceEjb.debugIc("JSessionRemote getHandle");

	if (myHandle == null) {
	    myHandle = new JSessionHandle(this);
	}
	return myHandle;
    }

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

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

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

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

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

}
