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;
8   
9   import org.mule.api.MuleContext;
10  import org.mule.api.MuleEvent;
11  import org.mule.api.MuleException;
12  import org.mule.api.construct.FlowConstruct;
13  import org.mule.api.context.MuleContextAware;
14  import org.mule.api.context.notification.ServerNotificationHandler;
15  import org.mule.api.processor.InterceptingMessageProcessor;
16  import org.mule.api.processor.MessageProcessor;
17  import org.mule.api.processor.MessageProcessorChain;
18  import org.mule.context.notification.MessageProcessorNotification;
19  import org.mule.processor.chain.DefaultMessageProcessorChain;
20  import org.mule.util.ObjectUtils;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  
25  /**
26   * Abstract implementation of {@link InterceptingMessageProcessor} that simply
27   * provides an implementation of setNext and holds the next message processor as an
28   * attribute.
29   */
30  public abstract class AbstractInterceptingMessageProcessor
31      implements InterceptingMessageProcessor, MuleContextAware
32  {
33      protected Log logger = LogFactory.getLog(getClass());
34  
35      protected ServerNotificationHandler notificationHandler;
36  
37      protected MuleContext muleContext;
38  
39      public void setMuleContext(MuleContext context)
40      {
41          this.muleContext = context;
42          notificationHandler = muleContext.getNotificationManager();
43          if (next instanceof DefaultMessageProcessorChain)
44          {
45              ((DefaultMessageProcessorChain) next).setMuleContext(context);
46          }
47      }
48  
49      public void setListener(MessageProcessor next)
50      {
51          this.next = next;
52      }
53  
54      protected MessageProcessor next;
55  
56      protected MuleEvent processNext(MuleEvent event) throws MuleException
57      {
58          if (next == null)
59          {
60              return event;
61          }
62          else if (event == null)
63          {
64              if (logger.isDebugEnabled())
65              {
66                  logger.trace("MuleEvent is null.  Next MessageProcessor '" + next.getClass().getName()
67                               + "' will not be invoked.");
68              }
69              return null;
70          }
71          else
72          {
73              if (logger.isTraceEnabled())
74              {
75                  logger.trace("Invoking next MessageProcessor: '" + next.getClass().getName() + "' ");
76              }
77  
78              boolean fireNotification = !(next instanceof MessageProcessorChain);
79  
80              if (fireNotification)
81              {
82                  // note that we're firing event for the next in chain, not this MP
83                  fireNotification(event.getFlowConstruct(), event, next,
84                      MessageProcessorNotification.MESSAGE_PROCESSOR_PRE_INVOKE);
85              }
86              final MuleEvent result = next.process(event);
87              if (fireNotification)
88              {
89                  fireNotification(event.getFlowConstruct(), result, next,
90                      MessageProcessorNotification.MESSAGE_PROCESSOR_POST_INVOKE);
91              }
92              return result;
93          }
94      }
95  
96      public MuleContext getMuleContext()
97      {
98          return muleContext;
99      }
100 
101     @Override
102     public String toString()
103     {
104         return ObjectUtils.toString(this);
105     }
106 
107     protected void fireNotification(FlowConstruct flowConstruct, MuleEvent event, MessageProcessor processor, int action)
108     {
109         if (notificationHandler != null
110             && notificationHandler.isNotificationEnabled(MessageProcessorNotification.class))
111         {
112             notificationHandler.fireNotification(new MessageProcessorNotification(flowConstruct, event, processor, action));
113         }
114     }
115 
116 }