View Javadoc

1   /*
2    * $Id: AbstractInterceptingMessageProcessorBase.java 22566 2011-07-27 22:52:53Z julien.eluard $
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 java.util.Collections;
14  import java.util.Map;
15  import java.util.concurrent.ConcurrentHashMap;
16  
17  import javax.xml.namespace.QName;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  import org.mule.api.AnnotatedObject;
22  import org.mule.api.MuleContext;
23  import org.mule.api.MuleEvent;
24  import org.mule.api.MuleException;
25  import org.mule.api.construct.FlowConstruct;
26  import org.mule.api.context.MuleContextAware;
27  import org.mule.api.context.notification.ServerNotificationHandler;
28  import org.mule.api.processor.MessageProcessor;
29  import org.mule.api.processor.MessageProcessorChain;
30  import org.mule.context.notification.MessageProcessorNotification;
31  import org.mule.processor.chain.DefaultMessageProcessorChain;
32  import org.mule.util.ObjectUtils;
33  
34  /**
35   * Abstract implementation that provides the infrastructure for intercepting message processors.
36   * It doesn't implement InterceptingMessageProcessor itself, to let individual subclasses make that decision \.
37   * This simply provides an implementation of setNext and holds the next message processor as an
38   * attribute.
39   */
40  public abstract class AbstractInterceptingMessageProcessorBase
41      implements MessageProcessor, MuleContextAware, AnnotatedObject
42  {
43      protected Log logger = LogFactory.getLog(getClass());
44  
45      protected ServerNotificationHandler notificationHandler;
46  
47      protected MuleContext muleContext;
48      private final Map<QName, Object> annotations = new ConcurrentHashMap<QName, Object>();
49  
50      public void setMuleContext(MuleContext context)
51      {
52          this.muleContext = context;
53          notificationHandler = muleContext.getNotificationManager();
54          if (next instanceof DefaultMessageProcessorChain)
55          {
56              ((DefaultMessageProcessorChain) next).setMuleContext(context);
57          }
58      }
59  
60      public final MessageProcessor getListener()
61      {
62          return next;
63      }
64  
65      public void setListener(MessageProcessor next)
66      {
67          this.next = next;
68      }
69  
70      protected MessageProcessor next;
71  
72      protected MuleEvent processNext(MuleEvent event) throws MuleException
73      {
74          if (next == null)
75          {
76              return event;
77          }
78          else if (event == null)
79          {
80              if (logger.isDebugEnabled())
81              {
82                  logger.trace("MuleEvent is null.  Next MessageProcessor '" + next.getClass().getName()
83                               + "' will not be invoked.");
84              }
85              return null;
86          }
87          else
88          {
89              if (logger.isTraceEnabled())
90              {
91                  logger.trace("Invoking next MessageProcessor: '" + next.getClass().getName() + "' ");
92              }
93  
94              boolean fireNotification = !(next instanceof MessageProcessorChain);
95  
96              if (fireNotification)
97              {
98                  // note that we're firing event for the next in chain, not this MP
99                  fireNotification(event.getFlowConstruct(), event, next,
100                     MessageProcessorNotification.MESSAGE_PROCESSOR_PRE_INVOKE);
101             }
102             final MuleEvent result;
103             try
104             {
105                 result = next.process(event);
106             }
107             catch (MuleException e)
108             {
109                 event.getSession().setValid(false);
110                 throw e;
111             }
112             if (fireNotification)
113             {
114                 fireNotification(event.getFlowConstruct(), result, next,
115                     MessageProcessorNotification.MESSAGE_PROCESSOR_POST_INVOKE);
116             }
117             return result;
118         }
119     }
120 
121     public MuleContext getMuleContext()
122     {
123         return muleContext;
124     }
125 
126     @Override
127     public String toString()
128     {
129         return ObjectUtils.toString(this);
130     }
131 
132     protected void fireNotification(FlowConstruct flowConstruct, MuleEvent event, MessageProcessor processor, int action)
133     {
134         if (notificationHandler != null
135             && notificationHandler.isNotificationEnabled(MessageProcessorNotification.class))
136         {
137             notificationHandler.fireNotification(new MessageProcessorNotification(flowConstruct, event, processor, action));
138         }
139     }
140 
141     public final Object getAnnotation(QName name)
142     {
143         return annotations.get(name);
144     }
145 
146     public final Map<QName, Object> getAnnotations()
147     {
148         return Collections.unmodifiableMap(annotations);
149     }
150 
151     public synchronized final void setAnnotations(Map<QName, Object> newAnnotations)
152     {
153         annotations.clear();
154         annotations.putAll(newAnnotations);
155     }
156 }