参考VMWare的Webservice API的文档,连接到ESX|ESXi主机。
版本: ESX|ESXi 4.0
JAVA代码如下:
- import ?java.net.URL;??
- ??
- import ?com.vmware.vim25.ManagedObjectReference;??
- import ?com.vmware.vim25.ServiceContent;??
- import ?com.vmware.vim25.VimPortType;??
- import ?com.vmware.vim25.VimServiceLocator;??
- ??
- /** ?
- ?*??获取到VMWare?ESX?or?ESXi的连接 ?
- ?*?ESX?|?ESXi版本:4.0 ?
- ?*?@author?forandever ?
- ?*?@date?2009-12-29 ?
- ?*? ?
- ?*/ ??
- public ? class ?ServerConn?{??
- ??
- ????private ?ManagedObjectReference?_svcRef;??
- ????private ?VimServiceLocator?_locator;??
- ????private ?VimPortType?_service;??
- ????private ?ServiceContent?_sic;??
- ??????
- ????private ? void ?createServiceRef()? throws ?Exception?{??
- ????????_svcRef?=?new ?ManagedObjectReference();??
- ????????_svcRef.setType("ServiceInstance" );??
- ????????_svcRef.set_value("ServiceInstance" );??
- ????}??
- ??????
- ????public ? void ?prepare(String?hostName,?String?userName,?String?password)? throws ?Exception?{??
- ????????System.setProperty("axis.socketSecureFactory" ,? "org.apache.axis.components.net.SunFakeTrustSocketFactory" );??
- ????????String?url?=?"https://" ?+?hostName?+? "/sdk/vimService" ;??
- ????????createServiceRef();??
- ????????_locator?=?new ?VimServiceLocator();??
- ????????_locator.setMaintainSession(true );??
- ????????_service?=?_locator.getVimPort(new ?URL(url));??
- ????????_sic?=?_service.retrieveServiceContent(_svcRef);??
- ????????if ?(_sic.getSessionManager()?!=? null )?{??
- ????????????_service.login(_sic.getSessionManager(),?userName,?password,?null );??
- ????????}??
- ????????System.out.println(_sic.getAbout().getFullName());??
- ????????System.out.println("API?type?is?" ?+?_sic.getAbout().getApiType());??
- ????????System.out.println("API?version?is?" ?+?_sic.getAbout().getApiVersion());??
- ????}??
- ??????
- ??????
- ????public ?ManagedObjectReference?get_svcRef()?{??
- ????????return ?_svcRef;??
- ????}??
- ??
- ????public ? void ?set_svcRef(ManagedObjectReference?ref)?{??
- ????????_svcRef?=?ref;??
- ????}??
- ??
- ????public ?VimServiceLocator?get_locator()?{??
- ????????return ?_locator;??
- ????}??
- ??
- ????public ? void ?set_locator(VimServiceLocator?_locator)?{??
- ????????this ._locator?=?_locator;??
- ????}??
- ??
- ????public ?VimPortType?get_service()?{??
- ????????return ?_service;??
- ????}??
- ??
- ????public ? void ?set_service(VimPortType?_service)?{??
- ????????this ._service?=?_service;??
- ????}??
- ??
- ????public ?ServiceContent?get_sic()?{??
- ????????return ?_sic;??
- ????}??
- ??
- ????public ? void ?set_sic(ServiceContent?_sic)?{??
- ????????this ._sic?=?_sic;??
- ????}??
- ??
- ????/** ?
- ?????*?@param?args ?
- ?????*/ ??
- ????public ? static ? void ?main(String[]?args)?{??
- ????????//?TODO?Auto-generated?method?stub ??
- ??
- ????}??
- ??
-
}?