CordaでConfigファイルを読み込む

どーも、のぶこふです。

QiitaでCorda記事はなかなか伸びないですね。
Hyperledger Fabricの方が需要はありそうな気がしますが、、、いや、私の力不足です。
めげずにもう少し続けていこうと思います。

今回は、Configファイルを読み込む処理を説明します。

公式では[このページ](https://github.com/corda/corda/tree/release/4.1/samples/cordapp-configuration)になります。

# やるよ！！
```terminal:ディレクトリ構成（略版）
cordapp-template-kotlin
 +-workflows
 |  +-src
 |    +-main
 |    |  +-kotlin/com/template/flows/MyFlows.kt
 |    +-config.conf
 +-build.gradle 
```
```kotlin:MyFlows.kt（一部抜粋）
@StartableByRPC
class GetStringConfigFlow(private val configKey: String) : FlowLogic<String>() {
    object READING : ProgressTracker.Step("Reading config")
    override val progressTracker = ProgressTracker(READING)

    @Suspendable
    override fun call(): String {
        progressTracker.currentStep = READING
        // configファイル（↓の「config.conf」）の読み込み
        val config = serviceHub.getAppContext().config
        // 引数で指定したKey(someStringValue)からValue(hello world)を取得する
        return config.getString(configKey)
    }
}
```
```config:config.conf
someStringValue=hello world
someIntValue=1
nested: {
   value: a string
}
```
```gradle:build.gradle(一部抜粋)、nodeに　cordapp(project(〜){〜〜}　を追加します
task deployNodes(type: net.corda.plugins.Cordform, dependsOn: ['jar']) {
    nodeDefaults {
        projectCordapp {
            deploy = false
        }
        cordapp project(':contracts')
        cordapp project(':workflows')
    }
    node {
        name "O=Notary,L=London,C=GB"
        notary = [validating : false]
        p2pPort 10002
        rpcSettings {
            address("localhost:10003")
            adminAddress("localhost:10043")
        }
    }
    node {
        name "O=PartyA,L=London,C=GB"
        p2pPort 10005
        rpcSettings {
            address("localhost:10006")
            adminAddress("localhost:10046")
        }
        cordapp (project(':workflows')){
            config project.file("src/config.conf")
        }
        rpcUsers = [[ user: "user1", "password": "test", "permissions": ["ALL"]]]
    }
```

基本はコピペでOKかと思います。

# 実行するよ！！！！
```terminal:「中身の確認をします！！」「中身はほぼテンプレのままだ。気にするな！！」
$ pwd
/cordapp-template-kotlin
$ ll
total 88
-rw-r--r--  1 nobkovskii  staff   579  6 13 16:32 LICENCE
-rw-r--r--  1 nobkovskii  staff  4798  6 13 16:32 README.md
-rw-r--r--  1 nobkovskii  staff   232  6 13 16:32 TRADEMARK
drwxr-xr-x  7 nobkovskii  staff   224  6 28 15:51 build
-rw-r--r--  1 nobkovskii  staff  4487  6 28 15:50 build.gradle
drwxr-xr-x  5 nobkovskii  staff   160  6 27 12:07 clients
drwxr-xr-x  4 nobkovskii  staff   128  6 13 16:32 config
drwxr-xr-x  5 nobkovskii  staff   160  6 28 15:51 contracts
drwxr-xr-x  3 nobkovskii  staff    96  6 13 16:32 gradle
-rw-r--r--  1 nobkovskii  staff    65  6 13 16:32 gradle.properties
-rwxr-xr-x  1 nobkovskii  staff  5296  6 13 16:32 gradlew
-rw-r--r--  1 nobkovskii  staff  2176  6 13 16:32 gradlew.bat
drwxr-xr-x  3 nobkovskii  staff    96  6 13 16:34 logs
-rw-r--r--  1 nobkovskii  staff    57  6 13 16:32 settings.gradle
drwxr-xr-x  5 nobkovskii  staff   160  6 28 15:51 workflows
```
```terminal:「デプロイを開始します！！」「うっ！！？？！！？」「ぐぎぎぎぎぎぎぎ、ぷぎゃーーーー」
$ ./gradlew clean deployNodes
```
```terminal:「PartyAのNodeへ移動し起動します！！」「きゅいいいいいぃぃぃぃいぃいいぃいんっっ！！！！」「ぐおんぐおんぐおんぐおん！！！」「キュインキュインキュインキュイン！！！」「ガチーンッ！！ガチーンッ！！」「DLT（分散台帳技術）Corda、起動確認しましたッ！！！」

$ cd /cordapp-template-kotlin/build/nodes/PartyA && java -jar corda.jar

   ______               __
  / ____/     _________/ /___ _
 / /     __  / ___/ __  / __ `/         Ah, Friday.
/ /___  /_/ / /  / /_/ / /_/ /          My second favourite F-word.
\____/     /_/   \__,_/\__,_/
```
```terminal:「コマンドを確認するっ！！flow　list（フローリストッ！！）」
Fri Jun 28 16:09:26 JST 2019>>> flow list
com.template.flows.GetStringConfigFlow
〜他にもたくさんあるよ〜
```
```terminal:「先程定義したGetStringConfigFlowを実行する！！」「引数を設定し、someStringValueを取得するぞ」
Fri Jun 28 16:34:10 JST 2019>>> start GetStringConfigFlow configKey: someStringValue                   

 ✅   Starting
 ✅   Reading config
➡️   Done
Flow completed with result: hello world
```
```terminal:「次いでsomeIntValueだっ！！」
Fri Jun 28 16:38:41 JST 2019>>> start GetStringConfigFlow configKey: someIntValue                      

 ✅   Starting
 ✅   Reading config
➡️   Done
Flow completed with result: 1
```
```terminal:「コレが最後。構造体からも取得が可能だーーー」
Fri Jun 28 16:38:54 JST 2019>>> start GetStringConfigFlow configKey: nested.value                      

 ✅   Starting
 ✅   Reading config
➡️   Done
Flow completed with result: a string
```

# ( ´ー｀)ﾌｩｰ．．．
雰囲気で察してください。

# Errorとか
パス間違いやタイポするとエラーになります。

```terminal:「うわあぁあああぁぁああああああああああああぁぁぁぁああぁぁぁああああーーーーーーーッッッ！！」
Fri Jun 28 16:44:11 JST 2019>>> start GetStringConfigFlow configKey: noPath

 ✅   Starting
 ✅   Starting
➡️   Reading config
🚫   Done
 ☠   Cordapp configuration is incorrect due to exception
[ERROR] 16:44:21+0900 [Node thread-1] proxies.ExceptionSerialisingRpcOpsProxy.log - Error during RPC invocation [errorCode=1ctylvw, moreInformationAt=https://errors.corda.net/OS/4.0/1ctylvw] {actor_id=internalShell, actor_owning_identity=O=PartyA, L=London, C=GB, actor_store_id=NODE_CONFIG, fiber-id=10000011, flow-id=ed669922-f809-459d-986f-b8f97b7299fa, invocation_id=683ccaa4-2061-4c1b-9122-552939d11009, invocation_timestamp=2019-06-28T07:44:21.676Z, origin=internalShell, session_id=d9c14f96-d2f0-4d50-a6ca-cf2166cecdd5, session_timestamp=2019-06-28T06:52:38.179Z, thread-id=154}
[ERROR] 16:44:21+0900 [Node thread-1] proxies.ExceptionSerialisingRpcOpsProxy.log - Error during RPC invocation [errorCode=1ctylvw, moreInformationAt=https://errors.corda.net/OS/4.0/1ctylvw] {actor_id=internalShell, actor_owning_identity=O=PartyA, L=London, C=GB, actor_store_id=NODE_CONFIG, fiber-id=10000011, flow-id=ed669922-f809-459d-986f-b8f97b7299fa, invocation_id=683ccaa4-2061-4c1b-9122-552939d11009, invocation_timestamp=2019-06-28T07:44:21.676Z, origin=internalShell, session_id=d9c14f96-d2f0-4d50-a6ca-cf2166cecdd5, session_timestamp=2019-06-28T06:52:38.179Z, thread-id=154}
[ERROR] 16:44:21+0900 [Node thread-1] proxies.ExceptionSerialisingRpcOpsProxy.log - Error during RPC invocation [errorCode=1ctylvw, moreInformationAt=https://errors.corda.net/OS/4.0/1ctylvw] {actor_id=internalShell, actor_owning_identity=O=PartyA, L=London, C=GB, actor_store_id=NODE_CONFIG, fiber-id=10000011, flow-id=ed669922-f809-459d-986f-b8f97b7299fa, invocation_id=683ccaa4-2061-4c1b-9122-552939d11009, invocation_timestamp=2019-06-28T07:44:21.676Z, origin=internalShell, session_id=d9c14f96-d2f0-4d50-a6ca-cf2166cecdd5, session_timestamp=2019-06-28T06:52:38.179Z, thread-id=154}
[ERROR] 16:44:21+0900 [Node thread-1] proxies.ExceptionSerialisingRpcOpsProxy.log - Error during RPC invocation [errorCode=1ctylvw, moreInformationAt=https://errors.corda.net/OS/4.0/1ctylvw] {actor_id=internalShell, actor_owning_identity=O=PartyA, L=London, C=GB, actor_store_id=NODE_CONFIG, fiber-id=10000011, flow-id=ed669922-f809-459d-986f-b8f97b7299fa, invocation_id=683ccaa4-2061-4c1b-9122-552939d11009, invocation_timestamp=2019-06-28T07:44:21.676Z, origin=internalShell, session_id=d9c14f96-d2f0-4d50-a6ca-cf2166cecdd5, session_timestamp=2019-06-28T06:52:38.179Z, thread-id=154}
[ERROR] 16:44:21+0900 [Node thread-1] proxies.ExceptionSerialisingRpcOpsProxy.log - Error during RPC invocation [errorCode=1ctylvw, moreInformationAt=https://errors.corda.net/OS/4.0/1ctylvw] {actor_id=internalShell, actor_owning_identity=O=PartyA, L=London, C=GB, actor_store_id=NODE_CONFIG, fiber-id=10000011, flow-id=ed669922-f809-459d-986f-b8f97b7299fa, invocation_id=683ccaa4-2061-4c1b-9122-552939d11009, invocation_timestamp=2019-06-28T07:44:21.676Z, origin=internalShell, session_id=d9c14f96-d2f0-4d50-a6ca-cf2166cecdd5, session_timestamp=2019-06-28T06:52:38.179Z, thread-id=154}
[ERROR] 16:44:21+0900 [Node thread-1] proxies.ExceptionSerialisingRpcOpsProxy.log - Error during RPC invocation [errorCode=1ctylvw, moreInformationAt=https://errors.corda.net/OS/4.0/1ctylvw] {actor_id=internalShell, actor_owning_identity=O=PartyA, L=London, C=GB, actor_store_id=NODE_CONFIG, fiber-id=10000011, flow-id=ed669922-f809-459d-986f-b8f97b7299fa, invocation_id=683ccaa4-2061-4c1b-9122-552939d11009, invocation_timestamp=2019-06-28T07:44:21.676Z, origin=internalShell, session_id=d9c14f96-d2f0-4d50-a6ca-cf2166cecdd5, session_timestamp=2019-06-28T06:52:38.179Z, thread-id=154}
[ERROR] 16:44:21+0900 [Node thread-1] proxies.ExceptionSerialisingRpcOpsProxy.log - Error during RPC invocation [errorCode=1ctylvw, moreInformationAt=https://errors.corda.net/OS/4.0/1ctylvw] {actor_id=internalShell, actor_owning_identity=O=PartyA, L=London, C=GB, actor_store_id=NODE_CONFIG, fiber-id=10000011, flow-id=ed669922-f809-459d-986f-b8f97b7299fa, invocation_id=683ccaa4-2061-4c1b-9122-552939d11009, invocation_timestamp=2019-06-28T07:44:21.676Z, origin=internalShell, session_id=d9c14f96-d2f0-4d50-a6ca-cf2166cecdd5, session_timestamp=2019-06-28T06:52:38.179Z, thread-id=154}
[ERROR] 16:44:21+0900 [Node thread-1] proxies.ExceptionSerialisingRpcOpsProxy.log - Error during RPC invocation [errorCode=1ctylvw, moreInformationAt=https://errors.corda.net/OS/4.0/1ctylvw] {actor_id=internalShell, actor_owning_identity=O=PartyA, L=London, C=GB, actor_store_id=NODE_CONFIG, fiber-id=10000011, flow-id=ed669922-f809-459d-986f-b8f97b7299fa, invocation_id=683ccaa4-2061-4c1b-9122-552939d11009, invocation_timestamp=2019-06-28T07:44:21.676Z, origin=internalShell, session_id=d9c14f96-d2f0-4d50-a6ca-cf2166cecdd5, session_timestamp=2019-06-28T06:52:38.179Z, thread-id=154}
```

# おわり
プレミアムフライデーということで、今回は記事の雰囲気を変えてみました。

今回は以上です。
ありがとうございました。