Search in sources :

Example 1 with StaticValueResolver

use of org.mule.runtime.module.extension.internal.runtime.resolver.StaticValueResolver in project mule by mulesoft.

the class ExtensionDefinitionParser method doParseDate.

private ValueResolver doParseDate(Object value, Class<?> type) {
    if (value instanceof String) {
        Object constructedValue = null;
        DateTime dateTime = getParsedDateTime((String) value);
        if (type.equals(LocalDate.class)) {
            constructedValue = LocalDate.of(dateTime.getYear(), dateTime.getMonthOfYear(), dateTime.getDayOfMonth());
        } else if (type.equals(Date.class)) {
            constructedValue = dateTime.toDate();
        } else if (type.equals(LocalDateTime.class)) {
            Instant instant = ofEpochMilli(dateTime.getMillis());
            constructedValue = LocalDateTime.ofInstant(instant, ZoneId.of(dateTime.getZone().getID()));
        } else if (type.equals(Calendar.class)) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(dateTime.toDate());
            constructedValue = calendar;
        }
        if (constructedValue == null) {
            throw new IllegalArgumentException(format("Could not construct value of type '%s' from String '%s'", type.getName(), value));
        } else {
            value = constructedValue;
        }
    }
    if (value instanceof Date || value instanceof LocalDate || value instanceof LocalDateTime || value instanceof Calendar) {
        return new StaticValueResolver<>(value);
    }
    throw new IllegalArgumentException(format("Could not transform value of type '%s' to a valid date type", value != null ? value.getClass().getName() : "null"));
}
Also used : LocalDateTime(java.time.LocalDateTime) Instant(java.time.Instant) Calendar(java.util.Calendar) StaticValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.StaticValueResolver) LocalDate(java.time.LocalDate) LocalDateTime(java.time.LocalDateTime) DateTime(org.joda.time.DateTime) Date(java.util.Date) LocalDate(java.time.LocalDate)

Example 2 with StaticValueResolver

use of org.mule.runtime.module.extension.internal.runtime.resolver.StaticValueResolver in project mule by mulesoft.

the class DefaultExtensionsClient method resolveParameters.

private Map<String, ValueResolver> resolveParameters(Map<String, Object> parameters, CoreEvent event) {
    LinkedHashMap<String, ValueResolver> values = new LinkedHashMap<>();
    parameters.forEach((name, value) -> {
        if (value instanceof ComplexParameter) {
            ComplexParameter complex = (ComplexParameter) value;
            DefaultObjectBuilder<?> builder = new DefaultObjectBuilder<>(complex.getType());
            resolveParameters(complex.getParameters(), event).forEach((propertyName, valueResolver) -> {
                try {
                    initialiseIfNeeded(valueResolver, true, muleContext);
                    builder.addPropertyResolver(propertyName, valueResolver);
                } catch (InitialisationException e) {
                    throw new MuleRuntimeException(e);
                }
            });
            try {
                values.put(name, new StaticValueResolver<>(builder.build(from(event))));
            } catch (MuleException e) {
                throw new MuleRuntimeException(createStaticMessage(format("Could not construct parameter [%s]", name)), e);
            }
        } else {
            if (value instanceof String && parser.isContainsTemplate((String) value)) {
                values.put(name, new ExpressionValueResolver((String) value));
            } else {
                values.put(name, new StaticValueResolver<>(value));
            }
        }
    });
    return values;
}
Also used : ComplexParameter(org.mule.runtime.extension.internal.client.ComplexParameter) StaticValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.StaticValueResolver) ValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.ValueResolver) ExpressionValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.ExpressionValueResolver) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) DefaultObjectBuilder(org.mule.runtime.module.extension.internal.runtime.objectbuilder.DefaultObjectBuilder) MuleException(org.mule.runtime.api.exception.MuleException) LinkedHashMap(java.util.LinkedHashMap) ExpressionValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.ExpressionValueResolver)

Example 3 with StaticValueResolver

use of org.mule.runtime.module.extension.internal.runtime.resolver.StaticValueResolver in project mule by mulesoft.

the class ReflectiveOperationExecutorFactory method createExecutor.

@Override
public ComponentExecutor<M> createExecutor(M operationModel, Map<String, Object> parameters) {
    DefaultObjectBuilder objectBuilder = new DefaultObjectBuilder(implementationClass);
    parameters.forEach((k, v) -> objectBuilder.addPropertyResolver(k, new StaticValueResolver<>(v)));
    Object delegate;
    CoreEvent initialiserEvent = null;
    try {
        initialiserEvent = getInitialiserEvent();
        delegate = objectBuilder.build(from(initialiserEvent));
    } catch (Exception e) {
        throw new MuleRuntimeException(createStaticMessage("Could not create instance of operation class " + implementationClass.getName()), e);
    } finally {
        if (initialiserEvent != null) {
            ((BaseEventContext) initialiserEvent.getContext()).success();
        }
    }
    return new ReflectiveMethodOperationExecutor(operationModel, operationMethod, delegate);
}
Also used : BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) StaticValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.StaticValueResolver) ReflectiveMethodOperationExecutor(org.mule.runtime.module.extension.internal.runtime.operation.ReflectiveMethodOperationExecutor) DefaultObjectBuilder(org.mule.runtime.module.extension.internal.runtime.objectbuilder.DefaultObjectBuilder) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException)

Example 4 with StaticValueResolver

use of org.mule.runtime.module.extension.internal.runtime.resolver.StaticValueResolver 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 5 with StaticValueResolver

use of org.mule.runtime.module.extension.internal.runtime.resolver.StaticValueResolver 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)

Aggregations

StaticValueResolver (org.mule.runtime.module.extension.internal.runtime.resolver.StaticValueResolver)7 DefaultObjectBuilder (org.mule.runtime.module.extension.internal.runtime.objectbuilder.DefaultObjectBuilder)3 ValueResolver (org.mule.runtime.module.extension.internal.runtime.resolver.ValueResolver)3 MetadataType (org.mule.metadata.api.model.MetadataType)2 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)2 ExpressionBasedParameterResolverValueResolver (org.mule.runtime.module.extension.internal.runtime.resolver.ExpressionBasedParameterResolverValueResolver)2 ExpressionTypedValueValueResolver (org.mule.runtime.module.extension.internal.runtime.resolver.ExpressionTypedValueValueResolver)2 NativeQueryParameterValueResolver (org.mule.runtime.module.extension.internal.runtime.resolver.NativeQueryParameterValueResolver)2 ProcessorChainValueResolver (org.mule.runtime.module.extension.internal.runtime.resolver.ProcessorChainValueResolver)2 StaticLiteralValueResolver (org.mule.runtime.module.extension.internal.runtime.resolver.StaticLiteralValueResolver)2 TypeSafeExpressionValueResolver (org.mule.runtime.module.extension.internal.runtime.resolver.TypeSafeExpressionValueResolver)2 Instant (java.time.Instant)1 LocalDate (java.time.LocalDate)1 LocalDateTime (java.time.LocalDateTime)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 LinkedHashMap (java.util.LinkedHashMap)1 Optional (java.util.Optional)1 DateTime (org.joda.time.DateTime)1 DateTimeType (org.mule.metadata.api.model.DateTimeType)1