Search in sources :

Example 1 with PropertyResolver

use of io.micronaut.core.value.PropertyResolver in project micronaut-core by micronaut-projects.

the class SslEnabledCondition method matches.

@Override
public boolean matches(ConditionContext context) {
    BeanContext beanContext = context.getBeanContext();
    if (beanContext instanceof PropertyResolver) {
        PropertyResolver resolver = (PropertyResolver) beanContext;
        boolean deprecated = enabledForPrefix(resolver, SslConfiguration.PREFIX);
        boolean server = enabledForPrefix(resolver, ServerSslConfiguration.PREFIX);
        if (!deprecated && !server) {
            context.fail("Neither the old deprecated " + SslConfiguration.PREFIX + ".build-self-signed, nor the new " + ServerSslConfiguration.PREFIX + ".build-self-signed were enabled.");
            return false;
        } else {
            return true;
        }
    } else {
        context.fail("Bean requires property but BeanContext does not support property resolution");
        return false;
    }
}
Also used : BeanContext(io.micronaut.context.BeanContext) PropertyResolver(io.micronaut.core.value.PropertyResolver)

Example 2 with PropertyResolver

use of io.micronaut.core.value.PropertyResolver in project micronaut-core by micronaut-projects.

the class AbstractInitializableBeanDefinition method resolveValue.

private Object resolveValue(BeanResolutionContext resolutionContext, BeanContext context, AnnotationMetadata parentAnnotationMetadata, Argument<?> argument, Qualifier qualifier) {
    if (!(context instanceof PropertyResolver)) {
        throw new DependencyInjectionException(resolutionContext, "@Value requires a BeanContext that implements PropertyResolver");
    }
    String valueAnnVal = argument.getAnnotationMetadata().stringValue(Value.class).orElse(null);
    Argument<?> argumentType;
    boolean isCollection = false;
    final boolean wrapperType = argument.isWrapperType();
    final Class<?> argumentJavaType = argument.getType();
    if (Collection.class.isAssignableFrom(argumentJavaType)) {
        argumentType = argument.getFirstTypeVariable().orElse(Argument.OBJECT_ARGUMENT);
        isCollection = true;
    } else if (wrapperType) {
        argumentType = argument.getWrappedType();
    } else {
        argumentType = argument;
    }
    if (isInnerConfiguration(argumentType.getType())) {
        qualifier = qualifier == null ? resolveQualifierWithInnerConfiguration(resolutionContext, argument, true) : qualifier;
        if (isCollection) {
            Collection beans = ((DefaultBeanContext) context).getBeansOfType(resolutionContext, argumentType, qualifier);
            return coerceCollectionToCorrectType((Class) argumentJavaType, beans, resolutionContext, argument);
        } else {
            return ((DefaultBeanContext) context).getBean(resolutionContext, argumentType, qualifier);
        }
    } else {
        String valString = resolvePropertyValueName(resolutionContext, parentAnnotationMetadata, argument.getAnnotationMetadata(), valueAnnVal);
        ArgumentConversionContext conversionContext = wrapperType ? ConversionContext.of(argumentType) : ConversionContext.of(argument);
        Optional value = resolveValue((ApplicationContext) context, conversionContext, valueAnnVal != null, valString);
        if (argument.isOptional()) {
            if (!value.isPresent()) {
                return value;
            } else {
                Object convertedOptional = value.get();
                if (convertedOptional instanceof Optional) {
                    return convertedOptional;
                } else {
                    return value;
                }
            }
        } else {
            if (wrapperType) {
                final Object v = value.orElse(null);
                if (OptionalInt.class == argumentJavaType) {
                    return v instanceof Integer ? OptionalInt.of((Integer) v) : OptionalInt.empty();
                } else if (OptionalLong.class == argumentJavaType) {
                    return v instanceof Long ? OptionalLong.of((Long) v) : OptionalLong.empty();
                } else if (OptionalDouble.class == argumentJavaType) {
                    return v instanceof Double ? OptionalDouble.of((Double) v) : OptionalDouble.empty();
                }
            }
            if (value.isPresent()) {
                return value.get();
            } else {
                if (argument.isDeclaredNullable()) {
                    return null;
                }
                return argument.getAnnotationMetadata().getValue(Bindable.class, "defaultValue", argument).orElseThrow(() -> DependencyInjectionException.missingProperty(resolutionContext, conversionContext, valString));
            }
        }
    }
}
Also used : Optional(java.util.Optional) Bindable(io.micronaut.core.bind.annotation.Bindable) PropertyResolver(io.micronaut.core.value.PropertyResolver) OptionalDouble(java.util.OptionalDouble) DependencyInjectionException(io.micronaut.context.exceptions.DependencyInjectionException) ArgumentConversionContext(io.micronaut.core.convert.ArgumentConversionContext) Value(io.micronaut.context.annotation.Value) OptionalLong(java.util.OptionalLong) Collection(java.util.Collection) OptionalLong(java.util.OptionalLong)

Example 3 with PropertyResolver

use of io.micronaut.core.value.PropertyResolver in project micronaut-core by micronaut-projects.

the class AbstractBeanDefinition method getValueForPath.

/**
 * Resolve a value for the given field of the given type and path.
 *
 * @param resolutionContext The resolution context
 * @param context           The bean context
 * @param propertyType      The required property type
 * @param propertyPath      The property path
 * @param <T1>              The generic type
 * @return An optional value
 */
@SuppressWarnings("unused")
@Internal
@UsedByGeneratedCode
protected final <T1> Optional<T1> getValueForPath(BeanResolutionContext resolutionContext, BeanContext context, Argument<T1> propertyType, String propertyPath) {
    if (context instanceof PropertyResolver) {
        PropertyResolver propertyResolver = (PropertyResolver) context;
        String valString = substituteWildCards(resolutionContext, propertyPath);
        return propertyResolver.getProperty(valString, ConversionContext.of(propertyType));
    }
    return Optional.empty();
}
Also used : PropertyResolver(io.micronaut.core.value.PropertyResolver)

Example 4 with PropertyResolver

use of io.micronaut.core.value.PropertyResolver in project micronaut-core by micronaut-projects.

the class RequiresCondition method matchesProperty.

private boolean matchesProperty(ConditionContext context, AnnotationValue<Requires> requirements) {
    if (requirements.contains(MEMBER_PROPERTY)) {
        String property = requirements.stringValue(MEMBER_PROPERTY).orElse(null);
        if (StringUtils.isNotEmpty(property)) {
            String value = requirements.stringValue().orElse(null);
            BeanContext beanContext = context.getBeanContext();
            if (beanContext instanceof PropertyResolver) {
                PropertyResolver propertyResolver = (PropertyResolver) beanContext;
                String defaultValue = requirements.stringValue(MEMBER_DEFAULT_VALUE).orElse(null);
                if (!propertyResolver.containsProperties(property) && StringUtils.isEmpty(defaultValue)) {
                    boolean hasNotEquals = requirements.contains(MEMBER_NOT_EQUALS);
                    if (hasNotEquals) {
                        return true;
                    } else {
                        context.fail("Required property [" + property + "] with value [" + value + "] not present");
                        return false;
                    }
                } else if (StringUtils.isNotEmpty(value)) {
                    String resolved = resolvePropertyValue(property, propertyResolver, defaultValue);
                    boolean result = resolved != null && resolved.equals(value);
                    if (!result) {
                        context.fail("Property [" + property + "] with value [" + resolved + "] does not equal required value: " + value);
                    }
                    return result;
                } else if (requirements.contains(MEMBER_NOT_EQUALS)) {
                    String notEquals = requirements.stringValue(MEMBER_NOT_EQUALS).orElse(null);
                    String resolved = resolvePropertyValue(property, propertyResolver, defaultValue);
                    boolean result = resolved == null || !resolved.equals(notEquals);
                    if (!result) {
                        context.fail("Property [" + property + "] with value [" + resolved + "] should not equal: " + notEquals);
                    }
                    return result;
                } else if (requirements.contains(MEMBER_PATTERN)) {
                    String pattern = requirements.stringValue(MEMBER_PATTERN).orElse(null);
                    if (pattern == null) {
                        return true;
                    }
                    String resolved = resolvePropertyValue(property, propertyResolver, defaultValue);
                    boolean result = resolved != null && resolved.matches(pattern);
                    if (!result) {
                        context.fail("Property [" + property + "] with value [" + resolved + "] does not match required pattern: " + pattern);
                    }
                    return result;
                }
            } else {
                context.fail("Bean requires property but BeanContext does not support property resolution");
                return false;
            }
        }
    }
    return true;
}
Also used : PropertyResolver(io.micronaut.core.value.PropertyResolver)

Example 5 with PropertyResolver

use of io.micronaut.core.value.PropertyResolver in project micronaut-core by micronaut-projects.

the class EndpointEnabledCondition method matches.

@Override
public boolean matches(ConditionContext context) {
    AnnotationMetadataProvider component = context.getComponent();
    AnnotationMetadata annotationMetadata = component.getAnnotationMetadata();
    if (annotationMetadata.hasDeclaredAnnotation(Endpoint.class)) {
        Boolean defaultEnabled = annotationMetadata.booleanValue(Endpoint.class, "defaultEnabled").orElse(true);
        String prefix = annotationMetadata.stringValue(Endpoint.class, "prefix").orElse(Endpoint.DEFAULT_PREFIX);
        String id = annotationMetadata.stringValue(Endpoint.class).orElse(null);
        String defaultId = annotationMetadata.stringValue(Endpoint.class, "defaultConfigurationId").orElse("all");
        BeanContext beanContext = context.getBeanContext();
        if (beanContext instanceof PropertyResolver) {
            PropertyResolver propertyResolver = (PropertyResolver) beanContext;
            Optional<Boolean> enabled = propertyResolver.getProperty(String.format("%s.%s.enabled", prefix, id), Boolean.class);
            if (enabled.isPresent()) {
                return enabled.get();
            } else {
                enabled = propertyResolver.getProperty(String.format("%s.%s.enabled", prefix, defaultId), Boolean.class);
                return enabled.orElse(defaultEnabled);
            }
        }
    }
    return true;
}
Also used : BeanContext(io.micronaut.context.BeanContext) Endpoint(io.micronaut.management.endpoint.annotation.Endpoint) AnnotationMetadataProvider(io.micronaut.core.annotation.AnnotationMetadataProvider) PropertyResolver(io.micronaut.core.value.PropertyResolver) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata)

Aggregations

PropertyResolver (io.micronaut.core.value.PropertyResolver)10 BeanContext (io.micronaut.context.BeanContext)3 ArgumentConversionContext (io.micronaut.core.convert.ArgumentConversionContext)2 Value (io.micronaut.context.annotation.Value)1 DefaultPropertyPlaceholderResolver (io.micronaut.context.env.DefaultPropertyPlaceholderResolver)1 PropertyPlaceholderResolver (io.micronaut.context.env.PropertyPlaceholderResolver)1 DependencyInjectionException (io.micronaut.context.exceptions.DependencyInjectionException)1 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)1 AnnotationMetadataProvider (io.micronaut.core.annotation.AnnotationMetadataProvider)1 Internal (io.micronaut.core.annotation.Internal)1 UsedByGeneratedCode (io.micronaut.core.annotation.UsedByGeneratedCode)1 Bindable (io.micronaut.core.bind.annotation.Bindable)1 MapPropertyResolver (io.micronaut.core.value.MapPropertyResolver)1 AbstractEnvironmentAnnotationMetadata (io.micronaut.inject.annotation.AbstractEnvironmentAnnotationMetadata)1 InterceptorBindingQualifier (io.micronaut.inject.qualifiers.InterceptorBindingQualifier)1 TypeAnnotationQualifier (io.micronaut.inject.qualifiers.TypeAnnotationQualifier)1 Endpoint (io.micronaut.management.endpoint.annotation.Endpoint)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Optional (java.util.Optional)1