View Javadoc

1   /*
2    * $Id: DefaultMessageProcessorChain.java 20320 2010-11-24 15:03:31Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.processor.chain;
12  
13  import org.mule.DefaultMuleEvent;
14  import org.mule.OptimizedRequestContext;
15  import org.mule.api.MuleEvent;
16  import org.mule.api.MuleException;
17  import org.mule.api.construct.FlowConstruct;
18  import org.mule.api.endpoint.OutboundEndpoint;
19  import org.mule.api.processor.MessageProcessor;
20  import org.mule.construct.SimpleFlowConstruct;
21  
22  import java.util.Arrays;
23  import java.util.List;
24  
25  public class DefaultMessageProcessorChain extends AbstractMessageProcessorChain
26  {
27  
28      public DefaultMessageProcessorChain(List<MessageProcessor> processors)
29      {
30          super(null, processors);
31      }
32  
33      public DefaultMessageProcessorChain(MessageProcessor... processors)
34      {
35          super(null, Arrays.asList(processors));
36      }
37  
38      public DefaultMessageProcessorChain(String name, List<MessageProcessor> processors)
39      {
40          super(name, processors);
41      }
42  
43      public DefaultMessageProcessorChain(String name, MessageProcessor... processors)
44      {
45          super(name, Arrays.asList(processors));
46      }
47  
48      protected MuleEvent doProcess(MuleEvent event) throws MuleException
49      {
50          FlowConstruct flowConstruct = event.getFlowConstruct();
51          MuleEvent currentEvent = event;
52          MuleEvent resultEvent;
53          for (MessageProcessor processor : processors)
54          {
55              // If the next message processor is an outbound router then create
56              // outbound event
57              if (processor instanceof OutboundEndpoint)
58              {
59                  currentEvent = new DefaultMuleEvent(currentEvent.getMessage(), (OutboundEndpoint) processor,
60                      currentEvent.getSession());
61              }
62              resultEvent = processor.process(currentEvent);
63              if (resultEvent != null)
64              {
65                  currentEvent = resultEvent;
66              }
67              else
68              {
69                  if (flowConstruct instanceof SimpleFlowConstruct)
70                  {
71                      currentEvent = OptimizedRequestContext.criticalSetEvent(currentEvent);
72                  }
73                  else
74                  {
75                      return null;
76                  }
77              }
78          }
79          return currentEvent;
80      }
81  
82  }