View Javadoc

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