View Javadoc

1   /*
2    * $Id: InboundAttachmentsAnnotationParser.java 20321 2010-11-24 15:21:24Z 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  package org.mule.config.parsers;
11  
12  import org.mule.api.annotations.meta.Evaluator;
13  import org.mule.api.annotations.param.InboundAttachments;
14  import org.mule.api.expression.ExpressionAnnotationParser;
15  import org.mule.expression.ExpressionConfig;
16  import org.mule.expression.MessageAttachmentExpressionEvaluator;
17  import org.mule.expression.MessageAttachmentsExpressionEvaluator;
18  import org.mule.expression.MessageAttachmentsListExpressionEvaluator;
19  import org.mule.expression.transformers.ExpressionArgument;
20  
21  import java.lang.annotation.Annotation;
22  import java.util.List;
23  import java.util.Map;
24  
25  import static org.mule.expression.ExpressionConstants.OPTIONAL_ARGUMENT;
26  
27  /**
28   * Responsible for parsing the {@link org.mule.api.annotations.param.InboundAttachments} annotation.  This is an iBeans
29   * framework class and cannot be used in any other context.
30   */
31  public class InboundAttachmentsAnnotationParser implements ExpressionAnnotationParser
32  {
33      public ExpressionArgument parse(Annotation annotation, Class<?> parameterType)
34      {
35          Evaluator evaluator = annotation.annotationType().getAnnotation(Evaluator.class);
36          if (evaluator != null)
37          {
38              String expr = ((InboundAttachments) annotation).value();
39              boolean optional = false;
40  
41              //Default to single attachment evaluator
42              String eval = MessageAttachmentExpressionEvaluator.NAME;
43              if (parameterType.isAssignableFrom(Map.class))
44              {
45                  eval = MessageAttachmentsExpressionEvaluator.NAME;
46              }
47              else if (parameterType.isAssignableFrom(List.class))
48              {
49                  eval = MessageAttachmentsListExpressionEvaluator.NAME;
50              }
51              else if(expr.endsWith(OPTIONAL_ARGUMENT))
52              {
53                  //We only set optional if we're dealing with a single header, List and Maps of attachments can contain
54                  //optional names but we will always return a Map or List even if it is empty
55                  optional = true;
56              }
57              return new ExpressionArgument(null, new ExpressionConfig(expr, eval, null), optional, parameterType);
58          }
59          else
60          {
61              throw new IllegalArgumentException("The @Evaluator annotation must be set on an Expression Annotation");
62          }
63      }
64  
65      public boolean supports(Annotation annotation)
66      {
67          return annotation instanceof InboundAttachments;
68      }
69  }