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

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

import org.jfox.ejb.Container;
import org.jfox.ejb.ContainerImpl;
import org.jfox.framework.extra.ActiveComponent;
import org.jfox.jndi.InitialContextHelper;
import org.jfox.system.AbstractService;

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

public abstract class ContainerServiceSupport extends AbstractService implements ActiveComponent {
    protected final static String INVOKER_JNDI_PREFIX = ConnectorHelper.INVOKER_JNDI_PREFIX;
    protected static transient Context INITIAL_CONTEXT = InitialContextHelper.getInitialContext();
    // singleton Container instance
    protected final static Container container = ContainerImpl.getInstance();


    protected void doInit() throws Exception {
        if(!container.isInitialized()) {
            container.init();
        }
    }

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

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


}