From b3b17b231e2f2840332ce6eb96f791865fdec6d5 Mon Sep 17 00:00:00 2001
From: zhubaomin <zhubaomin>
Date: 星期一, 14 四月 2025 16:38:16 +0800
Subject: [PATCH] 代码优化
---
pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/Config.java | 125 +++++++++++++++++++++++++++++++++++++++++
1 files changed, 125 insertions(+), 0 deletions(-)
diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/Config.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/Config.java
new file mode 100644
index 0000000..a1ba3a0
--- /dev/null
+++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/Config.java
@@ -0,0 +1,125 @@
+package com.dy.common.util;
+
+import java.io.InputStream;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+
+import org.jdom2.Document;
+import org.jdom2.Element;
+import org.jdom2.input.SAXBuilder;
+
+@SuppressWarnings("unused")
+public class Config {
+
+ private static HashMap<String, String> configs;
+ private static Config config;
+
+ /**
+ * 寰楀埌鍞竴瀹炰緥
+ * @return 杩斿洖瀹炰緥
+ */
+ public static Config getInstance() {
+ if (config == null) {
+ config = new Config();
+ configs = new HashMap<>();
+ }
+ return config;
+ }
+
+ private Config() {
+ }
+ /**
+ * 鏍规嵁閰嶇疆鍚嶇О寰楀埌閰嶇疆鍊�
+ * @param name 閰嶇疆鍚嶇О
+ * @return 閰嶇疆鍊�
+ */
+ public static String getConfig(String name) {
+ if (configs == null) {
+ return null;
+ }
+ if(name.trim().equals("")){
+ return "" ;
+ }
+ return configs.get(name);
+ }
+
+ /**
+ * 鍒濆鍖栭厤缃�
+ * @param fileUrl 閰嶇疆鏂囦欢璺緞
+ * @throws Exception 鎶涘嚭寮傚父
+ */
+ public void init(URL fileUrl) throws Exception {
+ parseConfig(createDom(fileUrl));
+ }
+ /**
+ * 閲嶆柊鍒濆鍖�
+ * @param fileUrl 閰嶇疆鏂囦欢璺緞
+ * @param propertiesInput 灞炴�ф枃浠惰緭鍏ユ祦
+ * @throws Exception 鎶涘嚭寮傚父
+ */
+ public void reInit(URL fileUrl, InputStream propertiesInput) throws Exception {
+ configs = new HashMap<>();
+ parseConfig(createDom(fileUrl));
+ }
+
+ /**
+ * 鍒涘缓dom瀵硅薄
+ * @param configFileURL 閰嶇疆鏂囦欢璺緞
+ * @return dom瀵硅薄
+ */
+ private Document createDom(URL configFileURL) throws Exception {
+ if (configFileURL == null) {
+ throw new Exception("娌℃湁寰楀埌閰嶇疆鏂囦欢!", null);
+ }
+ Document doc = null;
+ try {
+ SAXBuilder sb = new SAXBuilder();
+ doc = sb.build(configFileURL);
+ if (doc == null) {
+ throw new Exception("娌℃湁鐢熸垚閰嶇疆鏂囦欢鐨凞OM瀵硅薄!", null);
+ }
+ } catch (Exception e) {
+ throw new Exception("鐢熸垚閰嶇疆鏂囦欢鐨凞OM瀵硅薄澶辫触!", e);
+ }
+ return doc;
+ }
+
+ /**
+ * 鍒嗘瀽鏈嶅姟鍣ㄨ繍琛屽弬鏁�
+ * @param doc dom瀵硅薄
+ * @throws Exception 鎶涘嚭寮傚父
+ */
+ private void parseConfig(Document doc) throws Exception {
+ Element root = doc.getRootElement();
+ if (root == null) {
+ throw new Exception("娌℃湁寰楀埌閰嶇疆鏂囦欢鏍瑰厓绱燾onfig!");
+ }
+ List<Element> list = root.getChildren();
+ if (list == null) {
+ throw new Exception("閰嶇疆鏂囦欢娌℃湁閰嶇疆鍏冪礌!");
+ }
+ Iterator<Element> it = list.iterator();
+ Element e ;
+ String name ;
+ String tx ;
+ while (it.hasNext()) {
+ e = it.next();
+ if (e == null) {
+ throw new Exception("寰楀埌閰嶇疆鏂囦欢涓厓绱犻厤缃嚭閿�!");
+ }
+ name = e.getName();
+ if (configs.get(name) != null) {
+ throw new Exception("閰嶇疆鏂囦欢涓厓绱�" + name + "閲嶅悕锛�");
+ }
+ tx = e.getText();
+
+ if(tx != null && tx.startsWith("${") && tx.endsWith("}")){
+ tx = tx.substring(2, tx.length() - 1) ;
+ tx = ConfigProperties.getConfig(tx) ;
+ }
+ configs.put(name, tx);
+ }
+ }
+}
--
Gitblit v1.8.0