View Javadoc

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