Show Table of Contents
第2章 ソフトウェア開発キットの使用
本章には、Java ソフトウェア開発キットの使用方法に関する例を記載します。特に記載がない限り、本章の例はすべて、ソフトウェア開発キットのバージョン 3 を使用します。
2.1. バージョン 3 での Red Hat Enterprise Virtualization Manager への接続
Java ソフトウェア開発キットバージョン 3 では、
Api クラスは Red Hat Enterprise Virtualization 環境のオブジェクトに接続して操作するために使用する主要なクラスです。このクラスのインスタンスを宣言するには、ApiBuilder クラスのインスタンスを宣言して、builder メソッドでこのインスタンスに必要な引数を渡し、このインスタンス上で build メソッドを呼び出します。build メソッドは、Api クラスのインスタンスを返し、その後にこのクラスを変数に割り当てて、後続のアクションを実行する際に使用することができます。
Red Hat Enterprise Virtualization 環境との接続を作成して正常にシャットダウンし、接続を終了するとうい簡単な Java SE プログラムの例を以下に示します。
例2.1 Red Hat Enterprise Virtualization Manager への接続
package rhevm;
import org.ovirt.engine.sdk.Api;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.ovirt.engine.sdk.ApiBuilder;
import org.ovirt.engine.sdk.exceptions.ServerException;
import org.ovirt.engine.sdk.exceptions.UnsecuredConnectionAttemptError;
public class rhevm {
public static void main(String[] args) {
Api api = null;
try {
ApiBuilder myBuilder = new ApiBuilder()
.url("https://rhevm.example.com/api")
.user("admin@internal")
.password("p@ssw0rd")
.keyStorePath("/home/username/server.truststore")
.keyStorePassword("p@ssw0rd");
api = myBuilder.build();
api.shutdown();
} catch (ServerException | UnsecuredConnectionAttemptError | IOException ex) {
Logger.getLogger(Ovirt.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (api != null) {
try {
api.close();
} catch (Exception ex) {
Logger.getLogger(Ovirt.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
この例は、基本的な認証を使用した接続を作成していますが、ほかのメソッドも利用できます。
ApiBuilder クラスのインスタンスに渡すことができる主要な引数の一覧については、「付録A ApiBuilder メソッド」を参照してください。
注記
Api クラスは、Autocloseable インターフェースを実装しない点に注意してください。そのため、上記の例のように finally ブロック内の Api クラスのインスタンスをシャットダウンして Red Hat Virtualization Manager との接続が正常にシャットダウンされるようにすることを推奨します。

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.