View Javadoc

1   /*
2    * $Id: OptionalChildDefinitionParser.java 11695 2008-05-08 06:46:45Z 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  
11  package org.mule.config.spring.parsers.generic;
12  
13  import org.mule.config.spring.parsers.assembly.BeanAssembler;
14  import org.mule.util.StringUtils;
15  
16  import org.springframework.beans.factory.config.BeanDefinition;
17  import org.springframework.beans.factory.support.BeanDefinitionBuilder;
18  import org.springframework.beans.factory.xml.ParserContext;
19  import org.w3c.dom.Element;
20  
21  /**
22   * This class should be used when the same element can be configured as a child or an orphan 
23   * (i.e., top-level).  It will inject the bean into the parent if the parent exists, otherwise 
24   * it will not complain (ChildDefinitionParser throws an exception if no parent exists).
25   */
26  public class OptionalChildDefinitionParser extends ChildDefinitionParser
27  {
28      private boolean isChild;
29      
30      public OptionalChildDefinitionParser(String setterMethod)
31      {
32          super(setterMethod);
33      }
34      
35      public OptionalChildDefinitionParser(String setterMethod, Class clazz)
36      {
37          super(setterMethod, clazz);
38      }
39      
40      public OptionalChildDefinitionParser(String setterMethod, Class clazz, Class constraint)
41      {
42          super(setterMethod, clazz, constraint);
43      }
44      
45      public OptionalChildDefinitionParser(String setterMethod, Class clazz, Class constraint, boolean allowClassAttribute)
46      {
47          super(setterMethod, clazz, constraint, allowClassAttribute);
48      }
49  
50      // @Override
51      protected void parseChild(Element element, ParserContext parserContext, BeanDefinitionBuilder builder)
52      {
53          String parentBean = getParentBeanName(element);
54          isChild = !(StringUtils.isBlank(parentBean));
55  
56          super.parseChild(element, parserContext, builder);
57      }
58  
59      public BeanDefinition getParentBeanDefinition(Element element)
60      {
61          if (isChild)
62          {
63              return super.getParentBeanDefinition(element);
64          }
65          else
66          {
67              return null;
68          }
69      }
70  
71      protected void postProcess(ParserContext context, BeanAssembler assembler, Element element)
72      {
73          if (isChild)
74          {
75              super.postProcess(context, assembler, element);
76          }
77      }
78  }
79  
80