Sun WBEM SDK Developer's Guide

Deleting an Instance

Use the deleteInstance method to delete an instance.

Example — Deleting an Instance

The example in Example 4–7 connects the client application to the CIM Object Manager and uses the following interfaces to delete all instances of a class:


Example 4–7 Deleting Instances (deleteInstance)

 
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 program takes four required command-line arguments and 
 * deletes all instances of the specified class and its subclasses. The 
 * running this program must specify the username and password of an
 * account that has write permission to the specified namespace.
 * /
public class DeleteInstances {
    public static void main(String args[]) throws CIMException {
    
		// Initialize an instance of the CIM Client class
		CIMClient cc = null;
	
		// Requires 4 command-line arguments. If not all entered, prints command string.
	
		if(args.length != 4) {
	      System.out.println("Usage: DeleteClass host className username password"); 
	      System.exit(1);
		 }
		 try {
	
				 /**
				  * Creates a name space object (cns), which stores the host name
				  * (args[0]) from the command line.
				  */ 
				 CIMNameSpace cns = new CIMNameSpace(args[0]);
 
				 /** 
				  * Connects to the CIM Object Manager, and passes it the
				  * namespace object (cns) and the username (args[2]) and 
				  * password (args[3]) from the command line. 
				  */
 
				 cc = new CIMClient(cns, args[2], args[3]);
 
				 /**
				  * Construct an object containing the CIM object path
				  * of the class to delete (args[1]) from the command line.
				  */
 
				 CIMObjectPath cop = new CIMObjectPath(args[1]);
	    
				 /**
				  * Get an enumeration of the instance object paths of the 
				  * class and all subclasses of the class. An instance object 
				  * path is a reference used by the CIM Object Manager to locate
          * the instance.
			    */
	     
				 Enumeration e = cc.enumerateInstanceNames(cop);
	    
				 /**
				  * Iterate through the instance object paths in the enumeration.
				  * Construct an object to store the object path of each
				  * enumerated instance, print the instance, and then 
				  * delete it.
				  */
	 
				 while(e.hasMoreElements()) {
				     CIMObjectPath op = (CIMObjectPath)e.nextElement();
					   System.out.println(op);
					   cc.deleteInstance(op);
	    		 }
			} catch (Exception e) {
	    	    System.out.println("Exception: "+e);
			}
			if(cc != null) {
	   	    cc.close();
			}
   }
}