/* 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;

import java.rmi.RemoteException;
import java.util.Date;
import javax.naming.Context;

import org.huihoo.jfox.system.ServiceSupport;
import org.huihoo.jfox.system.State;
import org.jfox.ejb.Container;
import org.jfox.ejb.ContainerImpl;

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

public abstract class ContainerServiceSupport extends ServiceSupport {
  protected final static String INVOKER_JNDI_PREFIX = ContainerInvokerHelper.INVOKER_JNDI_PREFIX;
  protected static transient Context INITIAL_CONTEXT = ContainerInvokerHelper.INITIAL_CONTEXT;
  // singleton Container instance
  protected final static Container container = ContainerImpl.getInstance();


  protected void doInit() throws Exception {
    if(State.canInit(container.getState())) {
      container.init();
    }
  }

  public void ping() throws RemoteException {
    logger.debug("I'm alive :)   " + new Date());
  }

  public void run() {
    while(isRunning()){
      try{
        // 每隔 120s 运行 ping 函数,以防止该线程死掉
        Thread.sleep(5 * 60 * 1000L);
        ping();
      }
      catch(Exception e){
        e.printStackTrace();
      }
    }
    // do not need to implement, because is a rmi server
  }

}