An application frequently uses the getInstance method to retrieve CIM instances from the CIM Object Manager.
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.
{
//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 6-7 prints the value of the lockspeed property for all Solaris processes. This code segment uses the following methods:
enumInstances - to get the names of all instances of Solaris processor
getInstance - to get the instance data for each instance name
getProperty - to get the value of the lockspeed for each instance
println - to print the lockspeed value
/* 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());
}
|
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.
{
/* 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);
}
|