use of org.mule.runtime.module.extension.internal.runtime.resolver.ValueResolver in project mule by mulesoft.
the class OperationMessageProcessorTestCase method setUpValueResolvers.
private void setUpValueResolvers() throws MuleException {
final Map<String, ValueResolver<?>> valueResolvers = new HashMap<>();
when(resolverSet.getResolvers()).thenReturn(valueResolvers);
final ValueResolver valueResolver = mock(ValueResolver.class);
valueResolvers.put(SOME_PARAM_NAME, valueResolver);
when(valueResolver.resolve(any(ValueResolvingContext.class))).thenReturn("person");
}
use of org.mule.runtime.module.extension.internal.runtime.resolver.ValueResolver in project mule by mulesoft.
the class ExtensionDefinitionParser method getStaticValueResolver.
/**
* Generates the {@link ValueResolver} for non expression based values
*/
private ValueResolver getStaticValueResolver(String parameterName, MetadataType expectedType, Object value, Object defaultValue, Set<ModelProperty> modelProperties, boolean acceptsReferences, Class<?> expectedClass) {
Optional<StackedTypesModelProperty> optionalStackedTypeModelProperty = getStackedTypesModelProperty(modelProperties);
if (optionalStackedTypeModelProperty.isPresent()) {
StackedTypesModelProperty property = optionalStackedTypeModelProperty.get();
Optional<ValueResolver> optionalResolver = property.getValueResolverFactory().getStaticValueResolver(value, Literal.class);
if (optionalResolver.isPresent()) {
return optionalResolver.get();
}
}
if (isLiteral(expectedType)) {
return new StaticLiteralValueResolver<>(value != null ? value.toString() : null, expectedClass);
}
ValueResolver resolver;
resolver = value != null ? getValueResolverFromMetadataType(parameterName, expectedType, value, defaultValue, acceptsReferences, expectedClass) : new StaticValueResolver<>(defaultValue);
if (optionalStackedTypeModelProperty.isPresent()) {
resolver = optionalStackedTypeModelProperty.get().getValueResolverFactory().getWrapperValueResolver(resolver);
} else if (isParameterResolver(expectedType)) {
resolver = new ParameterResolverValueResolverWrapper(resolver);
} else if (isTypedValue(expectedType)) {
resolver = new TypedValueValueResolverWrapper(resolver);
}
return resolver;
}
use of org.mule.runtime.module.extension.internal.runtime.resolver.ValueResolver in project mule by mulesoft.
the class ExtensionDefinitionParser method getValueResolverFromMetadataType.
private ValueResolver getValueResolverFromMetadataType(final String parameterName, MetadataType expectedType, final Object value, final Object defaultValue, final boolean acceptsReferences, final Class<?> expectedClass) {
final Reference<ValueResolver> resolverValueHolder = new Reference<>();
expectedType.accept(new BasicTypeMetadataVisitor() {
@Override
protected void visitBasicType(MetadataType metadataType) {
if (conversionService.canConvert(value.getClass(), expectedClass)) {
resolverValueHolder.set(new StaticValueResolver<>(convertSimpleValue(value, expectedClass, parameterName)));
} else {
defaultVisit(metadataType);
}
}
@Override
public void visitDateTime(DateTimeType dateTimeType) {
resolverValueHolder.set(parseDate(value, dateTimeType, defaultValue));
}
@Override
public void visitDate(DateType dateType) {
resolverValueHolder.set(parseDate(value, dateType, defaultValue));
}
@Override
public void visitObject(ObjectType objectType) {
if (isMap(objectType)) {
defaultVisit(objectType);
return;
}
ValueResolver valueResolver;
Optional<? extends ParsingDelegate> delegate = locateParsingDelegate(valueResolverParsingDelegates, objectType);
Optional<DslElementSyntax> typeDsl = dslResolver.resolve(objectType);
if (delegate.isPresent() && typeDsl.isPresent()) {
valueResolver = (ValueResolver) delegate.get().parse(value.toString(), objectType, typeDsl.get());
} else {
valueResolver = acceptsReferences ? defaultValueResolverParsingDelegate.parse(value.toString(), objectType, null) : new StaticValueResolver<>(value);
}
resolverValueHolder.set(valueResolver);
}
@Override
protected void defaultVisit(MetadataType metadataType) {
ValueResolver delegateResolver = locateParsingDelegate(valueResolverParsingDelegates, metadataType).map(delegate -> delegate.parse(value.toString(), metadataType, null)).orElseGet(() -> acceptsReferences ? defaultValueResolverParsingDelegate.parse(value.toString(), metadataType, null) : new TypeSafeValueResolverWrapper(new StaticValueResolver<>(value), expectedClass));
resolverValueHolder.set(delegateResolver);
}
});
return resolverValueHolder.get();
}
use of org.mule.runtime.module.extension.internal.runtime.resolver.ValueResolver in project mule by mulesoft.
the class ComponentMessageProcessor method createFieldParameterGroupBuilder.
private ObjectBuilder createFieldParameterGroupBuilder(ParameterGroupDescriptor groupDescriptor, List<ParameterModel> fieldParameters) {
DefaultObjectBuilder groupBuilder = new DefaultObjectBuilder(groupDescriptor.getType().getDeclaringClass().get());
fieldParameters.forEach(p -> {
ValueResolver resolver = resolverSet.getResolvers().get(p.getName());
if (resolver != null) {
groupBuilder.addPropertyResolver(getMemberName(p), resolver);
}
});
return groupBuilder;
}
use of org.mule.runtime.module.extension.internal.runtime.resolver.ValueResolver in project mule by mulesoft.
the class ComponentMessageProcessor method createComponentExecutor.
private ComponentExecutor<T> createComponentExecutor() {
Map<String, Object> params = new HashMap<>();
LazyValue<Optional<ConfigurationInstance>> staticConfiguration = new LazyValue<>(this::getStaticConfiguration);
LazyValue<ValueResolvingContext> resolvingContext = new LazyValue<>(() -> {
CoreEvent initialiserEvent = null;
try {
initialiserEvent = getInitialiserEvent();
return from(initialiserEvent, staticConfiguration.get());
} finally {
if (initialiserEvent != null) {
((BaseEventContext) initialiserEvent.getContext()).success();
}
}
});
componentModel.getParameterGroupModels().stream().forEach(group -> {
if (group.getName().equals(DEFAULT_GROUP_NAME)) {
group.getParameterModels().stream().filter(p -> p.getModelProperty(FieldOperationParameterModelProperty.class).isPresent()).forEach(p -> {
ValueResolver<?> resolver = resolverSet.getResolvers().get(p.getName());
if (resolver != null) {
try {
params.put(getMemberName(p), resolveValue(resolver, resolvingContext.get()));
} catch (MuleException e) {
throw new MuleRuntimeException(e);
}
}
});
} else {
ParameterGroupDescriptor groupDescriptor = group.getModelProperty(ParameterGroupModelProperty.class).map(g -> g.getDescriptor()).orElse(null);
if (groupDescriptor == null) {
return;
}
List<ParameterModel> fieldParameters = getGroupsOfFieldParameters(group);
if (fieldParameters.isEmpty()) {
return;
}
ObjectBuilder groupBuilder = createFieldParameterGroupBuilder(groupDescriptor, fieldParameters);
try {
params.put(((Field) groupDescriptor.getContainer()).getName(), groupBuilder.build(resolvingContext.get()));
} catch (MuleException e) {
throw new MuleRuntimeException(e);
}
}
});
return getOperationExecutorFactory(componentModel).createExecutor(componentModel, params);
}
Aggregations