DELL
2025-02-11 cec8535c3bd288d555f0252e240fc19998febb84
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package com.config;
 
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
 
/**
 * 创建config.xml文件并更新  config.xml 文件中的属性值
 * @author 军
 *
 */
public class AppConfig {
    
private final String ConfigFileName = "config.xml";
    
    private String sqlServerIp = "127.0.0.1";        //数据库IP地址
    private int sqlServerPort = 5866;                //数据库端口号
    private int sqlConnCountMax = 200;                //数据库连接池数量
    private int lockAcceptPort = 9001;                //电子锁通信端口
    private String commType = "IP";                    //电子锁识别方式 IP地址-与串口服务器IP地址识别      SN-外网与GPRS模块SN识别
    
    private Logger logger = null;
    
    public AppConfig()
    {
        logger = LogManager.getLogger(this.getClass());
        /*
        String path = System.getProperty("user.dir");
        if(path.contains("\\"))
            path += "\\";
        else path += "/";
        */
        String path = ConfigFileName;
        //System.out.println(path);
        File f = new File(path);
        if(false == f.exists())
        {
            writeConfigToXml();
        }
        
        readConfigFromXml();
    }
 
    public void copyAppParam(AppConfig config) {
        this.sqlServerIp = config.sqlServerIp;                            //数据库IP地址
        this.sqlServerPort = config.sqlServerPort;                        //数据库端口号
        this.sqlConnCountMax = config.sqlConnCountMax;                    //数据库连接池数量
        
    }
    
    
 
    public void writeConfigToXml()
    {
        try {
            Document document = DocumentHelper.createDocument();
            Element root = document.addElement("root");
            //root.addComment("这个一个注释");
            
            Element param;
            
            //-------------------------------------------------------//
            param = root.addElement("sql_server_ip"); 
            param.addText(sqlServerIp);
            param = root.addElement("sql_server_port"); 
            param.addText(String.valueOf(sqlServerPort));
            param = root.addElement("sql_conncount_max"); 
            param.addText(String.valueOf(sqlConnCountMax));
            
            
            param = root.addElement("lockAcceptPort"); 
            param.addText(String.valueOf(lockAcceptPort));
 
            param = root.addElement("commType"); 
            param.addText(commType);
            //-------------------------------------------------------//
            
            //-------------------------------------------------------//
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("gbk");
            XMLWriter writer2 = new XMLWriter(new FileWriter(ConfigFileName), format);
            writer2.write(document);
            writer2.close();
        } catch (UnsupportedEncodingException e) {
            logger.error(e.toString(), e);
        } catch (IOException e) {
            logger.error(e.toString(), e);
        }
    }
    
    public void readConfigFromXml()
    {
        boolean res = true;
        try 
        {
            SAXReader reader = new SAXReader();
            Document document = reader.read(new File(ConfigFileName));
            Element rootnode = document.getRootElement();
            
            Element node;
            Element sub_node;
            
            //------------------------------------------------------------//
            //------------------------------------------------------------//
            node = rootnode.element("sql_server_ip"); 
            sqlServerIp = node.getTextTrim();
            //------------------------------------------------------------//
            node = rootnode.element("sql_server_port"); 
            sqlServerPort = Integer.parseInt(node.getTextTrim());
            //------------------------------------------------------------//
            node = rootnode.element("sql_conncount_max"); 
            sqlConnCountMax = Integer.parseInt(node.getTextTrim());
            
            if(sqlConnCountMax < 10)
                sqlConnCountMax = 10;
            if(sqlConnCountMax > 1000)
                sqlConnCountMax = 800;
            //------------------------------------------------------------//
            node = rootnode.element("lockAcceptPort"); 
            lockAcceptPort = Integer.parseInt(node.getTextTrim());
            
            if(lockAcceptPort < 10000) {
                lockAcceptPort = 9001;
            }
            node = rootnode.element("commType"); 
            commType = node.getTextTrim();
            //------------------------------------------------------------//
            
            
        } catch (NullPointerException | NumberFormatException | DocumentException e) {
            res = false;
            logger.error(e.toString(), e);
        } finally {
            if(false == res)
                writeConfigToXml();
        }
    }
    
 
 
    public String getConfigFileName() {
        return ConfigFileName;
    }
 
    public int getLockAcceptPort() {
        return lockAcceptPort;
    }
 
    public String getCommType() {
        return commType;
    }
 
    public Logger getLogger() {
        return logger;
    }
 
    public void setSqlServerIp(String sqlServerIp) {
        this.sqlServerIp = sqlServerIp;
    }
 
    public void setSqlServerPort(int sqlServerPort) {
        this.sqlServerPort = sqlServerPort;
    }
 
    public void setSqlConnCountMax(int sqlConnCountMax) {
        this.sqlConnCountMax = sqlConnCountMax;
    }
 
    public void setLockAcceptPort(int lockAcceptPort) {
        this.lockAcceptPort = lockAcceptPort;
    }
 
    public void setCommType(String commType) {
        this.commType = commType;
    }
 
    public void setLogger(Logger logger) {
        this.logger = logger;
    }
 
    public String getSqlServerIp() {
        return sqlServerIp;
    }
 
    public int getSqlServerPort() {
        return sqlServerPort;
    }
 
    public int getSqlConnCountMax() {
        return sqlConnCountMax;
    }
 
 
}