<commit_before>package com.vmware.vim25.mo;

import java.rmi.RemoteException;

import com.vmware.vim25.*;
import com.vmware.vim25.mo.util.*;
import com.vmware.vim25.mo.util.PropertyCollectorUtil;

public class InventoryNavigator
{
	private ManagedEntity rootEntity = null;
	
	public InventoryNavigator(ManagedEntity rootEntity)
	{
		this.rootEntity = rootEntity;
	}

	/**
	 * Retrieve container contents from specified parent recursively if requested.
	 * @param recurse retrieve contents recursively from the root down
	 * @throws RemoteException 
	 * @throws RuntimeFault 
	 * @throws InvalidProperty 
	 */
	public ManagedEntity[] searchManagedEntities(boolean recurse) throws InvalidProperty, RuntimeFault, RemoteException  
	{
		String[][] typeinfo = new String[][] { new String[] { "ManagedEntity", }};
		return searchManagedEntities(typeinfo, recurse);
	}

	/**
    * Get the first ManagedObjectReference from current node for the specified type
    */
	public ManagedEntity[] searchManagedEntities(String type) throws InvalidProperty, RuntimeFault, RemoteException  
	{
		String[][] typeinfo = new String[][] { new String[] { type,  "name",}, };
		return searchManagedEntities(typeinfo, true);
	}

	/**
	* Retrieve content recursively with multiple properties.
	* the typeinfo array contains typename + properties to retrieve.
	*
	* @param typeinfo 2D array of properties for each typename
	* @param recurse retrieve contents recursively from the root down
	*
	* @return retrieved object contents
	 * @throws RemoteException 
	 * @throws RuntimeFault 
	 * @throws InvalidProperty 
	*/
	public ManagedEntity[] searchManagedEntities(String[][] typeinfo, boolean recurse ) throws InvalidProperty, RuntimeFault, RemoteException  
	{
		ObjectContent[] ocs = retrieveObjectContents(typeinfo, recurse);
		return createManagedEntities(ocs);
	}
	
	private ObjectContent[] retrieveObjectContents(String[][] typeinfo, boolean recurse ) throws InvalidProperty, RuntimeFault, RemoteException
	{
		if (typeinfo == null || typeinfo.length == 0) 
		{
			return null;
		}
	
		PropertyCollector pc = rootEntity.getServerConnection().getServiceInstance().getPropertyCollector();
	
		SelectionSpec[] selectionSpecs = null;
		if (recurse) 
		{
			selectionSpecs = PropertyCollectorUtil.buildFullTraversal();
		}
	
		PropertySpec[] propspecary = PropertyCollectorUtil.buildPropertySpecArray(typeinfo);
	
		ObjectSpec os = new ObjectSpec();
		os.setObj(rootEntity.getMOR());
		os.setSkip(Boolean.FALSE);
		os.setSelectSet(selectionSpecs);
		
		PropertyFilterSpec spec = new PropertyFilterSpec();
		spec.setObjectSet(new ObjectSpec[] { os });
		spec.setPropSet(propspecary);
		
		return pc.retrieveProperties(new PropertyFilterSpec[] { spec } );
	}
			
	private ManagedEntity[] createManagedEntities(ObjectContent[] ocs) 
	{<commit_msg>Automatically detect the version of the target server and apply different inventory traversal specs<commit_after>package com.vmware.vim25.mo;

import java.rmi.RemoteException;

import com.vmware.vim25.*;
import com.vmware.vim25.mo.util.*;
import com.vmware.vim25.mo.util.PropertyCollectorUtil;

public class InventoryNavigator
{
	private ManagedEntity rootEntity = null;
	private SelectionSpec[] selectionSpecs = null;

	public InventoryNavigator(ManagedEntity rootEntity)
	{
		this.rootEntity = rootEntity;
	}

	/**
	 * Retrieve container contents from specified parent recursively if requested.
	 * @param recurse retrieve contents recursively from the root down
	 * @throws RemoteException 
	 * @throws RuntimeFault 
	 * @throws InvalidProperty 
	 */
	public ManagedEntity[] searchManagedEntities(boolean recurse) throws InvalidProperty, RuntimeFault, RemoteException  
	{
		String[][] typeinfo = new String[][] { new String[] { "ManagedEntity", }};
		return searchManagedEntities(typeinfo, recurse);
	}

	/**
    * Get the first ManagedObjectReference from current node for the specified type
    */
	public ManagedEntity[] searchManagedEntities(String type) throws InvalidProperty, RuntimeFault, RemoteException  
	{
		String[][] typeinfo = new String[][] { new String[] { type,  "name",}, };
		return searchManagedEntities(typeinfo, true);
	}

	/**
	* Retrieve content recursively with multiple properties.
	* the typeinfo array contains typename + properties to retrieve.
	*
	* @param typeinfo 2D array of properties for each typename
	* @param recurse retrieve contents recursively from the root down
	*
	* @return retrieved object contents
	 * @throws RemoteException 
	 * @throws RuntimeFault 
	 * @throws InvalidProperty 
	*/
	public ManagedEntity[] searchManagedEntities(String[][] typeinfo, boolean recurse ) throws InvalidProperty, RuntimeFault, RemoteException  
	{
		ObjectContent[] ocs = retrieveObjectContents(typeinfo, recurse);
		return createManagedEntities(ocs);
	}
	
	private ObjectContent[] retrieveObjectContents(String[][] typeinfo, boolean recurse ) throws InvalidProperty, RuntimeFault, RemoteException
	{
		if (typeinfo == null || typeinfo.length == 0) 
		{
			return null;
		}
	
		PropertyCollector pc = rootEntity.getServerConnection().getServiceInstance().getPropertyCollector();
	
		if (recurse && selectionSpecs==null) 
		{
		  AboutInfo ai = rootEntity.getServerConnection().getServiceInstance().getAboutInfo();
		  
		  /* The apiVersion values in all the shipped products
		  "2.0.0"    VI 3.0
		  "2.5.0"    VI 3.5 (and u1)
		  "2.5u2"   VI 3.5u2 (and u3, u4)
		  "4.0"       vSphere 4.0 (and u1)
      ******************************************************/
			selectionSpecs = ai.apiVersion.startsWith("4")? 
			    PropertyCollectorUtil.buildFullTraversalV4() : 
			    PropertyCollectorUtil.buildFullTraversal();
		}
	
		PropertySpec[] propspecary = PropertyCollectorUtil.buildPropertySpecArray(typeinfo);
	
		ObjectSpec os = new ObjectSpec();
		os.setObj(rootEntity.getMOR());
		os.setSkip(Boolean.FALSE);
		os.setSelectSet(selectionSpecs);
		
		PropertyFilterSpec spec = new PropertyFilterSpec();
		spec.setObjectSet(new ObjectSpec[] { os });
		spec.setPropSet(propspecary);
		
		return pc.retrieveProperties(new PropertyFilterSpec[] { spec } );
	}
			
	private ManagedEntity[] createManagedEntities(ObjectContent[] ocs) 
	{
{'fraction_non_alphanumeric': 0.10699854533699693, 'fraction_numerical': 0.007434944237918215, 'mean_word_length': 4.815789473684211, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6533108', 'n_tokens_mistral': 1900, 'n_tokens_neox': 1726, 'n_words': 457}