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.specific;
8   
9   import org.mule.config.spring.parsers.assembly.BeanAssembler;
10  import org.mule.config.spring.parsers.generic.ParentDefinitionParser;
11  
12  import org.springframework.beans.factory.config.BeanDefinition;
13  import org.springframework.beans.factory.xml.ParserContext;
14  import org.w3c.dom.Element;
15  
16  /**
17   * Use this BeanDefinitionParser when you need a "wrapper" element for an ObjectFactory.
18   * For example, suppose we have the following class:
19   * 
20   *   class Car
21   *   {
22   *     private ObjectFactory<Wheel> wheel;
23   *   }
24   * 
25   * The following registration in the namespace:
26   *
27   *   registerBeanDefinitionParser("wheel", new ObjectFactoryWrapper("wheel"));
28   * 
29   * would allow a config such as:
30   * 
31   *   <car>
32   *     <wheel>
33   *       <prototype-object class="com.wheelsrus.BigWheel">
34   *         <properties>
35   *           <spring:property name="tire" value="goodyear"/>
36   *           <spring:property name="diameter" value="35R"/>
37   *         </properties>
38   *       </prototype-object>
39   *     </wheel>
40   *   </car>
41   */
42  public class ObjectFactoryWrapper extends ParentDefinitionParser
43  {
44      public static final String OBJECT_FACTORY_SETTER = "objectFactoryPropertyName";
45      
46      private String objectFactoryPropertyName;
47      
48      public ObjectFactoryWrapper(String objectFactoryPropertyName)
49      {
50          super();
51          this.objectFactoryPropertyName = objectFactoryPropertyName;
52      }
53  
54      protected void postProcess(ParserContext context, BeanAssembler assembler, Element element)
55      {
56          super.postProcess(context, assembler, element);
57          BeanDefinition parent = getParentBeanDefinition(element);
58          parent.setAttribute(OBJECT_FACTORY_SETTER, objectFactoryPropertyName);
59      }
60  }