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.expression;
8   
9   import org.apache.commons.logging.Log;
10  import org.apache.commons.logging.LogFactory;
11  import org.mule.api.MuleContext;
12  import org.mule.api.expression.ExpressionAnnotationParser;
13  import org.mule.api.lifecycle.InitialisationException;
14  import org.mule.api.registry.RegistrationException;
15  import org.mule.api.transformer.TransformerException;
16  import org.mule.config.AnnotationsParserFactory;
17  import org.mule.config.i18n.AnnotationsMessages;
18  import org.mule.expression.transformers.ExpressionArgument;
19  import org.mule.expression.transformers.ExpressionTransformer;
20  
21  import java.lang.annotation.Annotation;
22  import java.lang.reflect.Method;
23  
24  /**
25   * TODO
26   */
27  public class ExpressionAnnotationsHelper
28  {
29      protected static Log logger = LogFactory.getLog(ExpressionAnnotationsHelper.class);
30  
31      public static ExpressionTransformer getTransformerForMethodWithAnnotations(Method method, MuleContext context) throws TransformerException, InitialisationException
32      {
33          ExpressionTransformer trans = new ExpressionTransformer();
34          trans.setMuleContext(context);
35  
36          Annotation[][] annotations = method.getParameterAnnotations();
37  
38          for (int i = 0; i < annotations.length; i++)
39          {
40              Annotation[] annotation = annotations[i];
41              for (int j = 0; j < annotation.length; j++)
42              {
43                  Annotation ann = annotation[j];
44                  ExpressionArgument arg = parseAnnotation(ann, method.getParameterTypes()[i], context);
45  
46                  if (arg != null)
47                  {
48                      trans.addArgument(arg);
49                  }
50              }
51          }
52          trans.initialise();
53          return trans;
54      }
55  
56      static synchronized ExpressionArgument parseAnnotation(Annotation annotation, 
57          Class<?> paramType, MuleContext muleContext)
58      {
59          AnnotationsParserFactory factory;
60          try
61          {
62              factory = muleContext.getRegistry().lookupObject(AnnotationsParserFactory.class);
63          }
64          catch (RegistrationException e)
65          {
66              //TODO better exception message
67              throw new IllegalArgumentException(AnnotationsMessages.noParserFoundForAnnotation(annotation).getMessage());
68          }
69          
70          ExpressionAnnotationParser parser = factory.getExpressionParser(annotation);
71          if (parser == null)
72          {
73              if (logger.isDebugEnabled())
74              {
75                  logger.debug(AnnotationsMessages.noParserFoundForAnnotation(annotation).getMessage());
76              }
77              return null;
78          }
79          return parser.parse(annotation, paramType);
80      }
81  }