View Javadoc

1   /*
2    * $Id: SimpleComponentDefinitionParser.java 11376 2008-03-16 17:44:10Z dfeist $
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.specific;
12  
13  import org.mule.api.lifecycle.Disposable;
14  import org.mule.api.lifecycle.Initialisable;
15  import org.mule.component.DefaultJavaComponent;
16  import org.mule.object.AbstractObjectFactory;
17  import org.mule.object.SingletonObjectFactory;
18  
19  import org.springframework.beans.factory.support.AbstractBeanDefinition;
20  import org.springframework.beans.factory.support.BeanDefinitionBuilder;
21  import org.springframework.beans.factory.support.GenericBeanDefinition;
22  import org.springframework.beans.factory.xml.ParserContext;
23  import org.w3c.dom.Element;
24  
25  /**
26   * Used to parse shortcut elements for simple built-in components such as
27   * {@link org.mule.component.simple.BridgeComponent},
28   * {@link import org.mule.component.simple.EchoComponent} and
29   * {@link import org.mule.component.simple.LogComponent}. This allows shortcuts like
30   * for example <i>&lt;mule:bridge-service/&gt;</i> to be used instead of having to
31   * use the <i>&lt;mule:service/&gt;</i> element and specify the class name (and
32   * scope) for built-in components that don't require configuration. <p/> <b>This
33   * DefinitionParser should only be used for state-less components.</b> <p/> In order
34   * to further customize components and use serviceFactory properties the
35   * &lt;mule:service/&gt; element should be used.
36   */
37  public class SimpleComponentDefinitionParser extends ComponentDefinitionParser
38  {
39      private static Class OBJECT_FACTORY_TYPE = SingletonObjectFactory.class;
40      private Class componentInstanceClass;
41  
42      public SimpleComponentDefinitionParser(Class component, Class componentInstanceClass)
43      {
44          super(DefaultJavaComponent.class);
45          this.componentInstanceClass = componentInstanceClass;
46      }
47  
48      protected void parseChild(Element element, ParserContext parserContext, BeanDefinitionBuilder builder)
49      {
50          Element parent = (Element) element.getParentNode();
51          String serviceName = parent.getAttribute(ATTRIBUTE_NAME);
52          builder.addPropertyReference("service", serviceName);
53  
54          // Create a BeanDefinition for the nested object factory and set it a
55          // property value for the component
56          AbstractBeanDefinition objectFactoryBeanDefinition = new GenericBeanDefinition();
57          objectFactoryBeanDefinition.setBeanClass(OBJECT_FACTORY_TYPE);
58          objectFactoryBeanDefinition.getPropertyValues().addPropertyValue(AbstractObjectFactory.ATTRIBUTE_OBJECT_CLASS,
59              componentInstanceClass);
60          objectFactoryBeanDefinition.setInitMethodName(Initialisable.PHASE_NAME);
61          objectFactoryBeanDefinition.setDestroyMethodName(Disposable.PHASE_NAME);
62  
63          builder.addPropertyValue("objectFactory", objectFactoryBeanDefinition);
64  
65          super.parseChild(element, parserContext, builder);
66      }
67  
68  }