Java Code Examples for org.eclipse.microprofile.config.spi.Converter#convert()

The following examples show how to use org.eclipse.microprofile.config.spi.Converter#convert() . 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: SmallRyeConfig.java    From smallrye-config with Apache License 2.0 6 votes vote down vote up
public <T> T getValue(String name, Converter<T> converter) {
    String value = getRawValue(name);
    final T converted;
    if (value != null) {
        converted = converter.convert(value);
    } else {
        try {
            converted = converter.convert("");
        } catch (IllegalArgumentException ignored) {
            throw ConfigMessages.msg.propertyNotFound(name);
        }
    }
    if (converted == null) {
        throw ConfigMessages.msg.propertyNotFound(name);
    }
    return converted;
}
 
Example 2
Source File: DefaultConfig.java    From microprofile-jwt-auth with Apache License 2.0 6 votes vote down vote up
/**
 * Use the {@link Converter}s registered with the config to convert a String value to a target property type.
 * @param svalue - the string value representation of the property
 * @param propertyType - the desired Java type of the property
 * @return the converted value
 * @throws TypeNotPresentException if there is no registered Converter
 */
public <T> T convertValue(String svalue, Class<T> propertyType) {
    T value = null;
    if(propertyType.isAssignableFrom(String.class)) {
        value = propertyType.cast(svalue);
    }
    else {
        Converter<T> converter = converters.get(propertyType);
        if(converter != null) {
            value = converter.convert(svalue);
        }
        else {
            System.err.printf("Failed to find Converter for type: %s\n", propertyType);
            throw new TypeNotPresentException(propertyType.getTypeName(), null);
        }
    }
    return value;
}
 
Example 3
Source File: ConfigProducerUtil.java    From smallrye-config with Apache License 2.0 5 votes vote down vote up
public static <T> T getValue(InjectionPoint injectionPoint, Config config) {
    String name = getName(injectionPoint);
    if (name == null) {
        return null;
    }
    final SmallRyeConfig src = (SmallRyeConfig) config;
    Converter<T> converter = resolveConverter(injectionPoint, src);
    String rawValue = getRawValue(name, src);
    if (rawValue == null) {
        rawValue = getDefaultValue(injectionPoint);
    }
    T converted;
    if (rawValue == null) {
        // convert an empty value
        try {
            converted = converter.convert("");
        } catch (IllegalArgumentException ignored) {
            throw InjectionMessages.msg.propertyNotFound(name);
        }
    } else {
        converted = converter.convert(rawValue);
    }
    if (converted == null) {
        throw InjectionMessages.msg.propertyNotFound(name);
    }
    return converted;
}