Sun WBEM SDK Developer's Guide

The Instance Provider Interface (InstanceProvider)

The following table describes the methods in the instance provider interface in the Provider package (com.sun.wbem.provider20).

These methods each take the op argument, the CIMObjectPath of the specified CIM class or CIM instance. The object path includes the namespace, class name, and keys (if the object is an instance). The namespace is a directory that can contain other namespaces, classes, instances, and qualifier types. A key is a property that uniquely identifies an instance of a class. Key properties have a KEY qualifier.

For example, the following object path has two parts:

\\myserver\root\cimv2\Solaris_ComputerSystem:Name=mycomputer:   CreationClassName=Solaris_ComputerSystem

Table 5–2 InstanceProvider Interface Methods

Method 

Description 

CIMObjectPath createInstance(CIMObjectPath op, CIMInstance ci)

Creates the instance cispecified by op, if it does not exist. If the CIM instance already exists, the provider should throw CIMInstanceException with ID CIM_ERR_ALREADY_EXISTS.Returns the CIMObjectPath of the created instance.

void deleteInstance(CIMObjectPath op)

Deletes the instance specified in the object path (op).

Vector enumInstances(CIMObjectPath path, boolean deep, CIMClass cc)

Returns the names of the instances for the class specified in path. If deepis true, returns the names of all instances of the specified class and all classes derived from the class. Otherwise, returns only the names of instances belonging to the specified class.

Providers that do not want to create instances from scratch can create a template for the new instance by calling the newInstance() method for the class to which the instance belongs (cc).

Vector enumInstances(CIMObjectPath path, boolean deep, CIMClass cc, boolean localOnly)

Returns the instances (the entire instance not just the name of the instance) for the class specified in path.

Providers that do not want to create instances from scratch can create a template for the new instance by calling the newInstance() method for the class to which the instance belongs (cc).

If localOnly is true, returns the local (non-inherited) properties in the enumerated instances. Otherwise, returns all inherited and local properties.

Vector execQuery (CIMObjectPath op, String query, int ql, CIMClass cc

Executes a query to retrieve CIM objects. This method returns a vector of CIM instances of the specified CIM class (cc)that match the specified query string.

CIMInstance getInstance(CIMObjectPath op, CIMClass cc, boolean localOnly)

Returns the instance specified in the object path (op).

Providers that do not want to create instances from scratch can create a template for the new instance by calling the newInstance() method for the class to which the instance belongs (cc).

If localOnly is true, only the local (non-inherited) properties are returned. Otherwise, returns all inherited and local properties.

void setInstance(CIMInstance ci)

Updates the specified CIM instance if it exists. If the instance does not exist, throws a CIMInstanceException with ID CIM_ERR_NOT_FOUND.

Example — Implementing an Instance Provider

The following example shows the Java source code for an instance provider, SimpleInstanceProvider, that implements the enumInstances and getInstance interfaces for the Ex_SimpleInstanceProvider class. For brevity, this example implements the deleteInstance, createInstance, setInstance, and execQuery interfaces by throwing a CIMException. In practice, an instance provider must implement all InstanceProvider interfaces.


Example 5–1 SimpleInstanceProvider Instance Provider

/*
 * "@(#)SimpleInstanceProvider.java"
 */
import com.sun.wbem.cim.*;
import com.sun.wbem.client.*;
import com.sun.wbem.provider.CIMProvider;
import com.sun.wbem.provider20.InstanceProvider;
import com.sun.wbem.provider.MethodProvider;
import java.util.*;
import java.io.*;

public class SimpleInstanceProvider implements InstanceProvider{
     static int loop = 0;
     public void initialize(CIMOMHandle cimom) throws CIMException {
     }
     public void cleanup() throws CIMException {
     }
     public Vector enumInstances(CIMObjectPath op, boolean deep, CIMClass cc,
            boolean localOnly) throws CIMException {
                return null;
     }
     /*
      * enumInstances:
      * The entire instances and not just the names are returned.
      * Deep or shallow enumeration is possible, however
      * currently the CIMOM only asks for shallow enumeration.
      */
     public Vector enumInstances(CIMObjectPath op, boolean deep, CIMClass cc)
             throws CIMException {
         if (op.getObjectName().equalsIgnoreCase("Ex_SimpleInstanceProvider")) 
             {
             Vector instances = new Vector();
             CIMObjectPath cop = new CIMObjectPath(op.getObjectName(),
                     op.getNameSpace());
                 if (loop == 0){
                     cop.addKey("First", new CIMValue("red"));
                     cop.addKey("Last", new CIMValue("apple"));
                     // To delete this class, comment this following 
	                    // line and compile it.
                     instances.addElement(cop);
                     loop += 1;
                 } else {
                     cop.addKey("First", new CIMValue("red"));
                     cop.addKey("Last", new CIMValue("apple"));
                     // To delete this class, comment this following 
                     // line and compile it.
                     instances.addElement(cop);
                     cop = new CIMObjectPath(op.getObjectName(),
                             op.getNameSpace());
                     cop.addKey("First", new CIMValue("green"));
                     cop.addKey("Last", new CIMValue("apple"));
                     // To delete this class, comment this following 
                     // line and compile it.
                     instances.addElement(cop);
                 }
             return instances;
             }
         return new Vector();
     }

     public CIMInstance getInstance(CIMObjectPath op, 
             CIMClass cc, boolean localOnly) throws CIMException {
             if (op.getObjectName().equalsIgnoreCase("Ex_SimpleInstanceProvider"))
             {
                 CIMInstance ci = cc.newInstance();
                 ci.setProperty("First", new CIMValue("yellow"));
                 ci.setProperty("Last", new CIMValue("apple"));
                 return ci;
             }
            return new CIMInstance();
      }

     public Vector execQuery(CIMObjectPath op, String query, int ql, CIMClass cc)
             throws CIMException {
                 throw(new CIMException(CIMException.CIM_ERR_NOT_SUPPORTED));
    }

     public void setInstance(CIMObjectPath op, CIMInstance ci)
             throws CIMException {
                 throw(new CIMException(CIMException.CIM_ERR_NOT_SUPPORTED));
    }

     public CIMObjectPath createInstance(CIMObjectPath op, CIMInstance ci)
              throws CIMException {
                 throw(new CIMException(CIMException.CIM_ERR_NOT_SUPPORTED));
    }

     public void deleteInstance(CIMObjectPath cp) throws CIMException {
         throw(new CIMException(CIMException.CIM_ERR_NOT_SUPPORTED));
     }
 }