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.ChildDefinitionParser;
11  import org.mule.processor.InvokerMessageProcessor;
12  import org.mule.util.StringUtils;
13  
14  import java.util.ArrayList;
15  import java.util.List;
16  
17  import org.springframework.beans.factory.support.BeanDefinitionBuilder;
18  import org.springframework.beans.factory.xml.ParserContext;
19  import org.w3c.dom.Element;
20  
21  public class InvokerMessageProcessorDefinitionParser extends ChildDefinitionParser
22  {
23      private final Class<?> objectType;
24      private final String methodName;
25      private final String[] parameterNames;
26  
27      public InvokerMessageProcessorDefinitionParser(String setterMethod,
28                                           Class<?> objectType,
29                                           String methodName,
30                                           String[] parameterNames)
31      {
32          super(setterMethod, InvokerMessageProcessor.class);
33          this.objectType = objectType;
34          this.methodName = methodName;
35          this.parameterNames = parameterNames;
36      }
37  
38      @Override
39      protected Class<?> getBeanClass(Element element)
40      {
41          return InvokerMessageProcessor.class;
42      }
43  
44      @Override
45      protected void parseChild(Element element, ParserContext parserContext, BeanDefinitionBuilder builder)
46      {
47          if (!StringUtils.isEmpty(element.getAttribute(getTargetPropertyConfiguration().getAttributeAlias(
48              "config-ref"))))
49          {
50              builder.addPropertyReference("object",
51                  element.getAttribute(getTargetPropertyConfiguration().getAttributeAlias("config-ref")));
52          }
53          else
54          {
55              builder.addPropertyValue("objectType", objectType);
56          }
57  
58          List<String> expressions = new ArrayList<String>();
59          if (parameterNames != null)
60          {
61              for (String parameterName : parameterNames)
62              {
63                  if (!StringUtils.isEmpty(element.getAttribute(parameterName)))
64                  {
65                      expressions.add(element.getAttribute(parameterName));
66                  }
67                  else
68                  {
69                      expressions.add(null);
70                  }
71              }
72          }
73          builder.addPropertyValue("arguments", expressions);
74          builder.addPropertyValue("methodName", methodName);
75  
76          BeanAssembler assembler = getBeanAssembler(element, builder);
77          postProcess(getParserContext(), assembler, element);
78      }
79  
80  }