public interface ConfigDataSpec
Multiple data sources can be specified. All specified data sources will be merged together to form the final configuration data. If a given value exists in multiple data sources, the value from the last specified source will be used.
By default, if loading a data source fails, the exception will be thrown.
If desired, this behavior can be adjusted using onError(ratpack.func.Action)
.
For example:
import java.nio.file.Files;
import java.nio.file.Path;
import ratpack.config.ConfigData;
import ratpack.func.Action;
import ratpack.server.RatpackServer;
import ratpack.server.ServerConfig;
import ratpack.test.http.TestHttpClient;
import static org.junit.Assert.*;
public class Example {
public static void main(String[] args) throws Exception {
Path jsonFile = Files.createTempFile("optionalConfig", ".json");
Files.delete(jsonFile);
Path yamlFile = Files.createTempFile("mandatoryConfig", ".yaml");
try {
Files.write(yamlFile, "server:\n threads: 7\n port: 0".getBytes());
RatpackServer server = RatpackServer.of(spec -> {
ConfigData config = ConfigData.of(c -> c
.onError(Action.noop()).json(jsonFile)
.onError(Action.throwException()).yaml(yamlFile)
);
return spec
.serverConfig(config.getServerConfig())
.handler(registry ->
(ctx) -> ctx.render("threads:" + ctx.get(ServerConfig.class).getThreads())
);
});
server.start();
TestHttpClient httpClient = TestHttpClient.testHttpClient(server);
assertEquals("threads:7", httpClient.getText());
server.stop();
} finally {
Files.delete(yamlFile);
}
}
}
Modifier and Type | Method and Description |
---|---|
ConfigDataSpec |
add(ConfigSource configSource)
Adds a configuration source.
|
ConfigData |
build()
Builds the configuration data from this specification.
|
ConfigDataSpec |
configureObjectMapper(Action<ObjectMapper> action)
Configures the object mapper used for binding configuration data to arbitrary objects.
|
ConfigDataSpec |
env()
Adds a configuration source for environment variables starting with the prefix
"RATPACK_".
|
ConfigDataSpec |
env(EnvironmentParser environmentParser)
Adds a configuration source for environment variables using custom parsing logic.
|
ConfigDataSpec |
env(String prefix)
Adds a configuration source for environment variables starting with the specified prefix.
|
ConfigDataSpec |
env(String prefix,
Function<String,String> mapFunc)
Adds a configuration source for environment variables starting with the specified prefix.
|
ConfigDataSpec |
json(ByteSource byteSource)
Adds a configuration source for a JSON file.
|
ConfigDataSpec |
json(Path path)
Adds a configuration source for a JSON file.
|
default ConfigDataSpec |
json(String path)
Adds a configuration source for a JSON file.
|
ConfigDataSpec |
json(URL url)
Adds a configuration source for a JSON file.
|
ConfigDataSpec |
onError(Action<? super Throwable> errorHandler)
Sets the error handler that will be used for added configuration sources.
|
ConfigDataSpec |
props(ByteSource byteSource)
Adds a configuration source for a properties file.
|
ConfigDataSpec |
props(Map<String,String> map)
Adds a configuration source for a Map (flat key-value pairs).
|
ConfigDataSpec |
props(Path path)
Adds a configuration source for a properties file.
|
ConfigDataSpec |
props(Properties properties)
Adds a configuration source for a properties object.
|
default ConfigDataSpec |
props(String path)
Adds a configuration source for a properties file.
|
ConfigDataSpec |
props(URL url)
Adds a configuration source for a properties file.
|
ConfigDataSpec |
sysProps()
Adds a configuration source for system properties starting with the prefix "ratpack.".
|
ConfigDataSpec |
sysProps(String prefix)
Adds a configuration source for system properties starting with the specified prefix.
|
ConfigDataSpec |
yaml(ByteSource byteSource)
Adds a configuration source for a YAML file.
|
ConfigDataSpec |
yaml(Path path)
Adds a configuration source for a YAML file.
|
default ConfigDataSpec |
yaml(String path)
Adds a configuration source for a YAML file.
|
ConfigDataSpec |
yaml(URL url)
Adds a configuration source for a YAML file.
|
ConfigDataSpec configureObjectMapper(Action<ObjectMapper> action)
action
- an action to perform upon the object mapperConfigDataSpec add(ConfigSource configSource)
configSource
- the configuration source to addConfigData build()
ConfigDataSpec env()
ConfigDataSpec env(String prefix)
prefix
- the prefix which should be used to identify relevant environment variablesConfigDataSpec env(String prefix, Function<String,String> mapFunc)
prefix
- the prefix which should be used to identify relevant environment variablesmapFunc
- the function to transform segments into field namesConfigDataSpec env(EnvironmentParser environmentParser)
environmentParser
- the parser to use to interpret environment variablesConfigDataSpec json(ByteSource byteSource)
byteSource
- the source of the JSON dataConfigDataSpec json(Path path)
path
- the source of the JSON datadefault ConfigDataSpec json(String path)
path
- the path to the source of the JSON dataConfigDataSpec json(URL url)
url
- the source of the JSON dataConfigDataSpec props(ByteSource byteSource)
byteSource
- the source of the properties dataConfigDataSpec props(Path path)
path
- the source of the properties dataConfigDataSpec props(Properties properties)
properties
- the properties objectConfigDataSpec props(Map<String,String> map)
map
- the mapdefault ConfigDataSpec props(String path)
path
- the path to the source of the properties dataConfigDataSpec props(URL url)
url
- the source of the properties dataConfigDataSpec sysProps()
ConfigDataSpec sysProps(String prefix)
prefix
- the prefix which should be used to identify relevant system properties;
the prefix will be removed before loading the dataConfigDataSpec yaml(ByteSource byteSource)
byteSource
- the source of the YAML dataConfigDataSpec yaml(Path path)
path
- the source of the YAML datadefault ConfigDataSpec yaml(String path)
path
- the path to the source of the YAML dataConfigDataSpec yaml(URL url)
url
- the source of the YAML dataConfigDataSpec onError(Action<? super Throwable> errorHandler)
errorHandler
- the error handlerAction.noop()
,
Action.throwException()