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.endpoint.support;
8   
9   import org.mule.config.spring.parsers.AbstractMuleBeanDefinitionParser;
10  import org.mule.config.spring.parsers.generic.AutoIdUtils;
11  import org.mule.config.spring.parsers.generic.ChildDefinitionParser;
12  import org.mule.util.StringUtils;
13  
14  import org.springframework.beans.factory.support.BeanDefinitionBuilder;
15  import org.w3c.dom.Element;
16  
17  /**
18   * A parser for "embedded" endpoints - ie inbound, outbound and response endpoints.
19   * Because we have automatic String -> MuleEnpointURI conversion via property editors
20   * this can be used in a variety of ways. It should work directly with a simple
21   * String address attribute or, in combination with a child element (handled by
22   * {@link ChildAddressDefinitionParser}, or embedded in
23   * {@link AddressedEndpointDefinitionParser} for a more compact single-element
24   * approach.
25   * <p>
26   * This class does support references to other endpoints.
27   * </p>
28   * TODO - check that references are global!
29   */
30  public class ChildEndpointDefinitionParser extends ChildDefinitionParser
31  {
32  
33      public ChildEndpointDefinitionParser(Class endpoint)
34      {
35          super("messageProcessor", endpoint);
36          addIgnored(AbstractMuleBeanDefinitionParser.ATTRIBUTE_REF);
37          EndpointUtils.addProperties(this);
38          EndpointUtils.addPostProcess(this);
39      }
40  
41      @Override
42      public BeanDefinitionBuilder createBeanDefinitionBuilder(Element element, Class beanClass)
43      {
44          BeanDefinitionBuilder builder = super.createBeanDefinitionBuilder(element, beanClass);
45          String global = element.getAttribute(AbstractMuleBeanDefinitionParser.ATTRIBUTE_REF);
46          if (StringUtils.isNotBlank(global))
47          {
48              builder.addConstructorArgReference(global);
49              builder.addDependsOn(global);
50          }
51          return builder;
52      }
53  
54      @Override
55      public String getPropertyName(Element e)
56      {
57          String parent = e.getParentNode().getLocalName().toLowerCase();
58          if (e.getLocalName() != null
59              && (e.getLocalName().toLowerCase().endsWith("inbound-endpoint") || e.getLocalName()
60                  .toLowerCase()
61                  .equals("poll")))
62          {
63              return "messageSource";
64          }
65          else if ("wire-tap".equals(parent) || "wire-tap-router".equals(parent))
66          {
67              return "tap";
68          }
69          else if ("binding".equals(parent) || "java-interface-binding".equals(parent)
70                   || "publish-notifications".equals(parent) || "remote-dispatcher-agent".equals(parent))
71          {
72              return "endpoint";
73          }
74          else
75          {
76              return super.getPropertyName(e);
77          }
78      }
79  
80      @Override
81      public String getBeanName(Element element)
82      {
83          if (null != element.getAttributeNode(AbstractMuleBeanDefinitionParser.ATTRIBUTE_REF))
84          {
85              return AutoIdUtils.uniqueValue("ref:"
86                                             + element.getAttribute(AbstractMuleBeanDefinitionParser.ATTRIBUTE_REF));
87          }
88          else
89          {
90              return super.getBeanName(element);
91          }
92      }
93  }