Sun WBEM SDK Developer's Guide

Example — Enumerating Namespaces

The sample program in Example 4–12 uses the enumNameSpace method in the CIMClient class to print the names of the namespace and all the namespaces contained within the namespace.


Example 4–12 Enumerating Namespaces (enumNameSpace)

 
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 CIMClient interface to get a list of the 
  * namespaces within the namespace specified by the CIMObjectPath,
  * (cop) 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 (Exception e) {
				    System.out.println("Exception: "+e);
			   }
         // If the client connection is open, close it.
		     if(cc != null) {
            cc.close();
         }
     }
}