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;
}
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;
}
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);
}
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);
}
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);
}
Aggregations