Java有序读取配置文件,有序读取ini配置文件

JSON 2016-08-22 15:49:59 15253

本项目中对 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

转载时必须以链接形式注明原始出处及本声明。

本文主题:

如果本文对你有帮助,那么请你赞助我,让我更有激情的写下去,帮助更多的人。

关于作者
一个低调而闷骚的男人。
相关文章
Shiro教程(六)Shiro整体的配置文件
Elasticsearch教程,Elasticsearch配置文件 — elasticsearch.yml
Shiro教程,Shiro 配置文件详细解释,Shiro自定义Filter配置
Springboot + Freemarker 集成配置
Urlrewrite Java 伪静态 urlrewrite.xml 配置参数描述
基于Centos7以上的JAVA相关配置(一)
Shiro教程(四)Shiro + Redis配置
Java API接口返回不是JSON的解决方案,SpringMVC返回JSON配置
Java web.xml 配置技巧—动态欢迎页地址
Springboot + Mybatis +Maven 自动生成 Mapper.xml,Entity,Dao。 generator 配置
最新文章
文件上传漏洞与防御 4058
前端构建工具选型指南:Webpack、Vite、Rollup、esbuild 深度对比 1444
物联网时代2026年时序数据库选型指南 1151
SaaS行业面临AI挑战:从“无限复用”到“灵活适应” 1269
神经网络:从构造到模型训练全链路解析 1168
一文吃透 Redis 核心存储结构:ziplist、listpack 与哈希表扩容 / 并发查询 1593
Linux sudo提权完整指南:从基础用法到生产级安全配置 691
XSS 和 CSRF 的本质区别及开发防御全解析 772
JVM垃圾回收(GC)全维度解析:从原理到调优实战 813
Linux动静态库与ELF加载全解析:从实操制作到底层原理 912
最热文章
免费天气API,天气JSON API,不限次数获取十五天的天气预报 783114
最新MyEclipse8.5注册码,有效期到2020年 (已经更新) 711464
苹果电脑Mac怎么恢复出厂系统?苹果系统怎么重装系统? 679993
Jackson 时间格式化,时间注解 @JsonFormat 用法、时差问题说明 562673
我为什么要选择RabbitMQ ,RabbitMQ简介,各种MQ选型对比 512621
Elasticsearch教程(四) elasticsearch head 插件安装和使用 484794
Jackson 美化输出JSON,优雅的输出JSON数据,格式化输出JSON数据... ... 302947
Java 信任所有SSL证书,HTTPS请求抛错,忽略证书请求完美解决 247433
Elasticsearch教程(一),全程直播(小白级别) 233097
谈谈斐讯路由器劫持,你用斐讯路由器,你需要知道的事情 228329
支付扫码

所有赞助/开支都讲公开明细,用于网站维护:赞助名单查看

查看我的收藏

正在加载... ...