Sun WBEM SDK Developer's Guide

Example — Enumerating Class Names

A Java GUI application might use the code segment in Example 4–13 to display a list of classes and subclasses to a user.


Example 4–13 Enumerating Class Names (enumClass)

...
{
    /* 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.enumClass(cop, cc.DEEP);
}
... 


An application might use the code segment in Example 4–14 to display the contents of a class and its subclasses.


Example 4–14 Enumerating Class Data (enumClass)

...
{
    /* 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 (cc.DEEP). This enumeration
    returns only the non-inherited methods and properties 
    for each class and subclass (localOnly is true).*/
 
    Enumeration e = cc.enumClass(cop, cc.DEEP, true);  
}
...

The sample program in Example 4–15 does a deep and shallow enumeration of classes and instances. This example also shows the use of the localOnly flag to return class and instance data, instead of returning the names of the classes and instances.


Example 4–15 Enumerating Classes and Instances

import java.rmi.*;
import com.sun.wbem.client.CIMClient;
import com.sun.wbem.cim.CIMInstance;
import com.sun.wbem.cim.CIMValue;
import com.sun.wbem.cim.CIMProperty;
import com.sun.wbem.cim.CIMNameSpace;
import com.sun.wbem.cim.CIMObjectPath;
import com.sun.wbem.cim.CIMClass;
import com.sun.wbem.cim.CIMException;
import java.util.Enumeration;
 
/** 
 * This example enumerates classes and instances. It does 
 * a deep and shallow enumeration of a class that is passed 
 * from the command line. It uses the localOnly flag to return 
 * class and instance details. 
 */
 public class ClientEnum {
     public static void main(String args[]) throws CIMException {
		     CIMClient cc = null;
			   CIMObjectPath cop = null;
			   if(args.length != 2) {
				     System.out.println("Usage: ClientEnum host className"); 
				     System.exit(1);
	    		 }
			   try {
				    // Create a CIMNameSpace object that contains the 
				    // hostname (args[0] from the command line).
	    		    CIMNameSpace cns = new CIMNameSpace(args[0]);
 
				    // Creates a client connection to the CIM Object Manager
				    // on the specified host (args[0]).
	    		    cc = new CIMClient(cns);
 
	    		    // Get the class name from the command line
	    		    cop = new CIMObjectPath(args[1]);
 
	    		    // Do a deep enumeration of the class, which
				    // returns the class names.
	    		    Enumeration e = cc.enumClass(cop, cc.DEEP);
 
	    		    // Print the names of all subclasses of the enumerated class.
	    		    for (; e.hasMoreElements(); System.out.println(e.nextElement()));
	    		        System.out.println("+++++");
 
	    		    // Do a shallow enumeration of the class, which
				    // returns the class names.
	    		    e = cc.enumClass(cop, cc.SHALLOW);
 
	    		    // Prints the names of the first-level subclasses.
	    		    for (; e.hasMoreElements(); System.out.println(e.nextElement()));
	    		        System.out.println("+++++");
 
	    		    // Do a shallow enumeration of the class, which
				    // returns the class data, not just the class
				    // name (localOnly is true).
	    		    e = cc.enumClass(cop, cc.SHALLOW, true);
 
	    		    // Prints the details of the first-level subclasses.
	    		    for (; e.hasMoreElements(); System.out.println(e.nextElement()));
	    		        System.out.println("+++++");
 
				    // Do a deep enumeration of the instances of the class, which
				    // returns the names of the instances.
	    		    e = cc.enumInstances(cop, cc.DEEP);
 
	    		    // Prints the names of all instances of the class and its subclasses.
	    		    for (; e.hasMoreElements(); System.out.println(e.nextElement()));
	    		        System.out.println("+++++");
 
				    // Do a deep enumeration of the instances of the class, which
				    // returns the actual instance data, not just the instance
				    // name. (localOnly is true).
	    		    e = cc.enumInstances(cop, cc.DEEP);
 
	    		    // Prints the details of the instances of the class and its subclasses.
	    		    for (; e.hasMoreElements(); System.out.println(e.nextElement()));
	    		        System.out.println("+++++");
 
				    // Do a shallow enumeration of the instances of the class,
				    // which returns the names of the instances.
	    		    e = cc.enumInstances(cop, cc.SHALLOW);
 
				    // Prints the names of the instances of the class.
	    		    for (; e.hasMoreElements(); System.out.println(e.nextElement()));
	    			      System.out.println("+++++");
			      } catch (Exception e) {
	    		        System.out.println("Exception: "+e);
			     }
			    // close session.
			    if(cc != null) {
	    		    cc.close();
			    }
    }
}