View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.processor.chain;
8   
9   import org.mule.api.MuleContext;
10  import org.mule.api.MuleEvent;
11  import org.mule.api.MuleException;
12  import org.mule.api.context.MuleContextAware;
13  import org.mule.api.processor.MessageProcessor;
14  import org.mule.api.processor.MessageProcessorChain;
15  import org.mule.api.processor.policy.Policies;
16  import org.mule.context.notification.MessageProcessorNotification;
17  
18  import java.util.List;
19  
20  /**
21   * Builder needs to return a composite rather than the first MessageProcessor in the
22   * chain. This is so that if this chain is nested in another chain the next
23   * MessageProcessor in the parent chain is not injected into the first in the nested
24   * chain.
25   */
26  public class InterceptingChainLifecycleWrapper extends AbstractMessageProcessorChain
27  {
28      private MessageProcessorChain chain;
29  
30      public InterceptingChainLifecycleWrapper(MessageProcessorChain chain,
31                                               List<MessageProcessor> processors,
32                                               String name)
33      {
34          super(name, processors);
35          this.chain = chain;
36      }
37  
38      @Override
39      public List<MessageProcessor> getMessageProcessors()
40      {
41          return chain.getMessageProcessors();
42      }
43  
44      @Override
45      public String getName()
46      {
47          return chain.getName();
48      }
49  
50      @Override
51      public Policies getPolicies()
52      {
53          return chain.getPolicies();
54      }
55  
56      @Override
57      protected MuleEvent doProcess(MuleEvent event) throws MuleException
58      {
59          return chain.process(event);
60      }
61  
62      @Override
63      public void setMuleContext(MuleContext context)
64      {
65          super.setMuleContext(context);
66          for (MessageProcessor processor : processors)
67          {
68              if (processor instanceof MuleContextAware)
69              {
70                  ((MuleContextAware) processor).setMuleContext(context);
71              }
72          }
73          if (chain instanceof MuleContextAware)
74          {
75              ((MuleContextAware) chain).setMuleContext(context);
76          }
77      }
78  
79      @Override
80      public MuleEvent process(MuleEvent event) throws MuleException
81      {
82          if (event == null)
83          {
84              return null;
85          }
86  
87          fireNotification(event.getFlowConstruct(), event, this,
88              MessageProcessorNotification.MESSAGE_PROCESSOR_PRE_INVOKE);
89  
90          MuleEvent result = super.process(event);
91  
92          fireNotification(event.getFlowConstruct(), result, this,
93              MessageProcessorNotification.MESSAGE_PROCESSOR_POST_INVOKE);
94  
95          return result;
96      }
97  }