0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

LibertyでJNDIをためす

Last updated at Posted at 2024-06-30

目的

LibertyでJNDIのプログラムをテストします。

Gitレポジトリ

用意したGitレポジトリをクローンします。

git clone https://github.com/pdprof/ejb-jndi.git
cd ejb-jndi/ejb-jndi-docker/

Libertyのイメージをビルドして開始

JNDIのlookupコードとサーバー構成を含むイメージをビルドします。

./setup-docker.sh
./start.sh

Libertyの構成

ここで使用するJNDIの登録は Liberty の server.xmlにしてあります。

サーバー構成ファイルからの定数の JNDI バインディングの使用

を参考に

<jndiEntry jndiName="qiita/Author" value="keniooi"/>
<jndiURLEntry jndiName="urls/Qiita" value="https://qiita.com/"/>

を書きます。

JNDI lookupのコード

server.xmlに書いた定数を lookupするコードを用意します。

InitialContext ic = new InitialContext();
String author = (String) ic.lookup("qiita/Author");
URL qiita = (URL) ic.lookup("urls/Qiita");
response.getWriter().append("URL: ").append(qiita.getProtocol()+"://" + qiita.getHost() + "/" + author);

jndiURLEntry は URLオブジェクト が戻されて、jndiEntry は Stringオブジェクト が戻されてました。

サーバー構成の確認

登録したJNDI構成が実行環境で反映されているかを server dump から確認します。

podman exec -it ejb-jndi bash
/opt/ol/wlp/bin/server dump defaultServer

以下のように出力ファイルがログされるので、このzipファイルの中身を確認します。

Dumping server defaultServer.
Server defaultServer dump complete in /opt/ol/wlp/output/defaultServer/defaultServer.dump-24.06.30_13.18.01.zip.

ホストにコピーする(zipのファイル名は変更が必要です。

cd /tmp
mkdir defaultServer
podman cp ejb-jndi:/opt/ol/wlp/output/defaultServer/defaultServer.dump-24.06.30_13.18.01.zip
unzip defaultServer.dump-24.06.30_13.18.01.zip

dump_.... ディレクトリにあるファイルでJNDIの構成を確認します。

$ ls -al
total 1228
drwxr-xr-x. 7 keniooi keniooi     187 Jun 30 22:21 .
drwxr-xr-x. 4 keniooi keniooi      43 Jun 30 22:21 ..
drwxr-x---. 3 keniooi keniooi      20 Jun 30 22:18 autopdzip
drwxr-xr-x. 4 keniooi keniooi      39 Jun 30 22:21 configDropins
-rw-r-----. 1 keniooi keniooi 1251754 Jun 30 22:18 defaultServer.dump-24.06.30_13.18.01.zip
drwxrwxrwx. 5 keniooi keniooi      98 Jun 30 22:18 dump_24.06.30_13.18.01
-rw-r--r--. 1 keniooi keniooi       0 Jun 30 22:03 jvm.options
drwxrwx---. 3 keniooi keniooi      42 Jun 30 22:16 logs
-rw-------. 1 keniooi keniooi    1227 Jun 30 22:18 server.xml
drwxrwx---. 5 keniooi keniooi      84 Jun 30 22:16 workarea

$ cat dump_24.06.30_13.18.01/introspections/JNDIDefaultNamespace.txt 
The description of this introspector:
JNDI default namespace


======================================================================================
Beginning of Dump
======================================================================================

Service.id      Service.name                                  ObjectClass
634             interrupt/iti                                 com.ibm.websphere.interrupt.InterruptibleThreadInfrastructure
426             qiita/Author                                  java.lang.String
504             services/cache/basecache                      com.ibm.websphere.cache.DistributedObjectCache
506             services/cache/distributedmap                 com.ibm.websphere.cache.DistributedObjectCache
433             urls/Qiita                                    javax.naming.Reference
Total number of items: 5

======================================================================================
End of Dump
======================================================================================

サーブレットを動かす

上のコード抜粋のように文字列を取得しているだけですが、動かして動作を確認します。

Lookupで取得したデータをもとにURLが出力されてます。

image.png

トラブルシューティング

毎度のことですが、よくつかう dockerコマンドを書いておきます。この文書でもpodman, dockerとまとまりがないですが、Linux では dockerがpodmanを呼び出すようになっていました。

docker ps # 動作中のContainer確認
docker logs -f ejb-jndi # ログ確認
docker restart ejb-jndi # 再起動
docker inspect ejb-jndi # container 確認
docker exec -it ejb-jndi bash # Container内状況確認

など試してみましょう。ejb-jndiの部分を docker ps で確認した container の別IDにすることでLiberty側も確認できます。

まとめ

LibertyのJNDI設定をして、プログラムから参照しました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?