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