liurunyu
2023-11-12 21eb47b061d16056f37eee47928c7fe629b63061
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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("没有生成配置文件的DOM对象!", null);
            }
        } catch (Exception e) {
            throw new Exception("生成配置文件的DOM对象失败!", 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("没有得到配置文件根元素config!");
        }
        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);    
        }
    }
}