/*
 * 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;

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

    protected JEntityFactory ebf;
    protected JEntitySwitch bs;

    /**
     * constructor
     */
    public JEntityRemote(JEntityFactory bf) throws RemoteException {
	super(bf);
	TraceEjb.debugIc("JEntityRemote constructor");
	this.ebf = bf;
    }

    /**
     * finish initialization
     */
    public void setEntitySwitch(JEntitySwitch bs) {
	TraceEjb.debugIc("JEntityRemote setEntitySwitch");
	this.bs = bs;
    }

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

    /**
     * Returns the enterprise Bean's home interface.
     */
    public EJBHome getEJBHome() {
	TraceEjb.debugIc("JEntityRemote getEJBHome");
	return ebf.getHome();
    }

    /**
     * returns the Primary Key for this EJBObject
     */
    public Object getPrimaryKey() {
	TraceEjb.debugIc("JEntityRemote getPrimaryKey");
	if (bs == null) {
	    throw new EJBException("No Primary Key yet");
	}
	return bs.getPrimaryKey();
    }
 
    /**
     * Tests if a given EJB is identical to the invoked EJB object.
     *
     * @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("JEntityRemote isIdentical");
	boolean ret = false;
	if (obj != null) {
	    ret = obj.equals(this);
	}
	return ret;
    }

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

    public RequestCtx preInvoke(int txa, String secu) throws RemoteException {
	TraceEjb.debugIc("JEntityRemote preInvoke");
	return bf.preInvokeRemote(txa, secu);
    }

    public void postInvoke(RequestCtx rctx) throws RemoteException {
	TraceEjb.debugIc("JEntityRemote postInvoke");
	try {
	    bf.postInvokeRemote(rctx);
	} finally {
	    if (rctx.sysExc != null) {
		bs.discardICtx(rctx.currTx);
	    } else {
		bs.releaseICtx(rctx.currTx);
	    }
	}
    }

}
