ConfigUtils.java 7.7 KB
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.airport.util;

import com.airport.bean.XMLHeader;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

public class ConfigUtils {
    private static final Logger logger = Logger.getLogger(ConfigUtils.class);
    public static String ROLE;
    public static String IMF_USERNAME;
    public static String IMF_PASSWORD;
    public static final String IFM_CONSUMER = "config/imf_config.properties";
    public static final String IMF_PRUDUCER = "config/imf_config.properties";
    public static final String INDEX_FILE_NAME = "data/fid_index.txt";
    public static int SEND_MESSAGE_INTERVAL = 1;
    public static int RECORD_COUNT = 1000;
    public static String XML_HEADER_SNDR = "";
    public static String XML_HEADER_TYPE = "";
    public static String XML_HEADER_STYPE = "";
    public static String XML_HEADER_RCVR = "";
    public static Map<Integer, XMLHeader> XML_HEADER_MAP = new HashMap();
    public static String RCVR = "";
    public static String APPID = "";
    public static String SNDR = "";
    public static String SQl="";
    public static String SQlMax="";
    public static Map<String, String> XTYPE_MAP = new HashMap();

    public ConfigUtils() {
    }
    private void initMap() {
        XTYPE_MAP.put("FSU_DEP", "UDEP");
        XTYPE_MAP.put("FSU_RCF", "URCF");
        XTYPE_MAP.put("FSU_FOH", "UFOH");
        XTYPE_MAP.put("FZE_DEP", "EDEP");
        XTYPE_MAP.put("FZE_RCF", "ERCF");
        XTYPE_MAP.put("FZE_FOH", "EFOH");
        XTYPE_MAP.put("CARGO_SERV", "CARG");
    }
    public void Initialize() throws LteException {
        logger.debug("Loading all the config parameters...");
        this.loadParameter();
        this.initMap();
    }

    private final void loadParameter() throws LteException {
        boolean ok = false;
        logger.info("================Initialize system parameter =================");
        Configuration config = (Configuration)SystemBean.getBean("configurationBean");
        ROLE = config.getProperty("role").trim();
        IMF_USERNAME = config.getProperty("imf_username").trim();
        IMF_PASSWORD = config.getProperty("imf_password").trim();
        SQl = config.getProperty("sql");
        SQlMax = config.getProperty("sqlmax");
        String interval = config.getProperty("interval").trim();
        String record_count = config.getProperty("record_count").trim();
        logger.info(String.format("role=%s", ROLE));
        logger.info(String.format("imf_username=%s", IMF_USERNAME));
        logger.info(String.format("imf_password=%s", IMF_PASSWORD));
        logger.info(String.format("interval=%s", interval));
        if (StringUtils.isBlank(ROLE)) {
            logger.error("role 参数错误");
            ok = true;
        } else if ("R".equalsIgnoreCase(ROLE)) {
            if (!isFileExists("config/imf_config.properties")) {
                logger.error("如果要从IMF接收数据,需要在config/imf_config.properties 配置IMF参数");
                ok = true;
            }
        } else if ("S".equalsIgnoreCase(ROLE)) {
            if (!isFileExists("config/imf_config.properties")) {
                logger.error("如果向IMF发送数据,需要在config/imf_config.properties 配置IMF参数");
                ok = true;
            }
        } else {
            logger.error("role 参数错误,必须是S(从IMF接收消息) 或者 R(发送消息到IMF)");
        }

        int int_total_group;
        if ("S".equalsIgnoreCase(ROLE)) {
            String total_group = config.getProperty("total_group").trim();
            int_total_group = 0;
            logger.info(String.format("record_count=%s", record_count));
            logger.info(String.format("total_group=%s", total_group));
            if (StringUtils.isBlank(total_group)) {
                logger.error("total_group 参数错误");
                ok = true;
            } else {
                int_total_group = Integer.parseInt(total_group);
                if (int_total_group <= 10 && int_total_group >= 1) {
                    this.loadXMLHeaderParameter(config, int_total_group);
                } else {
                    logger.error("total_group 参数设置错误,范围在1--10之间");
                    ok = true;
                }
            }

            int x;
            if (StringUtils.isBlank(record_count)) {
                logger.error("record_count 参数错误");
                ok = true;
            } else {
                x = Integer.parseInt(record_count);
                if (x <= 10000 && x >= 100) {
                    RECORD_COUNT = x;
                } else {
                    logger.error("record_count 参数设置错误,范围在100--10000之间");
                    ok = true;
                }
            }

            if (StringUtils.isBlank(interval)) {
                logger.error("interval 参数错误");
                ok = true;
            } else {
                x = Integer.parseInt(interval);
                if (x <= 60 && x >= 1) {
                    SEND_MESSAGE_INTERVAL = x * 1000;
                } else {
                    logger.error("interval 参数设置错误,范围在1---60之间");
                    ok = true;
                }
            }
        }

        if (!isFileExists("data/")) {
            File f = new File("data");
            f.mkdir();
            Utils.writeFIDIndex("data/fid_index.txt", 0);
            int_total_group = Utils.readFIDIndex("data/fid_index.txt");
            logger.info("FID=" + int_total_group);
        } else {
            logger.info("data/fid_index.txt exists");
        }

        boolean hasError = false;
        hasError = this.validate(IMF_USERNAME, "imf_username 参数错误");
        hasError = this.validate(IMF_PASSWORD, "imf_password 参数错误");
        if (ok && !hasError) {
            logger.error("参数错误 启动程序失败。");
            System.exit(-1);
        }

    }

    private void loadXMLHeaderParameter(Configuration config, int total) throws LteException {
        String sndr = "";
        String rcvr = "";
        String type = "";
        String stype = "";

        for(int i = 1; i <= total; ++i) {
            logger.info(String.format("group %d--------------------------", i));
            sndr = config.getProperty("sndr_" + i).trim();
            rcvr = config.getProperty("rcvr_" + i).trim();
            type = config.getProperty("type_" + i).trim();
            stype = config.getProperty("stype_" + i).trim();
            logger.info(String.format("SNDR_%d=%s", i, sndr));
            logger.info(String.format("RCVR_%d=%s", i, rcvr));
            logger.info(String.format("TYPE_%d=%s", i, type));
            logger.info(String.format("STYPE_%d=%s", i, stype));
            XMLHeader header = new XMLHeader();
            header.setSndr(sndr);
            header.setRcvr(rcvr);
            header.setType(type);
            header.setStype(stype);
            logger.info("group[" + i + "]\t" + header.toString());
            XML_HEADER_MAP.put(i, header);
        }

    }

    private boolean validate(String paraValue, String tipText) {
        if (StringUtils.isBlank(paraValue)) {
            logger.error(tipText);
            return false;
        } else {
            return true;
        }
    }

    private static boolean isFileExists(String filename) {
        File f = new File(filename);
        return f.exists();
    }

    public static void main(String[] argc) {
        File f = new File("config/producer_imf_config.properties");
        if (f.exists()) {
            System.out.println("file exists");
        } else {
            System.out.println("file not exists");
        }

    }
}