Sun WBEM SDK Developer's Guide

Getting and Setting Instances

An application frequently uses the getInstance method to retrieve CIM instances from the CIM Object Manager. When an instance of a class is created, it inherits the properties of the class it is derived from and all parent classes in its class hierarchy. The getInstance method takes the Boolean argument localOnly. If localOnly is true, getInstance returns only the non-inherited properties in the specified instance. The non-inherited properties are those defined in the instance itself. If localOnly is false, all properties in the class are returned – those defined in the instance and all properties inherited from all parent classes in its class hierarchy.

To create a new instance, use the CIMInstance method in the CIMClass class to create the instance on the local system. Then use the CIMClient.setInstance method to update an existing instance in a namespace or use the CIMClient.createInstance method to add a new instance to a namespace.

Example — Getting Instances

The code segment in Example 4–8 lists all processes on a given system. This example uses the enumerateInstanceNames 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 4–8 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, "root", "root_password"); 
 
/* Pass the CIM Object Path of the CIM_Process class 
to the CIM Object Manager. We want to get instances of 
this class. */ 
 
CIMObjectPath cop = new CIMObjectPath("CIM_Process"); 
 
/* The CIM Object Manager returns an enumeration of 
object paths, the names of instances of 
the CIM_Process class. */
Enumeration e = cc.enumerateInstanceNames(cop); 
 
/* Iterate through the enumeration of instance object paths.
Use the CIM Client getInstance class to get 
the instances referred to by each object name. */

while(e.hasMoreElements()) {
    CIMObjectPath op = (CIMObjectPath)e.nextElement();
    // Get the instance. Returns only the properties
		// that are local to the instance (localOnly is true).
		CIMInstance ci = cc.getInstance(op, true);
		}
...


Example — Getting a Property

Example 4–9 prints the value of the lockspeed property for all Solaris processes. This code segment uses the following methods:


Example 4–9 Printing Processor Information (getProperty)

...
{
/* Create an object (CIMObjectPath) to store the name of the
Solaris_Processor class. */ 
 
CIMObjectPath cop = new CIMObjectPath("Solaris_Processor"); 
 
/* The CIM Object Manager returns an enumeration containing the names 
of instances of the Solaris_Processor class and
all its subclasses (cc.DEEP). */
 
Enumeration e = cc.enumInstances(cop, cc.DEEP); 
 
/* Iterate through the enumeration of instance object paths.
Use the getProperty method to get the lockspeed
value for each Solaris processor. */
 
while(e.hasMoreElements()) {
			CIMValue cv = cc.getProperty(e.nextElement(), "lockspeed");
    	System.out.println(cv);
}
...
}
 


Example — Setting a Property

The code segment in Example — Setting a Property sets a hypothetical lockspeed value for all Solaris processors. This code segment uses the following methods:


Example 4–10 Setting Processor Information (setProperty)

...
{
    /* Create an object (CIMObjectPath) to store the name of the
    Solaris_Processor class. */ 
 
    CIMObjectPath cop = new CIMObjectPath("Solaris_Processor"); 
 
    /* The CIM Object Manager returns an enumeration containing the names 
    of instances of the Solaris_Processor class and
    all its subclasses. */
 
    Enumeration e = cc.enumerateInstanceNames(cop); 
 
    /* Iterate through the enumeration of instance object paths.
    Use the setProperty method to set the lockspeed
    value to 500 for each Solaris processor. */
 
    for (; e.hasMoreElements(); cc.setProperty(e.nextElement(), "lockspeed", 
        new CIMValue(new Integer(500))));
 
...
}
 


Example — Setting Instances

The code segment in Example 4–11 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 4–11 Setting Instances (setInstance)

...
{
    // Create an object path, an object that contains the
    // CIM name for "myclass"
    CIMObjectPath cop = new CIMObjectPath("myclass"); 
    /* Get instances for each instance object path in an enumeration, 
    update the property value of b to 10 in each instance, 
    and pass the updated instance to the CIM Object Manager. */
    
    while(e.hasMoreElements()) {
		    CIMInstance ci = cc.getInstance(CIMObjectPath)(e.nextElement(), 
                true, true, true, null);
        ci.setProperty("b", new CIMValue(new Integer(10)));
				cc.setInstance(new CIMObjectPath(),ci);
		}
}
...