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);
}
}
}
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
- Ini文件的读取
- INI文件的读取
- INI文件的读取
- ini文件的读取
- ini文件的读取操作
- Java 读取 INI 文件的示例
- C读取ini文件的例子
- VB读取ini文件的函数
- 读取和写入Windows的INI文件
- 读取 INI 文件的模块儿
- MFC读取.ini文件的方法
- VC++对ini文件的读取
- ASP读取ini文件的实现方法
- Win32环境下的ini文件读取
- INI 文件的创建/修改/读取
- 读取保存密码的ini文件
- C++ ini文件的读取与写入
- MFC .ini文件的读取与写入
- 由放诎伪竞喝椅镜缀疑陶焙钦际荒
- String字符串通过MD5加密(java)
- myeclipse10 自动生成hibernate配置文件
- OCP 1Z0 053 38
- ;拉萨的克啊;拉萨的克
- ini文件的读取
- call_user_func_array函数的用法
- 一个apk通过隐式Intent 启动另一个apk
- linux命令--grep
- perl mysql
- swift--Swift中下划线和"#"的妙用
- 商城二次开发入口简单研究---- 20140701
- hdu1166 敌兵布阵
- perl 基本语法介绍