All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mule.routing.ExpressionSplitter Maven / Gradle / Ivy

There is a newer version: 3.9.0
Show newest version
/*
 * $Id: ExpressionSplitter.java 23822 2012-02-06 14:47:37Z svacas $
 * --------------------------------------------------------------------------------------
 * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 *
 * The software in this package is published under the terms of the CPAL v1.0
 * license, a copy of which has been included with this distribution in the
 * LICENSE.txt file.
 */

package org.mule.routing;

import org.mule.DefaultMuleMessage;
import org.mule.api.MuleEvent;
import org.mule.api.MuleMessage;
import org.mule.api.expression.ExpressionManager;
import org.mule.api.lifecycle.Initialisable;
import org.mule.api.lifecycle.InitialisationException;
import org.mule.expression.ExpressionConfig;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 * Splits a message using the expression provided invoking the next message processor
 * one for each split part.
 * 

* EIP Reference: http://www.eaipatterns.com/Sequencer.html */ public class ExpressionSplitter extends AbstractSplitter implements Initialisable { protected ExpressionManager expressionManager; protected ExpressionConfig config = new ExpressionConfig(); public ExpressionSplitter() { // Used by spring } public ExpressionSplitter(ExpressionConfig config) { this.config = config; setEvaluator(config.getEvaluator()); } public void initialise() throws InitialisationException { expressionManager = muleContext.getExpressionManager(); config.validate(expressionManager); } protected List splitMessage(MuleEvent event) { Object result = event.getMuleContext().getExpressionManager().evaluate( config.getFullExpression(expressionManager), event.getMessage()); if (result instanceof List) { List messages = new ArrayList(); for (Object object : (List) result) { messages.add(new DefaultMuleMessage(object, muleContext)); } return messages; } else if (result instanceof Map) { List list = new LinkedList(); Set> set = ((Map) result).entrySet(); for (Entry entry : set) { MuleMessage message = new DefaultMuleMessage(entry.getValue(), muleContext); message.setInvocationProperty(MapSplitter.MAP_ENTRY_KEY, entry.getKey()); list.add(message); } return list; } else if (result instanceof MuleMessage) { return Collections.singletonList((MuleMessage) result); } else { return Collections. singletonList(new DefaultMuleMessage(result, muleContext)); } } public String getCustomEvaluator() { return config.getCustomEvaluator(); } public void setCustomEvaluator(String customEvaluator) { config.setCustomEvaluator(customEvaluator); } public String getEvaluator() { return config.getEvaluator(); } public void setEvaluator(String evaluator) { // Switch to XPath node since we want the Dom nodes not the value of the node if (evaluator.equals("xpath")) { evaluator = "xpath-node"; } config.setEvaluator(evaluator); } public String getExpression() { return config.getExpression(); } public void setExpression(String expression) { this.config.setExpression(expression); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy