次のコード例は、 Ex_SimpleCIMInstanceProvider クラスの enumerateInstances および getInstance インタフェースを実装します。わかりやすくするために、この例では、CIMException をスローすることによって deleteInstance、createInstance、setInstance、execQuery の各インタフェースを実装します。
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();
// passed in オブジェクトパスからキーを取得する必要がある
// 取得したいインスタンスを一意に識別する
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));
}
}