ini文件的读取

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);
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喵星程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值