View Javadoc

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