Search in sources :

Example 11 with ValueResolver

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");
}
Also used : HashMap(java.util.HashMap) ValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.ValueResolver) ValueResolvingContext(org.mule.runtime.module.extension.internal.runtime.resolver.ValueResolvingContext)

Example 12 with ValueResolver

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;
}
Also used : TypedValueValueResolverWrapper(org.mule.runtime.module.extension.internal.runtime.resolver.TypedValueValueResolverWrapper) StackedTypesModelProperty.getStackedTypesModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.stackabletypes.StackedTypesModelProperty.getStackedTypesModelProperty) StackedTypesModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.stackabletypes.StackedTypesModelProperty) ParameterResolverValueResolverWrapper(org.mule.runtime.module.extension.internal.runtime.resolver.ParameterResolverValueResolverWrapper) ExpressionBasedParameterResolverValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.ExpressionBasedParameterResolverValueResolver) StaticValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.StaticValueResolver) ExpressionTypedValueValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.ExpressionTypedValueValueResolver) NativeQueryParameterValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.NativeQueryParameterValueResolver) ValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.ValueResolver) ProcessorChainValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.ProcessorChainValueResolver) TypeSafeExpressionValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.TypeSafeExpressionValueResolver) StaticLiteralValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.StaticLiteralValueResolver) StaticLiteralValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.StaticLiteralValueResolver) StaticValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.StaticValueResolver)

Example 13 with ValueResolver

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();
}
Also used : SchedulingStrategyParsingDelegate(org.mule.runtime.module.extension.internal.config.dsl.object.SchedulingStrategyParsingDelegate) DefaultValueResolverParsingDelegate(org.mule.runtime.module.extension.internal.config.dsl.object.DefaultValueResolverParsingDelegate) MediaTypeValueResolverParsingDelegate(org.mule.runtime.module.extension.internal.config.dsl.object.MediaTypeValueResolverParsingDelegate) ParsingDelegate(org.mule.runtime.module.extension.internal.config.dsl.object.ParsingDelegate) DefaultObjectParsingDelegate(org.mule.runtime.module.extension.internal.config.dsl.object.DefaultObjectParsingDelegate) ObjectParsingDelegate(org.mule.runtime.module.extension.internal.config.dsl.object.ObjectParsingDelegate) CharsetValueResolverParsingDelegate(org.mule.runtime.module.extension.internal.config.dsl.object.CharsetValueResolverParsingDelegate) ValueResolverParsingDelegate(org.mule.runtime.module.extension.internal.config.dsl.object.ValueResolverParsingDelegate) FixedTypeParsingDelegate(org.mule.runtime.module.extension.internal.config.dsl.object.FixedTypeParsingDelegate) Optional(java.util.Optional) Reference(org.mule.runtime.api.util.Reference) MetadataType(org.mule.metadata.api.model.MetadataType) BasicTypeMetadataVisitor(org.mule.metadata.api.visitor.BasicTypeMetadataVisitor) ObjectType(org.mule.metadata.api.model.ObjectType) DateTimeType(org.mule.metadata.api.model.DateTimeType) TypeSafeValueResolverWrapper(org.mule.runtime.module.extension.internal.runtime.resolver.TypeSafeValueResolverWrapper) ExpressionBasedParameterResolverValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.ExpressionBasedParameterResolverValueResolver) StaticValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.StaticValueResolver) ExpressionTypedValueValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.ExpressionTypedValueValueResolver) NativeQueryParameterValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.NativeQueryParameterValueResolver) ValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.ValueResolver) ProcessorChainValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.ProcessorChainValueResolver) TypeSafeExpressionValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.TypeSafeExpressionValueResolver) StaticLiteralValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.StaticLiteralValueResolver) StaticValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.StaticValueResolver) DateType(org.mule.metadata.api.model.DateType)

Example 14 with ValueResolver

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;
}
Also used : ParameterValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.ParameterValueResolver) ValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.ValueResolver) DefaultObjectBuilder(org.mule.runtime.module.extension.internal.runtime.objectbuilder.DefaultObjectBuilder)

Example 15 with ValueResolver

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);
}
Also used : IntrospectionUtils.getMemberName(org.mule.runtime.module.extension.internal.util.IntrospectionUtils.getMemberName) FieldOperationParameterModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.FieldOperationParameterModelProperty) Optional.of(java.util.Optional.of) Mono.subscriberContext(reactor.core.publisher.Mono.subscriberContext) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) ResolverUtils.resolveValue(org.mule.runtime.module.extension.internal.runtime.resolver.ResolverUtils.resolveValue) ParameterValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.ParameterValueResolver) PROCESSOR_SCHEDULER_CONTEXT_KEY(org.mule.runtime.core.internal.processor.strategy.AbstractProcessingStrategy.PROCESSOR_SCHEDULER_CONTEXT_KEY) Scheduler(org.mule.runtime.api.scheduler.Scheduler) IMMEDIATE_SCHEDULER(org.mule.runtime.core.internal.util.rx.ImmediateScheduler.IMMEDIATE_SCHEDULER) MuleExtensionUtils.getOperationExecutorFactory(org.mule.runtime.module.extension.internal.util.MuleExtensionUtils.getOperationExecutorFactory) Map(java.util.Map) TARGET_PARAMETER_NAME(org.mule.runtime.extension.api.ExtensionConstants.TARGET_PARAMETER_NAME) OperationArgumentResolverFactory(org.mule.runtime.module.extension.internal.runtime.execution.OperationArgumentResolverFactory) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) LifecycleUtils.stopIfNeeded(org.mule.runtime.core.api.lifecycle.LifecycleUtils.stopIfNeeded) ParameterGroupModel(org.mule.runtime.api.meta.model.parameter.ParameterGroupModel) Exceptions(org.mule.runtime.core.api.rx.Exceptions) PolicyManager(org.mule.runtime.core.internal.policy.PolicyManager) ParametersResolverProcessor(org.mule.runtime.core.internal.processor.ParametersResolverProcessor) ParameterGroupDescriptor(org.mule.runtime.module.extension.internal.loader.ParameterGroupDescriptor) ConfigurationProvider(org.mule.runtime.extension.api.runtime.config.ConfigurationProvider) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ReflectionCache(org.mule.runtime.module.extension.internal.util.ReflectionCache) ConfigurationInstance(org.mule.runtime.extension.api.runtime.config.ConfigurationInstance) String.format(java.lang.String.format) OperationPolicy(org.mule.runtime.core.internal.policy.OperationPolicy) MuleExtensionUtils.getInitialiserEvent(org.mule.runtime.module.extension.api.util.MuleExtensionUtils.getInitialiserEvent) ObjectBuilder(org.mule.runtime.module.extension.internal.runtime.objectbuilder.ObjectBuilder) Lifecycle(org.mule.runtime.api.lifecycle.Lifecycle) List(java.util.List) ComponentExecutorModelProperty(org.mule.runtime.module.extension.api.loader.java.property.ComponentExecutorModelProperty) Optional(java.util.Optional) CursorProviderFactory(org.mule.runtime.core.api.streaming.CursorProviderFactory) ValueResolvingContext.from(org.mule.runtime.module.extension.internal.runtime.resolver.ValueResolvingContext.from) ComponentExecutorFactory(org.mule.runtime.extension.api.runtime.operation.ComponentExecutorFactory) Optional.empty(java.util.Optional.empty) ParameterModel(org.mule.runtime.api.meta.model.parameter.ParameterModel) RetryPolicyTemplate(org.mule.runtime.core.api.retry.policy.RetryPolicyTemplate) Flux.from(reactor.core.publisher.Flux.from) ComponentModel(org.mule.runtime.api.meta.model.ComponentModel) ComponentExecutor(org.mule.runtime.extension.api.runtime.operation.ComponentExecutor) HashMap(java.util.HashMap) LifecycleUtils.initialiseIfNeeded(org.mule.runtime.core.api.lifecycle.LifecycleUtils.initialiseIfNeeded) ResolverSet(org.mule.runtime.module.extension.internal.runtime.resolver.ResolverSet) Processor(org.mule.runtime.core.api.processor.Processor) TARGET_VALUE_PARAMETER_NAME(org.mule.runtime.extension.api.ExtensionConstants.TARGET_VALUE_PARAMETER_NAME) DefaultObjectBuilder(org.mule.runtime.module.extension.internal.runtime.objectbuilder.DefaultObjectBuilder) MuleException(org.mule.runtime.api.exception.MuleException) Flux.error(reactor.core.publisher.Flux.error) BiConsumer(java.util.function.BiConsumer) DEFAULT_GROUP_NAME(org.mule.runtime.api.meta.model.parameter.ParameterGroupModel.DEFAULT_GROUP_NAME) ParameterGroupModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.ParameterGroupModelProperty) Mono.fromCallable(reactor.core.publisher.Mono.fromCallable) ExecutionContext(org.mule.runtime.extension.api.runtime.operation.ExecutionContext) ExecutionContextAdapter(org.mule.runtime.module.extension.api.runtime.privileged.ExecutionContextAdapter) DefaultExecutionContext(org.mule.runtime.module.extension.internal.runtime.DefaultExecutionContext) Logger(org.slf4j.Logger) ValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.ValueResolver) MessageProcessors.processToApply(org.mule.runtime.core.privileged.processor.MessageProcessors.processToApply) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Publisher(org.reactivestreams.Publisher) Exceptions.checkedFunction(org.mule.runtime.core.api.rx.Exceptions.checkedFunction) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) ComponentLocationProvider.resolveProcessorRepresentation(org.mule.runtime.api.util.ComponentLocationProvider.resolveProcessorRepresentation) LifecycleUtils.startIfNeeded(org.mule.runtime.core.api.lifecycle.LifecycleUtils.startIfNeeded) Mono(reactor.core.publisher.Mono) Field(java.lang.reflect.Field) ExtensionComponent(org.mule.runtime.module.extension.internal.runtime.ExtensionComponent) LazyExecutionContext(org.mule.runtime.module.extension.internal.runtime.LazyExecutionContext) Interceptor(org.mule.runtime.extension.api.runtime.operation.Interceptor) ExtensionModel(org.mule.runtime.api.meta.model.ExtensionModel) LifecycleUtils.disposeIfNeeded(org.mule.runtime.core.api.lifecycle.LifecycleUtils.disposeIfNeeded) INTERCEPTION_RESOLVED_CONTEXT(org.mule.runtime.core.internal.interception.DefaultInterceptionEvent.INTERCEPTION_RESOLVED_CONTEXT) Collectors.toList(java.util.stream.Collectors.toList) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) LazyValue(org.mule.runtime.api.util.LazyValue) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) ExtensionManager(org.mule.runtime.core.api.extension.ExtensionManager) IntrospectionUtils.isVoid(org.mule.runtime.module.extension.internal.util.IntrospectionUtils.isVoid) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) DefaultFlowCallStack(org.mule.runtime.core.internal.context.notification.DefaultFlowCallStack) ValueResolvingContext(org.mule.runtime.module.extension.internal.runtime.resolver.ValueResolvingContext) OperationExecutionFunction(org.mule.runtime.core.internal.policy.OperationExecutionFunction) InternalEvent(org.mule.runtime.core.internal.message.InternalEvent) FieldOperationParameterModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.FieldOperationParameterModelProperty) LazyValue(org.mule.runtime.api.util.LazyValue) Optional(java.util.Optional) HashMap(java.util.HashMap) ParameterGroupDescriptor(org.mule.runtime.module.extension.internal.loader.ParameterGroupDescriptor) ObjectBuilder(org.mule.runtime.module.extension.internal.runtime.objectbuilder.ObjectBuilder) DefaultObjectBuilder(org.mule.runtime.module.extension.internal.runtime.objectbuilder.DefaultObjectBuilder) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) ParameterModel(org.mule.runtime.api.meta.model.parameter.ParameterModel) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ValueResolvingContext(org.mule.runtime.module.extension.internal.runtime.resolver.ValueResolvingContext) MuleException(org.mule.runtime.api.exception.MuleException) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException)

Aggregations

ValueResolver (org.mule.runtime.module.extension.internal.runtime.resolver.ValueResolver)18 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)6 StaticValueResolver (org.mule.runtime.module.extension.internal.runtime.resolver.StaticValueResolver)6 HashMap (java.util.HashMap)5 MuleException (org.mule.runtime.api.exception.MuleException)5 ExpressionBasedParameterResolverValueResolver (org.mule.runtime.module.extension.internal.runtime.resolver.ExpressionBasedParameterResolverValueResolver)4 ExpressionTypedValueValueResolver (org.mule.runtime.module.extension.internal.runtime.resolver.ExpressionTypedValueValueResolver)4 NativeQueryParameterValueResolver (org.mule.runtime.module.extension.internal.runtime.resolver.NativeQueryParameterValueResolver)4 ProcessorChainValueResolver (org.mule.runtime.module.extension.internal.runtime.resolver.ProcessorChainValueResolver)4 StaticLiteralValueResolver (org.mule.runtime.module.extension.internal.runtime.resolver.StaticLiteralValueResolver)4 TypeSafeExpressionValueResolver (org.mule.runtime.module.extension.internal.runtime.resolver.TypeSafeExpressionValueResolver)4 Map (java.util.Map)3 Optional (java.util.Optional)3 InitialisationException (org.mule.runtime.api.lifecycle.InitialisationException)3 ExtensionModel (org.mule.runtime.api.meta.model.ExtensionModel)3 String.format (java.lang.String.format)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 DefaultMuleException (org.mule.runtime.api.exception.DefaultMuleException)2