View Javadoc

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