Skip to content

Commit

Permalink
Merge pull request #36 from michaelrice/issue-35
Browse files Browse the repository at this point in the history
Issue 35
  • Loading branch information
michaelrice committed Mar 8, 2015
2 parents d0be029 + bc43395 commit 1c9f591
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 160 deletions.
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ apply plugin: 'signing'

group = "com.toastcoders"
archivesBaseName = "yavijava"
version = '5.5.09'
version = '5.5.10-DEVELOPMENT'
targetCompatibility = 1.6
sourceCompatibility = 1.6

repositories {
mavenCentral()
Expand Down
310 changes: 151 additions & 159 deletions src/main/java/com/vmware/vim25/mo/InventoryNavigator.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,165 +5,157 @@
import com.vmware.vim25.*;
import com.vmware.vim25.mo.util.*;
import com.vmware.vim25.mo.util.PropertyCollectorUtil;
import org.apache.log4j.Logger;

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)
"4.1" vSphere 4.1
"5.0" vSphere 5.0
******************************************************/
if(ai.apiVersion.startsWith("4") || ai.apiVersion.startsWith("5"))
{
selectionSpecs = PropertyCollectorUtil.buildFullTraversalV4();
}
else
{
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)
{
if(ocs==null)
{
return new ManagedEntity[] {};
}
ManagedEntity[] mes = new ManagedEntity[ocs.length];

for(int i=0; i<mes.length; i++)
{
ManagedObjectReference mor = ocs[i].getObj();
mes[i] = MorUtil.createExactManagedEntity(rootEntity.getServerConnection(), mor);
}
return mes;
}

/**
* Get the ManagedObjectReference for an item under the
* specified parent node that has the type and name specified.
*
* @param type type of the managed object
* @param name name to match
* @return First ManagedEntity object of the type / name pair found
* @throws RemoteException
* @throws RuntimeFault
* @throws InvalidProperty
*/
public ManagedEntity searchManagedEntity(String type, String name) throws InvalidProperty, RuntimeFault, RemoteException
{
if (name == null || name.length() == 0)
{
return null;
}

if(type==null)
{
type = "ManagedEntity";
}

String[][] typeinfo = new String[][] { new String[] { type, "name",}, };

ObjectContent[] ocs = retrieveObjectContents(typeinfo, true);

if (ocs==null || ocs.length == 0)
{
return null;
}

for (int i = 0; i < ocs.length; i++)
{
DynamicProperty[] propSet = ocs[i].getPropSet();

if (propSet.length > 0)
{
String nameInPropSet = (String) propSet[0].getVal();
if(name.equalsIgnoreCase(nameInPropSet))
{
ManagedObjectReference mor = ocs[i].getObj();
return MorUtil.createExactManagedEntity(rootEntity.getServerConnection(), mor);
}
}
}
return null;
}
public class InventoryNavigator {
private ManagedEntity rootEntity = null;
private SelectionSpec[] selectionSpecs = null;

private static Logger log = Logger.getLogger(InventoryNavigator.class);

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)
// "4.1" vSphere 4.1
// "5.0" vSphere 5.0
String[] versionArray = ai.apiVersion.split("\\.");
int majorVersion;
try {
majorVersion = Integer.parseInt(versionArray[0]);
}
catch (NumberFormatException ignore) {
majorVersion = 0;
}
if (majorVersion >= 4) {
log.debug("API version >= 4 detected. Using buildFullTraversalV4.");
selectionSpecs = PropertyCollectorUtil.buildFullTraversalV4();
}
else {
log.debug("API version < 4 detected. Using buildFullTraversal");
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) {
if (ocs == null) {
return new ManagedEntity[]{};
}
ManagedEntity[] mes = new ManagedEntity[ocs.length];

for (int i = 0; i < mes.length; i++) {
ManagedObjectReference mor = ocs[i].getObj();
mes[i] = MorUtil.createExactManagedEntity(rootEntity.getServerConnection(), mor);
}
return mes;
}

/**
* Get the ManagedObjectReference for an item under the
* specified parent node that has the type and name specified.
*
* @param type type of the managed object
* @param name name to match
* @return First ManagedEntity object of the type / name pair found
* @throws RemoteException
* @throws RuntimeFault
* @throws InvalidProperty
*/
public ManagedEntity searchManagedEntity(String type, String name) throws InvalidProperty, RuntimeFault, RemoteException {
if (name == null || name.length() == 0) {
return null;
}

if (type == null) {
type = "ManagedEntity";
}

String[][] typeinfo = new String[][]{new String[]{type, "name",},};

ObjectContent[] ocs = retrieveObjectContents(typeinfo, true);

if (ocs == null || ocs.length == 0) {
return null;
}

for (int i = 0; i < ocs.length; i++) {
DynamicProperty[] propSet = ocs[i].getPropSet();

if (propSet.length > 0) {
String nameInPropSet = (String) propSet[0].getVal();
if (name.equalsIgnoreCase(nameInPropSet)) {
ManagedObjectReference mor = ocs[i].getObj();
return MorUtil.createExactManagedEntity(rootEntity.getServerConnection(), mor);
}
}
}
return null;
}

}

0 comments on commit 1c9f591

Please sign in to comment.