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.interceptor;
8   
9   import org.mule.api.MuleEvent;
10  import org.mule.api.MuleException;
11  import org.mule.api.construct.FlowConstruct;
12  import org.mule.api.construct.FlowConstructAware;
13  import org.mule.api.interceptor.Interceptor;
14  import org.mule.management.stats.ProcessingTime;
15  import org.mule.processor.AbstractInterceptingMessageProcessor;
16  
17  /**
18   * <code>EnvelopeInterceptor</code> is an intercepter that will fire before and after
19   * an event is received.
20   */
21  public abstract class AbstractEnvelopeInterceptor extends AbstractInterceptingMessageProcessor
22                                                    implements Interceptor, FlowConstructAware
23  {
24  
25      protected FlowConstruct flowConstruct;
26  
27      /**
28       * This method is invoked before the event is processed
29       */
30      public abstract MuleEvent before(MuleEvent event) throws MuleException;
31  
32      /**
33       * This method is invoked after the event has been processed, unless an exception was thrown
34       */
35      public abstract MuleEvent after(MuleEvent event) throws MuleException;
36  
37      /**
38       *  This method is always invoked after the event has been processed,
39       */
40      public abstract MuleEvent last(MuleEvent event, ProcessingTime time, long startTime, boolean exceptionWasThrown) throws MuleException;
41  
42      public MuleEvent process(MuleEvent event) throws MuleException
43      {
44          boolean exceptionWasThrown = true;
45          long startTime = System.currentTimeMillis();
46          ProcessingTime time = event.getProcessingTime();
47          MuleEvent resultEvent = event;
48          try
49          {
50              resultEvent = before(event);
51              resultEvent = processNext(resultEvent);
52              resultEvent = after(resultEvent);
53              exceptionWasThrown = false;
54          }
55          finally
56          {
57              resultEvent = last(resultEvent, time, startTime, exceptionWasThrown);
58          }
59          return resultEvent;
60      }
61  
62      public void setFlowConstruct(FlowConstruct flowConstruct)
63      {
64          this.flowConstruct = flowConstruct;
65      }
66  }