Enumerating objects means getting a list of the names of the objects. Once you get a list of object names, you can get the instances of that object, its properties, or other information about the object. The Sun WBEM SDK provides APIs for enumerating namespaces, classes, and instances.
The enumeration APIs take two Boolean arguments, deep and shallow. The behavior of these parameters depends upon the particular method being used. A deep enumeration of instances of a class returns the class instances and all instances of its subclasses. A deep enumeration of a class returns all subclasses of the class, but does not return the class itself. A shallow enumeration of the instances of a class returns the instances of that class. A shallow enumeration of a class returns the direct subclasses of that class.
The following examples show how to use the enumeration APIs to enumerate a namespace and a class.
The sample program in Example 6-9 uses the enumNameSpace method in the CIM Client class to print the names of the namespace and all the namespaces contained within the namespace.
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 java.util.Enumeration; / ** * This program takes a namespace argument and calls the * enumNameSpace CIM Client interface to get a list of the * namespaces within the namespace specified by the CIMObjectPath. * and all the namespaces contained in the namespace * (CIMClient.DEEP). The program then prints the name of the specified * namespace (CIMClient.SHALLOW). /** public class EnumNameSpace { // EnumNameSpace takes a string of arguments public static void main (String args[ ]) { CIMClient cc = null; try { // Create a namespace object for the namespace passed as an argument CIMNameSpace cns = new CIMNameSpace(args[0], ""); // Connect to the CIM Object Manager in the namespace passed as an argument CIMClient cc = new CIMClient(cns); // Create an object path to store the namespace name on the current host CIMObjectPath cop = new CIMObjectPath("",args[1]); // Enumerate the namespace and all namespaces it contains // (deep is set to CIMClient.DEEP) Enumeration e = cc.enumNameSpace(cop, CIMClient.DEEP); // Iterate through the list of namespaces and print each name. for (; e.hasMoreElements(); System.out.println(e.nextElement())); System.out.println("++++++"); // Iterate through the list of namespaces (CIMClient.SHALLOW) and // print each name. e = cc.enumNamesSpace(cop, CIMClient.SHALLOW); for (; e.hasMoreElements(); System.out.println(e.nextElement())); } // Catch and print any exception returned catch (Exception e) { System.out.println("Exception: "+e); } // If the client connection is open, close it. if(cc != null) { cc.close(); } } } |
A Java GUI application might use the code segment in Example 6-10 to display a list of classes and subclasses to a user. Once the user selects a particular class, the code enumerates the class.
/* Creates an object containing the path of a CIM object. */ CIMObjectPath (op = new(CIMObjectPath()); /* Specifies the object path name as A. */ cop.setName("A"); /* Vector returns the object path of the object, classes, and all subclasses within those classes. The object path includes the namespace, class name, and keys (if the object is an instance). */ /* This vector contains the CIM Object Paths to the enumerated classes. */ Vector v = cc.enumClass(cop, true); |