VMwareConnectionUtil.java 2.67 KB
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;
	}
}