次のコード例は、Ex_SimpleCIMInstanceProvider クラスの enumerateInstances および getInstance メソッドを実装します。分かりやすくするために、この例での deleteInstance、createInstance、setInstance、および execQuery メソッドの実装は CIMException だけをスローします。
execQuery メソッドの実装についての詳細は、「照会の構文解析」を参照してください。
/* * "@(#)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)); } }