Solaris WBEM Developer's Guide

Enumerating Objects

An enumeration is a collection of objects that can be retrieved one object at a time. You can enumerate classes, class names, instances, instance names, and name spaces. The results of an enumeration depend on the method and the arguments used, as shown in the following table.

Enumerating Objects

Table 4–1 Enumerating Objects

Method 

No args 

deep

localOnly

enumerateClasses

Returns the contents of the class specified in path.

If true: Returns the contents of the subclasses of the specified class, but does not return the class.

If true: Returns only noninherited properties and methods of the specified class.

   

If false: Returns the contents of the direct subclasses of the specified class.

If false: Returns all properties of the specified class.

enumerateInstances

Returns the instances of the class specified in path.

If true: Returns the instances of the specified class and its subclasseses.

If true: Returns only noninherited properties of the instances of the specified class.

   

If false: Returns the instances of the specified class and its subclasses. The properties of the subclasses are filtered out.

If false: Returns all properties of the instances of the specified class.

enumerateClassNames

Returns the names of the class specified in path.

If true: Returns the names of all classes derived from the specified class.

N/A 

   

If false: Returns only the names of the first-level children of the specified class.

N/A 

enumerateInstanceNames

Returns the names of the instances of the class specified in path.

N/A 

N/A 

       

enumNameSpace

Returns a list of the name spaces within the name space specified in path.

If true: Returns the entire hierarchy of name spaces under the specified name space.

N/A 

   

If false: Returns only the first level children of the specified name space.

N/A 


Example 4–10 Enumerating Classes

The following example program returns the contents of a class and its subclasses.

...
{
    /* Creates a CIMObjectPath object and initializes it 
    with the name of the CIM class to be enumerated (myclass). */
     
    CIMObjectPath cop = new CIMObjectPath(myclass); 
     
    /* This enumeration contains the classes and subclasses
    in the enumerated class (deep=true). This enumeration
    returns only the noninherited methods and properties 
    for each class and subclass (localOnly is true).*/
 
    Enumeration e = cc.enumerateClasses(cop, true, true);  
}
...


Example 4–11 Enumerating Classes and Instances

The following example program performs a deep and shallow (deep=false) enumeration of classes and instances. The localOnly flag returns the contents of the classes and instances instead of the names of the classes and instances.

import java.rmi.*;
import java.util.Enumeration;

import javax.wbem.client.CIMClient;
import javax.wbem.cim.CIMClass;
import javax.wbem.cim.CIMException;
import javax.wbem.cim.CIMInstance;
import javax.wbem.cim.CIMNameSpace;
import javax.wbem.cim.CIMObjectPath;

import javax.wbem.client.UserPrincipal;
import javax.wbem.client.PasswordCredential;


/** 
 * This example enumerates classes and instances. It does deep and 
 * shallow enumerations on a class that is passed from the command line
 */
public class ClientEnum {

    public static void main(String args[]) throws CIMException {
        CIMClient cc = null;
        CIMObjectPath cop = null;
        if (args.length < 4) {
            System.out.println("Usage: ClientEnum host user passwd " + 
                               "classname");
            System.exit(1);
        }
       try {
           CIMNameSpace cns = new CIMNameSpace(args[0]);
           UserPrincipal up = new UserPrincipal(args[1]);
           PasswordCredential pc = new PasswordCredential(args[2]);
           cc = new CIMClient(cns, up, pc);

           // Get the class name from the command line    
           cop = new CIMObjectPath(args[3]);    
           // Do a deep enumeration of the class
           Enumeration e = cc.enumerateClasses(cop, true, true, true, 
                                               true);
           // Will print out all the subclasses of the class.
           while (e.hasMoreElements()) {
               System.out.println(e.nextElement());
           }
           System.out.println("+++++");
           // Do a shallow enumeration of the class
           e = cc.enumerateClasses(cop, false, true, true, true);
           // Will print out the first-level subclasses.
           while (e.hasMoreElements()) {
               System.out.println(e.nextElement());
           }
           System.out.println("+++++");
           // Do a deep enumeration of the instances of the class
           e = cc.enumerateInstances(cop, false, true, true, true, null);
           // Will print out all the instances of the class and its 
           // subclasses.
           while (e.hasMoreElements()) {
               System.out.println(e.nextElement());
           }
           System.out.println("+++++");
           // Do a shallow enumeration of the instances of the class
           e = cc.enumerateInstances(cop, false, false, true, true, null);
           // Will print out all the instances of the class.
           while (e.hasMoreElements()) {
               System.out.println(e.nextElement());
           }
           System.out.println("+++++");
           e = cc.enumerateInstanceNames(cop);
           while (e.hasMoreElements()) {
               System.out.println(e.nextElement());
            }
           System.out.println("+++++");
           e = cc.enumerateInstanceNames(cop);
           while (e.hasMoreElements()) {
               CIMObjectPath opInstance = (CIMObjectPath)e.nextElement();
               CIMInstance ci = cc.getInstance(opInstance, false, 
                                                true, true, null);
               System.out.println(ci); 
           }
           System.out.println("+++++");
       }
       catch (Exception e) {
           System.out.println("Exception: "+e);
       }
       // close session.
       if (cc != null) {
           cc.close();
       }
    }
}


Example 4–12 Enumerating Class Names

The following example program returns a list of class names and subclass names.

...
{
    /* Creates a CIMObjectPath object and initializes it 
    with the name of the CIM class to be enumerated (myclass). */
    CIMObjectPath cop = new CIMObjectPath(myclass); 
    
    /* This enumeration contains the names of the classes and subclasses
    in the enumerated class. */
    Enumeration e = cc.enumerateClassNames(cop, true);
}
... 


Example 4–13 Enumerating Name Spaces

This example program uses the enumNameSpace method in the CIMClient class to print the name of the name space and all the name spaces contained within the name space.

 
import java.rmi.*;
import java.util.Enumeration;

import javax.wbem.cim.CIMClass;
import javax.wbem.cim.CIMException;
import javax.wbem.cim.CIMInstance;
import javax.wbem.cim.CIMNameSpace;
import javax.wbem.cim.CIMObjectPath;

import javax.wbem.client.CIMClient;
import javax.wbem.client.PasswordCredential;
import javax.wbem.client.UserPrincipal;


/** 
 *
 */
public class EnumNameSpace {
    public static void main(String args[]) throws CIMException {
        CIMClient cc = null;
        // if not four arguments, show usage and exit
        if (args.length < 4) {
            System.out.println("Usage: EnumNameSpace host username " +
                               "password namespace");
            System.exit(1);
        }
        try {
            // args[0] contains the hostname. We create a CIMNameSpace 
            // (cns) pointing to the specified name space on the 
            // specified host
            CIMNameSpace cns = new CIMNameSpace(args[0], "");

            // args[1] and args[2] contain the username and password.
            // We create a UserPrincipal (up) using the username and
            // a PasswordCredential using the password.
            UserPrincipal up = new UserPrincipal(args[1]);
            PasswordCredential pc = new PasswordCredential(args[2]);

            // Connect to the CIM Object Manager and pass it the
            // CIMNameSpace, UserPrincipal and PasswordCredential objects
            // we created. 
            cc = new CIMClient(cns, up, pc);

            // Use the name space (args[3]) to create a CIMObjectPath
            CIMObjectPath cop = new CIMObjectPath("", args[3]);

            // Enumerate the name space
            Enumeration e = cc.enumNameSpace(cop);
            while (e.hasMoreElements()) {
                System.out.println((CIMObjectPath)e.nextElement());
            } // end while 

        } catch (Exception e) {
              // is we have an exception, catch it and print it out.
              System.out.println("Exception: "+ e);
        } // end catch

        // close session.
        if (cc != null) {
            cc.close();
        }
    }
}