Solaris WBEM Developer's Guide

Writing an Instance Provider

This following sample code implements the enumerateInstances and getInstance interfaces for the Ex_SimpleCIMInstanceProvider class. For brevity, this example implements the deleteInstance, createInstance, setInstance, and execQuery interfaces by throwing a CIMException.


Note –

For information on implementing the execQuery method, see Parsing Queries.



Example 6–1 CIMInstance Provider

/*
 * "@(#)SimpleCIMInstanceProvider.java"
 */
import javax.wbem.cim.*;
import javax.wbem.client.*;
import javax.wbem.provider.CIMProvider;
import javax.wbem.provider.CIMInstanceProvider;
import javax.wbem.provider.MethodProvider;
import java.util.*;
import java.io.*;

public class SimpleCIMInstanceProvider implements CIMInstanceProvider{
     static int loop = 0;
     public void initialize(CIMOMHandle cimom) throws CIMException {
     }
     public void cleanup() throws CIMException {
     }
     public CIMObjectPath[] enumerateInstanceNames(CIMObjectPath op, 
                                                   CIMClass cc) 
     throws CIMException {
                return null;
     }
     /*
      * enumerateInstances:
      * The entire instances and not just the names are returned.
      */
     public CIMInstance[] enumerateInstances(CIMObjectPath op,
                          boolean localOnly,boolean includeQualifiers,
                          boolean includeClassOrigin,String[] 
                          propertyList, CIMClass cc) throws CIMException
     {
     if (op.getObjectName().equalsIgnoreCase\
                            ("Ex_SimpleCIMInstanceProvider")) 
             {
             Vector instances = new Vector();
             CIMInstance ci = cc.newInstance();
                 if (loop == 0){
                     ci.setProperty("First", new CIMValue("red"));
                     ci.setProperty("Last", new CIMValue("apple"));
                     // only include the properties that were requested
       ci = ci.filterProperties(propertyList, includeQualifier,
       includeClassOrigin);
                     instances.addElement(ci);
                     loop += 1;
                 } else {
                     ci.setProperty("First", new CIMValue("red"));
                     ci.setProperty("Last", new CIMValue("apple"));
                     // only include the requested properties
         ci = ci.filterProperties(propertyList, includeQualifier,
         includeClassOrigin);
                     instances.addElement(ci);
                     ci = cc.newInstance();
                     ci.setProperty("First", new CIMValue("green"));
                     ci.setProperty("Last", new CIMValue("apple"));
                     // only include the requested properties
       ci = ci.filterProperties(propertyList, includeQualifier,
       includeClassOrigin);
                     instances.addElement(ci);
                 }
             return (CIMInstance[])instances.toArray();
             }
         throw new CIMException(CIM_ERR_INVALID_CLASS);
     }

     public CIMInstance getInstance(CIMObjectPath op, 
                                    boolean localOnly, 
                                    boolean includeQualifiers, 
                                    boolean includeClassOrigin,
                                    String[] propertyList, 
                                    CIMClass cc) 
         throws CIMException {
             if (op.getObjectName().equalsIgnoreCase
                             ("Ex_SimpleCIMInstanceProvider"))
             {
                 CIMInstance ci = cc.newInstance();
    // we need to get the keys from the passed in object path, this
    // will uniquely identify the instance we want to get
    java.util.Vector keys = cop.getKeys();
    // Since this is a contrived example we will simply place the keys
    // into the instance and be done.
                 ci.setProperties(keys);
    // if we had other non-key properties we should add them here.

                 // only include the properties that were requested
    ci = ci.filterProperties(propertyList, includeQualifiers,
    includeClassOrigin);
                 return ci;
             }
            throw new CIMException(CIM_ERR_INVALID_CLASS);
      }

     public CIMInstance[] execQuery(CIMObjectPath op, \
                            String query, String ql, CIMClass cc)
           throws CIMException {
           throw(new CIMException(CIMException.CIM_ERR_NOT_SUPPORTED));
    }

     public void setInstance(CIMObjectPath op, CIMInstance ci, boolean 
                             includeQualifiers, String[] propertyList)
           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));
     }
 }