yaml文件解析

r囧r小猫 2023-06-16 13:52 137阅读 0赞

代码实现

  1. package com.xxx.xxx.utils;
  2. import com.xxx.xxx.schedule.CronTaskDef;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.yaml.snakeyaml.Yaml;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.util.ArrayList;
  9. import java.util.LinkedHashMap;
  10. import java.util.regex.Pattern;
  11. /**
  12. * @description: 批量文件系统配置文件容器
  13. **/
  14. public class BatFileConfig {
  15. public LinkedHashMap configLinkedHashMap;
  16. private static String pattern = "([a-zA-Z0-9]+)((\\.[a-zA-Z0-9]+)*)";
  17. public static volatile boolean inited;
  18. public static final String PASSFILE_NFSDIR="nfsDir";
  19. public static Logger logger = LoggerFactory.getLogger(BatFileConfig.class);
  20. /**
  21. * @param configPath 配置文件路径
  22. * @description: 初始化所有证书
  23. * @return:
  24. */
  25. /**
  26. * 操作对象.
  27. */
  28. private static BatFileConfig config = new BatFileConfig();
  29. public static BatFileConfig getConfig() {
  30. return config;
  31. }
  32. /*
  33. * 初始化yaml数据进内存configLinkedHashMap
  34. */
  35. public void inti(String configPath) {
  36. logger.debug("初始化;配置文件路径:" + configPath);
  37. FileInputStream configFileInputStream = null;
  38. try {
  39. Yaml certsYaml = new Yaml();
  40. File certsYamlFile = new File(configPath);
  41. //加载公共证书,
  42. configFileInputStream = new FileInputStream(certsYamlFile);
  43. configLinkedHashMap = certsYaml.load(configFileInputStream);
  44. inited = true;
  45. } catch (Exception ex) {
  46. logger.error("初始化证书容器失败");
  47. logger.error(ex.getMessage());
  48. } finally {
  49. try {
  50. configFileInputStream.close();
  51. } catch (Exception e) {
  52. logger.error("关闭流失败: " + e.getMessage());
  53. }
  54. }
  55. }
  56. /*
  57. * 获取定时任务列表
  58. */
  59. public ArrayList<CronTaskDef> getCronList()
  60. {
  61. ArrayList<CronTaskDef> CronTasks=new ArrayList<>();
  62. try
  63. {
  64. ArrayList<LinkedHashMap> CronMaps=getList("scheduledTask");
  65. for(LinkedHashMap map:CronMaps)
  66. {
  67. CronTasks.add(new CronTaskDef(map.get("cronId")==null?"":map.get("cronId").toString(),
  68. map.get("cronClassName")==null?"":map.get("cronClassName").toString(),
  69. map.get("cronExpression")==null?"":map.get("cronExpression").toString(),
  70. map.get("cronPara")==null?"":map.get("cronPara").toString(),
  71. map.get("cronStatus")==null?"":map.get("cronStatus").toString()));
  72. }
  73. return CronTasks;
  74. }
  75. catch (Exception ex)
  76. {
  77. logger.error(ex.getMessage());
  78. }
  79. return null;
  80. }
  81. /*
  82. * 根据指定key获取列表数据
  83. */
  84. public ArrayList<LinkedHashMap> getList(String key) throws IllegalArgumentException {
  85. //正则验证
  86. if (!Pattern.matches(pattern, key)) {
  87. throw new IllegalArgumentException(String.format("key:%s not match pattern :%s", key, pattern));
  88. }
  89. String[] keyArray = key.split("\\.");
  90. LinkedHashMap map = configLinkedHashMap;
  91. for (int i = 0; i < keyArray.length; i++) {
  92. if (i < keyArray.length - 1) {
  93. if (!(map.get(keyArray[i]) instanceof LinkedHashMap)) {
  94. return null;
  95. } else {
  96. map = (LinkedHashMap) map.get(keyArray[i]);
  97. }
  98. } else {
  99. if (map.get(keyArray[i]) instanceof ArrayList<?>) {
  100. return (ArrayList<LinkedHashMap>)map.get(keyArray[i]);
  101. } else return null;
  102. }
  103. }
  104. return null;
  105. }
  106. /**
  107. * 根据指定key获取值
  108. * @param key 格式:xxx.xxx.xxx
  109. * @description:a
  110. * @return:a
  111. */
  112. public String getStringValue(String key) throws IllegalArgumentException {
  113. //正则验证
  114. if (!Pattern.matches(pattern, key)) {
  115. throw new IllegalArgumentException(String.format("key:%s not match pattern :%s", key, pattern));
  116. }
  117. String[] keyArray = key.split("\\.");
  118. LinkedHashMap map = configLinkedHashMap;
  119. for (int i = 0; i < keyArray.length; i++) {
  120. if (i < keyArray.length - 1) {
  121. if (!(map.get(keyArray[i]) instanceof LinkedHashMap)) {
  122. return "";
  123. } else {
  124. map = (LinkedHashMap) map.get(keyArray[i]);
  125. }
  126. } else {
  127. if (map.get(keyArray[i]) != null) {
  128. return map.get(keyArray[i]).toString();
  129. } else return "";
  130. }
  131. }
  132. return "";
  133. }
  134. }

学习Java的同学注意了!!!
学习过程中遇到什么问题或者想获取学习资源的话,欢迎加入Java学习交流群,群号码:543120397 我们一起学Java!

发表评论

表情:
评论列表 (有 0 条评论,137人围观)

还没有评论,来说两句吧...

相关阅读