summaryrefslogtreecommitdiff
path: root/src/com/nis/nmsclient/common/SysConfig.java
blob: e22a35f12d7e038c9f17ad0072f1c7e3b80d87b5 (plain)
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package com.nis.nmsclient.common;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import java.util.ResourceBundle;

import javax.swing.JOptionPane;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;


public class SysConfig {
	static Logger logger = Logger.getLogger(SysConfig.class);
	
	/** ==============myconfig.properties文件获取参数=============== **/
	private static Properties myProperties;
	private static String url = null;
	static {
		URL urlObj = SysConfig.class.getClassLoader().getResource("myconfig.properties");
		if(urlObj==null){
//			JOptionPane.showMessageDialog(null, "缺少配置文件,程序无法执行!\n请先执行参数配置程序进行配置", "错误", JOptionPane.ERROR_MESSAGE);
			JOptionPane.showMessageDialog(null, "i18n_client.Sysconfig.init_n81i", "i18n_client.Sysconfig.error_n81i", JOptionPane.ERROR_MESSAGE);
			logger.error("NMSClient program termination: lack of configuration files, programs can not be executed! Please execute the configuration program for configuration first");
			System.exit(0);
		}else{
			url = urlObj.getPath().replaceAll("%20", " ");
		}
		myProperties = new Properties();
		
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(url);
			myProperties.load(fis);
		} catch (IOException e) {
			logger.error("Reading myconfig.properties file error", e);
		}finally{
			try {
				if(fis!= null)fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		// 升级时更新参数配置使用
		updateConfigFile();
	}
	
	 /**
	  * 向资源配置文件host_uuid.properties中更新UUID键值
	  */
	 public static void setInterfaceNameValue(String value) {
	  try {
	   Properties properties = new Properties();
	   if (uuidUrl != null && !"".equals(uuidUrl)) {
	    properties.load(new FileInputStream(uuidUrl));
	    // 添加或更新键值对
	    properties.setProperty(AGENT_INTERFACE_NAME_KEY, value);
	    // 保存到文件
	    properties.store(new FileOutputStream(uuidUrl), "");
	   }
	   properties.clear();
	   
	   if(value != null && !"".equals(value)){
	    Contants.AGENT_INTERFACE_NAME_KEY = value;
	   }
	  } catch (Exception e) {
	   logger.error("Setting the network port name eth* attribute error", e);
	  } 
	 }

	
	/**
	 * 根据相应的参数配置来更新配置并写入文件
	 */
	private static void updateConfigFile(){
		FileInputStream fis = null;
		BufferedReader reader = null;
		BufferedWriter writer = null;
		try {
			ResourceBundle resource = ResourceBundle.getBundle(UpdateParams.class.getName());
			//判断是否更新properties
			String updateFlag = myProperties.getProperty(UpdateParams.CONFIG_UPDATE_FLAG,"-1");
			if(updateFlag.equals(resource.getString(UpdateParams.CONFIG_UPDATE_FLAG))){	//配置文件已经更新,退出操作
				return;
			}
			
			List<String> proList = new LinkedList<String>();
			
			String encode = System.getProperty("file.encoding");
			logger.debug("----file.encoding----" + encode);
			
			//读取配置文件原有的参数到proList
			fis = new FileInputStream(url);
			reader = new BufferedReader(new InputStreamReader(fis,Charset.forName(encode)));
			String str =null;
			while((str = reader.readLine() )!=null){
				proList.add(str);
			}
			
			//将UpdateParams中的值更新到proList和myProperties中
			Enumeration<String> en = resource.getKeys();
			while (en.hasMoreElements()) {
				String elem = (String) en.nextElement();
				String value = resource.getString(elem);
				boolean addFlag = true;
				try {
					for (int i = 0; i < proList.size(); i++) {
						String strV = proList.get(i);
						if (StringUtils.isEmpty(strV)) {
							continue;
						}
						if(strV.split("=", 2)[0].trim().equals(elem)){
							if(elem.equalsIgnoreCase(UpdateParams.CONFIG_UPDATE_FLAG)){
								proList.set(i, elem + " = " + value);// 更新配置文件中某属性的值
								logger.info("参数更新:" + elem + " = " + (StringUtils.isBlank(value) ? "" : value));
								myProperties.put(elem, value); 
							}
							addFlag = false;
							break ;
						}
					}
					
					if(addFlag){
						proList.add(elem + " = " + value);
						logger.info("参数新增:" + elem + " = " + (StringUtils.isBlank(value) ? "" : value));
						myProperties.put(elem, value); 
					}
				} catch (Exception e) {
					logger.error("Update the configuration file myconfig.properties parameter " + elem + "error", e);
				}
			}
			
			//将文件信息写入到文件中
			writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(url),Charset.forName(encode)));
			Iterator<String> it = proList.iterator();
			while (it.hasNext()) {
				String elem = (String) it.next();
				writer.write((elem==null?"":elem)+"\r\n");
			}
			writer.flush();
			
		} catch (Exception e) {
			logger.error("Update configuration file myconfig.properties exception", e);
		} finally{
			try {
				if(reader!= null)reader.close();
				if(fis!= null)fis.close();
				if(writer!= null ){
					writer.close();
					writer = null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void updateConfigFile(String key, String value){
		FileInputStream fis = null;
		BufferedReader reader = null;
		BufferedWriter writer = null;
		try {
			List<String> proList = new LinkedList<String>();
			
			String encode = System.getProperty("file.encoding");
			logger.debug("----file.encoding----" + encode);
			
			//读取配置文件原有的参数到proList
			fis = new FileInputStream(url);
			reader = new BufferedReader(new InputStreamReader(fis,Charset.forName(encode)));
			String str =null;
			while((str = reader.readLine() )!=null){
				proList.add(str);
			}
			
			//将值更新到proList中
			try {
				for (int i = 0; i < proList.size(); i++) {
					String strV = proList.get(i);
					if (StringUtils.isEmpty(strV)) {
						continue;
					}
					if(strV.split("=", 2)[0].trim().equals(key)){
						proList.set(i, key + " = " + value);// 更新配置文件中某属性的值
						logger.info("参数更新:" + key + " = " + (StringUtils.isBlank(value) ? "" : value));
						break ;
					}
				}
			} catch (Exception e) {
				logger.error("Update the configuration file myconfig.properties parameter " + key + "error", e);
			}
			
			//将文件信息写入到文件中
			writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(url),Charset.forName(encode)));
			Iterator<String> it = proList.iterator();
			while (it.hasNext()) {
				String elem = (String) it.next();
				writer.write((elem==null?"":elem)+"\r\n");
			}
			writer.flush();
			
		} catch (Exception e) {
			logger.error("Update configuration file myconfig.properties exception", e);
		} finally{
			try {
				if(reader!= null)reader.close();
				if(fis!= null)fis.close();
				if(writer!= null ){
					writer.close();
					writer = null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static String getStringVal(String key, String... defaultVal){
		String dStr = "";
		if(defaultVal!=null && defaultVal.length>=1 && defaultVal[0]!=null && defaultVal.length>0){
			dStr = defaultVal[0];
		}
		return myProperties.getProperty(key,dStr).trim();
	}
	
	public static Integer getIntegerVal(String name, String... defaultVal){
		try {
			String val = getStringVal(name, defaultVal);
			return StringUtils.isEmpty(val)? null : Integer.parseInt(val);
		} catch (Exception e) {
			logger.error("Digital formatting error", e);
		}
		return null;
	}
	
	public static String getSystemDir() {
		return System.getProperty("user.dir");
	}

	public static String getLogPath(){
		FileInputStream fis = null;
		try {
			String log4jUrl = SysConfig.class.getClassLoader().getResource("log4j.properties").getPath().replaceAll("%20", " ");
			fis = new FileInputStream(log4jUrl);
			Properties log4jProperties = new Properties();
			log4jProperties.load(fis);
			String logFile = log4jProperties.getProperty("log4j.appender.logfile.File");
			if(logFile!=null){
				return new File(logFile).getParent();
			}
		} catch (IOException e) {
			logger.error("Reading log4j.properties file error"+"",e);
		}finally{
			try {
				if(fis!= null)fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return "../logs";
	}
	
	/** ==============host_uuid.properties文件获取参数=============== **/
	static final String AGENT_HOST_UUID_KEY = "agent_host_uuid";
	static final String AGENT_OPERATE_SYSTEM_KEY = "agent_operate_system";
	static final String AGENT_LOCAL_IP_KEY = "agent_local_ip";
	static String uuidUrl = null;
	static final String AGENT_INTERFACE_NAME_KEY = "agent_interface_name";
	static {
		String name = "host_uuid.properties";
		// 2014-11-13 jzz modify 由于将该配置文件放于系统安装目录中容易使节点混乱,故将其放于nmsdata指定目录
		/*URL urlObj = Contants.class.getClassLoader().getResource(name);
		if(urlObj==null){
			uuidUrl = new File(url).getParent() + File.separator + name;
		}else{
			uuidUrl = urlObj.getPath().replaceAll("%20", " ");
		}*/
		String path = SysConfig.getStringVal("local.data.path") + File.separator + "nc_sysconf";
		File filePath = new File(path);
		if(!filePath.exists()){
			filePath.mkdirs();
		}		
		
		uuidUrl = path + File.separator + name;
		
		Properties properties = new Properties();
		try {
			File file = new File(uuidUrl);
			if(!file.exists()){
				file.createNewFile();
			}
			properties.load(new FileInputStream(uuidUrl));
			String uuidStr = properties.getProperty(AGENT_HOST_UUID_KEY);
			if(uuidStr != null && !"".equals(uuidStr.trim())){
				Contants.AGENT_HOST_UUID = Long.parseLong(uuidStr.trim());
			}
			Contants.AGENT_OPERATE_SYSTEM = properties.getProperty(AGENT_OPERATE_SYSTEM_KEY);
			Contants.AGENT_LOCAL_IP = properties.getProperty(AGENT_LOCAL_IP_KEY);
			Contants.AGENT_INTERFACE_NAME_KEY = properties.getProperty(AGENT_INTERFACE_NAME_KEY);
		} catch (IOException e) {
			logger.error("Reading host_uuid.properties file error", e);
		}
		properties.clear();
	}
	
	/**
	 * 向资源配置文件host_uuid.properties中更新UUID键值
	 */
	public static void setUUIDValue(String value) {
		try {
			Properties properties = new Properties();
			if (uuidUrl != null && !"".equals(uuidUrl)) {
				properties.load(new FileInputStream(uuidUrl));
				// 添加或更新键值对
				properties.setProperty(AGENT_HOST_UUID_KEY, value);
				// 保存到文件
				properties.store(new FileOutputStream(uuidUrl), "");
			}
			properties.clear();
			
			if(value != null && !"".equals(value)){
				Contants.AGENT_HOST_UUID = Long.parseLong(value);
			}
		} catch (Exception e) {
			logger.error("Setting the UUID attribute error", e);
		} 
	}
	
	/**
	 * 向资源配置文件host_uuid.properties中更新OperateSystem和LocalIP键值
	 */
	public static void setUUIDValue(String sysTypeValue,String localIpValue) {
		try {
			Properties properties = new Properties();
			if (uuidUrl != null && !"".equals(uuidUrl)) {
				properties.load(new FileInputStream(uuidUrl));
				// 添加或更新键值对
				properties.setProperty(AGENT_OPERATE_SYSTEM_KEY, sysTypeValue);
				properties.setProperty(AGENT_LOCAL_IP_KEY, localIpValue);
				// 保存到文件
				properties.store(new FileOutputStream(uuidUrl), "");
			}
			properties.clear();
			Contants.AGENT_OPERATE_SYSTEM = sysTypeValue;
			Contants.AGENT_LOCAL_IP = localIpValue;
		} catch (Exception e) {
			logger.error("Setting OperateSystemType and LocalIP attribute errors",e);
		} 
	}
}