VMwareConnectFactory.java
7.31 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
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
package com.sitech.ismp.coll.host;
import com.sitech.base.AgentProperties;
import com.vmware.vim25.mo.ServerConnection;
import com.vmware.vim25.mo.ServiceInstance;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
//import com.sitech.monitor.exception.Exception;
//import com.sitech.util.VMwareConnectionProperty;
/**
*
* <p>
* Title: VMwareConnectFactory
* </p>
* <p>
* Description: VMware连接工厂
* </p>
* <p>
* Copyright: Copyright (c) 2013
* </p>
* <p>
* Company: SI-TECH
* </p>
*
* @author huojla
* @version 1.0
* @createtime 2013-7-12 上午10:25:28
*
*/
public class VMwareConnectFactory {
private static final Log LOG = LogFactory.getLog(VMwareConnectFactory.class);
private static VMwareConnectFactory _instance = null;
public synchronized static VMwareConnectFactory getInstance() {
if (_instance != null) {
} else {
_instance = new VMwareConnectFactory();
}
return _instance;
}
private static Map<String, ServiceInstance> serviceInstanceMap = new HashMap<String, ServiceInstance>();
private VMwareConnectionProperty vmwareConnectionConfig;// Vmware连接配置
private VMwareConnectionProperty vmwareInControlConfig;// Vmware可管控连接配置
public VMwareConnectFactory(VMwareConnectionProperty vmwareConnectionConfig,
VMwareConnectionProperty vmwareInControlConfig) {
try {
this.vmwareConnectionConfig = vmwareConnectionConfig;
this.vmwareInControlConfig = vmwareInControlConfig;
} catch (Exception e) {
LOG.error("实例VMware错误--VMwareConnectFactory : " + e.getMessage(), e);
}
}
public VMwareConnectFactory() {
VMwareConnectionProperty _vmwareConnectionConfig = new VMwareConnectionProperty("vcenter-config.xml");
VMwareConnectionProperty _vmwareInControlConfig = new VMwareConnectionProperty("vcenter-in-control.xml");
this.vmwareConnectionConfig = _vmwareConnectionConfig;
this.vmwareInControlConfig = _vmwareInControlConfig;
}
/**
*
* @Title: initVMwareVCenterConnection
* @Description: 实例化VMware-vCenter连接,暂未使用
* @param
* @return void
* @throws
* @author huojla
* @version 1.0
* @throws Exception
* @createtime 2013-7-12 上午10:33:03
*/
@Deprecated
public void initVMwareVCenterConnection(VMwareConnectionProperty vmwareConnectionConfig,
VMwareConnectionProperty vmwareInControlConfig) throws Exception {
Map<String, String> connectionMap = vmwareConnectionConfig.getMap();
Map<String, String> inControlMap = vmwareInControlConfig.getMap();
if (connectionMap != null) {
Set<String> inControlKeySet = inControlMap.keySet();
for (String inControlKey : inControlKeySet) {
ServiceInstance si = initOneServceInstance(vmwareConnectionConfig, vmwareInControlConfig, inControlKey);
serviceInstanceMap.put(inControlKey, si);
}
} else {
String error = "获取VMware连接配置文件错误!";
LOG.error(error);
throw new Exception(error);
}
}
/**
*
* @Title: initOneServceInstance
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param
* @return ServiceInstance
* @throws
* @author huojla
* @version 1.0
* @createtime 2013-7-22 下午2:56:46
*/
private synchronized ServiceInstance initOneServceInstance(
VMwareConnectionProperty vmwareConnectionConfig,
VMwareConnectionProperty vmwareInControlConfig, String inControlKey)
throws Exception {
Map<String, String> connectionMap = vmwareConnectionConfig.getMap();
Map<String, String> inControlMap = vmwareInControlConfig.getMap();
ServiceInstance si = null;
if (!inControlMap.containsKey(inControlKey)) {
throw new Exception("vCenter Id " + inControlKey + "不在管控范围内!");
}
String vcenterUrl = connectionMap.get(inControlKey + ".URL");
String vcenterUser = connectionMap.get(inControlKey + ".USER");
String vcenterPassword = connectionMap.get(inControlKey + ".PASSWORD");
try {
si = new ServiceInstance(new URL(vcenterUrl), vcenterUser, vcenterPassword, true);
} catch (RemoteException e) {
String error = "vCenter连接错误:" + vcenterUser + ":" + vcenterUrl;
LOG.error(error, e);
throw new Exception(error, e);
} catch (MalformedURLException e) {
String error = "vCenterURL解析错误:" + vcenterUser + ":" + vcenterUrl;
LOG.error(error, e);
throw new Exception(error, e);
}
return si;
}
/**
*
* @Title: getServiceInstanceByKey
* @Description: update by 20130821 : 调整获取连接为调用时才获取连接
// * @param vCenterkey: 来自inControl文件config/vmware/vcenter-in-control.xml
* @return ServiceInstance
* @throws
* @author huojla
* @version 1.0
* @throws Exception
* @createtime 2013-7-22 上午9:21:51
*/
public synchronized ServiceInstance getServiceInstanceByKey(String key)
throws Exception {
// if (this.serviceInstanceMap != null) {
ServiceInstance si = this.serviceInstanceMap.get(key);
if (si == null) {
si = initOneServceInstance(vmwareConnectionConfig, vmwareInControlConfig, key);
// 将连接对象放入Map
this.serviceInstanceMap.put(key, si);
} else {
// 判断当前连接是否有效
boolean isActive = VMwareConnectionUtil.isActive(si);
if (!isActive) {
si = initOneServceInstance(vmwareConnectionConfig, vmwareInControlConfig, key);
// 将连接对象放入Map
this.serviceInstanceMap.put(key, si);
}
}
return si;
// } else {
// throw new Exception("VMware连接实例数据配置文件或实例错误!请联系管理员!");
// }
}
/**
*
* @Title: closeAllConnection
* @Description: 关闭所有连接
* @param
* @return void
* @throws
* @author huojla
* @version 1.0
* @createtime 2013-8-10 下午3:47:53
*/
public void closeAllConnection() {
Set<String> keySet = serviceInstanceMap.keySet();
Iterator<String> iterator = keySet.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
closeConnectionByKey(key);
}
}
/**
*
* @Title: closeConnectionByKey
* @Description: 根据Key关闭连接
* @param
* @return void
* @throws
* @author huojla
* @version 1.0
* @createtime 2013-8-10 下午3:50:35
*/
public synchronized void closeConnectionByKey(String key) {
ServiceInstance si = serviceInstanceMap.get(key);
if (si != null) {
ServerConnection sc = si.getServerConnection();
if (sc != null) {
sc.logout();
}
si = null;
serviceInstanceMap.remove(key);
}
}
/**
*
* @Title: dealConnectionException
* @Description: 处理异常情况
* @param
* @return void
* @throws
* @author huojla
* @version 1.0
* @createtime 2013-9-3 下午12:29:54
*/
public synchronized void dealConnectionException(String key) {
closeConnectionByKey(key);
ServiceInstance si = null;
try {
si = getServiceInstanceByKey(key);
} catch (Exception e) {
LOG.error("dealConnectionException 重新获取连接失败!重新获取一次!" + e.getMessage() + e, e);
try {
si = getServiceInstanceByKey(key);
} catch (Exception e1) {
LOG.error("dealConnectionException 重新获取连接失败!" + e.getMessage() + e, e);
}
}
serviceInstanceMap.put(key, si);
}
}