View Javadoc

1   /*
2    * $Id: ParentDefinitionParser.java 12259 2008-07-09 14:18:28Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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  package org.mule.config.spring.parsers.generic;
11  
12  import org.mule.config.spring.MuleHierarchicalBeanDefinitionParserDelegate;
13  import org.mule.config.spring.parsers.AbstractHierarchicalDefinitionParser;
14  import org.mule.config.spring.parsers.assembly.BeanAssembler;
15  
16  import org.springframework.beans.factory.config.BeanDefinition;
17  import org.springframework.beans.factory.support.AbstractBeanDefinition;
18  import org.springframework.beans.factory.support.BeanDefinitionBuilder;
19  import org.springframework.beans.factory.xml.ParserContext;
20  import org.springframework.util.Assert;
21  import org.w3c.dom.Element;
22  
23  /**
24   * Processes child property elements in Xml but sets the properties on the parent object.  This is
25   * useful when an object has lots of properties and it's more readable to break those properties into
26   * groups that can be represented as a sub-element in Xml.
27   */
28  public class ParentDefinitionParser extends AbstractHierarchicalDefinitionParser
29  {
30  
31      public ParentDefinitionParser()
32      {
33          addBeanFlag(MuleHierarchicalBeanDefinitionParserDelegate.MULE_NO_REGISTRATION);
34      }
35  
36      protected Class getBeanClass(Element element)
37      {
38          try
39          {
40              return Class.forName(getParentBeanDefinition(element).getBeanClassName());
41          }
42          catch (Exception e)
43          {
44              // Should continue to work, but automatic collection detection etc will fail
45              logger.debug("No class for " + element);
46              return Object.class;
47          }
48      }
49  
50      protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext)
51      {
52          preProcess(element);
53          setParserContext(parserContext);
54          setRegistry(parserContext.getRegistry());
55          Class beanClass = getBeanClass(element);
56          Assert.state(beanClass != null, "Class returned from getBeanClass(Element) must not be null, element is: " + element.getNodeName());
57          BeanDefinitionBuilder builder = createBeanDefinitionBuilder(element, beanClass);
58          builder.getRawBeanDefinition().setSource(parserContext.extractSource(element));
59          if (parserContext.isNested())
60          {
61              // Inner bean definition must receive same singleton status as containing bean.
62              builder.setScope(parserContext.getContainingBeanDefinition().isSingleton()
63                  ? BeanDefinition.SCOPE_SINGLETON : BeanDefinition.SCOPE_PROTOTYPE);
64          }
65          doParse(element, parserContext, builder);
66          BeanAssembler beanAssembler = getBeanAssembler(element, builder);
67          beanAssembler.copyBeanToTarget();
68          return (AbstractBeanDefinition) beanAssembler.getTarget();
69      }
70  
71      protected void postProcess(ParserContext context, BeanAssembler assembler, Element element)
72      {
73          // by default the name matches the "real" bean
74          if (null == element.getAttributeNode(ATTRIBUTE_NAME))
75          {
76              element.setAttribute(ATTRIBUTE_NAME, getParentBeanName(element));
77          }
78          super.postProcess(context, assembler, element);
79      }
80  
81  }