Java Code Examples for org.eclipse.microprofile.config.spi.ConfigSource#getValue()

The following examples show how to use org.eclipse.microprofile.config.spi.ConfigSource#getValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultConfig.java    From microprofile-jwt-auth with Apache License 2.0 6 votes vote down vote up
@Override
public <T> Optional<T> getOptionalValue(String propertyName, Class<T> propertyType) {
    Optional<T> value = Optional.empty();
    for (ConfigSource cs : sources) {
        String svalue = cs.getValue(propertyName);
        if(svalue == null) {
            value = Optional.empty();
        }
        else if(propertyType.isAssignableFrom(String.class)) {
            value = Optional.of(propertyType.cast(svalue));
            break;
        }
        else {
            Converter<T> converter = converters.get(propertyType);
            if(converter != null) {
                value = Optional.of(converter.convert(svalue));
            }
            else {
                System.err.printf("Failed to find Converter for: %s of type: %s\n", propertyName, propertyType);
            }
            break;
        }
    }
    return value;
}
 
Example 2
Source File: HammockConfigSourceProvider.java    From hammock with Apache License 2.0 6 votes vote down vote up
@Override
public List<ConfigSource> getConfigSources(ClassLoader classLoader) {
    List<ConfigSource> configSources = new ArrayList<>();
    ConfigSource cli = CLIPropertySource.parseMainArgs(Bootstrap.ARGS);
    configSources.add(cli);
    String propertyValue = cli.getValue("hammock.external.config");
    if(propertyValue != null) {
        try {
            URL url = Paths.get(propertyValue).toUri().toURL();
            configSources.add(new PropertyFileConfigSource(url));
        } catch (MalformedURLException e) {
            throw new RuntimeException("Unable to load "+propertyValue,e);
        }
    }

    for(String prop : defaultPropertyFiles) {
        configSources.addAll(new PropertyFileConfigSourceProvider(prop, true, classLoader).getConfigSources(classLoader));
    }

    return configSources;
}
 
Example 3
Source File: CLIPropertySourceTest.java    From hammock with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReadPropertiesFromDefinedLocation() {
    String[] args = {"--key=value","-s","j"};
    ConfigSource cliPropertySource = CLIPropertySource.parseMainArgs(args);
    String key = cliPropertySource.getValue("key");
    assertEquals(key, "value");
    String s = cliPropertySource.getValue("s");
    assertEquals("j", s);
}