com.founder.e5.config.ConfigReader.java Source code

Java tutorial

Introduction

Here is the source code for com.founder.e5.config.ConfigReader.java

Source

package com.founder.e5.config;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;

import org.apache.commons.digester.Digester;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.xml.sax.SAXException;

import com.founder.e5.context.Context;
import com.founder.e5.context.EUID;
import com.founder.e5.db.DBSessionFactory;

/**
 * <p></p>
 * <p>Description: 
 * getConfigure(String)
 * e5-config.xml
 * <BR>
 * </p>
 * <p>
 * web.xmlload-on-startup
 * servlet
 * </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: Founder</p>
 * @created 2005-7-13
 * @author Gong Lijie
 * @version 1.0
 */
public class ConfigReader {
    //
    private CacheConfig cacheConfig;
    //
    private InfoCentralDB centralDB;
    //
    private List listRestart = new ArrayList(10);
    // key=id(in config file) value=InfoField
    private Map mapField = new HashMap(17);
    //
    private List listFactory = new ArrayList(10);
    //
    private List listOrg = new ArrayList(10);
    //
    private List listServer = new ArrayList(2);
    //DBSession
    private List listDBSession = new ArrayList(2);
    //ID-Config
    private List listID = new ArrayList(3);
    //TAB
    private List listSysPermissionPage = new ArrayList(4);

    private InfoSystemLoad systemLoad;
    //Locale
    private InfoLocale infoLocale;
    //item
    private InfoCustomize infoCustomize = new InfoCustomize();

    /**
     * Singleton
     */
    private static ConfigReader reader;

    private ConfigReader() {
    }

    public synchronized static ConfigReader getInstance() {
        if (reader == null)
            reader = new ConfigReader();
        return reader;
    }

    public String toString() {
        StringBuffer sbInfo = new StringBuffer(500);

        sbInfo.append(cacheConfig.toString());
        sbInfo.append("\n=========centralDB\n");
        sbInfo.append(centralDB.toString());
        sbInfo.append("\n=========listFactory\n");
        sbInfo.append(listFactory.toString());
        sbInfo.append("\n=========listOrg\n");
        sbInfo.append(listOrg.toString());
        sbInfo.append("\n=========listRestart\n");
        sbInfo.append(listRestart.toString());
        sbInfo.append("\n=========listServer\n");
        sbInfo.append(listServer.toString());
        sbInfo.append("\n=========mapField\n");
        sbInfo.append(mapField.toString());

        sbInfo.append("\n=========listDBSession\n");
        sbInfo.append(listDBSession.toString());
        sbInfo.append("\n=========listID\n");
        sbInfo.append(listID.toString());

        return sbInfo.toString();
    }

    /**
    * @return Returns the cacheConfig.
    */
    public CacheConfig getCacheConfig() {
        return cacheConfig;
    }

    /**
     * @return Returns the centralDB.
     */
    public InfoCentralDB getCentralDB() {
        return centralDB;
    }

    /**
     * @return Returns the Field Map.
     * key:id(in config file) 
     * value:InfoField
     */
    public Map getFields() {
        return mapField;
    }

    /**
     * @return Returns the listOrg.
     */
    public List getOrgs() {
        return listOrg;
    }

    /**
     * @return List
     */
    public List getFactories() {
        return listFactory;
    }

    /**
     * @return Returns the listRestart.
     */
    public List getServers() {
        return listServer;
    }

    /**
     * @return Returns the System Permission Page List
     */
    public List getPermissionPages() {
        return listSysPermissionPage;
    }

    /**
     * @return DBSession
     */
    public List getDBSessions() {
        return listDBSession;
    }

    /**
     * @return ID
     */
    public List getIDs() {
        return listID;
    }

    /**
     * @return 
     */
    public InfoSystemLoad getSystemLoad() {
        return systemLoad;
    }

    /**
     * @return Returns the listRestart.
     */
    public List getRestarts() {
        return listRestart;
    }

    public InfoLocale getLocale() {
        return infoLocale;
    }

    public InfoCustomize getCustomize() {
        return infoCustomize;
    }

    //===========AddInfo Method================
    /**
     * @param info
     */
    public void addInfo(InfoRestart info) {
        listRestart.add(info);
    }

    public void addInfo(CacheConfig info) {
        cacheConfig = info;
    }

    public void addInfo(InfoCentralDB info) {
        centralDB = info;
    }

    public void addInfo(InfoField info) {
        mapField.put(info.getId(), info);
    }

    public void addInfo(InfoOrg info) {
        listOrg.add(info);
    }

    public void addInfo(InfoDBSession info) {
        listDBSession.add(info);
    }

    public void addInfo(InfoPermissionPage info) {
        listSysPermissionPage.add(info);
    }

    public void addInfo(InfoID info) {
        listID.add(info);
    }

    public void addInfo(InfoFactory info) {
        listFactory.add(info);
    }

    public void addInfo(InfoSystemLoad info) {
        systemLoad = info;
    }

    public void addInfo(InfoServer server) {
        listServer.add(server);
    }

    public void addInfo(InfoLocale info) {
        infoLocale = info;
    }

    //TAB
    public void addInfo(InfoCustomizeItem info) {
        infoCustomize.addInfo(info);
    }

    //
    public void addCustomizeToolkit(String info) {
        infoCustomize.setCustomizeToolkit(info);
    }

    //
    public void addToolkitButtonStyle(String info) {
        infoCustomize.setDefaultButtonStyle(info);
    }
    //===========end.(AddInfo Method)================

    private static boolean checkLicense() {
        return E5SecurityManager.getInstance().checkLicense();
    }

    public static String getLicenseValue(String key) {
        return E5SecurityManager.getInstance().getLicenseValue(key);
    }

    /**
    * e5-config.xml
    * @param configFile WEB
    * @throws Exception
    * @return ConfigRestart
    */
    public synchronized boolean getConfigure(String configFile) {
        if (!checkLicense()) {
            return false;
        }

        InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(configFile);
        if (in == null)
            throw new RuntimeException("No configuration file!!" + configFile);
        try {
            getConfigure(in);
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                System.out.println("[E5 getConfigure]Configuration file close exception");
                e.printStackTrace();
            }
        }
        return true;
    }

    /**
      * 
      * @param in 
      */
    public synchronized void getConfigure(InputStream in) {
        //
        if (centralDB != null)
            return;

        Digester digester = new Digester();
        digester.setNamespaceAware(true);
        digester.setValidating(false);
        digester.setUseContextClassLoader(true);
        digester.push(this);
        digester.addRuleSet(new RuleSetConfig());

        try {
            digester.parse(in);
        } catch (IOException e) {
            throw new RuntimeException("Configuration file io exception!!! ", e);
        } catch (SAXException e) {
            throw new RuntimeException("Configuration file parse exception!!! ", e);
        }
    }

    private String configFilePath = "e5-config.xml";

    /**
     * Springcontext
     * configcontext
     */
    private void setApplicationContext() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("e5-context.xml");
        Context.setApplicationContext(ctx);
    }

    /**
     * e5-config.xml
     * <BR>
     * <BR>
     * @throws Exception
     * @return ConfigRestart
     */
    public synchronized void getConfigure() {
        setApplicationContext();
        getConfigure(configFilePath);

        //
        try {
            initRestart();
        } catch (Exception e) {
            throw new RuntimeException("Service initialization exception!!! ", e);
        }
    }

    /**
     * 
     * configcontext
     */
    private void initRestart() throws Exception {
        InfoRestart[] infos = (InfoRestart[]) listRestart.toArray(new InfoRestart[0]);
        if (infos == null)
            return;
        //Restart init
        for (int i = 0; i < infos.length; i++) {
            Class myClass = Class.forName(infos[i].getInvokeClass());
            Method method = myClass.getMethod(infos[i].getInvokeMethod(), null);
            if (Modifier.isStatic(method.getModifiers()))
                method.invoke(null, null);
            else
                method.invoke(myClass.newInstance(), null);
        }
        //DBSession Init
        for (int i = 0; i < listDBSession.size(); i++) {
            InfoDBSession db = (InfoDBSession) getDBSessions().get(i);
            DBSessionFactory.registerDB(db.getName(), db.getImplementation());
        }
        //ID Init
        for (int i = 0; i < listID.size(); i++) {
            InfoID id = (InfoID) getIDs().get(i);
            EUID.registerID(id.getName(), id.getType(), id.getParam());
        }
    }

    public static void main(String[] args) {
        ConfigReader.getInstance().getConfigure();
    }
}