lodsve.core.properties.Ini Maven / Gradle / Ivy
/*
* Copyright (C) 2018 Sun.Hao
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package lodsve.core.properties;
import lodsve.core.properties.ini.IniLoader;
import lodsve.core.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* ini配置获取.
*
* @author sunhao([email protected])
* @version V1.0
* @createTime 2014-12-31 14:19
*/
public class Ini {
private static final Logger logger = LoggerFactory.getLogger(Ini.class);
private static Map> inis;
static {
init();
}
private static void init() {
try {
inis = IniLoader.getIni();
} catch (Exception e) {
throw new RuntimeException("加载配置文件发生IO异常");
}
}
/**
* 获取keys
*
* @return
*/
public static Set getSections() {
return inis.keySet();
}
/**
* 根据section获取配置
*
* @param section
* @return
*/
public static Map getConfigsBySection(String section) {
return inis.get(section);
}
/**
* 获取配置
*
* @param section
* @param key
* @return
*/
public static String getConfig(String section, String key) {
Map sections = inis.get(section);
if (sections == null || sections.isEmpty()) {
return StringUtils.EMPTY;
}
return sections.get(key);
}
/**
* 获取布尔型配置
*
* @param section
* @param key
* @return
*/
public static boolean getBooleanConfig(String section, String key) {
String config = getConfig(section, key);
return "true".equals(config) || "1".equals(config) || "y".equalsIgnoreCase(config) || "yes".equalsIgnoreCase(config);
}
/**
* 获取所有配置
*
* @return
*/
public static Map> getAllConfigs() {
return inis;
}
/**
* 获取指定ini资源
*
* @param resource
* @return
*/
public static Map> getFileConfig(Resource resource) {
Assert.notNull(resource, "资源不能为空!");
Map> maps = new HashMap<>(16);
try {
maps.putAll(IniLoader.getIni(resource));
} catch (Exception e) {
logger.error("加载资源'{}'失败!", resource);
logger.error(e.getMessage(), e);
}
return maps;
}
}