View Javadoc

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