Sun WBEM SDK Developer's Guide

Example — Typical Sun WBEM SDK Application

Example 4–1 is a simple application that connects to the CIM Object Manager, using all default values. The program gets a class and then enumerates and prints the instances in that class.


Example 4–1 Typical Sun WBEM SDK Application

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;
 
/**
 * Returns all instances of the specified class.
 * This method takes two arguments: hostname (args[0]) 
 * and name of class to list (args[1]). 
 */
public class WBEMsample {
    public static void main(String args[]) throws CIMException {
        CIMClient cc = null;
            try {
                /* args[0] contains the namespace. We create
                a CIM namespace (cns) pointing to the default
	              root\cimv2 namespace on the specified host. *	/
						   CIMNameSpace cns = new CIMNameSpace(args[0]);
						   /* Connect to the CIM Object manager and pass it 
							 the namespace object containing the namespace. */
						   cc = new CIMClient(cns, "root", "root_password");
						   /* Create a CIMObjectPath from the class name. */
						   CIMObjectPath cop = new CIMObjectPath(args[1]);
						   /* Get the class, including qualifiers, 
	              class origin, and properties. */
	              cc.getClass(cop, true, true, true, null);
                // Return all instances names belonging to the class.
                Enumeration e = cc.enumerateInstanceNames(cop);
                while(e.hasMoreElements()) {
                    CIMObjectPath op = (CIMObjectPath)e.nextElement();
                    System.out.println(op);
                  } // end while
	  	        } catch (Exception e) {
                System.out.println("Exception: "+e);
            }
            if(cc != null) {
               cc.close();
            } 
     } // end main
 } // end WBEMsample