Solaris WBEM Developer's Guide

Deleting an Instance

Use the deleteInstance method to delete an instance.


Example 4–6 Deleting Instances

The example does the following:

 
import java.rmi.*;
import java.util.Enumeration;

import javax.wbem.cim.CIMClass;
import javax.wbem.cim.CIMException;
import javax.wbem.cim.CIMInstance;
import javax.wbem.cim.CIMNameSpace;
import javax.wbem.cim.CIMObjectPath;

import javax.wbem.client.CIMClient;
import javax.wbem.client.PasswordCredential;
import javax.wbem.client.UserPrincipal;

/** 
 * Returns all instances of the specified class.
 * This example takes five arguments: hostname (args[0]), username
 * (args[1]), password (args[2]) namespace (args[3] and classname (args[4]) 
 * It will delete all instances of the specified classname.  The specified 
 * username must have write permissions to the specified namespace  
 */
public class DeleteInstances {
    public static void main(String args[]) throws CIMException {
        CIMClient cc = null;
        // if not five arguments, show usage and exit
        if (args.length != 5) {
           System.out.println("Usage: DeleteInstances host username " +
                              "password namespace classname ");
           System.exit(1);
           }
       try {
          // args[0] contains the hostname and args[3] contains the 
          // namespace.  We create a CIMNameSpace (cns) pointing to 
          // the specified namespace on the specified host
          CIMNameSpace cns = new CIMNameSpace(args[0], args[3]);

         // args[1] and args[2] contain the username and password.
         // We create a UserPrincipal (up) using the username and
         // a PasswordCredential using the password.
         UserPrincipal up = new UserPrincipal(args[1]);
         PasswordCredential pc = new PasswordCredential(args[2]);

        // Connect to the CIM Object Manager and pass it the
        // CIMNameSpace, UserPrincipal and PasswordCredential objects
        // we created. 
        cc = new CIMClient(cns, up, pc);

        // Get the class name (args[4]) and create a CIMObjectPath
        CIMObjectPath cop = new CIMObjectPath(args[4]);

        // Get an enumeration of all 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);
        } // end while 
  } catch (Exception e) {
       // if we have an exception, catch it and print it out.
       System.out.println("Exception: "+e);
  } // end catch

 // close session.
 if (cc != null) {
     cc.close();
       }
    }
}