Search in sources :

Example 1 with IteratorMessageSequence

use of org.mule.runtime.core.internal.routing.outbound.IteratorMessageSequence 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)

Aggregations

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 Message (org.mule.runtime.api.message.Message)1 TypedValue (org.mule.runtime.api.metadata.TypedValue)1 CursorIteratorProvider (org.mule.runtime.api.streaming.object.CursorIteratorProvider)1 ExpressionRuntimeException (org.mule.runtime.core.api.expression.ExpressionRuntimeException)1 CollectionMessageSequence (org.mule.runtime.core.internal.routing.outbound.CollectionMessageSequence)1 IteratorMessageSequence (org.mule.runtime.core.internal.routing.outbound.IteratorMessageSequence)1 NodeListMessageSequence (org.mule.runtime.core.internal.routing.outbound.NodeListMessageSequence)1 ImmutableEntry (org.mule.runtime.internal.util.collection.ImmutableEntry)1 NodeList (org.w3c.dom.NodeList)1