Search in sources :

Example 1 with PropertyPlaceholderResolver

use of io.micronaut.context.env.PropertyPlaceholderResolver in project micronaut-core by micronaut-projects.

the class AbstractEnvironmentAnnotationMetadata method stringValues.

@NonNull
@Override
public String[] stringValues(@NonNull Class<? extends Annotation> annotation, @NonNull String member) {
    Environment environment = getEnvironment();
    if (environment != null) {
        PropertyPlaceholderResolver resolver = environment.getPlaceholderResolver();
        Function<Object, Object> valueMapper = val -> {
            String[] values;
            if (val instanceof CharSequence) {
                values = new String[] { val.toString() };
            } else if (val instanceof String[]) {
                values = (String[]) val;
            } else {
                return null;
            }
            String[] resolvedValues = Arrays.copyOf(values, values.length);
            boolean expandValues = false;
            for (int i = 0; i < values.length; i++) {
                String value = values[i];
                if (value != null && value.contains(resolver.getPrefix())) {
                    value = resolver.resolveRequiredPlaceholders(value);
                    if (value.contains(",")) {
                        expandValues = true;
                    }
                }
                resolvedValues[i] = value;
            }
            if (expandValues) {
                return Stream.of(resolvedValues).flatMap(s -> {
                    if (s.contains(",")) {
                        return Arrays.stream(resolver.resolveRequiredPlaceholder(s, String[].class));
                    }
                    return Stream.of(s);
                }).toArray(String[]::new);
            } else {
                return resolvedValues;
            }
        };
        return environmentAnnotationMetadata.stringValues(annotation, member, valueMapper);
    } else {
        return environmentAnnotationMetadata.stringValues(annotation, member, null);
    }
}
Also used : Environment(io.micronaut.context.env.Environment) java.util(java.util) Array(java.lang.reflect.Array) Internal(io.micronaut.core.annotation.Internal) OptionalValues(io.micronaut.core.value.OptionalValues) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) NonNull(io.micronaut.core.annotation.NonNull) StringUtils(io.micronaut.core.util.StringUtils) Stream(java.util.stream.Stream) Nullable(io.micronaut.core.annotation.Nullable) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) Annotation(java.lang.annotation.Annotation) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) ArgumentUtils(io.micronaut.core.util.ArgumentUtils) PropertyPlaceholderResolver(io.micronaut.context.env.PropertyPlaceholderResolver) Argument(io.micronaut.core.type.Argument) Environment(io.micronaut.context.env.Environment) PropertyPlaceholderResolver(io.micronaut.context.env.PropertyPlaceholderResolver) NonNull(io.micronaut.core.annotation.NonNull)

Example 2 with PropertyPlaceholderResolver

use of io.micronaut.context.env.PropertyPlaceholderResolver in project micronaut-core by micronaut-projects.

the class EnvironmentConvertibleValuesMap method get.

@Override
public <T> Optional<T> get(CharSequence name, ArgumentConversionContext<T> conversionContext) {
    V value = map.get(name);
    if (value instanceof AnnotationClassValue) {
        AnnotationClassValue acv = (AnnotationClassValue) value;
        return environment.convert(acv, conversionContext);
    } else if (value instanceof CharSequence) {
        PropertyPlaceholderResolver placeholderResolver = environment.getPlaceholderResolver();
        String str = doResolveIfNecessary((CharSequence) value, placeholderResolver);
        return environment.convert(str, conversionContext);
    } else if (value instanceof String[]) {
        PropertyPlaceholderResolver placeholderResolver = environment.getPlaceholderResolver();
        String[] resolved = Arrays.stream((String[]) value).flatMap(val -> {
            try {
                String[] values = placeholderResolver.resolveRequiredPlaceholder(val, String[].class);
                return Arrays.stream(values);
            } catch (ConfigurationException e) {
                return Stream.of(doResolveIfNecessary(val, placeholderResolver));
            }
        }).toArray(String[]::new);
        return environment.convert(resolved, conversionContext);
    } else if (value instanceof io.micronaut.core.annotation.AnnotationValue[]) {
        io.micronaut.core.annotation.AnnotationValue[] annotationValues = (io.micronaut.core.annotation.AnnotationValue[]) value;
        io.micronaut.core.annotation.AnnotationValue[] b = new AnnotationValue[annotationValues.length];
        for (int i = 0; i < annotationValues.length; i++) {
            io.micronaut.core.annotation.AnnotationValue annotationValue = annotationValues[i];
            b[i] = new EnvironmentAnnotationValue(environment, annotationValue);
        }
        return environment.convert(b, conversionContext);
    } else if (value instanceof io.micronaut.core.annotation.AnnotationValue) {
        io.micronaut.core.annotation.AnnotationValue av = (io.micronaut.core.annotation.AnnotationValue) value;
        av = new EnvironmentAnnotationValue(environment, av);
        return environment.convert(av, conversionContext);
    } else {
        return super.get(name, conversionContext);
    }
}
Also used : Environment(io.micronaut.context.env.Environment) ConfigurationException(io.micronaut.context.exceptions.ConfigurationException) ConvertibleValues(io.micronaut.core.convert.value.ConvertibleValues) Arrays(java.util.Arrays) ConvertibleValuesMap(io.micronaut.core.convert.value.ConvertibleValuesMap) ArgumentConversionContext(io.micronaut.core.convert.ArgumentConversionContext) Collection(java.util.Collection) Internal(io.micronaut.core.annotation.Internal) Collectors(java.util.stream.Collectors) ConversionContext(io.micronaut.core.convert.ConversionContext) Stream(java.util.stream.Stream) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) Map(java.util.Map) Optional(java.util.Optional) PropertyPlaceholderResolver(io.micronaut.context.env.PropertyPlaceholderResolver) Argument(io.micronaut.core.type.Argument) AnnotationClassValue(io.micronaut.core.annotation.AnnotationClassValue) PropertyPlaceholderResolver(io.micronaut.context.env.PropertyPlaceholderResolver) AnnotationClassValue(io.micronaut.core.annotation.AnnotationClassValue) ConfigurationException(io.micronaut.context.exceptions.ConfigurationException) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) AnnotationValue(io.micronaut.core.annotation.AnnotationValue)

Example 3 with PropertyPlaceholderResolver

use of io.micronaut.context.env.PropertyPlaceholderResolver in project micronaut-graphql by micronaut-projects.

the class GraphiQLController method replaceParameters.

private String replaceParameters(final String str, final Map<String, String> parameters) {
    Map<String, Object> map = new HashMap<>();
    map.putAll(parameters);
    PropertyResolver propertyResolver = new MapPropertyResolver(map);
    PropertyPlaceholderResolver propertyPlaceholderResolver = new DefaultPropertyPlaceholderResolver(propertyResolver, conversionService);
    return propertyPlaceholderResolver.resolvePlaceholders(str).get();
}
Also used : DefaultPropertyPlaceholderResolver(io.micronaut.context.env.DefaultPropertyPlaceholderResolver) HashMap(java.util.HashMap) MapPropertyResolver(io.micronaut.core.value.MapPropertyResolver) DefaultPropertyPlaceholderResolver(io.micronaut.context.env.DefaultPropertyPlaceholderResolver) PropertyPlaceholderResolver(io.micronaut.context.env.PropertyPlaceholderResolver) PropertyResolver(io.micronaut.core.value.PropertyResolver) MapPropertyResolver(io.micronaut.core.value.MapPropertyResolver)

Example 4 with PropertyPlaceholderResolver

use of io.micronaut.context.env.PropertyPlaceholderResolver in project micronaut-core by micronaut-projects.

the class EnvironmentOptionalValuesMap method resolveValues.

@SuppressWarnings("unchecked")
private static Map<CharSequence, ?> resolveValues(Environment environment, Map<CharSequence, ?> values) {
    PropertyPlaceholderResolver placeholderResolver = environment.getPlaceholderResolver();
    return values.entrySet().stream().map((Function<Map.Entry<CharSequence, ?>, Map.Entry<CharSequence, ?>>) entry -> {
        Object value = entry.getValue();
        if (value instanceof CharSequence) {
            value = placeholderResolver.resolveRequiredPlaceholders(value.toString());
        } else if (value instanceof String[]) {
            String[] a = (String[]) value;
            for (int i = 0; i < a.length; i++) {
                a[i] = placeholderResolver.resolveRequiredPlaceholders(a[i]);
            }
        }
        Object finalValue = value;
        return new Map.Entry<CharSequence, Object>() {

            Object val = finalValue;

            @Override
            public CharSequence getKey() {
                return entry.getKey();
            }

            @Override
            public Object getValue() {
                return val;
            }

            @Override
            public Object setValue(Object value) {
                Object old = val;
                val = value;
                return old;
            }
        };
    }).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
Also used : Function(java.util.function.Function) PropertyPlaceholderResolver(io.micronaut.context.env.PropertyPlaceholderResolver) Map(java.util.Map) OptionalValuesMap(io.micronaut.core.value.OptionalValuesMap)

Aggregations

PropertyPlaceholderResolver (io.micronaut.context.env.PropertyPlaceholderResolver)4 Environment (io.micronaut.context.env.Environment)2 AnnotationValue (io.micronaut.core.annotation.AnnotationValue)2 Internal (io.micronaut.core.annotation.Internal)2 Argument (io.micronaut.core.type.Argument)2 Map (java.util.Map)2 Function (java.util.function.Function)2 Collectors (java.util.stream.Collectors)2 Stream (java.util.stream.Stream)2 DefaultPropertyPlaceholderResolver (io.micronaut.context.env.DefaultPropertyPlaceholderResolver)1 ConfigurationException (io.micronaut.context.exceptions.ConfigurationException)1 AnnotationClassValue (io.micronaut.core.annotation.AnnotationClassValue)1 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)1 NonNull (io.micronaut.core.annotation.NonNull)1 Nullable (io.micronaut.core.annotation.Nullable)1 ArgumentConversionContext (io.micronaut.core.convert.ArgumentConversionContext)1 ConversionContext (io.micronaut.core.convert.ConversionContext)1 ConvertibleValues (io.micronaut.core.convert.value.ConvertibleValues)1 ConvertibleValuesMap (io.micronaut.core.convert.value.ConvertibleValuesMap)1 ArgumentUtils (io.micronaut.core.util.ArgumentUtils)1