Beispiel #1
0
  /**
   * Uses the new RetrievePropertiesEx method to emulate the now deprecated RetrieveProperties
   * method.
   *
   * @param listpfs
   * @return list of object content
   * @throws Exception
   */
  private static List<ObjectContent> retrievePropertiesAllObjects(List<PropertyFilterSpec> listpfs)
      throws Exception {

    RetrieveOptions propObjectRetrieveOpts = new RetrieveOptions();

    List<ObjectContent> listobjcontent = new ArrayList<ObjectContent>();

    try {
      RetrieveResult rslts =
          vimPort.retrievePropertiesEx(propCollectorRef, 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 = vimPort.continueRetrievePropertiesEx(propCollectorRef, token);
        token = null;
        if (rslts != null) {
          token = rslts.getToken();
          if (rslts.getObjects() != null && !rslts.getObjects().isEmpty()) {
            listobjcontent.addAll(rslts.getObjects());
          }
        }
      }
    } catch (SOAPFaultException sfe) {
      printSoapFaultException(sfe);
    } catch (Exception e) {
      System.out.println(" : Failed Getting Contents");
      e.printStackTrace();
    }

    return listobjcontent;
  }
  /**
   * Returns all the MOREFs of the specified type that are present under the container
   *
   * @param folder {@link ManagedObjectReference} of the container to begin the search from
   * @param morefType Type of the managed entity that needs to be searched
   * @return Map of name and MOREF of the managed objects present. If none exist then empty Map is
   *     returned
   * @throws InvalidPropertyFaultMsg
   * @throws RuntimeFaultFaultMsg
   */
  Map<String, ManagedObjectReference> getMOREFsInContainerByType(
      ManagedObjectReference folder, String morefType)
      throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg {
    String PROP_ME_NAME = "name";
    ManagedObjectReference viewManager = serviceContent.getViewManager();
    ManagedObjectReference containerView =
        vimPort.createContainerView(viewManager, folder, Arrays.asList(morefType), true);

    Map<String, ManagedObjectReference> tgtMoref = new HashMap<String, ManagedObjectReference>();

    // Create Property Spec
    PropertySpec propertySpec = new PropertySpec();
    propertySpec.setAll(Boolean.FALSE);
    propertySpec.setType(morefType);
    propertySpec.getPathSet().add(PROP_ME_NAME);

    TraversalSpec ts = new TraversalSpec();
    ts.setName("view");
    ts.setPath("view");
    ts.setSkip(false);
    ts.setType("ContainerView");

    // Now create Object Spec
    ObjectSpec objectSpec = new ObjectSpec();
    objectSpec.setObj(containerView);
    objectSpec.setSkip(Boolean.TRUE);
    objectSpec.getSelectSet().add(ts);

    // Create PropertyFilterSpec using the PropertySpec and ObjectPec
    // created above.
    PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec();
    propertyFilterSpec.getPropSet().add(propertySpec);
    propertyFilterSpec.getObjectSet().add(objectSpec);

    List<PropertyFilterSpec> propertyFilterSpecs = new ArrayList<PropertyFilterSpec>();
    propertyFilterSpecs.add(propertyFilterSpec);

    RetrieveResult rslts =
        vimPort.retrievePropertiesEx(
            serviceContent.getPropertyCollector(), propertyFilterSpecs, new RetrieveOptions());
    List<ObjectContent> listobjcontent = new ArrayList<ObjectContent>();
    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 = vimPort.continueRetrievePropertiesEx(serviceContent.getPropertyCollector(), token);
      token = null;
      if (rslts != null) {
        token = rslts.getToken();
        if (rslts.getObjects() != null && !rslts.getObjects().isEmpty()) {
          listobjcontent.addAll(rslts.getObjects());
        }
      }
    }
    for (ObjectContent oc : listobjcontent) {
      ManagedObjectReference mr = oc.getObj();
      String entityNm = null;
      List<DynamicProperty> dps = oc.getPropSet();
      if (dps != null) {
        for (DynamicProperty dp : dps) {
          entityNm = (String) dp.getVal();
        }
      }
      tgtMoref.put(entityNm, mr);
    }
    return tgtMoref;
  }