Search in sources :

Example 1 with ExpressionRuntimeException

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;
}
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 2 with ExpressionRuntimeException

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();
    }
}
Also used : MessageRedeliveredException(org.mule.runtime.core.privileged.exception.MessageRedeliveredException) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) ExpressionRuntimeException(org.mule.runtime.core.api.expression.ExpressionRuntimeException) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) ObjectStoreException(org.mule.runtime.api.store.ObjectStoreException) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) ExpressionRuntimeException(org.mule.runtime.core.api.expression.ExpressionRuntimeException) MuleException(org.mule.runtime.api.exception.MuleException) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) MessageRedeliveredException(org.mule.runtime.core.privileged.exception.MessageRedeliveredException) Lock(java.util.concurrent.locks.Lock)

Example 3 with ExpressionRuntimeException

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);
    }
}
Also used : ExpressionRuntimeException(org.mule.runtime.core.api.expression.ExpressionRuntimeException) NodeListMessageSequence(org.mule.runtime.core.internal.routing.outbound.NodeListMessageSequence) Set(java.util.Set) Message(org.mule.runtime.api.message.Message) ArrayList(java.util.ArrayList) CursorIteratorProvider(org.mule.runtime.api.streaming.object.CursorIteratorProvider) ImmutableEntry(org.mule.runtime.internal.util.collection.ImmutableEntry) CollectionMessageSequence(org.mule.runtime.core.internal.routing.outbound.CollectionMessageSequence) Iterator(java.util.Iterator) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) TypedValue(org.mule.runtime.api.metadata.TypedValue) ImmutableEntry(org.mule.runtime.internal.util.collection.ImmutableEntry) NodeList(org.w3c.dom.NodeList) IteratorMessageSequence(org.mule.runtime.core.internal.routing.outbound.IteratorMessageSequence) ExpressionRuntimeException(org.mule.runtime.core.api.expression.ExpressionRuntimeException) NodeListMessageSequence(org.mule.runtime.core.internal.routing.outbound.NodeListMessageSequence) IteratorMessageSequence(org.mule.runtime.core.internal.routing.outbound.IteratorMessageSequence) CollectionMessageSequence(org.mule.runtime.core.internal.routing.outbound.CollectionMessageSequence) Collection(java.util.Collection) Map(java.util.Map)

Example 4 with ExpressionRuntimeException

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);
    }
}
Also used : ExpressionRuntimeException(org.mule.runtime.core.api.expression.ExpressionRuntimeException) OBJECT(org.mule.runtime.api.metadata.DataType.OBJECT) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) ExpressionRuntimeException(org.mule.runtime.core.api.expression.ExpressionRuntimeException) IOException(java.io.IOException) CompileException(org.mule.mvel2.CompileException)

Example 5 with ExpressionRuntimeException

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;
    }
}
Also used : ExpressionRuntimeException(org.mule.runtime.core.api.expression.ExpressionRuntimeException) MessageTransformerException(org.mule.runtime.core.api.transformer.MessageTransformerException)

Aggregations

ExpressionRuntimeException (org.mule.runtime.core.api.expression.ExpressionRuntimeException)6 InitialisationException (org.mule.runtime.api.lifecycle.InitialisationException)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Lock (java.util.concurrent.locks.Lock)1 DescriptorKey (javax.management.DescriptorKey)1 Test (org.junit.Test)1 CompileException (org.mule.mvel2.CompileException)1 MuleException (org.mule.runtime.api.exception.MuleException)1 Message (org.mule.runtime.api.message.Message)1 OBJECT (org.mule.runtime.api.metadata.DataType.OBJECT)1 TypedValue (org.mule.runtime.api.metadata.TypedValue)1 ObjectStoreException (org.mule.runtime.api.store.ObjectStoreException)1 CursorIteratorProvider (org.mule.runtime.api.streaming.object.CursorIteratorProvider)1