/* 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.invoker.jrmp;

import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import javax.ejb.EJBException;

import org.jfox.ejb.Invocation;
import org.jfox.ejb.meta.Protocol;
import org.jfox.ejb.invoker.ContainerRemote;
import org.jfox.ejb.invoker.ContainerServiceSupport;

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

public class JRMPContainerService extends ContainerServiceSupport implements ContainerRemote {

  // invoker stub
  private Remote stub = null;
  private String protocol = Protocol.JRMP;

  protected void doInit() throws Exception {
    super.doInit();
    try {
      UnicastRemoteObject.unexportObject(this,true);
    }
    catch(Exception e){
      // ignore
    }
  }

  /**
   * export this invoker and set container's invoker to this stub
   * @throws Exception
   */
  protected void doStart() throws Exception {
    stub = UnicastRemoteObject.exportObject(this);
    INITIAL_CONTEXT.rebind(INVOKER_JNDI_PREFIX + "/" + protocol.toUpperCase(),stub);
    // 启动一个线程,用来每隔一段时间 ping 一下,防止 rmi 线程死掉
    Thread pingThread = new Thread(this,getClass().getName());
    pingThread.setPriority(Thread.MIN_PRIORITY);
    pingThread.start();
  }

  protected void doStop() throws Exception {
    INITIAL_CONTEXT.unbind(INVOKER_JNDI_PREFIX + "/" + protocol.toUpperCase());
    UnicastRemoteObject.unexportObject(this,true);
  }

  protected synchronized void doDestroy() throws Exception {
    stub = null;
  }

  /**
   * just forward to container
   * 应该把客户的调用组装成一个 invocation 对象
   */
  public Object invoke(Invocation invocation) throws RemoteException {
    logger.debug("invoke " + invocation.toString());
    try {
      return container.invoke(invocation);
    }
    catch(RemoteException e){
      logger.error(e.getMessage(),e);
      throw e;
    }
    catch(Exception e){
      logger.error(e.getMessage(),e);
      throw new EJBException(e);
    }
  }

  public static void main(String[] args) throws Exception{

  }
}