public interface ConfigDataBuilder
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);
}
}
}
ConfigData.builder()
Modifier and Type | Field and Description |
---|---|
static String |
DEFAULT_ENV_PREFIX |
static String |
DEFAULT_PROP_PREFIX |
Modifier and Type | Method and Description |
---|---|
ConfigDataBuilder |
add(ConfigSource configSource)
Adds a configuration source.
|
ConfigData |
build()
Creates the config data, based on the state of this builder.
|
ConfigDataBuilder |
configureObjectMapper(Action<ObjectMapper> action)
Configures the object mapper used for binding configuration data to arbitrary objects.
|
ConfigDataBuilder |
env()
Adds a configuration source for environment variables starting with the prefix "RATPACK_".
|
ConfigDataBuilder |
env(EnvironmentParser environmentParser)
Adds a configuration source for environment variables using custom parsing logic.
|
ConfigDataBuilder |
env(String prefix)
Adds a configuration source for environment variables starting with the specified prefix.
|
ConfigDataBuilder |
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.
|
default ConfigDataBuilder |
jacksonModules(Module... modules)
Adds
Jackson modules to the object mapper. |
ConfigDataBuilder |
json(ByteSource byteSource)
Adds a configuration source for a JSON file.
|
ConfigDataBuilder |
json(Path path)
Adds a configuration source for a JSON file.
|
ConfigDataBuilder |
json(String path)
Adds the JSON file at the given path as a configuration source.
|
ConfigDataBuilder |
json(URL url)
Adds a configuration source for a JSON file.
|
ConfigDataBuilder |
onError(Action<? super Throwable> errorHandler)
Sets the error all that will be used for added configuration sources.
|
ConfigDataBuilder |
props(ByteSource byteSource)
Adds a configuration source for a properties file.
|
ConfigDataBuilder |
props(Map<String,String> map)
Adds a configuration source for a Map (flat key-value pairs).
|
ConfigDataBuilder |
props(Path path)
Adds a configuration source for a properties file.
|
ConfigDataBuilder |
props(Properties properties)
Adds a configuration source for a properties object.
|
ConfigDataBuilder |
props(String path)
Adds the properties file at the given path as a configuration source.
|
ConfigDataBuilder |
props(URL url)
Adds a configuration source for a properties file.
|
ConfigDataBuilder |
sysProps()
Adds a configuration source for system properties starting with the prefix "ratpack.".
|
ConfigDataBuilder |
sysProps(String prefix)
Adds a configuration source for system properties starting with the specified prefix.
|
ConfigDataBuilder |
yaml(ByteSource byteSource)
Adds a configuration source for a YAML file.
|
ConfigDataBuilder |
yaml(Path path)
Adds a configuration source for a YAML file.
|
ConfigDataBuilder |
yaml(String path)
Adds the YAML file at the given path as a configuration source.
|
ConfigDataBuilder |
yaml(URL url)
Adds a configuration source for a YAML file.
|
static final String DEFAULT_ENV_PREFIX
static final String DEFAULT_PROP_PREFIX
ConfigData build()
ConfigDataBuilder configureObjectMapper(Action<ObjectMapper> action)
action
- an action to perform upon the object mapperthis
ConfigDataBuilder add(ConfigSource configSource)
configSource
- the configuration source to addthis
ConfigDataBuilder env()
The prefix will be removed before loading the data. The environment variable name is split into per-object segments using double underscore as an object boundary. Segments are transformed into camel-case field names using a single underscore as a word boundary.
ConfigDataBuilder env(String prefix)
prefix
- the prefix which should be used to identify relevant environment variablesConfigDataBuilder 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 namesConfigDataBuilder env(EnvironmentParser environmentParser)
environmentParser
- the parser to use to interpret environment variablesthis
ConfigDataBuilder json(ByteSource byteSource)
byteSource
- the source of the JSON datathis
ConfigDataBuilder json(Path path)
path
- the source of the JSON datathis
ConfigDataBuilder json(String path)
The default implementation of ConfigDataBuilder
will resolve the given path relative to the actual file system root.
Alternative implementations, such as ServerConfigBuilder.json(String)
may resolve the file location differently.
path
- the path to the source of the JSON datathis
ConfigDataBuilder json(URL url)
url
- the source of the JSON datathis
ConfigDataBuilder props(ByteSource byteSource)
byteSource
- the source of the properties datathis
ConfigDataBuilder props(Path path)
path
- the source of the properties datathis
ConfigDataBuilder props(Properties properties)
properties
- the properties objectthis
ConfigDataBuilder 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
.builder()
.props(ImmutableMap.of("server.port", "5060"))
.sysProps()
.build();
assertEquals(5060, serverConfig.getPort());
}
}
map
- the mapthis
ConfigDataBuilder props(String path)
The default implementation of ConfigDataBuilder
will resolve the given path relative to the actual file system root.
Alternative implementations, such as ServerConfigBuilder.props(String)
may resolve the file location differently.
path
- the path to the source of the properties datathis
ConfigDataBuilder props(URL url)
url
- the source of the properties datathis
ConfigDataBuilder sysProps()
this
ConfigDataBuilder sysProps(String prefix)
prefix
- the prefix which should be used to identify relevant system properties;
the prefix will be removed before loading the datathis
ConfigDataBuilder yaml(ByteSource byteSource)
byteSource
- the source of the YAML datathis
ConfigDataBuilder yaml(Path path)
path
- the source of the YAML datathis
ConfigDataBuilder yaml(String path)
The default implementation of ConfigDataBuilder
will resolve the given path relative to the actual file system root.
Alternative implementations, such as ServerConfigBuilder.yaml(String)
may resolve the file location differently.
path
- the path to the source of the YAML datathis
ConfigDataBuilder yaml(URL url)
url
- the source of the YAML datathis
ConfigDataBuilder onError(Action<? super Throwable> errorHandler)
errorHandler
- the error allthis
Action.noop()
,
Action.throwException()
ObjectMapper getObjectMapper()
default ConfigDataBuilder jacksonModules(Module... modules)
Jackson modules
to the object mapper.modules
- the modules to addObjectMapper.registerModule(Module)
ImmutableList<ConfigSource> getConfigSources()
Add sources via add(ConfigSource)
.