ini文件的读取

来源:互联网 发布:软件的开发过程 编辑:程序博客网 时间:2024/04/19 18:47
package com.sosgps.system.util.customized;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.util.IOUtils;

import com.sosgps.framework.util.ConfigureCache;
import com.sosgps.framework.util.IniEditor;
import com.sosgps.system.util.customized.parser.RequestUrlParser;
import com.sosgps.system.util.customized.parser.RequestUrlParserFactory;

/**
 * 读取定制化相关信息,并判断是否为定制化企业
 *
 * @author jianli.mo
 *
 */
public class CustomizedInfoLoad {

    private static final String CUSTOMIZED_INFO = "customized_info.ini";

    //存放ini文件键值map(key为“企业编码:请求action名称”,value为企业编码集合),用来判断是否为定制化
    private static final Map<String, List<String>> CUSTOMIZED_KEY_MAP = new HashMap<String, List<String>>();

    //存放ini文件中targetAciton(key为“企业编码:请求action名称”,value为“targetAction”)
    private static final Map<String, String> CUSTOMIZED_ACTION_NAME_DATA = new HashMap<String, String>();

    //存放ini文件中targetAciton(key为“企业编码:请求action名称”,value为“targetAction”对应的parser实现类名称)
    private static final Map<String, String> CUSTOMIZED_REQUEST_PARSER_DATA = new HashMap<String, String>();

    static {
        loadConfig(CUSTOMIZED_INFO);
    }

    /**
     * 根据登录企业编码判断是否为定制化企业请求url
     *
     * @param loginEntCode 当前登录企业code
     * @param targetEntCode 目标定制化企业code
     * @return
     */
    public static boolean isCustomizedUrl(String loginEntCode, String actionName) {
        List<String> list = null;
        if (StringUtils.isNotBlank(loginEntCode) && StringUtils.isNotBlank(actionName)) {
            String key = loginEntCode.toLowerCase() + ":" + actionName.toLowerCase();
            list = CUSTOMIZED_KEY_MAP.get(key);
            return list != null ? list.contains(loginEntCode.toLowerCase()) : false;
        }
        return false;
    }

    /**
     * 根据登录企业编码获取重定向的action名称
     *
     * @param loginEntCode 当前登录企业code
     * @return
     */
    public static String getCustomizedAction(String loginEntCode, String actionName,
            HttpServletRequest request) {
        String customizedActionName = "";
        if (StringUtils.isNotBlank(loginEntCode) && StringUtils.isNotBlank(actionName)) {
            String key = loginEntCode.toLowerCase() + ":" + actionName.toLowerCase();

            //获取paser实现类名称
            String customizedRequestParserName = CUSTOMIZED_REQUEST_PARSER_DATA.get(key);
            RequestUrlParser requestParser = null;
            if (StringUtils.isNotBlank(customizedRequestParserName)) {
                //获取paser实现类对象
                requestParser = RequestUrlParserFactory.getParser(customizedRequestParserName);
            }
            //先从实现类对象中获取action名,若实现类对象不存在,则从ini文件中读取
            if (null != requestParser) {
                customizedActionName = requestParser.getTargetAction(request);
            } else {
                customizedActionName = CUSTOMIZED_ACTION_NAME_DATA.get(key);
            }
        }
        return customizedActionName;
    }

    /**
     * 读取ini文件数据
     *
     * @param configureFileName
     */
    private static void loadConfig(String configureFileName) {
        InputStream is = null;
        try {
            is = ConfigureCache.class.getClassLoader().getResourceAsStream(configureFileName);
            if (is == null) {
                throw new RuntimeException("Configure file not found.");
            }
            IniEditor ini = new IniEditor();
            ini.load(is);

            List<String> sectionNames = ini.sectionNames();
            for (String sectionName : sectionNames) {
                //":"分割片段名
                String[] splitSection = sectionName.split(":");
                //企业编码
                String entCodes = splitSection[0];
                //请求action名
                String actionName = splitSection[1];
                //","分割企业编码获得数组
                String[] splitEntCodes = entCodes.split(",");
                List<String> entCodelist = new ArrayList<String>();
                for (int i = 0; i < splitEntCodes.length; i++) {
                    entCodelist.add(splitEntCodes[i]);
                    String key = splitEntCodes[i].toLowerCase() + ":" + actionName.toLowerCase();
                    String targetAction = ini.get(sectionName, "targetAction");
                    if (StringUtils.isNotBlank(targetAction)) {
                        CUSTOMIZED_ACTION_NAME_DATA.put(key, ini.get(sectionName, "targetAction"));
                        CUSTOMIZED_KEY_MAP.put(key, entCodelist);
                    }

                    //获取配置的requestParser
                    String requestParser = ini.get(sectionName, "requestParser");
                    if (StringUtils.isNotBlank(requestParser)) {
                        try {
                            CUSTOMIZED_REQUEST_PARSER_DATA.put(key, requestParser);
                            CUSTOMIZED_KEY_MAP.put(key, entCodelist);
                            RequestUrlParserFactory.addParser(requestParser);
                        } catch (ClassNotFoundException e) {
                            throw new RuntimeException("Bad configure file.", e);
                        } catch (InstantiationException e) {
                            throw new RuntimeException("InstantiationException", e);
                        } catch (IllegalAccessException e) {
                            throw new RuntimeException("IllegalAccessException", e);
                        }
                    }
                }
            }
        } catch (IOException e) {
            throw new RuntimeException("Read configure file failed.", e);
        } finally {
            IOUtils.closeQuietly(is);
        }
    }

}

0 0
原创粉丝点击