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);
}
}
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);
}
}
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();
}
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));
}
Aggregations