View Javadoc

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