Search in sources :

Example 11 with Transformer

use of org.mule.runtime.core.api.transformer.Transformer in project mule by mulesoft.

the class ExpressionArgument method evaluate.

/**
 * Evaluates this Expression against the passed in Message. If a returnClass is set on this Expression Argument it will be
 * checked to ensure the Argument returns the correct class type.
 *
 * @param event the event to execute the expression on
 * @return the result of the expression
 * @throws ExpressionRuntimeException if the wrong return type is returned from the expression.
 */
public Object evaluate(CoreEvent event) throws ExpressionRuntimeException {
    // MULE-4797 Because there is no way to specify the class-loader that script
    // engines use and because scripts when used for expressions are compiled in
    // runtime rather than at initialization the only way to ensure the correct
    // class-loader to used is to switch it out here. We may want to consider
    // passing the class-loader to the MuleExpressionLanguage and only doing this for
    // certain ExpressionEvaluators further in.
    Object result = withContextClassLoader(expressionEvaluationClassLoader, () -> muleContext.getExpressionManager().evaluate(getExpression(), event).getValue());
    if (getReturnClass() != null && result != null) {
        if (!getReturnClass().isInstance(result)) {
            // If the return type does not match, lets attempt to transform it before throwing an error
            try {
                Transformer t = ((MuleContextWithRegistries) muleContext).getRegistry().lookupTransformer(DataType.fromObject(result), DataType.fromType(getReturnClass()));
                result = t.transform(result);
            } catch (TransformerException e) {
                throw new ExpressionRuntimeException(transformUnexpectedType(result.getClass(), getReturnClass()), e);
            }
        }
    // if(result instanceof Collection && ((Collection)result).size()==0 && !isOptional())
    // {
    // throw new ExpressionRuntimeException(CoreMessages.expressionEvaluatorReturnedNull(this.getEvaluator(),
    // this.getExpression()));
    // }
    }
    return result;
}
Also used : ExpressionRuntimeException(org.mule.runtime.core.api.expression.ExpressionRuntimeException) Transformer(org.mule.runtime.core.api.transformer.Transformer) TransformerException(org.mule.runtime.core.api.transformer.TransformerException)

Example 12 with Transformer

use of org.mule.runtime.core.api.transformer.Transformer in project mule by mulesoft.

the class GraphTransformerResolver method resolve.

@Override
public Transformer resolve(DataType source, DataType result) throws ResolverException {
    String cacheKey = getDataTypeSourceResultPairHash(source, result);
    readWriteLock.readLock().lock();
    try {
        if (cache.containsKey(cacheKey)) {
            return (Converter) cache.get(cacheKey);
        }
    } finally {
        readWriteLock.readLock().unlock();
    }
    List<Converter> converters = converterFilter.filter(lookupStrategyTransformation.lookupConverters(source, result), source, result);
    if (converters.size() > 1) {
        throw new ResolverException(CoreMessages.transformHasMultipleMatches(source.getType(), result.getType(), converters));
    }
    Transformer converter = (converters.size() == 0) ? null : converters.get(0);
    readWriteLock.writeLock().lock();
    try {
        cache.put(cacheKey, converter);
    } finally {
        readWriteLock.writeLock().unlock();
    }
    return converter;
}
Also used : ResolverException(org.mule.runtime.core.internal.transformer.ResolverException) Transformer(org.mule.runtime.core.api.transformer.Transformer) Converter(org.mule.runtime.core.api.transformer.Converter)

Example 13 with Transformer

use of org.mule.runtime.core.api.transformer.Transformer in project mule by mulesoft.

the class TransformationServiceTestCase method skipsConverterThatDoesNotMatchWhenOriginalPayloadMatchesExpectedOutputType.

@Test
public void skipsConverterThatDoesNotMatchWhenOriginalPayloadMatchesExpectedOutputType() throws MuleException {
    // Converter(B->C), payload C: OK - skips transformer but C is the expected output type -> OK
    Transformer converter1 = new MockConverterBuilder().from(dataTypeB).to(dataTypeC).build();
    Message message = of(new C());
    message = transformationService.applyTransformers(message, null, converter1);
    assertTrue(message.getPayload().getValue() instanceof C);
    verifyTransformerNotExecuted(converter1);
}
Also used : Transformer(org.mule.runtime.core.api.transformer.Transformer) MockConverterBuilder(org.mule.runtime.core.internal.transformer.builder.MockConverterBuilder) Message(org.mule.runtime.api.message.Message) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 14 with Transformer

use of org.mule.runtime.core.api.transformer.Transformer in project mule by mulesoft.

the class TransformationServiceTestCase method failsOnConverterWhenSourceAndReturnTypeDoesNotMatchAndThereIsNoImplicitConversion.

@Test
public void failsOnConverterWhenSourceAndReturnTypeDoesNotMatchAndThereIsNoImplicitConversion() throws MuleException {
    // Converter(B->C), payload A: FAIL
    Transformer converter1 = new MockConverterBuilder().from(dataTypeB).to(dataTypeC).build();
    Message message = of(new A());
    try {
        transformationService.applyTransformers(message, null, converter1);
        fail("Transformation is supposed to fail");
    } catch (IllegalArgumentException expected) {
    }
    verifyTransformerNotExecuted(converter1);
}
Also used : Transformer(org.mule.runtime.core.api.transformer.Transformer) MockConverterBuilder(org.mule.runtime.core.internal.transformer.builder.MockConverterBuilder) Message(org.mule.runtime.api.message.Message) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 15 with Transformer

use of org.mule.runtime.core.api.transformer.Transformer in project mule by mulesoft.

the class TransformationServiceTestCase method skipsConverterFailsOnTransformerWhenOriginalPayloadMatchesExpectedOutputType.

@Test
public void skipsConverterFailsOnTransformerWhenOriginalPayloadMatchesExpectedOutputType() throws MuleException {
    // Converter(B -> D) Transformer(C->D), payload D: FAIL
    Transformer converter1 = new MockConverterBuilder().from(dataTypeB).to(dataTypeD).build();
    Transformer transformer2 = new MockTransformerBuilder().from(dataTypeC).to(dataTypeD).build();
    Message message = of(new D());
    try {
        transformationService.applyTransformers(message, null, converter1, transformer2);
        fail("Transformation is supposed to fail");
    } catch (IllegalArgumentException expected) {
    }
    verifyTransformerNotExecuted(converter1);
    verifyTransformerNotExecuted(transformer2);
}
Also used : Transformer(org.mule.runtime.core.api.transformer.Transformer) MockConverterBuilder(org.mule.runtime.core.internal.transformer.builder.MockConverterBuilder) Message(org.mule.runtime.api.message.Message) MockTransformerBuilder(org.mule.runtime.core.internal.transformer.builder.MockTransformerBuilder) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Aggregations

Transformer (org.mule.runtime.core.api.transformer.Transformer)87 Test (org.junit.Test)65 SmallTest (org.mule.tck.size.SmallTest)54 MockConverterBuilder (org.mule.runtime.core.internal.transformer.builder.MockConverterBuilder)37 Message (org.mule.runtime.api.message.Message)35 MockTransformerBuilder (org.mule.runtime.core.internal.transformer.builder.MockTransformerBuilder)19 DataType (org.mule.runtime.api.metadata.DataType)14 Converter (org.mule.runtime.core.api.transformer.Converter)12 ArrayList (java.util.ArrayList)10 TransformerException (org.mule.runtime.core.api.transformer.TransformerException)10 AbstractTransformer (org.mule.runtime.core.api.transformer.AbstractTransformer)9 CompositeConverter (org.mule.runtime.core.privileged.transformer.CompositeConverter)9 Serializable (java.io.Serializable)6 MuleContextWithRegistries (org.mule.runtime.core.internal.context.MuleContextWithRegistries)6 TransformerWeighting (org.mule.runtime.core.internal.registry.TransformerWeighting)6 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)4 MessageTransformerException (org.mule.runtime.core.api.transformer.MessageTransformerException)4 IOException (java.io.IOException)3 LinkedList (java.util.LinkedList)3 List (java.util.List)3