WBEMfor Solaris on Sun Developer's Guide

Working with Instances

This section describes how to create a CIM instance, delete a CIM instance, and update an instance (get and set the property values of one or more instances).

Creating an Instance

Use the newInstance method to create an instance of an existing class. If the existing class has a key property, an application must set it to a value that is guaranteed to be unique. As an option, an instance can define additional qualifiers that are not defined for the class. These qualifiers can be defined for the instance or for a particular property of the instance and do not need to appear in the class declaration.

Applications can use the getQualifiers method to get the set of qualifiers defined for a class.

Example -- Creating an Instance

The code segment in Example 6-4 uses the newInstance method to create a Java class representing a CIM instance (for example, a Solaris package) from the Solaris_Package class.


Example 6-4 Creating an Instance (newInstance())

 
/*Connect to the CIM Object Manager in the root\cimv2 namespace 
on the local host. */
CIMClient cc = new CIMClient();
 
// Get the Solaris_Package class
cimclass = cc.getClass(newCIMObjectPath("Solaris_Package");
 
/* Create a new instance of the Solaris_Package class,
 populated with the default values for properties. If the provider
 for the class does not specify default values, the values of the 
 properties will be null and must be explicitly set. */
 ci = cimclass.newInstance();
 


Deleting an Instance

Use the deleteInstance method to delete an instance.

Example -- Deleting an Instance

The example in Example 6-5 connects the client application to the CIM Object Manager and uses the following interfaces to delete all instances of a class:


Example 6-5 Deleting an Instance (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 java.util.Enumeration;
 
public class DeleteInstances {
		public static void main(String args[]) throws CIMException {
			CIMClient cc = null;
			try {
					/* Construct a namespace object containing the 
					   command line arguments. */
					CIMNameSpace cns = new CIMNameSpace(args[0]);
 
					/* Pass the namespace object to the CIM Object Manager.*/
 				  CIMClient cc = new CIMClient(cns);
 
					/* Construct an object containing the CIM object 
					   path of the object we want to delete. */
 				  CIMObjectPath cop = new CIMObjectPath(args[1]);
 
					/* Do a deep enumeration (deep is set to 
            CIMClient.DEEP) of the instances of the object. A deep 
            enumeration of the instances of a class returns the 
            class instances and all instances of its subclasses. */
					Enumeration e = cc.enumInstances(cop, CIMClient.DEEP);
	    
					// Print the name of each object and delete the instance.
					while(e.hasMoreElements()) {
						CIMObjectPath op = (CIMObjectPath)e.nextElement();
						System.out.println(op);
						cc.deleteInstance(op);
	   		 	}
				}
			catch (Exception e) {
	    			System.out.println("Exception: "+e);
			}
 
  		// If the client connection is open, close it.
			if(cc != null) {
          	cc.close();
    	}
		}
}
 


Getting and Setting Instances

An application frequently uses the getInstance method to retrieve CIM instances from the CIM Object Manager.

Example -- Getting Instances

The code segment in Example 6-6 lists all processes on a given system. This example uses the enumInstances method to get the names of instances of the CIM_Process class. Running this code on a Microsoft Windows 32 system returns Windows 32 processes. Running this same code on a Solaris system returns Solaris processes.


Example 6-6 Getting Instances of a Class (getInstance)

{
//Create namespace cns
CIMnameSpace cns = new CIMNameSpace; 
 
//Connect to the cns namespace on the CIM Object Manager
cc = new CIMClient(cns); 
 
/* Pass the CIM Object Path of the CIM_Process class 
to the CIM Object Manager. We want to get instances of this class. */ 
 
CIMObjectPath op = new CIMObjectPath("CIM_Process"); 
 
/* The CIM Object Manager returns a vector of object paths, the names 
of instances of the CIM_Process class. */
Vector v = cc.enumInstances(op, true); 
 
/* Iterate through the vector of instance object paths.
Use the CIM Client getInstance interface to get 
the instances referred to by each object name. */
 
for (int i=0; i < v.size(); i++) {
 
		// Get the instance
		CIMInstance ci = cc.getInstance(v.elementAt(i));
 
		/* Get the process ID string for each instance of 
		CIM_Process. */
		CIMProperty cp = ci.getProperty("Handle");
}


Example -- Getting a Property

Example 6-7 prints the value of the lockspeed property for all Solaris processes. This code segment uses the following methods:


Example 6-7 Printing Processor Information (getProperty)

/* Connect to the CIM Object Manager as user mary with password 
contrary in the /root namespace on myhost */
{
CIMNameSpace cns = new CIMNamesSpace ("myhost" "/root");
cc = new CIMClient (cns, "/root", "mary", "contrary");
 
// Get names of all instances of Solaris_Processor
Vector op cc.enumInstances("Solaris_Processor")
 
// For each Solaris processor, get its instance data
while (vector has more elements) {
        cn.getNextElement();
        cc.getInstance (cn);
 
// Print the lockspeed of each processor
p = ci.getProperty("lockspeed")
        System.out.println(p.getValue().getValue());
}


Example -- Setting Instances

The code segment in Example 6-8 gets a CIM instance, updates one of its property values, and passes the updated instances to the CIM Object Manager.

A CIM property is a value used to describe a characteristic of a CIM class. Properties can be thought of as a pair of functions, one to set the property value and one to get the property value.


Example 6-8 Setting Instances (setInstance)

{
/* Get instances for each element in a vector, update the 
property value of b to 10 in each instance, 
and pass the updated instance to the CIM Object Manager. */
 
For (int i=0; i(v.size(); i++) {
		CIMInstance ci = cc.getInstance(v.elementAt(i));
		ci.setProperty("b",new CIMValue(10)); 
		cc.setInstance(new CIMObjectPath(),ci); 
 
}