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.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 -> {
ServerConfig serverConfig = ServerConfig
.embedded()
.onError(Action.noop()).json(jsonFile)
.onError(Action.throwException()).yaml(yamlFile)
.build();
spec
.serverConfig(serverConfig)
.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.
|
ImmutableList<ConfigSource> |
getConfigSources()
Returns the config sources used for configuration binding.
|
ObjectMapper |
getObjectMapper()
Returns the object mapper used for configuration binding.
|
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 mapperthis
ConfigDataSpec add(ConfigSource configSource)
configSource
- the configuration source to addthis
ConfigData 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 variablesthis
ConfigDataSpec json(ByteSource byteSource)
byteSource
- the source of the JSON datathis
ConfigDataSpec json(Path path)
path
- the source of the JSON datathis
default ConfigDataSpec json(String path)
path
- the path to the source of the JSON datathis
ConfigDataSpec json(URL url)
url
- the source of the JSON datathis
ConfigDataSpec props(ByteSource byteSource)
byteSource
- the source of the properties datathis
ConfigDataSpec props(Path path)
path
- the source of the properties datathis
ConfigDataSpec props(Properties properties)
properties
- the properties objectthis
ConfigDataSpec props(Map<String,String> map)
import com.google.common.collect.ImmutableMap;
import ratpack.config.ConfigData;
import ratpack.server.ServerConfig;
import static org.junit.Assert.*;
public class Example {
public static void main(String[] args) throws Exception {
ServerConfig serverConfig = ServerConfig
.noBaseDir()
.props(ImmutableMap.of("server.port", "5060"))
.sysProps()
.build();
assertEquals(5060, serverConfig.getPort());
}
}
map
- the mapthis
default ConfigDataSpec props(String path)
path
- the path to the source of the properties datathis
ConfigDataSpec props(URL url)
url
- the source of the properties datathis
ConfigDataSpec sysProps()
this
ConfigDataSpec sysProps(String prefix)
prefix
- the prefix which should be used to identify relevant system properties;
the prefix will be removed before loading the datathis
ConfigDataSpec yaml(ByteSource byteSource)
byteSource
- the source of the YAML datathis
ConfigDataSpec yaml(Path path)
path
- the source of the YAML datathis
default ConfigDataSpec yaml(String path)
path
- the path to the source of the YAML datathis
ConfigDataSpec yaml(URL url)
url
- the source of the YAML datathis
ConfigDataSpec onError(Action<? super Throwable> errorHandler)
errorHandler
- the error handlerthis
Action.noop()
,
Action.throwException()
ObjectMapper getObjectMapper()
ImmutableList<ConfigSource> getConfigSources()