代碼實現
package com.xxx.xxx.utils;
import com.xxx.xxx.schedule.CronTaskDef;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.Yaml;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.regex.Pattern;
/**
* @description: 批量文件系統配置文件容器
**/
public class BatFileConfig {
public LinkedHashMap configLinkedHashMap;
private static String pattern = "([a-zA-Z0-9]+)((\\.[a-zA-Z0-9]+)*)";
public static volatile boolean inited;
public static final String PASSFILE_NFSDIR="nfsDir";
public static Logger logger = LoggerFactory.getLogger(BatFileConfig.class);
/**
* @param configPath 配置文件路徑
* @description: 初始化所有證書
* @return:
*/
/**
* 操作對象.
*/
private static BatFileConfig config = new BatFileConfig();
public static BatFileConfig getConfig() {
return config;
}
/*
* 初始化yaml數據進內存configLinkedHashMap
*/
public void inti(String configPath) {
logger.debug("初始化;配置文件路徑:" + configPath);
FileInputStream configFileInputStream = null;
try {
Yaml certsYaml = new Yaml();
File certsYamlFile = new File(configPath);
//加載公共證書,
configFileInputStream = new FileInputStream(certsYamlFile);
configLinkedHashMap = certsYaml.load(configFileInputStream);
inited = true;
} catch (Exception ex) {
logger.error("初始化證書容器失敗");
logger.error(ex.getMessage());
} finally {
try {
configFileInputStream.close();
} catch (Exception e) {
logger.error("關閉流失敗: " + e.getMessage());
}
}
}
/*
* 獲取定時任務列表
*/
public ArrayList<CronTaskDef> getCronList()
{
ArrayList<CronTaskDef> CronTasks=new ArrayList<>();
try
{
ArrayList<LinkedHashMap> CronMaps=getList("scheduledTask");
for(LinkedHashMap map:CronMaps)
{
CronTasks.add(new CronTaskDef(map.get("cronId")==null?"":map.get("cronId").toString(),
map.get("cronClassName")==null?"":map.get("cronClassName").toString(),
map.get("cronExpression")==null?"":map.get("cronExpression").toString(),
map.get("cronPara")==null?"":map.get("cronPara").toString(),
map.get("cronStatus")==null?"":map.get("cronStatus").toString()));
}
return CronTasks;
}
catch (Exception ex)
{
logger.error(ex.getMessage());
}
return null;
}
/*
* 根據指定key獲取列表數據
*/
public ArrayList<LinkedHashMap> getList(String key) throws IllegalArgumentException {
//正則驗證
if (!Pattern.matches(pattern, key)) {
throw new IllegalArgumentException(String.format("key:%s not match pattern :%s", key, pattern));
}
String[] keyArray = key.split("\\.");
LinkedHashMap map = configLinkedHashMap;
for (int i = 0; i < keyArray.length; i++) {
if (i < keyArray.length - 1) {
if (!(map.get(keyArray[i]) instanceof LinkedHashMap)) {
return null;
} else {
map = (LinkedHashMap) map.get(keyArray[i]);
}
} else {
if (map.get(keyArray[i]) instanceof ArrayList<?>) {
return (ArrayList<LinkedHashMap>)map.get(keyArray[i]);
} else return null;
}
}
return null;
}
/**
* 根據指定key獲取值
* @param key 格式:xxx.xxx.xxx
* @description:a
* @return:a
*/
public String getStringValue(String key) throws IllegalArgumentException {
//正則驗證
if (!Pattern.matches(pattern, key)) {
throw new IllegalArgumentException(String.format("key:%s not match pattern :%s", key, pattern));
}
String[] keyArray = key.split("\\.");
LinkedHashMap map = configLinkedHashMap;
for (int i = 0; i < keyArray.length; i++) {
if (i < keyArray.length - 1) {
if (!(map.get(keyArray[i]) instanceof LinkedHashMap)) {
return "";
} else {
map = (LinkedHashMap) map.get(keyArray[i]);
}
} else {
if (map.get(keyArray[i]) != null) {
return map.get(keyArray[i]).toString();
} else return "";
}
}
return "";
}
}
學習Java的同學注意了!!!
學習過程中遇到什麼問題或者想獲取學習資源的話,歡迎加入Java學習交流羣,羣號碼:543120397 我們一起學Java!