/* 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.lang.reflect.Proxy;
import java.rmi.RemoteException;
import javax.ejb.EJBObject;
import javax.ejb.Handle;

import org.jfox.ejb.connector.ContainerInvokerSupport;

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

public class HandleImpl implements Handle {
    private ContainerInvokerSupport invocationHandler = null;

    public HandleImpl(ContainerInvokerSupport invocationHandler) {
        this.invocationHandler = invocationHandler;
    }

    public EJBObject getEJBObject() throws RemoteException {
        try {
            ClassLoader ctxLoader = Thread.currentThread().getContextClassLoader();
            // get the EJBObject class name
            EJBObjectId objectId = invocationHandler.getObjectId();
            String remoteInterfaceName = objectId.getRemoteInterfaceName();
            Class remoteInterface = ctxLoader.loadClass(remoteInterfaceName);
            // 构造本地的 EJBObject 代理对象
            return (EJBObject) Proxy.newProxyInstance(ctxLoader,
                    new Class[]{remoteInterface},
                    invocationHandler);
        }
        catch(Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {

    }
}