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.parsers.MuleChildDefinitionParser;
10  import org.mule.config.spring.parsers.MuleDefinitionParser;
11  import org.mule.config.spring.parsers.assembly.BeanAssembler;
12  import org.mule.config.spring.parsers.assembly.TwoStageMapBeanAssemblerFactory;
13  import org.mule.config.spring.parsers.assembly.configuration.PropertyConfiguration;
14  import org.mule.config.spring.parsers.generic.ChildDefinitionParser;
15  
16  import java.util.HashMap;
17  import java.util.Map;
18  
19  import org.springframework.beans.factory.config.BeanDefinition;
20  import org.springframework.beans.factory.support.AbstractBeanDefinition;
21  import org.springframework.beans.factory.xml.ParserContext;
22  import org.w3c.dom.Element;
23  
24  /**
25   * This changes a {@link org.mule.config.spring.parsers.generic.ChildDefinitionParser}
26   * so that it generates a map instead of a bean definition.  This is useful for converting
27   * parsers to work with the object factory (which requires a map).
28   */
29  public class MapDefinitionParserMutator
30          extends AbstractDelegatingDefinitionParser
31          implements TwoStageMapBeanAssemblerFactory.BeanAssemblerStore, MuleChildDefinitionParser
32  {
33  
34      private String setter;
35      private Element currentElement;
36      private Map pendingAssemblers = new HashMap();
37  
38      public MapDefinitionParserMutator(String setter, ChildDefinitionParser delegate)
39      {
40          super(new MuleDefinitionParser[]{delegate});
41          this.setter = setter;
42          // this is where we set the callback
43          delegate.setBeanAssemblerFactory(new TwoStageMapBeanAssemblerFactory(this));
44      }
45  
46      public AbstractBeanDefinition muleParse(Element element, ParserContext parserContext)
47      {
48          if (pendingAssemblers.containsKey(element))
49          {
50              // this is the second call, after the children have been processed
51              BeanAssembler beanAssembler = (BeanAssembler) pendingAssemblers.get(element);
52              pendingAssemblers.remove(element);
53              beanAssembler.insertBeanInTarget(setter);
54              return null;
55          }
56          else
57          {
58              // first call, so process in normal manner, but set current element so that
59              // when the store callback is called, we can associate the assembler with this
60              // element
61              currentElement = element;
62              return getChildDelegate().muleParse(element, parserContext);
63          }
64      }
65  
66      public void saveBeanAssembler(BeanAssembler beanAssembler)
67      {
68          // this is called by the bean assembler from inside parseDelegate above.
69          pendingAssemblers.put(currentElement, beanAssembler);
70      }
71  
72      protected ChildDefinitionParser getChildDelegate()
73      {
74          return (ChildDefinitionParser) getDelegate(0);
75      }
76  
77      public void forceParent(BeanDefinition parent)
78      {
79          getChildDelegate().forceParent(parent);
80      }
81  
82      public PropertyConfiguration getTargetPropertyConfiguration()
83      {
84          return getChildDelegate().getTargetPropertyConfiguration();
85      }
86  
87  }