大发彩票平台出租搭建
1148916888.com
带手机版。源码开源。
根据实体对象的属性获取相关对象时,在虚拟机中,获取的仍然可能是一个托管对象引用,如根据虚拟机的属性runtime获取到的VirtualMachineRuntimeInfo对象,要获取该对象中的主机名,通过getHost()方法获取的是host的一个ManagedObjectReference,这样就需要进一步根据托管对象获取其对应的实体名称。
代码如下:
/**
- @Title: getObjectName
- @Description: 根据托管对象引用获取对象名称
- @param mor
- @return
- @throws Exception
- @version 1.0br/>*/
@Override
public String getObjectNameByMor(ManagedObjectReference mor) throws Exception {
String objectName = null;
// 遍历属性规范
PropertySpec propSpec = new PropertySpec();
propSpec.setAll(new Boolean(false));
propSpec.getPathSet().add("name");
propSpec.setType(mor.getType());ObjectSpec objSpec = new ObjectSpec();
objSpec.setObj(mor);
objSpec.setSkip(new Boolean(false));// 属性过滤规范
PropertyFilterSpec spec = new PropertyFilterSpec();
spec.getPropSet().add(propSpec);
spec.getObjectSet().add(objSpec);ArrayList<PropertyFilterSpec> listpfs = new ArrayList<PropertyFilterSpec>();
listpfs.add(spec);List<ObjectContent> listobjcont = retrievePropertiesAllObjects(listpfs);
if (listobjcont != null) {
ObjectContent oc = listobjcont.get(0);
objectName = (String) oc.getPropSet().get(0).getVal();
}
return objectName;
}
根据属性检索要查询的对象信息(该方法参考文章【1】):
/**
- @Title: retrievePropertiesAllObjects
- @Description: 根据属性检索要查询的对象信息
- @param listpfs
- @return
- @throws Exception
- @version 1.0
*/
private List<ObjectContent> retrievePropertiesAllObjects(List<PropertyFilterSpec> listpfs) throws Exception {
RetrieveOptions propObjectRetrieveOpts = new RetrieveOptions();
List<ObjectContent> listobjcontent = new ArrayList<>();
VimPortType vimPortType = this.vmClientSession.getVimPortType();
ServiceContent serviceContent = this.vmClientSession.getServiceContent();
try {
// 检索属性
RetrieveResult rslts =
vimPortType.retrievePropertiesEx(serviceContent.getPropertyCollector(), listpfs, propObjectRetrieveOpts);
if (rslts != null && rslts.getObjects() != null && !rslts.getObjects().isEmpty()) {
listobjcontent.addAll(rslts.getObjects());
}
String token = null;
if (rslts != null && rslts.getToken() != null) {
token = rslts.getToken();
}
while (token != null && !token.isEmpty()) {
rslts = vimPortType.continueRetrievePropertiesEx(serviceContent.getPropertyCollector(), token);
token = null;
if (rslts != null) {
token = rslts.getToken();
if (rslts.getObjects() != null && !rslts.getObjects().isEmpty()) {
listobjcontent.addAll(rslts.getObjects());
}
}
}
return listobjcontent;
} catch (Throwable e) {
e.printStackTrace();
}
}
原文地址:http://blog.51cto.com/13840452/2133351