View Javadoc

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