View Javadoc

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