アプリケーションでデータをログファイルに書き込むには、次の作業を行います。
Solaris_LogRecord クラスの新しいインスタンスを作成する
ログファイルに書き込まれるプロパティを設定し、プロパティの修飾子の値を設定する
出力する新しいインスタンスとプロパティを設定する
必要なすべての java クラスをインポートします。以下に示すクラスは必要な最小限のクラスです。
import java.rmi.*; import com.sun.wbem.client.CIMClient; import com.sun.wbem.cim.CIMInstance; import com.sun.wbem.cim.CIMValue; import com.sun.wbem.cim.CIMProperty; import com.sun.wbem.cim.CIMNameSpace; import com.sun.wbem.cim.CIMObjectPath; import com.sun.wbem.cim.CIMClass; import com.sun.wbem.cim.CIMException; import com.sun.wbem.solarisprovider.*; import java.util.*; |
CIMClient インスタンス
CIMObjectPath インスタンス
CIMNameSpace インスタンス
public class CreateLog {
public static void main(String args[]) throws CIMException {
if ( args.length != 3) {
System.out.println("Usage: CreateLog host username password");
System.exit(1);
}
CIMClient cc = null;
CIMObjectPath cop = null;
try {
CIMNameSpace cns = new CIMNameSpace(args[0]);
cc = new CIMClient(cns, args[1], args[2]);
|
プロパティのベクトルが返されるように指定します。修飾子のプロパティに値を設定します。
Vector keys = new Vector();
CIMProperty logsvcKey;
logsvcKey = new CIMProperty("category");
logsvcKey.setValue(new CIMValue(new Integer(2)));
keys.addElement(logsvcKey);
logsvcKey = new CIMProperty("severity");
logsvcKey.setValue(new CIMValue(new Integer(2)));
keys.addElement(logsvcKey);
logsvcKey = new CIMProperty("AppName");
logsvcKey.setValue(new CIMValue("SomeApp"));
keys.addElement(logsvcKey);
logsvcKey = new CIMProperty("UserName");
logsvcKey.setValue(new CIMValue("molly"));
keys.addElement(logsvcKey);
logsvcKey = new CIMProperty("ClientMachineName");
logsvcKey.setValue(new CIMValue("dragonfly"));
keys.addElement(logsvcKey);
logsvcKey = new CIMProperty("ServerMachineName");
logsvcKey.setValue(new CIMValue("spider"));
keys.addElement(logsvcKey);
logsvcKey = new CIMProperty("SummaryMessage");
logsvcKey.setValue(new CIMValue("brief_description"));
keys.addElement(logsvcKey);
logsvcKey = new CIMProperty("DetailedMessage");
logsvcKey.setValue(new CIMValue("detailed_description"));
keys.addElement(logsvcKey);
logsvcKey = new CIMProperty("data");
logsvcKey.setValue(new CIMValue("0xfe 0x45 0xae 0xda"));
keys.addElement(logsvcKey);
logsvcKey = new CIMProperty("SyslogFlag");
logsvcKey.setValue(new CIMValue(new Boolean(true)));
keys.addElement(logsvcKey);
|
ログ記録用に CIMObjectPath クラスの新しいインスタンスを宣言します。
CIMObjectPath logreccop = new CIMObjectPath("Solaris_LogRecord", keys);
|
Solaris_LogRecord の新しいインスタンスを宣言します。ファイルに書き込むようにプロパティのベクトルを設定します。
CIMInstance ci = new CIMInstance();
ci.setClassName("Solaris_LogRecord");
ci.setProperties(keys);
//System.out.println(ci.toString());
cc.setInstance(logreccop,ci);
}
catch (Exception e) {
System.out.println("Exception: "+e);
e.printStackTrace();
}
|
データがログファイルに書きこまれたあと、セッションを閉じます。
// セッションを閉じる
if(cc != null) {
cc.close();
}
}
}
|