VMwareConnectionProperty.java
4.81 KB
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
package com.sitech.ismp.coll.host;
import java.io.*;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.apache.log4j.Logger;
/**
*
* <p>
* Title: VMwareConnectionProperty
* </p>
* <p>
* Description: VMware连接-xml文件加载工具类
* </p>
* <p>
* Copyright: Copyright (c) 2013
* </p>
* <p>
* Company: SI-TECH
* </p>
*
* @author huojla
* @version 1.0
* @createtime 2013-7-8 上午11:01:01
*
*/
public class VMwareConnectionProperty {
// private final static Logger logger = LoggerFactory.getLogger(VMwareConnectionProperty.class.getName());
private Logger logger = Logger.getLogger(CollEsxiWithCMD.class);
private Map<String, String> map = new HashMap<String, String>();
private Properties prop = new Properties();
private String resourceFile;
public VMwareConnectionProperty(String resourceFile) {
this.resourceFile = resourceFile;
try {
// System.out.println("Thread.currentThread().getContextClassLoader().getResource(\"\").toString() is: " + Thread.currentThread().getContextClassLoader().getResource("").toString());
// logger.info("Thread.currentThread().getContextClassLoader().getResource(\"\").toString() is: " + Thread.currentThread().getContextClassLoader().getResource("").toString());
prop.loadFromXML(Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceFile));
// prop.loadFromXML(new FileInputStream(new File(resourceFile)));
Enumeration<?> enumeration = prop.propertyNames();
while (enumeration.hasMoreElements()) {
String key = (String) enumeration.nextElement();
map.put(key, (String) prop.get(key));
}
} catch (FileNotFoundException e) {
logger.error("FileNotFoundException:" + e.getMessage());
} catch (IOException e) {
logger.error("IOException:" + e.getMessage());
}
}
/**
*
* @Title: getString
* @Description: 通过key值获取配置文件中对应的值
* @param
* @return String
* @throws
* @author huojla
* @version 1.0
* @createtime 2013-7-7 下午3:28:02
*/
public String getString(String key) {
return getString(key, null);
}
/**
*
* @Title: getString
* @Description: 通过key值获取配置文件中对应的值,若不存在,则给予默认值
* @param
* @return String
* @throws
* @author huojla
* @version 1.0
* @createtime 2013-7-7 下午3:27:50
*/
public String getString(String key, String defaultValue) {
String value = (String) map.get(key);
if (value != null) {
return value;
} else {
return defaultValue;
}
}
/**
*
* @Title: getInt
* @Description: 获取整数值
* @param
* @return int
* @throws
* @author huojla
* @version 1.0
* @createtime 2013-7-7 下午3:27:20
*/
public int getInt(String key) {
return getInt(key, 0);
}
/**
*
* @Title: getInt
* @Description: 获取整数值,若无,则赋予默认值
* @param
* @return int
* @throws
* @author huojla
* @version 1.0
* @createtime 2013-7-7 下午3:27:08
*/
public int getInt(String key, int defaultValue) {
String value = (String) map.get(key);
if (value != null) {
return Integer.parseInt(value);
} else {
return defaultValue;
}
}
/**
*
* @Title: getInt
* @Description: 获取整数值
* @param
* @return int
* @throws
* @author huojla
* @version 1.0
* @createtime 2013-7-7 下午3:27:20
*/
public Boolean getBoolean(String key) {
return getBoolean(key, false);
}
/**
*
* @Title: getBoolean
* @Description: 获取布尔值,若无,则赋予默认值
* @param
* @return int
* @throws
* @author huojla
* @version 1.0
* @createtime 2013-7-7 下午3:27:08
*/
public Boolean getBoolean(String key, Boolean defaultValue) {
String value = (String) map.get(key);
if (value != null) {
return Boolean.parseBoolean(value);
} else {
return defaultValue;
}
}
/**
*
* @Title: setPropValue
* @Description: 添加配置文件属性值
* @param
* @return void
* @throws
* @author huojla
* @version 1.0
* @createtime 2013-7-7 下午3:26:21
*/
public void setPropValue(String key, String value) {
prop.setProperty(key, value);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(resourceFile);
prop.store(fos, "Monitor Agent Configration");
fos.close();
} catch (FileNotFoundException e) {
logger.error(e.getMessage());
} catch (IOException ie) {
logger.error(ie.getMessage());
}
map.put(key, value);
}
/**
* @return the map
*/
public Map<String, String> getMap() {
return map;
}
/**
* @return the resourceFile
*/
public String getResourceFile() {
return resourceFile;
}
public static void main(String[] args) {
VMwareConnectionProperty vm = new VMwareConnectionProperty("vcenter-config.xml");
System.out.println(vm.getMap());
}
}