VMwareConnectionUtil.java
2.67 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
package com.sitech.ismp.coll.host;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.vmware.vim25.UserSession;
import com.vmware.vim25.mo.Folder;
import com.vmware.vim25.mo.ServerConnection;
import com.vmware.vim25.mo.ServiceInstance;
import com.vmware.vim25.mo.SessionManager;
/**
*
* <p>
* Title: VMwareConnectionUtil
* </p>
* <p>
* Description: VMwareConnectiong工具类
* </p>
* <p>
* Copyright: Copyright (c) 2013
* </p>
* <p>
* Company: SI-TECH
* </p>
*
* @author huojla
* @version 1.0
* @createtime 2013-7-25 上午9:10:27
*
*/
public class VMwareConnectionUtil {
private final static Logger LOG = LoggerFactory.getLogger(VMwareConnectionUtil.class);
// /**
// *
// * @Title: activeService
// * @Description: 获取一个vCenter连接
// * @param
// * @return ServiceInstance
// * @throws
// * @author huojla
// * @version 1.0
// * @createtime 2013-7-25 上午9:11:05
// */
// public static ServiceInstance activeService(String vcenterCode) {
// ServiceInstance result = null;
// try {
// KeyedConnectionPool<String, ServiceInstance> cp = KeyedConnectionPool.instance();
// for (int i = 0; i < KeyedConnectionPool.maxActive; i++) {
// result = cp.borrowObject(vcenterCode);
// if (isActive(result)) {
// break;
// } else {
// cp.invalidateObject(vcenterCode, result);
// result = null;
// }
// }
// } catch (Exception ex) {
// LOG.error("获取VMware连接异常,vCenter Code : " + vcenterCode + "!", ex);
// result = null;
// }
//
// return result;
// }
/**
*
* @Title: isActive
* @Description: 确认vCenter连接是否激活
* @param
* @return Boolean
* @throws
* @author huojla
* @version 1.0
* @createtime 2013-7-25 上午9:11:35
*/
public static Boolean isActive(ServiceInstance si) {
Boolean result = true;
try {
if (si == null) {
return false;
}
ServerConnection sc = si.getServerConnection();
if (sc == null) {
return false;
} else {
if (sc.getVimService() == null) {
return false;
}
if (sc.getServiceInstance() == null) {
return false;
}
}
SessionManager sm = si.getSessionManager();
if (sm == null) {
return false;
}
UserSession us = sm.getCurrentSession();
if (us == null) {
return false;
}
String sessionUserName = us.getUserName();
String sessionid = us.getKey();
boolean sessionIsActive = sm.sessionIsActive(sessionid, sessionUserName);
if (!sessionIsActive) {
return false;
}
Folder root = si.getRootFolder();
if (root == null) {
return false;
}
} catch (Exception ex) {
LOG.error("确认vCenter连接是否激活异常!", ex);
result = false;
}
return result;
}
}