/* JFox, the OpenSource J2EE Application Server
 *
 * Distributable under GNU LGPL license by gun.org
 * more details please visit http://www.huihoo.org/jfox
 */

package org.jfox.ejb;

import java.rmi.RemoteException;
import javax.ejb.EJBHome;
import javax.ejb.HomeHandle;
import javax.naming.Context;
import javax.naming.InitialContext;

/**
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public class HomeHandleImpl implements HomeHandle {
    private String jndiName = null;

    public HomeHandleImpl(String jndiName) {
        this.jndiName = jndiName;
    }

    /**
     * 从 jndi 重新得到 EJBHome
     * 因为是通过 jndi 按照名称重新获得 EJBHome，所以必须保证 jndi 能用，而且该名称没有被覆盖
     *
     * @return EJBHome
     * @throws RemoteException
     */
    public EJBHome getEJBHome() throws RemoteException {
        try {
            Context ctx = new InitialContext();
            Object obj = ctx.lookup(jndiName);
            EJBHome home = (EJBHome) javax.rmi.PortableRemoteObject.narrow(obj, EJBHome.class);
            return home;
        }
        catch(Exception e) {
            throw new RemoteException(e.getMessage(), e);
        }
    }

}
