例: JNDI を使用した EJB ホームまたはビジネス・インターフェースの検索
Java™ Naming and Directory Interface (JNDI) を使用するアプリケーションの大部分は、コンテナーで実行されます。 一部にはそうでないものもあります。 オブジェクトの検索に使用される名前は、アプリケーションがコンテナーで実行されるかどうかによって異なります。アプリケーションでは、検索名として corbaname URL を使用する方が便利な場合があります。コンテナー・ベースの JNDI クライアントおよび Java シン・クライアントは、corbaname URL を使用することができます。
以下の例は、さまざまなタイプのアプリケーションから JNDI 検索を実行する方法を示したものです。
コンテナーで実行されるアプリケーションからの JNDI 検索
コンテナーで実行されるアプリケーションは、java: 検索名を使用できます。 この形式の検索名は、 オブジェクトの検索に使用される検索名が、 ネーム・サーバーの名前空間にバインドされているためにオブジェクトの名前には依存しないという間接レベルを提供します。 アプリケーションのデプロイメント記述子は、java: 名とネーム・サーバーの検索名からのマッピングを提供します。 コンテナーは、java: 名が対応するオブジェクトに正しくマップされるよう、 デプロイメント記述子の情報に基づき java: 名前空間をセットアップします。
// Get the initial context as shown in a previous example.
...
// Look up the business interface using the JNDI name.
try {
java.lang.Object ejbBusIntf =
initialContext.lookup(
"java:comp/env/com/mycompany/accounting/Account");
accountIntf =
(Account)javax.rmi.PortableRemoteObject.narrow(ejbBusIntf, Account.class);
}
catch (NamingException e) { // Error getting the business interface
...
}
以下の例は、EJB 1.x または 2.x EJB ホームの検索を示しています。実際のホームの検索名は、 アプリケーションのデプロイメント記述子によって決定されます。 エンタープライズ Bean (EJB) は、EJB コンテナーにあります。 このコンテナーでは、Bean とその Bean が置かれているアプリケーション・サーバーとの間のインターフェースが提供されます。
// Get the initial context as shown in a previous example
...
// Look up the home interface using the JNDI name
try {
java.lang.Object ejbHome =
initialContext.lookup(
"java:comp/env/com/mycompany/accounting/AccountEJB");
accountHome = (AccountHome)javax.rmi.PortableRemoteObject.narrow(
(org.omg.CORBA.Object) ejbHome, AccountHome.class);
}
catch (NamingException e) { // Error getting the home interface
...
}
コンテナーで実行されないアプリケーションからの JNDI 検索
コンテナーで実行されないアプリケーションは、java: 検索名を使用することはできません。 これは、コンテナーがアプリケーションの java: 名前空間をセットアップするためです。 その代わり、 このようなアプリケーションは、ネーム・サーバーから直接オブジェクトを検索する必要があります。各アプリケーション・サーバーは、ネーム・サーバーを含んでいます。EJB ホームなどのシステム成果物は、そのネーム・サーバーのサーバー・ルート・コンテキストにバインドされています。各種のネーム・サーバーが、 システムの名前空間構造により統合されています。異なるサーバーのオブジェクトを検索するには、 セル内のいずれの初期コンテキストからも名前解決されるよう、名前を修飾する方法をお勧めします。 相対名が使用されている場合、初期コンテキストは、オブジェクトがバインドされているサーバー・ルート・コンテキストと同じサーバー・ルート・コンテキストである必要があります。修飾名の形式は、その修飾名がトポロジー・ベースの名前か 固定名かによって異なります。以下の例は、修飾名の各形式を示しています。
- トポロジー・ベースの修飾名
トポロジー・ベースの修飾名は、システム名前空間で、ターゲット・オブジェクトがバインドされているサーバー・ルート・コンテキストを全探索します。 トポロジー・ベースの修飾名は、セル内のいずれの初期コンテキストからも解決されます。
トポロジー・ベースの修飾名は、オブジェクトが単一サーバーかサーバー・クラスターにあるかによって異なります。以下の例は、それぞれの検索を示しています。
- 単一サーバー
- 以下の例は、ノード Node1 に構成された単一サーバー MyServer で実行されている EJB ビジネス・インターフェースの検索を示しています。
// Get the initial context as shown in a previous example. // Using the form of lookup name below, it does not matter which // server in the cell is used to obtain the initial context. ... // Look up the business interface using the JNDI name try { java.lang.Object ejbBusIntf = initialContext.lookup( "cell/nodes/Node1/servers/MyServer/com/mycompany/accounting/Account"); accountIntf = (Account)javax.rmi.PortableRemoteObject.narrow(ejbBusIntf, Account.class); } catch (NamingException e) { // Error getting the business interface ... }
以下の例は、ノード Node1 に構成された単一サーバー MyServer で実行されている EJB ホームの検索を示しています。
// Get the initial context as shown in a previous example // Using the form of lookup name below, it doesn't matter which // server in the cell is used to obtain the initial context. ... // Look up the home interface using the JNDI name try { java.lang.Object ejbHome = initialContext.lookup( "cell/nodes/Node1/servers/MyServer/com/mycompany/accounting/AccountEJB"); accountHome = (AccountHome)javax.rmi.PortableRemoteObject.narrow( (org.omg.CORBA.Object) ejbHome, AccountHome.class); } catch (NamingException e) { // Error getting the home interface ... }
- サーバー・クラスター
- 以下の例には、クラスター MyCluster で実行されている EJB ビジネス・インターフェースの検索が示されています。名前解決は、いずれかのクラスター・メンバーが実行されている場合に可能です。
// Get the initial context as shown in a previous example. // Using the form of lookup name below, it does not matter which // server in the cell is used to obtain the initial context. ... // Look up the business interface using the JNDI name try { java.lang.Object ejbBusIntf = initialContext.lookup( "cell/clusters/MyCluster/com/mycompany/accounting/Account"); accountIntf = (Account)javax.rmi.PortableRemoteObject.narrow(ejbBusIntf, Account.class); } catch (NamingException e) { // Error getting the business interface ... }
以下の例には、クラスター MyCluster で実行されている EJB ホームの検索が示されています。名前解決は、いずれかのクラスター・メンバーが実行されている場合に可能です。
// Get the initial context as shown in a previous example // Using the form of lookup name below, it does not matter which // server in the cell is used to obtain the initial context. ... // Look up the home interface using the JNDI name try { java.lang.Object ejbHome = initialContext.lookup( "cell/clusters/MyCluster/com/mycompany/accounting/AccountEJB"); accountHome = (AccountHome)javax.rmi.PortableRemoteObject.narrow( (org.omg.CORBA.Object) ejbHome, AccountHome.class); } catch (NamingException e) { // Error getting the home interface ... }
- 修飾された固定名
ターゲット・オブジェクトにセル・スコープの固定名が定義されている場合、トポロジー・ベースの修飾名の代わりに、その名前の修飾形式を使用することができます。トポロジー・ベースの名前が機能する場合でも、固定名は、特定のセル・トポロジー、またはターゲット・オブジェクトの他のサーバーへの移動に伴って変わることがありません。
以下の例は、修飾された固定名を使用した EJB ビジネス・インターフェースの検索を示しています。// Get the initial context as shown in a previous example. // Using the form of lookup name below, it does not matter which // server in the cell is used to obtain the initial context. ... // Look up the business interface using the JNDI name try { java.lang.Object ejbBusIntf = initialContext.lookup( "cell/persistent/com/mycompany/accounting/Account"); accountIntf = (Account)javax.rmi.PortableRemoteObject.narrow(ejbBusIntf, Account.class); } catch (NamingException e) { // Error getting the business interface ... }
以下の例は、修飾された固定名を使用した検索を示しています。
// Get the initial context as shown in a previous example // Using the form of lookup name below, it doesn't matter which // server in the cell is used to obtain the initial context. ... // Look up the home interface using the JNDI name try { java.lang.Object ejbHome = initialContext.lookup( "cell/persistent/com/mycompany/accounting/AccountEJB"); accountHome = (AccountHome)javax.rmi.PortableRemoteObject.narrow( (org.omg.CORBA.Object) ejbHome, AccountHome.class); } catch (NamingException e) { // Error getting the home interface ... }
corbaname URL を使用した JNDI 検索
corbaname は、検索名として役立つ場合があります。例えば、ターゲット・オブジェクトが統合済み名前空間のメンバーでなく、修飾名を使用して位置指定することができない場合、corbaname を使用すると、簡単にオブジェクトの検索を行うことができます。
// Get the initial context as shown in a previous example.
...
// Look up the business interface using a corbaname URL.
try {
java.lang.Object ejbBusIntf = initialContext.lookup(
"corbaname:iiop:someHost:2809#com/mycompany/accounting/Account");
accountIntf =
(Account)javax.rmi.PortableRemoteObject.narrow(ejbBusIntf, Account.class);
}
catch (NamingException e) { // Error getting the business interface
...
}
以下に、corbaname URL を使用した検索を示します。
// Get the initial context as shown in a previous example
...
// Look up the home interface using a corbaname URL
try {
java.lang.Object ejbHome = initialContext.lookup(
"corbaname:iiop:someHost:2809#com/mycompany/accounting/AccountEJB");
accountHome = (AccountHome)javax.rmi.PortableRemoteObject.narrow(
(org.omg.CORBA.Object) ejbHome, AccountHome.class);
}
catch (NamingException e) { // Error getting the home interface
...
}