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.handlers;
8   
9   import org.mule.config.spring.parsers.generic.OrphanDefinitionParser;
10  import org.mule.config.spring.parsers.specific.InvokerMessageProcessorDefinitionParser;
11  import org.mule.config.spring.util.ParamReader;
12  
13  import java.io.IOException;
14  import java.lang.reflect.Method;
15  
16  /**
17   * Turns a POJO into a config element and a set of method tags which map to
18   * MessageProcessors.
19   */
20  public abstract class AbstractPojoNamespaceHandler extends AbstractMuleNamespaceHandler
21  {
22      public void registerPojo(String configElementName, Class<?> cls)
23      {
24          // register a generic configuration element
25          OrphanDefinitionParser parser = new OrphanDefinitionParser(cls, true);
26          parser.addIgnored("name");
27          registerMuleBeanDefinitionParser(configElementName, parser);
28  
29          // register invoker parser for each non setter
30          try
31          {
32              ParamReader paramReader = new ParamReader(cls); // use the parameter
33              // names from our class
34              for (Method m : cls.getDeclaredMethods())
35              {
36                  // don't create parsers for setters
37                  if (!m.getName().startsWith("set"))
38                  {
39                      String[] parameterNames = paramReader.getParameterNames(m);
40  
41                      registerMuleBeanDefinitionParser(splitCamelCase(m.getName()),
42                          new InvokerMessageProcessorDefinitionParser("messageProcessor", cls, m.getName(),
43                              parameterNames));
44                  }
45              }
46          }
47          catch (IOException e)
48          {
49              throw new RuntimeException(e);
50          }
51      }
52  
53      protected static String splitCamelCase(String s)
54      {
55          if (s.contains("get"))
56          {
57              s = s.substring(3);
58          }
59          return s.replaceAll(
60              String.format("%s|%s|%s", "(?<=[A-Z])(?=[A-Z][a-z][0-9])", "(?<=[^A-Z])(?=[A-Z])",
61                  "(?<=[A-Za-z0-9])(?=[^A-Za-z0-9])"), "-").toLowerCase();
62      }
63      
64  }