View Javadoc

1   /*
2    * $Id$
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.config.spring.parsers.AbstractMuleBeanDefinitionParser;
14  import org.mule.util.StringUtils;
15  import org.springframework.beans.factory.config.BeanDefinition;
16  import org.springframework.beans.factory.support.BeanDefinitionBuilder;
17  import org.springframework.beans.factory.xml.ParserContext;
18  import org.w3c.dom.Element;
19  
20  public abstract class AbstractFlowConstructDefinitionParser extends AbstractMuleBeanDefinitionParser
21  {
22      private static final String MODEL_ELEMENT = "model";
23      private static final String ABSTRACT_ATTRIBUTE = "abstract";
24      private static final String PARENT_ATTRIBUTE = "parent";
25  
26      protected static final String ENDPOINT_REF_ATTRIBUTE = "endpoint-ref";
27      protected static final String ADDRESS_ATTRIBUTE = "address";
28  
29      protected static final String INBOUND_ADDRESS_ATTRIBUTE = "inboundAddress";
30      protected static final String INBOUND_ENDPOINT_REF_ATTRIBUTE = "inboundEndpoint-ref";
31      protected static final String INBOUND_ENDPOINT_CHILD = "inbound-endpoint";
32  
33      protected static final String OUTBOUND_ADDRESS_ATTRIBUTE = "outboundAddress";
34      protected static final String OUTBOUND_ENDPOINT_REF_ATTRIBUTE = "outboundEndpoint-ref";
35      protected static final String OUTBOUND_ENDPOINT_CHILD = "outboundEndpoint";
36  
37      protected static final String TRANSFORMER_REFS_ATTRIBUTE = "transformer-refs";
38      protected static final String RESPONSE_TRANSFORMER_REFS_ATTRIBUTE = "responseTransformer-refs";
39  
40      @Override
41      @SuppressWarnings("unchecked")
42      protected BeanDefinitionBuilder createBeanDefinitionBuilder(Element element, Class beanClass)
43      {
44          return BeanDefinitionBuilder.genericBeanDefinition(beanClass);
45      }
46  
47      @Override
48      protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder)
49      {
50          if (MODEL_ELEMENT.equals(element.getParentNode().getLocalName()))
51          {
52              logger.warn("Support for pattern elements in model will be removed from Mule 3.1: move the "
53                          + element.getLocalName() + " named '" + element.getAttribute("name")
54                          + "' out of model as soon as possible!");
55          }
56  
57          builder.setScope(BeanDefinition.SCOPE_SINGLETON);
58          handleAbstractAttribute(element, builder);
59          handleParentAttribute(element, builder);
60          super.doParse(element, parserContext, builder);
61      }
62  
63      private void handleParentAttribute(Element element, BeanDefinitionBuilder builder)
64      {
65          final String parentAttribute = element.getAttribute(PARENT_ATTRIBUTE);
66          if (StringUtils.isNotBlank(parentAttribute))
67          {
68              builder.setParentName(parentAttribute);
69          }
70          element.removeAttribute(PARENT_ATTRIBUTE);
71      }
72  
73      private void handleAbstractAttribute(Element element, BeanDefinitionBuilder builder)
74      {
75          final String abstractAttribute = element.getAttribute(ABSTRACT_ATTRIBUTE);
76          if (StringUtils.isNotBlank(abstractAttribute))
77          {
78              builder.setAbstract(Boolean.parseBoolean(abstractAttribute));
79          }
80          element.removeAttribute(ABSTRACT_ATTRIBUTE);
81      }
82  }