Search in sources :

Example 6 with PropertyResolver

use of io.micronaut.core.value.PropertyResolver 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 7 with PropertyResolver

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

the class AuthenticationModeCondition method matches.

@Override
public boolean matches(ConditionContext context) {
    PropertyResolver propertyResolver = context.getBeanContext().getBean(PropertyResolver.class);
    final String propertyName = SecurityConfigurationProperties.PREFIX + ".authentication";
    if (!propertyResolver.containsProperty(propertyName)) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("{}} is not fulfilled because {} is not set.", getClass().getSimpleName(), propertyName);
        }
        return false;
    }
    Optional<String> propertyValueOptional = propertyResolver.get(propertyName, String.class);
    if (!propertyValueOptional.isPresent()) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("{} is not fulfilled because {} property is not a String.", getClass().getSimpleName(), propertyName);
        }
        return false;
    }
    final String propertyvalue = propertyValueOptional.get();
    final boolean result = acceptableModes.stream().map(AuthenticationMode::toString).anyMatch(propertyvalue::equals);
    if (!result && LOG.isDebugEnabled()) {
        LOG.debug("{} is not fulfilled because {} is not one of {}.", getClass().getSimpleName(), propertyName, acceptableModes);
    }
    return result;
}
Also used : PropertyResolver(io.micronaut.core.value.PropertyResolver)

Example 8 with PropertyResolver

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

the class BuildSelfSignedCondition 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);
        return validate(context, deprecated, server);
    } 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 9 with PropertyResolver

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

the class AbstractInitializableBeanDefinition 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
 */
@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) Internal(io.micronaut.core.annotation.Internal) UsedByGeneratedCode(io.micronaut.core.annotation.UsedByGeneratedCode)

Example 10 with PropertyResolver

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

the class AbstractBeanDefinition method getValueForField.

/**
 * Obtains a value for the given field from the bean context
 * <p>
 * Warning: this method is used by internal generated code and should not be called by user code.
 *
 * @param resolutionContext The resolution context
 * @param context           The context
 * @param fieldIndex        The index of the field
 * @return The resolved bean
 */
@SuppressWarnings("WeakerAccess")
@Internal
@UsedByGeneratedCode
protected final Object getValueForField(BeanResolutionContext resolutionContext, BeanContext context, int fieldIndex) {
    FieldInjectionPoint injectionPoint = fieldInjectionPoints.get(fieldIndex);
    BeanResolutionContext.Path path = resolutionContext.getPath();
    path.pushFieldResolve(this, injectionPoint);
    try {
        if (context instanceof PropertyResolver) {
            final AnnotationMetadata annotationMetadata = injectionPoint.getAnnotationMetadata();
            String valueAnnVal = annotationMetadata.stringValue(Value.class).orElse(null);
            Argument<?> fieldArgument = injectionPoint.asArgument();
            Argument<?> argumentType;
            boolean isCollection = false;
            if (Collection.class.isAssignableFrom(injectionPoint.getType())) {
                argumentType = fieldArgument.getFirstTypeVariable().orElse(Argument.OBJECT_ARGUMENT);
                isCollection = true;
            } else {
                argumentType = fieldArgument;
            }
            if (isInnerConfiguration(argumentType, context)) {
                Qualifier qualifier = resolveQualifier(resolutionContext, fieldArgument, true);
                if (isCollection) {
                    Collection beans = ((DefaultBeanContext) context).getBeansOfType(resolutionContext, argumentType, qualifier);
                    return coerceCollectionToCorrectType(fieldArgument.getType(), beans);
                } else {
                    return ((DefaultBeanContext) context).getBean(resolutionContext, argumentType, qualifier);
                }
            } else {
                String valString = resolvePropertyValueName(resolutionContext, injectionPoint, valueAnnVal, annotationMetadata);
                ArgumentConversionContext conversionContext = ConversionContext.of(fieldArgument);
                Optional value = resolveValue((ApplicationContext) context, conversionContext, valueAnnVal != null, valString);
                if (argumentType.isOptional()) {
                    return resolveOptionalObject(value);
                } else {
                    if (value.isPresent()) {
                        return value.get();
                    } else {
                        if (fieldArgument.isDeclaredNullable()) {
                            return null;
                        }
                        throw new DependencyInjectionException(resolutionContext, injectionPoint, "Error resolving field value [" + valString + "]. Property doesn't exist or cannot be converted");
                    }
                }
            }
        } else {
            throw new DependencyInjectionException(resolutionContext, injectionPoint, "@Value requires a BeanContext that implements PropertyResolver");
        }
    } finally {
        path.pop();
    }
}
Also used : PropertyResolver(io.micronaut.core.value.PropertyResolver) AbstractEnvironmentAnnotationMetadata(io.micronaut.inject.annotation.AbstractEnvironmentAnnotationMetadata) ArgumentConversionContext(io.micronaut.core.convert.ArgumentConversionContext) TypeAnnotationQualifier(io.micronaut.inject.qualifiers.TypeAnnotationQualifier) InterceptorBindingQualifier(io.micronaut.inject.qualifiers.InterceptorBindingQualifier)

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