Search in sources :

Example 6 with DataType

use of org.mule.runtime.api.metadata.DataType in project mule by mulesoft.

the class DynamicDataTypeConverterResolverTestCase method doesNotFailWhenThereIsNoDataTypeResolution.

@Test
public void doesNotFailWhenThereIsNoDataTypeResolution() throws TransformerException {
    when(muleContext.getRegistry()).thenReturn(muleRegistry);
    when(muleRegistry.lookupTransformer(Mockito.any(DataType.class), Mockito.any(DataType.class))).thenReturn(null);
    DynamicDataTypeConversionResolver resolver = new DynamicDataTypeConversionResolver(muleContext);
    List<DataType> targetValues = new ArrayList<>();
    targetValues.add(DataType.STRING);
    Transformer resolvedConverter = resolver.resolve(DataType.INPUT_STREAM, targetValues);
    assertEquals(null, resolvedConverter);
}
Also used : Transformer(org.mule.runtime.core.api.transformer.Transformer) ArrayList(java.util.ArrayList) DataType(org.mule.runtime.api.metadata.DataType) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Example 7 with DataType

use of org.mule.runtime.api.metadata.DataType in project mule by mulesoft.

the class MVELExpressionLanguage method evaluate.

@Override
public TypedValue evaluate(String expression, CoreEvent event, CoreEvent.Builder eventBuilder, ComponentLocation componentLocation, BindingContext bindingContext) {
    expression = removeExpressionMarker(expression);
    Map<String, Object> bindingMap = bindingContext.identifiers().stream().collect(toMap(id -> id, id -> bindingContext.lookup(id).get().getValue()));
    final Object value = evaluateUntyped(expression, (PrivilegedEvent) event, (PrivilegedEvent.Builder) eventBuilder, componentLocation, bindingMap);
    if (value instanceof TypedValue) {
        return (TypedValue) value;
    } else {
        final Serializable compiledExpression = expressionExecutor.getCompiledExpression(expression);
        DataType dataType = event != null ? dataTypeResolver.resolve(value, (PrivilegedEvent) event, compiledExpression) : OBJECT;
        return new TypedValue(value, dataType);
    }
}
Also used : ExtendedExpressionLanguageAdaptor(org.mule.runtime.core.internal.el.ExtendedExpressionLanguageAdaptor) PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) CoreMessages(org.mule.runtime.core.api.config.i18n.CoreMessages) AbstractDataTypeBuilderFactory(org.mule.runtime.api.metadata.AbstractDataTypeBuilderFactory) ExpressionCompiler(org.mule.mvel2.compiler.ExpressionCompiler) BindingContext(org.mule.runtime.api.el.BindingContext) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) HashMap(java.util.HashMap) DEFAULT_EXPRESSION_POSTFIX(org.mule.runtime.core.api.el.ExpressionManager.DEFAULT_EXPRESSION_POSTFIX) DataHandler(javax.activation.DataHandler) ValidationResult.success(org.mule.runtime.api.el.ValidationResult.success) CachedMapVariableResolverFactory(org.mule.mvel2.integration.impl.CachedMapVariableResolverFactory) ParserConfiguration(org.mule.mvel2.ParserConfiguration) Inject(javax.inject.Inject) BigDecimal(java.math.BigDecimal) ExpressionRuntimeException(org.mule.runtime.core.api.expression.ExpressionRuntimeException) Collectors.toMap(java.util.stream.Collectors.toMap) MuleContext(org.mule.runtime.core.api.MuleContext) Map(java.util.Map) MimeType(javax.activation.MimeType) Function(org.mule.mvel2.ast.Function) Collections.singletonMap(java.util.Collections.singletonMap) BigInteger(java.math.BigInteger) IOUtils(org.mule.runtime.core.api.util.IOUtils) StringUtils.replace(org.apache.commons.lang3.StringUtils.replace) ValidationResult.failure(org.mule.runtime.api.el.ValidationResult.failure) DEFAULT_EXPRESSION_PREFIX(org.mule.runtime.core.api.el.ExpressionManager.DEFAULT_EXPRESSION_PREFIX) Iterator(java.util.Iterator) DataType(org.mule.runtime.api.metadata.DataType) PREFIX_EXPR_SEPARATOR(org.mule.runtime.core.internal.el.DefaultExpressionManager.PREFIX_EXPR_SEPARATOR) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) ValidationResult(org.mule.runtime.api.el.ValidationResult) IOException(java.io.IOException) OBJECT(org.mule.runtime.api.metadata.DataType.OBJECT) Initialisable(org.mule.runtime.api.lifecycle.Initialisable) MvelEnricherDataTypePropagator(org.mule.runtime.core.internal.el.mvel.datatype.MvelEnricherDataTypePropagator) AbstractComponent(org.mule.runtime.api.component.AbstractComponent) Serializable(java.io.Serializable) TypedValue(org.mule.runtime.api.metadata.TypedValue) MEL_PREFIX(org.mule.runtime.core.internal.el.DefaultExpressionManager.MEL_PREFIX) VariableResolverFactory(org.mule.mvel2.integration.VariableResolverFactory) CoreMessages.objectIsNull(org.mule.runtime.core.api.config.i18n.CoreMessages.objectIsNull) CompileException(org.mule.mvel2.CompileException) Entry(java.util.Map.Entry) ComponentLocation(org.mule.runtime.api.component.location.ComponentLocation) CompilerTools(org.mule.mvel2.util.CompilerTools) MvelDataTypeResolver(org.mule.runtime.core.internal.el.mvel.datatype.MvelDataTypeResolver) Pattern(java.util.regex.Pattern) Serializable(java.io.Serializable) PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) DataType(org.mule.runtime.api.metadata.DataType) TypedValue(org.mule.runtime.api.metadata.TypedValue)

Example 8 with DataType

use of org.mule.runtime.api.metadata.DataType in project mule by mulesoft.

the class MvelDataTypeResolver method resolve.

/**
 * Resolves expression data type
 *
 * @param value result of evaluating the expression on the given message
 * @param event event under evaluation
 * @param serializedExpression expression
 * @return the data type corresponding to the given expression if there is an expression resolver able to resolved it. A default
 *         data type for the value if the expression was not resolved.
 */
public DataType resolve(Object value, PrivilegedEvent event, Serializable serializedExpression) {
    DataType result = null;
    if (serializedExpression instanceof CompiledExpression) {
        CompiledExpression compiledExpression = (CompiledExpression) serializedExpression;
        for (ExpressionDataTypeResolver resolver : resolvers) {
            result = resolver.resolve(event, compiledExpression);
            if (result != null) {
                break;
            }
        }
    }
    if (result == null) {
        Class<?> type = value == null ? Object.class : value.getClass();
        result = DataType.fromType(type);
    }
    return result;
}
Also used : DataType(org.mule.runtime.api.metadata.DataType) CompiledExpression(org.mule.mvel2.compiler.CompiledExpression)

Example 9 with DataType

use of org.mule.runtime.api.metadata.DataType in project mule by mulesoft.

the class TransformerWeighting method init.

private void init() {
    inputWeighting = Integer.MAX_VALUE;
    List<DataType> sourceTypes = transformer.getSourceDataTypes();
    for (DataType type : sourceTypes) {
        int weighting = getWeighting(-1, inputClass, type.getType());
        if (weighting < inputWeighting && weighting != -1) {
            inputWeighting = weighting;
        }
    }
    outputWeighting = getWeighting(-1, outputClass, transformer.getReturnDataType().getType());
    inputWeighting = (inputWeighting == Integer.MAX_VALUE ? -1 : inputWeighting);
    outputWeighting = (outputWeighting == Integer.MAX_VALUE ? -1 : outputWeighting);
}
Also used : DataType(org.mule.runtime.api.metadata.DataType)

Example 10 with DataType

use of org.mule.runtime.api.metadata.DataType in project mule by mulesoft.

the class TransformationGraph method addConverter.

public void addConverter(Converter converter) {
    if (registeredConverters.contains(converter)) {
        if (logger.isDebugEnabled()) {
            logger.debug("Attempting to register an already registered converter: " + converter);
        }
        return;
    }
    DataType returnDataType = converter.getReturnDataType();
    if (!containsVertex(returnDataType)) {
        addVertex(returnDataType);
    }
    for (DataType sourceDataType : converter.getSourceDataTypes()) {
        if (!containsVertex(sourceDataType)) {
            addVertex(sourceDataType);
        }
        addEdge(sourceDataType, returnDataType, new TransformationEdge(converter));
    }
    registeredConverters.add(converter);
}
Also used : DataType(org.mule.runtime.api.metadata.DataType)

Aggregations

DataType (org.mule.runtime.api.metadata.DataType)102 Test (org.junit.Test)67 SmallTest (org.mule.tck.size.SmallTest)29 DefaultCollectionDataType (org.mule.runtime.core.internal.metadata.DefaultCollectionDataType)24 FunctionDataType (org.mule.runtime.api.metadata.FunctionDataType)20 DefaultFunctionDataType (org.mule.runtime.core.internal.metadata.DefaultFunctionDataType)20 DefaultMapDataType (org.mule.runtime.core.internal.metadata.DefaultMapDataType)20 SimpleDataType (org.mule.runtime.core.internal.metadata.SimpleDataType)20 Message (org.mule.runtime.api.message.Message)18 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)18 TypedValue (org.mule.runtime.api.metadata.TypedValue)12 PrivilegedEvent (org.mule.runtime.core.privileged.event.PrivilegedEvent)12 CompiledExpression (org.mule.mvel2.compiler.CompiledExpression)10 CollectionDataType (org.mule.runtime.api.metadata.CollectionDataType)10 Transformer (org.mule.runtime.core.api.transformer.Transformer)10 ArrayList (java.util.ArrayList)9 List (java.util.List)9 ParserContext (org.mule.mvel2.ParserContext)7 MapDataType (org.mule.runtime.api.metadata.MapDataType)6 MVELExpressionLanguage (org.mule.runtime.core.internal.el.mvel.MVELExpressionLanguage)6