/* JFox, the OpenSource J2EE Application Server
 *
 * Copyright (C) 2002 huihoo.com
 * Distributable under GNU LGPL license
 * See the GNU Lesser General Public License for more details.
 */

package org.jfox.ejb;

import java.rmi.RemoteException;
import java.lang.reflect.Proxy;
import javax.ejb.Handle;
import javax.ejb.EJBObject;

import org.jfox.ejb.invoker.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
    ObjectId 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) {

  }
}