Solaris WBEM 開発ガイド

インスタンスプロバイダの作成

次のコード例は、 Ex_SimpleCIMInstanceProvider クラスの enumerateInstances および getInstance インタフェースを実装します。わかりやすくするために、この例では、CIMException をスローすることによって deleteInstancecreateInstancesetInstanceexecQuery の各インタフェースを実装します。


注 –

execQuery メソッドの実装についての詳細は、照会の構文解析を参照してください。



例 6–1 CIMInstance プロバイダ

/*
 * "@(#)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:
      * 名前だけではなくインスタンス全体を返す
      */
     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"));
             // 要求されたプロパティのみを含める
             ci = ci.filterProperties(propertyList, includeQualifier,
                                                 includeClassOrigin);
             instances.addElement(ci);
             loop += 1;
         } else {
             ci.setProperty("First", new CIMValue("red"));
             ci.setProperty("Last", new CIMValue("apple"));
             // 要求されたプロパティのみを含める
             ci = ci.filterProperties(propertyList, includeQualifier,
                                                 includeClassOrigin);
             instances.addElement(ci);
             ci = cc.newInstance();
             ci.setProperty("First", new CIMValue("green"));
             ci.setProperty("Last", new CIMValue("apple"));
             // 要求されたプロパティのみを含める
             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();
                 // 取得したいインスタンスを一意に識別するため
                 // オブジェクトパス内の渡された項目からキーを取得することが
                 // 必要である
                 java.util.Vector keys = cop.getKeys();
                 // これは例なので、キーをインスタンス内に配置するだけで
                 // 終了する
                 ci.setProperties(keys);
                 // キープロパティ以外のプロパティがある場合はここに追加する

                 // 要求されたプロパティのみを含める
                 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));
     }
 }