use of org.mule.runtime.core.api.expression.ExpressionRuntimeException 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.expression.ExpressionRuntimeException in project mule by mulesoft.
the class IdempotentRedeliveryPolicy method process.
@Override
public CoreEvent process(CoreEvent event) throws MuleException {
Optional<Exception> exceptionSeen = empty();
String messageId = null;
try {
messageId = getIdForEvent(event);
} catch (ExpressionRuntimeException e) {
logger.warn("The message cannot be processed because the digest could not be generated. Either make the payload serializable or use an expression.");
return null;
} catch (Exception ex) {
exceptionSeen = of(ex);
}
Lock lock = lockFactory.createLock(idrId + "-" + messageId);
lock.lock();
try {
RedeliveryCounter counter = findCounter(messageId);
if (exceptionSeen.isPresent()) {
throw new MessageRedeliveredException(messageId, counter.counter.get(), maxRedeliveryCount, exceptionSeen.get());
} else if (counter != null && counter.counter.get() > maxRedeliveryCount) {
throw new MessageRedeliveredException(messageId, counter.errors, counter.counter.get(), maxRedeliveryCount);
}
try {
CoreEvent returnEvent = processNext(CoreEvent.builder(DefaultEventContext.child((BaseEventContext) event.getContext(), empty()), event).build());
counter = findCounter(messageId);
if (counter != null) {
resetCounter(messageId);
}
return returnEvent;
} catch (Exception ex) {
if (ex instanceof MessagingException) {
incrementCounter(messageId, (MessagingException) ex);
throw ex;
} else {
MessagingException me = createMessagingException(event, ex, this);
incrementCounter(messageId, me);
throw ex;
}
}
} finally {
lock.unlock();
}
}
use of org.mule.runtime.core.api.expression.ExpressionRuntimeException in project mule by mulesoft.
the class EventToMessageSequenceSplittingStrategy method split.
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public MessageSequence<?> split(CoreEvent event) {
if (expressionSplitterStrategy.hasDefaultExpression()) {
Message msg = event.getMessage();
Object payload = msg.getPayload().getValue();
if (payload instanceof MessageSequence<?>) {
return ((MessageSequence<?>) payload);
}
if (payload instanceof Iterator<?>) {
return new IteratorMessageSequence(((Iterator<Object>) payload));
}
if (payload instanceof Collection) {
return new CollectionMessageSequence(copyCollection((Collection) payload));
} else if (payload instanceof CursorIteratorProvider) {
return new IteratorMessageSequence(((CursorIteratorProvider) payload).openCursor());
} else if (payload instanceof Iterable<?>) {
return new IteratorMessageSequence(((Iterable<Object>) payload).iterator());
} else if (payload instanceof Object[]) {
return new ArrayMessageSequence((Object[]) payload);
} else if (payload instanceof NodeList) {
return new NodeListMessageSequence((NodeList) payload);
} else if (payload instanceof Map<?, ?>) {
List<Object> list = new LinkedList<>();
Set<Map.Entry<?, ?>> set = ((Map) payload).entrySet();
for (Map.Entry<?, ?> entry : set) {
list.add(new ImmutableEntry<>(entry));
}
return new CollectionMessageSequence(list);
}
}
try {
Iterator<TypedValue<?>> valueIterator = expressionSplitterStrategy.split(event);
if (hasMelExpression(expressionSplitterStrategy.getExpression())) {
List<Object> iteratorCollection = new ArrayList<>();
valueIterator.forEachRemaining(iteratorCollection::add);
return new CollectionMessageSequence<>(iteratorCollection);
}
return new IteratorMessageSequence(valueIterator);
} catch (ExpressionRuntimeException e) {
throw e;
} catch (Exception e) {
throw new IllegalArgumentException(format("Could not split result of expression %s. The provided value is not instance of %s java " + "type or it's not a collection in any other format", expressionSplitterStrategy.getExpression(), new Class[] { Iterable.class, Iterator.class, MessageSequence.class, Collection.class }), e);
}
}
use of org.mule.runtime.core.api.expression.ExpressionRuntimeException in project mule by mulesoft.
the class MVELExpressionLanguage method evaluateInternal.
@SuppressWarnings("unchecked")
protected <T> T evaluateInternal(String expression, MVELExpressionLanguageContext variableResolverFactory) {
validate(expression);
expression = removeExpressionMarker(expression);
try {
return (T) expressionExecutor.execute(expression, variableResolverFactory);
} catch (Exception e) {
throw new ExpressionRuntimeException(CoreMessages.expressionEvaluationFailed(e.getMessage(), expression), e);
}
}
use of org.mule.runtime.core.api.expression.ExpressionRuntimeException in project mule by mulesoft.
the class ExpressionTransformer method transformMessage.
@Override
public Object transformMessage(CoreEvent event, Charset outputEncoding) throws MessageTransformerException {
Object[] results = new Object[arguments.size()];
int i = 0;
for (Iterator<ExpressionArgument> iterator = arguments.iterator(); iterator.hasNext(); i++) {
ExpressionArgument argument = iterator.next();
try {
results[i] = argument.evaluate(event);
} catch (ExpressionRuntimeException e) {
throw new MessageTransformerException(this, e, event.getMessage());
}
if (!argument.isOptional() && results[i] == null) {
throw new MessageTransformerException(expressionReturnedNull(argument.getExpression()), this, event.getMessage());
}
}
if (isReturnSourceIfNull() && checkIfAllAreNull(results)) {
return event.getMessage();
}
if (results.length == 1) {
return results[0];
} else {
return results;
}
}
Aggregations