ConfigSlurper
ConfigSlurper is a utility class within Groovy for writing properties file like scripts for performing configuration. Unlike regular Java properties files ConfigSlurper scripts support native Java types and are structured like a tree. |
ConfigSlurperは、プロパティファイルに似た設定用スクリプトを書くためのGroovyのユーティリティクラスです。通常のJavaのプロパティファイルとは違い、ConfigSlurperスクリプトはネイティブなJavaの型をサポートし、ツリー状に構造化されます。
Below is an example of how you could configure Log4j with a ConfigSlurper script: |
以下はConfigSlurperスクリプトでLog4jを設定する例です:
log4j.appender.stdout = "org.apache.log4j.ConsoleAppender"
log4j.appender."stdout.layout"="org.apache.log4j.PatternLayout"
log4j.rootLogger="error,stdout"
log4j.logger.org.springframework="info,stdout"
log4j.additivity.org.springframework=false
|
To load this into a readable config you can do: |
これを読取り可能な設定としてロードするためには次のようにします:
def config = new ConfigSlurper().parse(new File('myconfig.groovy').toURL())
assert "info,stdout" == config.log4j.logger.org.springframework
assert false == config.log4j.additivity.org.springframework
|
As you can see from the example above you can navigate the config using dot notation and the return values are Java types like strings and booleans. |
上の例でわかるようにドット記法を使って設定値を読取ることができ、そのリターン値は文字列やbooleanのようなJava型になります。
You can also use scoping in config scripts to avoid repeating yourself. So the above config could also be written as: |
記述の繰り返しを避けるため、設定スクリプトの中でスコープを利用することも可能です。つまり、上記の設定は次のようにも書けます:
log4j {
appender.stdout = "org.apache.log4j.ConsoleAppender"
appender."stdout.layout"="org.apache.log4j.PatternLayout"
rootLogger="error,stdout"
logger {
org.springframework="info,stdout"
}
additivity {
org.springframework=false
}
}
|
Javaプロパティファイルとの相互変換
Converting to and from Java properties files |
|
You can convert ConfigSlurper configs to and from Java properties files. For example: |
ConfigSlurperの設定はJavaプロパティファイルとの相互変換が可能です。例えば:
java.util.Properties props = // どこかからロードする
def config = new ConfigSlurper().parse(props)
props = config.toProperties()
|
設定のマージ
You can merge config objects so if you have multiple config files and want to create one central config object you can do: |
設定オブジェクトはマージ可能です。複数の設定ファイルから一つの中央設定オブジェクトを作る場合は次のようにできます:
def config1 = new ConfigSlurper().parse(..)
def config2 = new ConfigSlurper().parse(..)
config1 = config1.merge(config2)
|
設定のシリアライズ
Serializing a configuration to disk |
You can serialize a config object to disk. Each config object implements the groovy.lang.Writable interface that allows you to write out the config to any java.io.Writer: |
設定オブジェクトはシリアライズしてディスクに書き出すことができます。各設定オブジェクトは groovy.lang.Writable インタフェースを実装しているので、どのような java.io.Writer に対してもその設定を書き出すことが可能です:
def config = new ConfigSlurper().parse(..)
new File("..").withWriter { writer ->
config.writeTo(writer)
}
|
特殊な "環境定義" 構成
Special "environments" Configuration |
ConfigSlurperクラスには「環境」パラメータ引数に持つ、デフォルトコンストラクタと異なるコンストラクタが存在します。
この特別なコンストラクタは環境定義ファイルと連携して動きます。
既存の環境定義設定は環境定義クロージャの定義によって上書きすることができます。
複数の関連した構成が同じファイルに保存することも可能です。
|
The ConfigSlurper class has a special constructor other than the default constructor that takes an "environment" parameter.
This special constructor works in concert with a property setting called environments.
This allows a default setting to exist in the property file that can be superceded by a setting in the appropriate environments closure.
This allows multiple related configurations to be stored in the same file. |
構成の設定ファイル例
Given this groovy property file: |
Sample.groovy
sample {
foo = "default_foo"
bar = "default_bar"
}
environments {
development {
sample {
foo = "dev_foo"
}
}
test {
sample {
bar = "test_bar"
}
}
}
|
この構成を実行するコードは下記となります:
Here is the demo code that exercises this configuration |
def config = new ConfigSlurper("development").parse(new File('Sample.groovy').toURL())
assert config.sample.foo == "dev_foo"
assert config.sample.bar == "default_bar"
config = new ConfigSlurper("test").parse(new File('Sample.groovy').toURL())
assert config.sample.foo == "default_foo"
assert config.sample.bar == "test_bar"
|
追記:
環境定義のクロージャは直接解析されるわけでありません。
特別な環境下で使われる場合を除いて、このコンストラクタのクロージャは無視されます
|
Note: the environments closure is not directly parsable. Without using the special environment constructor the closure is ignored. |
環境定義値のコンストラクタには構成ファイルを利用できます。
下記のような構成を設定する事もできます:
|
The value of the environment constructor is also available in the configuration file, allowing you to build the configuration like this: |
switch (environment) {
case 'development':
baseUrl = "devServer/"
break
case 'test':
baseUrl = "testServer/"
break
default:
baseUrl = "localhost/"
}
|
さらに詳しい情報
Using Groovy ConfigSlurper to Configure Spring Beans