/*
 * 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.rmi.RemoteException;
import javax.ejb.EJBHome;
import javax.ejb.EJBMetaData;
import javax.ejb.Handle;
import javax.ejb.HomeHandle;
import javax.ejb.RemoveException;
import javax.naming.NamingException;
import javax.naming.Reference;
import javax.naming.StringRefAddr;
import org.objectweb.common.Env;
import org.objectweb.common.RemoteObject;
import org.objectweb.jonas_ejb.deployment.api.BeanDesc;

/**
 * This class represents an EJBHome Remote Interface
 * It is shared between Sessions and Entities.
 */
public abstract class JHome extends RemoteObject implements EJBHome {

    protected JMetaData ejbMetaData = null;
    protected JHomeHandle homeHandle = null;
    protected BeanDesc dd;
    protected JFactory bf;
    protected boolean isDavid = false;
    protected boolean unregistered = true;

    /**
     * Constructor for the base class of the specific generated Home object.
     */
    public JHome(BeanDesc dd, JFactory bf) throws RemoteException {
	TraceEjb.debugIc("JHome constructor");
	this.dd = dd;
	this.bf = bf;
	if (Env.isDavid()) isDavid = true;
    }

    // ---------------------------------------------------------------
    // EJBHome Implementation
    // ---------------------------------------------------------------

    /**
     * Obtains the EJBMetaData for the enterprise Bean. 
     * @return The enterprise Bean's EJBMetaData
     */
    public EJBMetaData getEJBMetaData() throws RemoteException {
	TraceEjb.debugIc("JHome getEJBMetaData");
	if (ejbMetaData == null) {
	    ejbMetaData = new JMetaData(this, dd);
	}
	return ejbMetaData;
    }

    /**
     * Obtain a handle for the home object. The handle can be used at later time to re-obtain a
     * reference to the home object, possibly in a different Java Virtual Machine.
     * 
     * @return A handle for the home object.
     * 
     * @exception java.rmi.RemoteException - Thrown when the method failed due to a system-level
     * failure.
     */
    public HomeHandle getHomeHandle() throws java.rmi.RemoteException {
	TraceEjb.debugIc("JHome getHomeHandle");
	if (homeHandle == null) {
	    homeHandle = new JHomeHandle(dd.getJndiName(), bf.getEnv());
	}
	return homeHandle;
    }

    /**
     * Removes an EJB object identified by its handle. 
     */
    abstract public void remove(Handle handle) throws RemoteException, RemoveException;

    /**
     * Removes an EJB object identified by its primary key.
     */
    abstract public void remove(Object primaryKey) throws RemoteException, RemoveException;

    // ---------------------------------------------------------------
    // other public methods
    // ---------------------------------------------------------------

    /**
     * register this bean to JNDI (rebind)
     */
    protected void register() throws NamingException {
	TraceEjb.debugIc("JHome register");
	String name = dd.getJndiName();
	
	if (isDavid) {
	    // register in CosNaming
	    bf.getCorbaInitialContext().rebind(name, this);
	    
	    // register a reference of the home bean in the rmiregistry so
	    // EJBs can lookup other beans
	    Reference ref = new Reference (getClass().getName(), 
					   "org.objectweb.jonas_ejb.container.HomeFactory",
					   null);
	    ref.add(new StringRefAddr("bean.name", name));
	    bf.getInitialContext().rebind(name, ref);
	} else {
	    // register in default InitialContext
	    bf.getInitialContext().rebind(name, this);
	}
	unregistered = false;
    }

    /**
     * unregister this bean in JNDI (unbind)
     */
    protected void unregister() throws NamingException {
	TraceEjb.debugIc("JHome unregister");
	String name = dd.getJndiName();
	
	if (isDavid) {
	    /*
	    // unregister in CosNaming
	    getCorbaInitialContext().unbind(name);
	    */
	} else {
	    // unregister in default InitialContext
	    bf.getInitialContext().unbind(name);
	}
	unregistered = true;
    }
            
}
