View Javadoc

1   /*
2    * $Id: InterceptingChainLifecycleWrapper.java 22549 2011-07-24 21:47:28Z 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.processor.chain;
12  
13  import org.mule.api.MuleContext;
14  import org.mule.api.MuleEvent;
15  import org.mule.api.MuleException;
16  import org.mule.api.context.MuleContextAware;
17  import org.mule.api.processor.MessageProcessor;
18  import org.mule.api.processor.MessageProcessorChain;
19  import org.mule.context.notification.MessageProcessorNotification;
20  
21  import java.util.List;
22  
23  /**
24   * Builder needs to return a composite rather than the first MessageProcessor in the
25   * chain. This is so that if this chain is nested in another chain the next
26   * MessageProcessor in the parent chain is not injected into the first in the nested
27   * chain.
28   */
29  public class InterceptingChainLifecycleWrapper extends AbstractMessageProcessorChain
30  {
31      private MessageProcessorChain chain;
32  
33      public InterceptingChainLifecycleWrapper(MessageProcessorChain chain,
34                                               List<MessageProcessor> processors,
35                                               String name)
36      {
37          super(name, processors);
38          this.chain = chain;
39      }
40  
41      @Override
42      public List<MessageProcessor> getMessageProcessors()
43      {
44          return chain.getMessageProcessors();
45      }
46  
47      @Override
48      public String getName()
49      {
50          return chain.getName();
51      }
52  
53      @Override
54      protected MuleEvent doProcess(MuleEvent event) throws MuleException
55      {
56          return chain.process(event);
57      }
58  
59      @Override
60      public void setMuleContext(MuleContext context)
61      {
62          super.setMuleContext(context);
63          for (MessageProcessor processor : processors)
64          {
65              if (processor instanceof MuleContextAware)
66              {
67                  ((MuleContextAware) processor).setMuleContext(context);
68              }
69          }
70          if (chain instanceof MuleContextAware)
71          {
72              ((MuleContextAware) chain).setMuleContext(context);
73          }
74      }
75  
76      @Override
77      public MuleEvent process(MuleEvent event) throws MuleException
78      {
79          if (event == null)
80          {
81              return null;
82          }
83  
84          fireNotification(event.getFlowConstruct(), event, this,
85              MessageProcessorNotification.MESSAGE_PROCESSOR_PRE_INVOKE);
86  
87          MuleEvent result = super.process(event);
88  
89          fireNotification(event.getFlowConstruct(), result, this,
90              MessageProcessorNotification.MESSAGE_PROCESSOR_POST_INVOKE);
91  
92          return result;
93      }
94  }