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.processor.MessageProcessorChain;
10  import org.mule.util.CollectionUtils;
11  
12  import java.util.Collections;
13  import java.util.LinkedList;
14  import java.util.List;
15  
16  import org.apache.commons.beanutils.BeanPropertyValueEqualsPredicate;
17  
18  public class Policies
19  {
20  
21      private final MessageProcessorChain messageProcessorChain;
22      private LinkedList<AroundPolicy> policies = new LinkedList<AroundPolicy>();
23  
24      public Policies(MessageProcessorChain messageProcessorChain)
25      {
26          this.messageProcessorChain = messageProcessorChain;
27      }
28  
29      public void add(AroundPolicy policy)
30      {
31          // TODO concurrency
32          if (find(policy.getName()) != null)
33          {
34              final String msg = String.format("There's already a policy registered under name [%s] for chain [%s]:%s",
35                                               policy.getName(), messageProcessorChain.getName(), messageProcessorChain);
36              throw new IllegalArgumentException(msg);
37          }
38          this.policies.add(policy);
39      }
40  
41      public AroundPolicy remove(String policyName)
42      {
43          // TODO concurrency
44          final AroundPolicy policy = find(policyName);
45          if (policy == null)
46          {
47              return null;
48          }
49          this.policies.remove(policy);
50  
51          return policy;
52      }
53  
54      public List<AroundPolicy> list()
55      {
56          // TODO concurrency
57          return Collections.unmodifiableList(this.policies);
58      }
59  
60      public void clear()
61      {
62          // TODO concurrency
63          this.policies.clear();
64      }
65  
66      /**
67       * @return policy with that name or null if not found
68       */
69      public AroundPolicy find(String policyName)
70      {
71          // TODO concurrency
72          // find { policy.name == policyName }
73          return (AroundPolicy) CollectionUtils.find(this.policies,
74                                                     new BeanPropertyValueEqualsPredicate("name", policyName));
75      }
76  }