Java有序读取配置文件,有序读取ini配置文件
本项目中对 shiro 的权限配置,需要有序配置,因为权限控制是个漏斗形式匹配模式。
如:第一个匹配不中就找第二个,这样的话如果顺序随机或者乱了的话,直接匹配第二个了。或者第三个。
本工具实现于给ini
配置文件读取,其实换做其他配置文件(properties
)都可以。你看下思路即可了解。
提供了三个构造方法:
一、参数为文件(File)。
/**
* 读取
* @param file 文件
* @throws FileNotFoundException
*/
public INI4j(File file) throws FileNotFoundException {
this.init(new BufferedReader(new FileReader(file)));
}
二、参数为Path文件路径
/***
* 重载读取
* @param path 给文件路径
* @throws FileNotFoundException
*/
public INI4j(String path) throws FileNotFoundException {
this.init(new BufferedReader(new FileReader(path)));
}
三、参数为ClassPathResource。
/***
* 重载读取
* @param source ClassPathResource 文件,如果文件在resource 里,那么直接 new ClassPathResource("file name");
* @throws IOException
*/
public INI4j(ClassPathResource source) throws IOException {
this(source.getFile());
}
全部代码:
package com.sojson.core.config; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.LinkedHashMap; import java.util.Map; import org.springframework.core.io.ClassPathResource; /** * * 开发公司:SOJSON在线工具 <p> * 版权所有:© www.sojson.com<p> * 博客地址:https://www.sojson.com/blog/ <p> * <p> * * 有序读取 ini配置文件 * * <p> * * 区分 责任人 日期 说明<br/> * 创建 周柏成 2016年6月2日 <br/> * * @author zhou-baicheng * @email so@sojson.com * @version 1.0,2016年6月2日 <br/> * */ public class INI4j { /** * 用linked hash map 来保持有序的读取 * */ final LinkedHashMap<String,LinkedHashMap<String, String>> coreMap = new LinkedHashMap<String, LinkedHashMap<String,String>>(); /** * 当前Section的引用 */ String currentSection = null; /** * 读取 * @param file 文件 * @throws FileNotFoundException */ public INI4j(File file) throws FileNotFoundException { this.init(new BufferedReader(new FileReader(file))); } /*** * 重载读取 * @param path 给文件路径 * @throws FileNotFoundException */ public INI4j(String path) throws FileNotFoundException { this.init(new BufferedReader(new FileReader(path))); } /*** * 重载读取 * @param source ClassPathResource 文件,如果文件在resource 里,那么直接 new ClassPathResource("file name"); * @throws IOException */ public INI4j(ClassPathResource source) throws IOException { this(source.getFile()); } void init(BufferedReader bufferedReader){ try { read(bufferedReader); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("IO Exception:" + e); } } /** * 读取文件 * @param reader * @throws IOException */ void read(BufferedReader reader) throws IOException { String line = null; while((line=reader.readLine())!=null) { parseLine(line); } } /** * 转换 * @param line */ void parseLine(String line) { line = line.trim(); // 此部分为注释 if(line.matches("^\\#.*$")) { return; }else if (line.matches("^\\[\\S+\\]$")) { // section String section = line.replaceFirst("^\\[(\\S+)\\]$","$1"); addSection(section); }else if (line.matches("^\\S+=.*$")) { // key ,value int i = line.indexOf("="); String key = line.substring(0, i).trim(); String value =line.substring(i + 1).trim(); addKeyValue(currentSection,key,value); } } /** * 增加新的Key和Value * @param currentSection * @param key * @param value */ void addKeyValue(String currentSection,String key, String value) { if(!coreMap.containsKey(currentSection)) { return; } Map<String, String> childMap = coreMap.get(currentSection); childMap.put(key, value); } /** * 增加Section * @param section */ void addSection(String section) { if (!coreMap.containsKey(section)) { currentSection = section; LinkedHashMap<String,String> childMap = new LinkedHashMap<String,String>(); coreMap.put(section, childMap); } } /** * 获取配置文件指定Section和指定子键的值 * @param section * @param key * @return */ public String get(String section,String key){ if(coreMap.containsKey(section)) { return get(section).containsKey(key) ? get(section).get(key): null; } return null; } /** * 获取配置文件指定Section的子键和值 * @param section * @return */ public Map<String, String> get(String section){ return coreMap.containsKey(section) ? coreMap.get(section) : null; } /** * 获取这个配置文件的节点和值 * @return */ public LinkedHashMap<String, LinkedHashMap<String, String>> get(){ return coreMap; } }
调用方式:
ClassPathResource cp = new ClassPathResource("shiro_base_auth.ini"); INI4j ini = null; try { ini = new INI4j(cp.getFile()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
配置文件书写规范(本范例配置文件):
[base_auth] /u/**=anon /user/**=simple,login /js/**=anon /css/**=anon /open/**=anon #不用校验地址是否有权限 /permission/selectPermissionById.shtml=simple,login /member/onlineDetails/**=simple,login /role/mypermission.shtml=simple,login /role/getPermissionTree.shtml=simple,login /role/selectRoleByUserId.shtml=simple,login #需要根据地址校验有无权限 /permission/**=simple,login,permission /role/**=simple,login,permission /member/**=simple,login,permission /**=simple,login
版权所属:SO JSON在线解析
原文地址:https://www.sojson.com/blog/139.html
转载时必须以链接形式注明原始出处及本声明。
本文主题:
如果本文对你有帮助,那么请你赞助我,让我更有激情的写下去,帮助更多的人。