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.config.spring.parsers.delegate;
8   
9   import org.mule.config.spring.MuleHierarchicalBeanDefinitionParserDelegate;
10  import org.mule.config.spring.parsers.MuleDefinitionParser;
11  import org.mule.config.spring.parsers.MuleDefinitionParserConfiguration;
12  import org.mule.config.spring.parsers.PostProcessor;
13  import org.mule.config.spring.parsers.PreProcessor;
14  import org.mule.config.spring.parsers.assembly.configuration.ValueMap;
15  import org.mule.config.spring.parsers.generic.AutoIdUtils;
16  import org.mule.util.ArrayUtils;
17  
18  import java.util.Map;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.springframework.beans.factory.support.AbstractBeanDefinition;
23  import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
24  import org.springframework.beans.factory.xml.ParserContext;
25  import org.w3c.dom.Element;
26  
27  /**
28   * This allows a definition parsers to be dynamically represented by different
29   * definition parsers, depending on the context.  For example, a single model may
30   * be defined across file - the first use defines the model and subsequent uses
31   * extend it (for this particular case, see {@link InheritDefinitionParser}).
32   *
33   * <p>Note that the sub-parsers must be consistent.  That includes matching the
34   * same schema, for example.</p>
35   */
36  public abstract class AbstractDelegatingDefinitionParser extends AbstractBeanDefinitionParser
37      implements MuleDefinitionParser
38  {
39  
40      protected Log logger = LogFactory.getLog(getClass());
41      
42      private MuleDefinitionParser[] delegates;
43  
44      protected AbstractDelegatingDefinitionParser()
45      {
46          this(new MuleDefinitionParser[0]);
47      }
48  
49      protected AbstractDelegatingDefinitionParser(MuleDefinitionParser[] delegates)
50      {
51          this.delegates = delegates;
52          addBeanFlag(MuleHierarchicalBeanDefinitionParserDelegate.MULE_FORCE_RECURSE);
53      }
54  
55      protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext)
56      {
57          return muleParse(element, parserContext);
58      }
59  
60      protected MuleDefinitionParserConfiguration addDelegate(MuleDefinitionParser delegate)
61      {
62          delegates = (MuleDefinitionParser[]) ArrayUtils.add(delegates, delegate);
63          return delegate;
64      }
65  
66      protected int size()
67      {
68          return delegates.length;
69      }
70  
71      protected MuleDefinitionParser getDelegate(int index)
72      {
73          return delegates[index];
74      }
75  
76      public MuleDefinitionParserConfiguration registerPreProcessor(PreProcessor preProcessor)
77      {
78          for (int i = 0; i < delegates.length; ++i)
79          {
80              delegates[i].registerPreProcessor(preProcessor);
81          }
82          return this;
83      }
84  
85      public MuleDefinitionParserConfiguration registerPostProcessor(PostProcessor postProcessor)
86      {
87          for (int i = 0; i < delegates.length; ++i)
88          {
89              delegates[i].registerPostProcessor(postProcessor);
90          }
91          return this;
92      }
93  
94      public MuleDefinitionParserConfiguration addReference(String propertyName)
95      {
96          for (int i = 0; i < delegates.length; ++i)
97          {
98              delegates[i].addReference(propertyName);
99          }
100         return this;
101     }
102 
103     public MuleDefinitionParserConfiguration addMapping(String propertyName, Map mappings)
104     {
105         for (int i = 0; i < delegates.length; ++i)
106         {
107             delegates[i].addMapping(propertyName, mappings);
108         }
109         return this;
110     }
111 
112     public MuleDefinitionParserConfiguration addMapping(String propertyName, String mappings)
113     {
114         for (int i = 0; i < delegates.length; ++i)
115         {
116             delegates[i].addMapping(propertyName, mappings);
117         }
118         return this;
119     }
120 
121     public MuleDefinitionParserConfiguration addMapping(String propertyName, ValueMap mappings)
122     {
123         for (int i = 0; i < delegates.length; ++i)
124         {
125             delegates[i].addMapping(propertyName, mappings);
126         }
127         return this;
128     }
129 
130     public MuleDefinitionParserConfiguration addAlias(String alias, String propertyName)
131     {
132         for (int i = 0; i < delegates.length; ++i)
133         {
134             delegates[i].addAlias(alias, propertyName);
135         }
136         return this;
137     }
138 
139     public MuleDefinitionParserConfiguration addCollection(String propertyName)
140     {
141         for (int i = 0; i < delegates.length; ++i)
142         {
143             delegates[i].addCollection(propertyName);
144         }
145         return this;
146     }
147 
148     public MuleDefinitionParserConfiguration addIgnored(String propertyName)
149     {
150         for (int i = 0; i < delegates.length; ++i)
151         {
152             delegates[i].addIgnored(propertyName);
153         }
154         return this;
155     }
156 
157     public MuleDefinitionParserConfiguration removeIgnored(String propertyName)
158     {
159         for (int i = 0; i < delegates.length; ++i)
160         {
161             delegates[i].removeIgnored(propertyName);
162         }
163         return this;
164     }
165 
166     public MuleDefinitionParserConfiguration setIgnoredDefault(boolean ignoreAll)
167     {
168         for (int i = 0; i < delegates.length; ++i)
169         {
170             delegates[i].setIgnoredDefault(ignoreAll);
171         }
172         return this;
173     }
174 
175     public String getBeanName(Element element)
176     {
177         return AutoIdUtils.getUniqueName(element, "delegate");
178     }
179 
180     public MuleDefinitionParserConfiguration addBeanFlag(String flag)
181     {
182         for (int i = 0; i < delegates.length; ++i)
183         {
184             delegates[i].addBeanFlag(flag);
185         }
186         return this;
187     }
188 
189     public void setDeprecationWarning(String deprecationWarning)
190     {
191         for (int i = 0; i < delegates.length; ++i)
192         {
193             delegates[i].setDeprecationWarning(deprecationWarning);
194         }
195     }
196 }