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.api.processor.policy;
8   
9   import org.mule.api.MuleEvent;
10  import org.mule.api.MuleException;
11  import org.mule.api.processor.MessageProcessor;
12  
13  import java.util.LinkedList;
14  import java.util.List;
15  
16  /**
17   *
18   */
19  public class PolicyInvocation
20  {
21      private MuleEvent event;
22      private List<AroundPolicy> policies = new LinkedList<AroundPolicy>();
23      private volatile int currentPolicyIndex = 0;
24      private MessageProcessor messageProcessor;
25  
26      public PolicyInvocation(MuleEvent event, List<AroundPolicy> policies, MessageProcessor processor)
27      {
28          this.event = event;
29          this.policies = policies;
30          this.messageProcessor = processor;
31      }
32  
33      /**
34       * Proceed using the current event.
35       *
36       * @see #setEvent(org.mule.api.MuleEvent)
37       */
38      public MuleEvent proceed() throws MuleException
39      {
40          currentPolicyIndex++;
41          if (currentPolicyIndex == policies.size())
42          {
43              // end of chain
44              return messageProcessor.process(event);
45          }
46          final AroundPolicy currentPolicy = getCurrentPolicy();
47          final MuleEvent result = currentPolicy.invoke(this);
48          setEvent(result);
49          return event;
50      }
51  
52      public MuleEvent getEvent()
53      {
54          return event;
55      }
56  
57      /**
58       * Replace the event object completely. Note that most of the time it's enough to simply
59       * modify the event without any rewriting.
60       *
61       * @see #getEvent()
62       */
63      public void setEvent(MuleEvent event)
64      {
65          this.event = event;
66      }
67  
68      /**
69       * @return policy at the current index in the list
70       */
71      public AroundPolicy getCurrentPolicy()
72      {
73          return policies.get(currentPolicyIndex);
74      }
75  
76      public MessageProcessor getMessageProcessor()
77      {
78          return messageProcessor;
79      }
80  
81      public List<AroundPolicy> getPolicies()
82      {
83          return policies;
84      }
85  }