com.pureinfo.force.runtime.engine.EngineServer.java Source code

Java tutorial

Introduction

Here is the source code for com.pureinfo.force.runtime.engine.EngineServer.java

Source

/**
 * PureInfo Force
 * @(#)EngineServer.java   1.0 Oct 10, 2005
 * 
 * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. 
 * All rights reserved, see the license file.
 * 
 * www.pureinfo.com.cn
 */

package com.pureinfo.force.runtime.engine;

import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.dom4j.Element;

import com.pureinfo.force.exception.PureException;
import com.pureinfo.force.io.ClassResourceUtil;
import com.pureinfo.force.lang.DateTimeUtil;
import com.pureinfo.force.xml.XMLUtil;

/**
 * <P>
 * Created on Oct 10, 2005 9:05:06 PM <BR>
 * Last modified on Oct 10, 2005
 * </P>
 * EngineServer: engine helper.
 * 
 * @author Why
 * @version 1.0, Oct 10, 2005
 * @since Force 1.0
 */
public class EngineServer extends EngineBase {
    //logger
    private final static Logger logger = Logger.getLogger(EngineServer.class.getName());

    /** resource file with enginer server configuration */
    public final static String RESOURCE = "plugin/engines-plugin.xml";

    //engine beans cache in format: <id(String), EngineBean>
    private Map m_hEngineBeans = new LinkedHashMap();

    private Date m_dtStartTime = null;

    //singleton modal
    private static EngineServer s_instance = new EngineServer();

    /**
     * Constructor: forbid to call
     */
    private EngineServer() {
        super();
    }

    /**
     * Returns the engine server instance.
     * 
     * @return the engine server instance.
     */
    public static EngineServer getInstance() {
        return s_instance;
    }

    /**
     * Returns the start time of this engine server.
     * 
     * @return the start time of this engine server.
     */
    public Date getStartTime() {
        return m_dtStartTime;
    }

    /**
     * Starts up this engine server.
     * 
     * @throws PureException
     *             if failed.
     */
    public void doStartup() throws PureException {
        m_dtStartTime = DateTimeUtil.now();
        Runtime runtime = Runtime.getRuntime();
        if (logger.isInfoEnabled()) {
            logger.info("engine server: to start ...");
            logger.info("memeory before start: free/total=" + (runtime.freeMemory() / 1024 / 1024) + "M/"
                    + (runtime.totalMemory() / 1024 / 1024) + "M");
        }

        //1. to load engine beans from resource
        loadEngineBeans();

        //2. to start all engines
        Iterator itr = getEngineBeans().iterator();
        EngineBean bean;
        while (itr.hasNext()) {
            bean = (EngineBean) itr.next();
            logger.debug("engine server: to start " + bean.getName());
            try {
                bean.getEngine(true).startup();
            } catch (Exception ex) {
                PureException ex2 = new PureException(PureException.SERVICE_STARTUP, bean.getName(), ex);
                logger.error("failed to start engine " + bean.getName(), ex2);
                if (!bean.isIgnoreFailure()) {
                    throw ex2;
                }
            }
        }

        System.gc();
        if (logger.isInfoEnabled()) {
            logger.info("engine server: started");
            logger.info("memeory after start: free/total=" + (runtime.freeMemory() / 1024 / 1024) + "M/"
                    + (runtime.totalMemory() / 1024 / 1024) + "M");
        }
    }

    /**
     * Shuts down this engine server.
     * 
     * @throws PureException
     */
    public void doShutdown() throws PureException {
        Runtime runtime = Runtime.getRuntime();
        if (logger.isInfoEnabled()) {
            logger.info("engine server: to shut down ...");
            logger.info("memeory before shutdown: free/total=" + (runtime.freeMemory() / 1024 / 1024) + "M/"
                    + (runtime.totalMemory() / 1024 / 1024) + "M");
        }

        //to shudown all the engines from the last one
        EngineBean bean;
        IEngine engine;
        Object[] beans = getEngineBeans().toArray();
        for (int i = beans.length - 1; i >= 0; i--) {
            bean = (EngineBean) beans[i];
            engine = bean.getEngine(false);
            if (engine == null)
                continue;
            //else
            try {
                engine.shutdown();
            } catch (Exception ex) {
                PureException ex2 = new PureException(PureException.SERVICE_SHUTDOWN, bean.getName(), ex);
                logger.error("failed to shutdown engine " + bean.getName(), ex2);
            }
        }

        //clear cache
        m_hEngineBeans.clear();
        System.gc();
        if (logger.isInfoEnabled()) {
            logger.info("engine server: shutted down");
            logger.info("memeory after shutdown: free/total=" + (runtime.freeMemory() / 1024 / 1024) + "M/"
                    + (runtime.totalMemory() / 1024 / 1024) + "M");
        }
    }

    /**
     * Returns the engine been by id.
     * 
     * @param _sId
     *            the engine id
     * @return the engine bean found or <code>null</code>.
     */
    public EngineBean getEngineBean(String _sId) {
        return (EngineBean) m_hEngineBeans.get(_sId);
    }

    /**
     * Returns all the engine beans collection (in order).
     * 
     * @return all the engine beans collection (in order).
     */
    public Collection getEngineBeans() {
        return m_hEngineBeans.values();
    }

    //=========================================================================
    //inside logic

    /**
     * Loads engine beans from resource.
     * 
     * @throws PureException
     *             if failed.
     */
    private void loadEngineBeans() throws PureException {
        logger.debug("engine server: to load beans from " + RESOURCE);
        String sFileName = ClassResourceUtil.mapFullPath(RESOURCE, false);
        if (sFileName != null) {
            Element xml = XMLUtil.fileToElement(sFileName);
            List list = xml.elements("engine");
            Element element;
            EngineBean bean;
            for (int i = 0; i < list.size(); i++) {
                element = (Element) list.get(i);
                bean = new EngineBean();
                bean.fromXML(element);
                m_hEngineBeans.put(bean.getId(), bean);
            }
        }
        logger.debug("engine server: loaded beans from " + sFileName);
    }

}