0
0

Nablarch 6のRESTfulウェブサービスのExampleをOpen Liberty+PostgreSQLを使うように変更してみる

Last updated at Posted at 2024-06-07

What's?

前にNablarchのRESTfulウェブサービスのExampleをPostgreSQLに変更して動かしてみました。

今回は、Nablarch 6を使ってRESTfulウェブサービスのExampleをOpen Liberty+PostgreSQLで動かしてみたいと思います。

Nablarch 6をOpen Libertyで動かすことが主題、とも言えます。

Nablarch 6

Nablarch 6は2023年12月にリリースされており、現在のバージョンは6u1です。

Nablarch 6はJakarta EE 10に対応しています。

NablarchのRESTfulウェブサービスのExampleはこちらです。

masterブランチはNablarch 5向けのものになっているため、Nablarch 6向けの最新版を参照するためにはv6-masterへ切り替える必要があります。

今回は6u1のタグを指定します。

こちらをOpen Libertyで動かすようにし、データベースはPostgreSQLに切り替えるようにしてみます。

なおExampleには以下の注意点があります。

ExampleはNablarchの機能の使用方法を示した実装例であり、Exampleを改修して本格的なアプリケーションを作成することは想定していない。

本格的なアプリケーションを作成する場合は ブランクプロジェクト から作成すること。

環境

今回の環境はこちら。Java 21を使います。

$ java --version
openjdk 21.0.3 2024-04-16
OpenJDK Runtime Environment (build 21.0.3+9-Ubuntu-1ubuntu122.04.1)
OpenJDK 64-Bit Server VM (build 21.0.3+9-Ubuntu-1ubuntu122.04.1, mixed mode, sharing)


$ mvn --version
Apache Maven 3.9.7 (8b094c9513efc1b9ce2d952b3b9c8eaedaf8cbf0)
Maven home: /home/charon/.sdkman/candidates/maven/current
Java version: 21.0.3, vendor: Ubuntu, runtime: /usr/lib/jvm/java-21-openjdk-amd64
Default locale: ja_JP, platform encoding: UTF-8
OS name: "linux", version: "5.15.0-107-generic", arch: "amd64", family: "unix"

Nablarchについては6u1を使います。

PostgreSQLについてはDockerコンテナで用意。

$ docker container run -it --rm -p 5432:5432 \
  -e POSTGRES_DB=rest_example \
  -e POSTGRES_USER=rest_example_user \
  -e POSTGRES_PASSWORD=rest_example_password \
  --name postgres \
  postgres:16.3-bookworm

今回はNablarch 6のRESTfulウェブサービスのExampleをOpen Libertyに乗せるようにして、上記のコマンドで起動したPostgreSQLを利用するように変更することをゴールにします。

なお、テーブル定義はObjectBrowser ERによるファイルで作成されていますが、今回はそのまま利用できました。

nablarch-example-restリポジトリのclone

nablarch-example-restリポジトリをclone。

$ git clone https://github.com/nablarch/nablarch-example-rest.git

6u1タグをチェックアウト。

$ cd nablarch-example-rest
$ git checkout 6u1

Java 21に対応させる

nablarch-example-restリポジトリのNablarch 6向けのExampleはJava 17を前提としているので、Java 21に対応させます。

といっても、以下の2つを変更するだけです。

    <maven.compiler.source>21</maven.compiler.source>
    <maven.compiler.target>21</maven.compiler.target>

Open LibertyとPostgreSQLを使うように構成変更する

それでは、Open LibertyとPostgreSQLを使うように構成変更していきます。

PostgreSQLへのアクセスですが、Open Liberty側のデータソースを定義してNablarchからはJNDIルックアップするようにしてみましょう。

以下あたりを参考に。

Open Libertyの設定

まずはpom.xmlにliberty-maven-pluginを追加します。

pom.xml
      <plugin>
        <groupId>io.openliberty.tools</groupId>
        <artifactId>liberty-maven-plugin</artifactId>
        <version>3.10.3</version>
        <configuration>
          <runtimeArtifact>
            <groupId>io.openliberty</groupId>
            <artifactId>openliberty-kernel</artifactId>
            <version>24.0.0.5</version>
            <type>zip</type>
          </runtimeArtifact>
          <copyDependencies>
            <dependencyGroup>
              <location>jdbc</location>
              <dependency>
                <groupId>org.postgresql</groupId>
                <artifactId>postgresql</artifactId>
                <version>42.7.3</version>
              </dependency>
            </dependencyGroup>
          </copyDependencies>
        </configuration>
      </plugin>

こちらを参考に、PostgreSQLのJDBCドライバを依存関係に加えています。

Open Libertyの構成ファイルを作成。

src/main/liberty/config/server.xml
<?xml version="1.0" encoding="UTF-8"?>
<server description="new server">

    <!-- Enable features -->
    <featureManager>
        <feature>servlet-6.0</feature>
        <feature>pages-3.1</feature>
        <feature>jdbc-4.3</feature>
        <feature>jndi-1.0</feature>
    </featureManager>

    <!-- This template enables security. To get the full use of all the capabilities, a keystore and user registry are required. -->

    <!-- For the keystore, default keys are generated and stored in a keystore. To provide the keystore password, generate an
        encoded password using bin/securityUtility encode and add it below in the password attribute of the keyStore element.
        Then uncomment the keyStore element. -->
    <!--
    <keyStore password=""/>
    -->

    <!--For a user registry configuration, configure your user registry. For example, configure a basic user registry using the
        basicRegistry element. Specify your own user name below in the name attribute of the user element. For the password,
        generate an encoded password using bin/securityUtility encode and add it in the password attribute of the user element.
        Then uncomment the user element. -->
    <basicRegistry id="basic" realm="BasicRealm">
        <!--
        <user name="yourUserName" password="" />
        -->
    </basicRegistry>

    <!-- To access this server from a remote client add a host attribute to the following element, e.g. host="*" -->
    <httpEndpoint id="defaultHttpEndpoint"
                  httpPort="9080"
                  httpsPort="9443" />

    <!-- Automatically expand WAR files and EAR files -->
    <applicationManager autoExpand="true"/>

    <!-- Configures the application on a specified context root -->
    <webApplication contextRoot="/nablarch-example-rest" location="nablarch-example-rest-6u1.war" />

    <!-- Default SSL configuration enables trust for default certificates from the Java runtime -->
    <ssl id="defaultSSLConfig" trustDefaultCerts="true" />

    <library id="jdbcLib">
        <fileset dir="jdbc" includes="*.jar"/>
    </library>

    <dataSource jndiName="jdbc/myDataSource">
       <jdbcDriver libraryRef="jdbcLib"/>
        <properties serverName="localhost"
                    portNumber="5432"
                    databaseName="rest_example"
                    user="rest_example_user"
                    password="rest_example_password"/>
    </dataSource>
</server>

これは、Open Libertyのstarterで作成したデフォルトのserver.xmlを少し修正したものです。

フィーチャーはJakarta Servlet、Jakarta Server Pages、JDBC、JNDIの4つにしました。

    <!-- Enable features -->
    <featureManager>
        <feature>servlet-6.0</feature>
        <feature>pages-3.1</feature>
        <feature>jdbc-4.3</feature>
        <feature>jndi-1.0</feature>
    </featureManager>

だいぶ細かくフィーチャーを指定しています。

もっとざっくり指定したい場合は、このあたりを使うとよいと思います。

WARファイルとコンテキストパスはこんな感じに。

    <!-- Configures the application on a specified context root -->
    <webApplication contextRoot="/nablarch-example-rest" location="nablarch-example-rest-6u1.war" />

JDBCドライバのコピーとデータソースの定義。

    <library id="jdbcLib">
        <fileset dir="jdbc" includes="*.jar"/>
    </library>

    <dataSource jndiName="jdbc/myDataSource">
       <jdbcDriver libraryRef="jdbcLib"/>
        <properties serverName="localhost"
                    portNumber="5432"
                    databaseName="rest_example"
                    user="rest_example_user"
                    password="rest_example_password"/>
    </dataSource>

こちらを参考にしています。

ちなみに、この部分のコピー元なのですが

    <library id="jdbcLib">
        <fileset dir="jdbc" includes="*.jar"/>
    </library>

これはliberty-maven-pluginでコピーした依存関係を取り込んだものですね。

          <copyDependencies>
            <dependencyGroup>
              <location>jdbc</location>
              <dependency>
                <groupId>org.postgresql</groupId>
                <artifactId>postgresql</artifactId>
                <version>42.7.3</version>
              </dependency>
            </dependencyGroup>
          </copyDependencies>

このデータソースを使うようにNablarchの設定ファイルを作成します。

JNDI名はこちらに定義。

src/main/resources/env.properties
##
## 開発環境用設定ファイル
##

nablarch.connectionFactory.jndiResourceName=jdbc/myDataSource

こちらの設定ファイルをリネームして、データソースをJNDIで取得するようにカスタマイズ。

src/main/resources/com/nablarch/example/db/connection-factory-jndi.xml
<?xml version="1.0" encoding="UTF-8"?>

<!-- DataSourceを使用したコネクションファクトリの設定 -->
<component-configuration
        xmlns="http://tis.co.jp/nablarch/component-configuration"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://tis.co.jp/nablarch/component-configuration https://nablarch.github.io/schema/component-configuration.xsd">

  <!-- ステートメントファクトリ -->
  <import file="com/nablarch/example/db/db-statement-factory.xml"/>

  <import file="nablarch/core/db/db-access-exception-factory.xml"/>

  <!-- データベース接続用設定 -->
  <component name="connectionFactory"
             class="nablarch.core.db.connection.BasicDbConnectionFactoryForJndi">
    <property name="dialect" ref="dialect" />
    <property name="jndiResourceName" value="${nablarch.connectionFactory.jndiResourceName}" />
    <property name="statementFactory" ref="statementFactory" />
    <property name="dbAccessExceptionFactory" ref="dbAccessExceptionFactory" />
  </component>

</component-configuration>

こちらのファイルをimportするように修正。

src/main/resources/db.xml
<?xml version="1.0" encoding="UTF-8"?>
<component-configuration
        xmlns="http://tis.co.jp/nablarch/component-configuration"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://tis.co.jp/nablarch/component-configuration https://nablarch.github.io/schema/component-configuration.xsd">

  <import file="com/nablarch/example/db/connection-factory-jndi.xml"/>

</component-configuration>

DialectをPostgreSQLに変更。

src/main/resources/rest-component-configuration.xml
  <!-- ダイアレクト設定 -->
  <component name="dialect" class="nablarch.core.db.dialect.PostgreSQLDialect" />

gsp-dba-maven-pluginの設定を行う

次はgsp-dba-maven-pluginの設定を行います。

なんとなく、今回はユーザー名とパスワードを別々にしたのでプロパティを追加。

pom.xml
    <db.adminUser>rest_example_user</db.adminUser>
    <db.adminUserPassword>rest_example_password</db.adminUserPassword>
    <db.user>rest_example_user</db.user>
    <db.userPassword>rest_example_password</db.userPassword>

gsp-dba-maven-pluginの設定のうち、driverurladminPasswordpasswordを変更。
そして使用するJDBCドライバをH2からPostgreSQLに変更します。

      <plugin>
        <groupId>jp.co.tis.gsp</groupId>
        <artifactId>gsp-dba-maven-plugin</artifactId>
        <version>5.0.0</version>
        <configuration>
          <erdFile>src/main/resources/entity/data-model.edm</erdFile>

          <driver>org.postgresql.Driver</driver>
          <url>jdbc:postgresql://localhost:5432/rest_example?ApplicationName=gsp-dba-maven-plugin</url>

          <adminUser>${db.adminUser}</adminUser>
          <adminPassword>${db.adminUserPassword}</adminPassword>
          <user>${db.user}</user>
          <password>${db.userPassword}</password>
          <schema>PUBLIC</schema>

          <rootPackage>com.nablarch.example</rootPackage>
          <entityPackageName>entity</entityPackageName>
          <useAccessor>true</useAccessor>
          <javaFileDestDir>${project.build.directory}/generated-sources/entity</javaFileDestDir>

          <dataDirectory>src/test/resources/data</dataDirectory>
        </configuration>
        <dependencies>
          <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.7.3</version>
            <scope>runtime</scope>
          </dependency>
        </dependencies>
        <executions>
          <execution>
            <id>default-cli</id>
            <phase>generate-resources</phase>
            <goals>
              <goal>generate-ddl</goal>
              <goal>execute-ddl</goal>
              <goal>generate-entity</goal>
              <goal>load-data</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

テストの設定変更

この時点で実行はできるようになるのですが、テストも通せるようにしましょうか。

src/main/resources/env.propertiesにあった項目をテスト用に移植します。
こんな感じでプロパティファイルを作成。

src/test/resources/unit-test.properties
# JDBC接続ドライバクラス(DataSourceを直接使用する際の項目)
nablarch.db.jdbcDriver=org.postgresql.Driver

# JDBC接続URL(DataSourceを直接使用する際の項目)
nablarch.db.url=jdbc:postgresql://localhost:5432/rest_example?ApplicationName=rest_example_unit_test

# DB接続ユーザ名(DataSourceを直接使用する際の項目)
nablarch.db.user=rest_example_user

# DB接続パスワード(DataSourceを直接使用する際の項目)
nablarch.db.password=rest_example_password

# DBスキーマ名(DataSourceを直接使用する際の項目)
nablarch.db.schema=PUBLIC

# 最大プールサイズ(DataSourceを直接使用する際の項目)
nablarch.db.maxPoolSize=5

プロパティファイルの読み込み。

src/test/resources/unit-test.xml
  <!-- 設定ファイル -->
  <config-file file="unit-test.properties" />

データソースも定義します。

xml:src/test/resources/unit-test.xml
  <!-- データソース設定 -->
  <component name="dataSource"
             class="com.zaxxer.hikari.HikariDataSource" autowireType="None">
    <property name="driverClassName" value="${nablarch.db.jdbcDriver}"/>
    <property name="jdbcUrl"         value="${nablarch.db.url}"/>
    <property name="username"        value="${nablarch.db.user}"/>
    <property name="password"        value="${nablarch.db.password}"/>
    <property name="maximumPoolSize" value="${nablarch.db.maxPoolSize}"/>
  </component>

最後に依存関係です。H2は使わなくなるので削除、HikariCPはテストでのみ使います。

pom.xml
    <dependency>
      <groupId>com.h2database</groupId>
      <artifactId>h2</artifactId>
      <version>2.2.220</version>
      <scope>runtime</scope>
    </dependency>

    <dependency>
      <groupId>com.zaxxer</groupId>
      <artifactId>HikariCP</artifactId>
      <version>3.3.1</version>
      <scope>runtime</scope>
    </dependency>

というわけでこうなります。

pom.xml
    <dependency>
      <groupId>org.postgresql</groupId>
      <artifactId>postgresql</artifactId>
      <version>42.7.3</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>com.zaxxer</groupId>
      <artifactId>HikariCP</artifactId>
      <version>3.3.1</version>
      <scope>test</scope>
    </dependency>

その他

本題とは関係ないのですが、以下の部分を削除しておきました。

pom.xml
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.3.2</version>
        <configuration>
          <webXml>${webxml.path}</webXml>
          <!--
          <classifier>${env.classifier}</classifier>
          <archive>
            <manifestEntries>
              <Target-Environment>${env.name}</Target-Environment>
            </manifestEntries>
          </archive>
          -->
        </configuration>
      </plugin>

ここまでで準備は完了です。

動作確認

まずはテストを実行してみます。

$ mvn test

成功しました。

[INFO] Results:
[INFO]
[INFO] Tests run: 79, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

PostgreSQLに対してテーブルの作成やデータの登録ができているか、確認してみます。

$ docker container exec -it postgres psql -U rest_example_user rest_example
psql (16.3 (Debian 16.3-1.pgdg120+1))
Type "help" for help.

rest_example=#

OKですね。

rest_example=# \d
                         List of relations
 Schema |            Name            |   Type   |       Owner
--------+----------------------------+----------+-------------------
 public | client                     | table    | rest_example_user
 public | client_client_id_seq       | sequence | rest_example_user
 public | industry                   | table    | rest_example_user
 public | password_history           | table    | rest_example_user
 public | project                    | table    | rest_example_user
 public | project_project_id_seq     | sequence | rest_example_user
 public | system_account             | table    | rest_example_user
 public | system_account_user_id_seq | sequence | rest_example_user
 public | users                      | table    | rest_example_user
(9 rows)

rest_example=# select * from client limit 5;
 client_id | client_name | industry_code
-----------+-------------+---------------
         1 | 1株式会社  | 26
         2 | 2株式会社  | 26
         3 | 3株式会社  | 26
         4 | 4株式会社  | 26
         5 | 5株式会社  | 26
(5 rows)

Open Libertyで実行してみます。

$ mvn compile liberty:run

確認。

$ curl -s localhost:9080/nablarch-example-rest/projects | jq
[
  {
    "projectId": 1,
    "projectName": "プロジェクト001",
    "projectType": "development",
    "projectClass": "s",
    "projectStartDate": "2010/09/18",
    "projectEndDate": "2015/04/09",
    "clientId": 1,
    "projectManager": "鈴木",
    "projectLeader": "佐藤",
    "userId": 100,
    "note": "備考欄",
    "sales": 10000,
    "costOfGoodsSold": 1000,
    "sga": 2000,
    "allocationOfCorpExpenses": 3000,
    "client": null,
    "systemAccount": null
  },
  {
    "projectId": 2,
    "projectName": "プロジェクト002",
    "projectType": "maintenance",
    "projectClass": "b",
    "projectStartDate": "2010/09/18",
    "projectEndDate": "2014/11/10",
    "clientId": 2,
    "projectManager": "佐藤",
    "projectLeader": "鈴木",
    "userId": 100,
    "note": null,
    "sales": null,
    "costOfGoodsSold": null,
    "sga": null,
    "allocationOfCorpExpenses": null,
    "client": null,
    "systemAccount": null
  },
  {
    "projectId": 3,
    "projectName": "プロジェクト003",
    "projectType": "development",
    "projectClass": "c",
    "projectStartDate": "2015/04/09",
    "projectEndDate": "2015/04/09",
    "clientId": 3,
    "projectManager": "田中",
    "projectLeader": "佐藤",
    "userId": 100,
    "note": "空白",
    "sales": 10000,
    "costOfGoodsSold": 1000,
    "sga": 2000,
    "allocationOfCorpExpenses": 3000,
    "client": null,
    "systemAccount": null
  },
  {
    "projectId": 4,
    "projectName": "プロジェクト004",
    "projectType": "development",
    "projectClass": "a",
    "projectStartDate": "2012/06/22",
    "projectEndDate": "2013/04/01",
    "clientId": 4,
    "projectManager": "山田",
    "projectLeader": "田中",
    "userId": 100,
    "note": "なし",
    "sales": 10000,
    "costOfGoodsSold": 1000,
    "sga": 2000,
    "allocationOfCorpExpenses": 3000,
    "client": null,
    "systemAccount": null
  },
  {
    "projectId": 5,
    "projectName": "プロジェクト005",
    "projectType": "maintenance",
    "projectClass": "ss",
    "projectStartDate": "2012/12/01",
    "projectEndDate": "2014/12/31",
    "clientId": 5,
    "projectManager": "鈴木",
    "projectLeader": "山田",
    "userId": 100,
    "note": "テスト",
    "sales": 10000,
    "costOfGoodsSold": 1000,
    "sga": 2000,
    "allocationOfCorpExpenses": 3000,
    "client": null,
    "systemAccount": null
  },
  {
    "projectId": 6,
    "projectName": "プロジェクト006",
    "projectType": "maintenance",
    "projectClass": "d",
    "projectStartDate": "2012/06/22",
    "projectEndDate": "2015/01/08",
    "clientId": 6,
    "projectManager": "佐藤",
    "projectLeader": "鈴木",
    "userId": 100,
    "note": null,
    "sales": 10000,
    "costOfGoodsSold": 1000,
    "sga": 2000,
    "allocationOfCorpExpenses": 3000,
    "client": null,
    "systemAccount": null
  },
  {
    "projectId": 7,
    "projectName": "プロジェクト007",
    "projectType": "development",
    "projectClass": "s",
    "projectStartDate": "2012/12/01",
    "projectEndDate": "2015/04/09",
    "clientId": 7,
    "projectManager": "鈴木",
    "projectLeader": "佐藤",
    "userId": 100,
    "note": "備考欄1",
    "sales": 10000,
    "costOfGoodsSold": 1000,
    "sga": 2000,
    "allocationOfCorpExpenses": 3000,
    "client": null,
    "systemAccount": null
  },
  {
    "projectId": 8,
    "projectName": "プロジェクト008",
    "projectType": "development",
    "projectClass": "s",
    "projectStartDate": "2010/09/18",
    "projectEndDate": "2014/11/10",
    "clientId": 8,
    "projectManager": "佐藤",
    "projectLeader": "佐藤",
    "userId": 100,
    "note": "備考欄2",
    "sales": 10000,
    "costOfGoodsSold": 1000,
    "sga": 2000,
    "allocationOfCorpExpenses": 3000,
    "client": null,
    "systemAccount": null
  },
  {
    "projectId": 9,
    "projectName": "プロジェクト009",
    "projectType": "development",
    "projectClass": "b",
    "projectStartDate": "2015/04/09",
    "projectEndDate": "2015/04/09",
    "clientId": 9,
    "projectManager": "田中",
    "projectLeader": "鈴木",
    "userId": 100,
    "note": "備考欄3",
    "sales": 10000,
    "costOfGoodsSold": 1000,
    "sga": 2000,
    "allocationOfCorpExpenses": 3000,
    "client": null,
    "systemAccount": null
  },
  {
    "projectId": 10,
    "projectName": "プロジェクト010",
    "projectType": "maintenance",
    "projectClass": "c",
    "projectStartDate": "2012/06/22",
    "projectEndDate": "2013/04/01",
    "clientId": 10,
    "projectManager": "山田",
    "projectLeader": "佐藤",
    "userId": 100,
    "note": "備考欄4",
    "sales": 10000,
    "costOfGoodsSold": 1000,
    "sga": 2000,
    "allocationOfCorpExpenses": 3000,
    "client": null,
    "systemAccount": null
  }
]

OKですね。

最後に、実行可能JARファイルにしてみましょう。

$ mvn package liberty:create liberty:install-feature liberty:deploy liberty:package -Dinclude=runnable -DskipTests=true

起動。

$ java -jar target/nablarch-example-rest-6u1.jar

確認。結果はまったく同じなので、割愛します。

$ curl -s localhost:9080/nablarch-example-rest/projects | jq

これで、NablarchのRESTfulウェブサービスのExampleをOpen Liberty上で動かし、PostgreSQLを使うように変更できました。

変更差分

最後に、今回の内容で変更したファイルの差分を載せておきます。

diff --git a/pom.xml b/pom.xml
index 0180c23..1f41edd 100644
--- a/pom.xml
+++ b/pom.xml
@@ -9,14 +9,16 @@
   <packaging>war</packaging>

   <properties>
-    <maven.compiler.source>17</maven.compiler.source>
-    <maven.compiler.target>17</maven.compiler.target>
+    <maven.compiler.source>21</maven.compiler.source>
+    <maven.compiler.target>21</maven.compiler.target>

     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

-    <db.adminUser>REST_EXAMPLE</db.adminUser>
-    <db.user>REST_EXAMPLE</db.user>
+    <db.adminUser>rest_example_user</db.adminUser>
+    <db.adminUserPassword>rest_example_password</db.adminUserPassword>
+    <db.user>rest_example_user</db.user>
+    <db.userPassword>rest_example_password</db.userPassword>

   </properties>

@@ -136,20 +138,6 @@
       <artifactId>nablarch-common-dao</artifactId>
     </dependency>

-    <dependency>
-      <groupId>com.h2database</groupId>
-      <artifactId>h2</artifactId>
-      <version>2.2.220</version>
-      <scope>runtime</scope>
-    </dependency>
-
-    <dependency>
-      <groupId>com.zaxxer</groupId>
-      <artifactId>HikariCP</artifactId>
-      <version>3.3.1</version>
-      <scope>runtime</scope>
-    </dependency>
-
     <dependency>
       <groupId>com.nablarch.integration</groupId>
       <artifactId>slf4j-nablarch-adaptor</artifactId>
@@ -224,6 +212,20 @@
       <artifactId>junit-jupiter</artifactId>
       <scope>test</scope>
     </dependency>
+
+    <dependency>
+      <groupId>org.postgresql</groupId>
+      <artifactId>postgresql</artifactId>
+      <version>42.7.3</version>
+      <scope>test</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>com.zaxxer</groupId>
+      <artifactId>HikariCP</artifactId>
+      <version>3.3.1</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>

   <build>
@@ -235,13 +237,13 @@
         <configuration>
           <erdFile>src/main/resources/entity/data-model.edm</erdFile>

-          <driver>org.h2.Driver</driver>
-          <url>jdbc:h2:./h2/db/rest_example</url>
+          <driver>org.postgresql.Driver</driver>
+          <url>jdbc:postgresql://localhost:5432/rest_example?ApplicationName=gsp-dba-maven-plugin</url>

           <adminUser>${db.adminUser}</adminUser>
-          <adminPassword>${db.adminUser}</adminPassword>
+          <adminPassword>${db.adminUserPassword}</adminPassword>
           <user>${db.user}</user>
-          <password>${db.user}</password>
+          <password>${db.userPassword}</password>
           <schema>PUBLIC</schema>

           <rootPackage>com.nablarch.example</rootPackage>
@@ -253,9 +255,9 @@
         </configuration>
         <dependencies>
           <dependency>
-            <groupId>com.h2database</groupId>
-            <artifactId>h2</artifactId>
-            <version>2.2.220</version>
+            <groupId>org.postgresql</groupId>
+            <artifactId>postgresql</artifactId>
+            <version>42.7.3</version>
             <scope>runtime</scope>
           </dependency>
         </dependencies>
@@ -316,12 +318,6 @@
         <version>3.3.2</version>
         <configuration>
           <webXml>${webxml.path}</webXml>
-          <classifier>${env.classifier}</classifier>
-          <archive>
-            <manifestEntries>
-              <Target-Environment>${env.name}</Target-Environment>
-            </manifestEntries>
-          </archive>
         </configuration>
       </plugin>
       <!-- カバレッジ取得 -->
@@ -335,6 +331,29 @@
           </excludes>
         </configuration>
       </plugin>
+      <plugin>
+        <groupId>io.openliberty.tools</groupId>
+        <artifactId>liberty-maven-plugin</artifactId>
+        <version>3.10.3</version>
+        <configuration>
+          <runtimeArtifact>
+            <groupId>io.openliberty</groupId>
+            <artifactId>openliberty-kernel</artifactId>
+            <version>24.0.0.5</version>
+            <type>zip</type>
+          </runtimeArtifact>
+          <copyDependencies>
+            <dependencyGroup>
+              <location>jdbc</location>
+              <dependency>
+                <groupId>org.postgresql</groupId>
+                <artifactId>postgresql</artifactId>
+                <version>42.7.3</version>
+              </dependency>
+            </dependencyGroup>
+          </copyDependencies>
+        </configuration>
+      </plugin>
     </plugins>
     <extensions>
       <extension>
diff --git a/src/main/liberty/config/server.xml b/src/main/liberty/config/server.xml
new file mode 100644
index 0000000..024c750
--- /dev/null
+++ b/src/main/liberty/config/server.xml
@@ -0,0 +1,57 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<server description="new server">
+
+    <!-- Enable features -->
+    <featureManager>
+        <feature>servlet-6.0</feature>
+        <feature>pages-3.1</feature>
+        <feature>jdbc-4.3</feature>
+        <feature>jndi-1.0</feature>
+    </featureManager>
+
+    <!-- This template enables security. To get the full use of all the capabilities, a keystore and user registry are required. -->
+
+    <!-- For the keystore, default keys are generated and stored in a keystore. To provide the keystore password, generate an
+        encoded password using bin/securityUtility encode and add it below in the password attribute of the keyStore element.
+        Then uncomment the keyStore element. -->
+    <!--
+    <keyStore password=""/>
+    -->
+
+    <!--For a user registry configuration, configure your user registry. For example, configure a basic user registry using the
+        basicRegistry element. Specify your own user name below in the name attribute of the user element. For the password,
+        generate an encoded password using bin/securityUtility encode and add it in the password attribute of the user element.
+        Then uncomment the user element. -->
+    <basicRegistry id="basic" realm="BasicRealm">
+        <!--
+        <user name="yourUserName" password="" />
+        -->
+    </basicRegistry>
+
+    <!-- To access this server from a remote client add a host attribute to the following element, e.g. host="*" -->
+    <httpEndpoint id="defaultHttpEndpoint"
+                  httpPort="9080"
+                  httpsPort="9443" />
+
+    <!-- Automatically expand WAR files and EAR files -->
+    <applicationManager autoExpand="true"/>
+
+    <!-- Configures the application on a specified context root -->
+    <webApplication contextRoot="/nablarch-example-rest" location="nablarch-example-rest-6u1.war" />
+
+    <!-- Default SSL configuration enables trust for default certificates from the Java runtime -->
+    <ssl id="defaultSSLConfig" trustDefaultCerts="true" />
+
+    <library id="jdbcLib">
+        <fileset dir="jdbc" includes="*.jar"/>
+    </library>
+
+    <dataSource jndiName="jdbc/myDataSource">
+       <jdbcDriver libraryRef="jdbcLib"/>
+        <properties serverName="localhost"
+                    portNumber="5432"
+                    databaseName="rest_example"
+                    user="rest_example_user"
+                    password="rest_example_password"/>
+    </dataSource>
+</server>
diff --git a/src/main/resources/com/nablarch/example/db/connection-factory-datasource.xml b/src/main/resources/com/nablarch/example/db/connection-factory-jndi.xml
similarity index 85%
rename from src/main/resources/com/nablarch/example/db/connection-factory-datasource.xml
rename to src/main/resources/com/nablarch/example/db/connection-factory-jndi.xml
index fe221bb..bab6f18 100644
--- a/src/main/resources/com/nablarch/example/db/connection-factory-datasource.xml
+++ b/src/main/resources/com/nablarch/example/db/connection-factory-jndi.xml
@@ -13,11 +13,10 @@

   <!-- データベース接続用設定 -->
   <component name="connectionFactory"
-             class="nablarch.core.db.connection.BasicDbConnectionFactoryForDataSource">
+             class="nablarch.core.db.connection.BasicDbConnectionFactoryForJndi">
     <property name="dialect" ref="dialect" />
-    <property name="dataSource" ref="dataSource" />
+    <property name="jndiResourceName" value="${nablarch.connectionFactory.jndiResourceName}" />
     <property name="statementFactory" ref="statementFactory" />
-    <property name="statementReuse" value="${nablarch.connectionFactory.statementReuse}" />
     <property name="dbAccessExceptionFactory" ref="dbAccessExceptionFactory" />
   </component>

diff --git a/src/main/resources/db.xml b/src/main/resources/db.xml
index e18171d..1fa36c4 100644
--- a/src/main/resources/db.xml
+++ b/src/main/resources/db.xml
@@ -4,16 +4,6 @@
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://tis.co.jp/nablarch/component-configuration https://nablarch.github.io/schema/component-configuration.xsd">

-  <import file="com/nablarch/example/db/connection-factory-datasource.xml"/>
-
-  <!-- データソース設定 -->
-  <component name="dataSource"
-             class="com.zaxxer.hikari.HikariDataSource" autowireType="None">
-    <property name="driverClassName" value="${nablarch.db.jdbcDriver}"/>
-    <property name="jdbcUrl"         value="${nablarch.db.url}"/>
-    <property name="username"        value="${nablarch.db.user}"/>
-    <property name="password"        value="${nablarch.db.password}"/>
-    <property name="maximumPoolSize" value="${nablarch.db.maxPoolSize}"/>
-  </component>
+  <import file="com/nablarch/example/db/connection-factory-jndi.xml"/>

 </component-configuration>
diff --git a/src/main/resources/env.properties b/src/main/resources/env.properties
index 2dee20f..2dedb55 100644
--- a/src/main/resources/env.properties
+++ b/src/main/resources/env.properties
@@ -2,20 +2,4 @@
 ## 開発環境用設定ファイル
 ##

-# JDBC接続ドライバクラス(DataSourceを直接使用する際の項目)
-nablarch.db.jdbcDriver=org.h2.Driver
-
-# JDBC接続URL(DataSourceを直接使用する際の項目)
-nablarch.db.url=jdbc:h2:./h2/db/rest_example
-
-# DB接続ユーザ名(DataSourceを直接使用する際の項目)
-nablarch.db.user=REST_EXAMPLE
-
-# DB接続パスワード(DataSourceを直接使用する際の項目)
-nablarch.db.password=REST_EXAMPLE
-
-# DBスキーマ名(DataSourceを直接使用する際の項目)
-nablarch.db.schema=PUBLIC
-
-# 最大プールサイズ(DataSourceを直接使用する際の項目)
-nablarch.db.maxPoolSize=5
+nablarch.connectionFactory.jndiResourceName=jdbc/myDataSource
diff --git a/src/main/resources/rest-component-configuration.xml b/src/main/resources/rest-component-configuration.xml
index a92b4f5..d780757 100644
--- a/src/main/resources/rest-component-configuration.xml
+++ b/src/main/resources/rest-component-configuration.xml
@@ -45,7 +45,7 @@
   </component>

   <!-- ダイアレクト設定 -->
-  <component name="dialect" class="nablarch.core.db.dialect.H2Dialect" />
+  <component name="dialect" class="nablarch.core.db.dialect.PostgreSQLDialect" />

   <!-- ハンドラキュー構成 -->
   <component name="webFrontController" class="nablarch.fw.web.servlet.WebFrontController">
diff --git a/src/test/resources/unit-test.properties b/src/test/resources/unit-test.properties
new file mode 100644
index 0000000..43bea7b
--- /dev/null
+++ b/src/test/resources/unit-test.properties
@@ -0,0 +1,17 @@
+# JDBC接続ドライバクラス(DataSourceを直接使用する際の項目)
+nablarch.db.jdbcDriver=org.postgresql.Driver
+
+# JDBC接続URL(DataSourceを直接使用する際の項目)
+nablarch.db.url=jdbc:postgresql://localhost:5432/rest_example?ApplicationName=rest_example_unit_test
+
+# DB接続ユーザ名(DataSourceを直接使用する際の項目)
+nablarch.db.user=rest_example_user
+
+# DB接続パスワード(DataSourceを直接使用する際の項目)
+nablarch.db.password=rest_example_password
+
+# DBスキーマ名(DataSourceを直接使用する際の項目)
+nablarch.db.schema=PUBLIC
+
+# 最大プールサイズ(DataSourceを直接使用する際の項目)
+nablarch.db.maxPoolSize=5
diff --git a/src/test/resources/unit-test.xml b/src/test/resources/unit-test.xml
index ef3816f..10d22ea 100644
--- a/src/test/resources/unit-test.xml
+++ b/src/test/resources/unit-test.xml
@@ -7,18 +7,41 @@
   <!-- プロダクションのコンポーネント設定を読み込む -->
   <import file="rest-component-configuration.xml"/>

+  <!-- 設定ファイル -->
+  <config-file file="unit-test.properties" />
+
   <!-- テスティングフレームワークの設定 -->
   <import file="nablarch/test/test-data.xml"/>
   <import file="nablarch/test/test-transaction.xml"/>
   <import file="nablarch/test/rest-request-test.xml"/>
   <import file="nablarch/core/date_test.xml"/>

+  <!-- データソース設定 -->
+  <component name="dataSource"
+             class="com.zaxxer.hikari.HikariDataSource" autowireType="None">
+    <property name="driverClassName" value="${nablarch.db.jdbcDriver}"/>
+    <property name="jdbcUrl"         value="${nablarch.db.url}"/>
+    <property name="username"        value="${nablarch.db.user}"/>
+    <property name="password"        value="${nablarch.db.password}"/>
+    <property name="maximumPoolSize" value="${nablarch.db.maxPoolSize}"/>
+  </component>
+
   <!-- DBの接続設定を定義する -->
   <component name="dbInfo" class="nablarch.test.core.db.GenericJdbcDbInfo">
     <property name="dataSource" ref="dataSource"/>
     <property name="schema" value="PUBLIC"/>
   </component>

+  <!-- データベース接続用設定 -->
+  <component name="connectionFactory"
+             class="nablarch.core.db.connection.BasicDbConnectionFactoryForDataSource">
+    <property name="dialect" ref="dialect" />
+    <property name="dataSource" ref="dataSource" />
+    <property name="statementFactory" ref="statementFactory" />
+    <property name="statementReuse" value="${nablarch.connectionFactory.statementReuse}" />
+    <property name="dbAccessExceptionFactory" ref="dbAccessExceptionFactory" />
+  </component>
+
   <!-- テスト用HttpServerにJetty12を定義する -->
   <component name="httpServerFactory" class="nablarch.fw.web.httpserver.HttpServerFactoryJetty12"/>

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