View Javadoc

1   /*
2    * $Id: MapSplitter.java 21307 2011-02-17 15:35:44Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.routing;
12  
13  import org.mule.DefaultMuleMessage;
14  import org.mule.api.MuleEvent;
15  import org.mule.api.MuleMessage;
16  import org.mule.config.i18n.CoreMessages;
17  
18  import java.util.LinkedList;
19  import java.util.List;
20  import java.util.Map;
21  import java.util.Set;
22  import java.util.Map.Entry;
23  
24  /**
25   * Splits a message that has a map payload invoking the next message processor one
26   * for each item in the map in order. The Map entry value is used as the new payload
27   * and the map key is set as a message property with the following property name
28   * 'key'.
29   * <p>
30   * <b>EIP Reference:</b> <a
31   * href="http://www.eaipatterns.com/Sequencer.html">http://www
32   * .eaipatterns.com/Sequencer.html</a>
33   */
34  public class MapSplitter extends AbstractSplitter
35  {
36      public static String MAP_ENTRY_KEY = "key";
37  
38      protected List<MuleMessage> splitMessage(MuleEvent event)
39      {
40          MuleMessage message = event.getMessage();
41          if (message.getPayload() instanceof Map<?, ?>)
42          {
43              List<MuleMessage> list = new LinkedList<MuleMessage>();
44              Set<Map.Entry<?, ?>> set = ((Map) message.getPayload()).entrySet();
45              for (Entry<?, ?> entry : set)
46              {
47                  MuleMessage splitMessage = new DefaultMuleMessage(entry.getValue(), muleContext);
48                  splitMessage.setInvocationProperty(MAP_ENTRY_KEY, entry.getKey());
49                  list.add(splitMessage);
50              }
51              return list;
52          }
53          else
54          {
55              throw new IllegalArgumentException(CoreMessages.objectNotOfCorrectType(
56                  message.getPayload().getClass(), Map.class).getMessage());
57          }
58      }
59  }