Example: Simple PropertyCollector Example (Java) is a simple
PropertyCollector example written in Java. The example uses a
ContainerView for efficient access to the inventory and a
PropertyFilterSpec that contains one
ObjectSpec, one
TraversalSpec, and one
PropertySpec. The program performs the following tasks:
The following procedure uses code fragments from Example: Simple PropertyCollector Example (Java). The complete example includes server connection code; this procedure only describes the task of using the
PropertyCollector. For a description of server connection, see
To build a simple vSphere client application.
In the example, sContent is the variable for the
ServiceContent data object.
sContent provides the methods to retrieve the managed object references to the vSphere services.
methods is the variable for the
VimPortType object.
VimPortType defines the Java methods that correspond to the vSphere API methods. The
createContainerView parameters
container (the inventory root folder, returned by the method
sContent.getRootFolder) and
type (“Virtual Machine”) direct the
ViewManager to select virtual machines, starting at the root folder. The value
true for the
recursive parameter extends the selection beyond the root folder so that the
ViewManager will follow child folder paths to add virtual machines to the view. The container view provides references to all virtual machines in the inventory.
The ObjectSpec.obj property identifies the starting object (the container view). This example collects only virtual machine data, so the
skip property is set to
true to ignore the container view itself during collection.
The TraversalSpec properties
type and
path determine path traversal.
TraversalSpec.type identifies an object type.
TraversalSpec.path identifies a property in the
type object. The
PropertyCollector uses the
path object to select additional objects.
This example uses a single TraversalSpec to walk the list of virtual machines that are available through the container view. The following code fragment specifies the
ContainerView object for the
TraversalSpec.type property and the
view property in the
ContainerView for the
TraversalSpec.path property. The
skip property is set to
false, so the
PropertyCollector will collect data from the
path objects (the virtual machines in the container view).
The example program creates a PropertySpec data object to specify the properties to be collected. The
type property is set to
VirtualMachine to match the object selections in the container view. The
pathSet property identifies one or more properties in the
type object.
To invoke a single property collection operation, call the RetrievePropertiesEx method. The example application passes the populated
PropertyFilterSpec and an empty
options structure to the method. The default for the
RetrieveOptions.maxObjects specifies that no maximum for the number of objects that can be returned is set. The
PropertyCollector can impose a maximum. If the number of collected objects is greater than the maximum, the
PropertyCollector returns a
token value in the
RetrieveResult data object and this token is used to retrieve the remaining properties using the
ContinueRetrievePropertiesEx API method. For more information, see
Server Data Transmission.
The following code fragment walks the list of ObjectContent objects returned in the
RetrieveResult object. For each object (
ObjectContent), the inner loop prints the name-value pairs.