use of io.helidon.config.Config in project helidon by oracle.
the class DeserializationExample method main.
/**
* Executes the example.
*
* @param args arguments
*/
public static void main(String... args) {
Config config = Config.create(ConfigSources.classpath("application.conf"));
AppConfig appConfig = config.get("app").as(AppConfig.class).get();
System.out.println(appConfig);
assert appConfig.getGreeting().equals("Hello");
assert appConfig.getPageSize() == 20;
assert appConfig.getBasicRange().size() == 2;
assert appConfig.getBasicRange().get(0) == -20;
assert appConfig.getBasicRange().get(1) == 20;
}
use of io.helidon.config.Config in project helidon by oracle.
the class FactoryMethodExample method main.
/**
* Executes the example.
*
* @param args arguments
*/
public static void main(String... args) {
Config config = Config.create(ConfigSources.classpath("application.conf"));
AppConfig appConfig = config.get("app").as(AppConfig.class).get();
System.out.println(appConfig);
assert appConfig.getGreeting().equals("Hello");
assert appConfig.getPageSize() == 20;
assert appConfig.getBasicRange().size() == 2;
assert appConfig.getBasicRange().get(0) == -20;
assert appConfig.getBasicRange().get(1) == 20;
}
use of io.helidon.config.Config in project helidon by oracle.
the class Main method corsSupportForGreeting.
private static CorsSupport corsSupportForGreeting(Config config) {
// The default CorsSupport object (obtained using CorsSupport.create()) allows sharing for any HTTP method and with any
// origin. Using CorsSupport.create(Config) with a missing config node yields a default CorsSupport, which might not be
// what you want. This example warns if either expected config node is missing and then continues with the default.
Config restrictiveConfig = config.get("restrictive-cors");
if (!restrictiveConfig.exists()) {
Logger.getLogger(Main.class.getName()).warning("Missing restrictive config; continuing with default CORS support");
}
CorsSupport.Builder corsBuilder = CorsSupport.builder();
// Use possible overrides first.
config.get("cors").ifExists(c -> {
Logger.getLogger(Main.class.getName()).info("Using the override configuration");
corsBuilder.mappedConfig(c);
});
corsBuilder.config(// restricted sharing for PUT, DELETE
restrictiveConfig).addCrossOrigin(// open sharing for other methods
CrossOriginConfig.create()).build();
return corsBuilder.build();
}
use of io.helidon.config.Config in project helidon by oracle.
the class AsSupplierExample method run.
/**
* Executes the example.
*/
public void run() {
Config config = Config.create(file("conf/dev.yaml").optional().changeWatcher(FileSystemWatcher.create()), file("conf/config.yaml").optional().pollingStrategy(regular(Duration.ofSeconds(2))), classpath("default.yaml"));
// greeting.get() always return up-to-date value
final Supplier<String> greeting = config.get("app.greeting").asString().supplier();
// name.get() always return up-to-date value
final Supplier<String> name = config.get("app.name").asString().supplier();
// first greeting
printIfChanged(greeting.get() + " " + name.get() + ".");
// use same Supplier instances to get up-to-date value
executor.scheduleWithFixedDelay(() -> printIfChanged(greeting.get() + " " + name.get() + "."), // check every 1 second for changes
0, 1, TimeUnit.SECONDS);
}
use of io.helidon.config.Config in project helidon by oracle.
the class WithSourcesExample method main.
/**
* Executes the example.
*
* @param args arguments
*/
public static void main(String... args) {
/*
Creates a config source composed of following sources:
- conf/dev.yaml - developer specific configuration, should not be placed in VCS;
- conf/config.yaml - deployment dependent configuration, for example prod, stage, etc;
- default.yaml - application default values, loaded form classpath;
with a filter which convert values with keys ending with "level" to upper case
*/
Config config = Config.builder(file("conf/dev.yaml").optional(), file("conf/config.yaml").optional(), classpath("default.yaml")).addFilter((key, stringValue) -> key.name().equals("level") ? stringValue.toUpperCase() : stringValue).build();
// Environment type, from dev.yaml:
ConfigValue<String> env = config.get("meta.env").asString();
env.ifPresent(e -> System.out.println("Environment: " + e));
assert env.get().equals("DEV");
// Default value (default.yaml): Config Sources Example
String appName = config.get("app.name").asString().get();
System.out.println("Name: " + appName);
assert appName.equals("Config Sources Example");
// Page size, from config.yaml: 10
int pageSize = config.get("app.page-size").asInt().get();
System.out.println("Page size: " + pageSize);
assert pageSize == 10;
// Applied filter (uppercase logging level), from dev.yaml: finest -> FINEST
String level = config.get("component.audit.logging.level").asString().get();
System.out.println("Level: " + level);
assert level.equals("FINE");
}
Aggregations