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.expression.transformers;
8   
9   import org.mule.api.lifecycle.InitialisationException;
10  import org.mule.config.i18n.CoreMessages;
11  import org.mule.transformer.AbstractMessageTransformer;
12  import org.mule.transformer.types.DataTypeFactory;
13  
14  import java.util.ArrayList;
15  import java.util.Iterator;
16  import java.util.List;
17  
18  /**
19   * This transformer will evaluate one or more expressions on the current message and return the
20   * results as an Array. If only one expression is defined it will return the object returned from
21   * the expression.
22   * <p/>
23   * You can use expressions to extract
24   * <ul>
25   * <li>headers (single, map or list)</li>
26   * <li>attachments (single, map or list)</li>
27   * <li>payload</li>
28   * <li>xpath</li>
29   * <li>groovy</li>
30   * <li>bean</li>
31   * </ul>
32   * and more.
33   * <p/>
34   * This transformer provides a very powerful way to pull different bits of information from the
35   * message and pass them to the service.
36   */
37  public abstract class AbstractExpressionTransformer extends AbstractMessageTransformer
38  {
39      protected List<ExpressionArgument> arguments;
40  
41      public AbstractExpressionTransformer()
42      {
43          //No type checking by default
44          registerSourceType(DataTypeFactory.OBJECT);
45          setReturnDataType(DataTypeFactory.OBJECT);
46          arguments = new ArrayList<ExpressionArgument>(4);
47      }
48  
49      public void addArgument(ExpressionArgument argument)
50      {
51          arguments.add(argument);
52      }
53  
54      public boolean removeArgument(ExpressionArgument argument)
55      {
56          return arguments.remove(argument);
57      }
58  
59      /**
60       * Template method were deriving classes can do any initialisation after the
61       * properties have been set on this transformer
62       *
63       * @throws org.mule.api.lifecycle.InitialisationException
64       *
65       */
66      @Override
67      public void initialise() throws InitialisationException
68      {
69          if (arguments == null || arguments.size() == 0)
70          {
71              throw new InitialisationException(CoreMessages.objectIsNull("arguments[]"), this);
72          }
73  
74          for (Iterator<ExpressionArgument> iterator = arguments.iterator(); iterator.hasNext();)
75          {
76              ExpressionArgument argument = iterator.next();
77              argument.setMuleContext(muleContext);
78              argument.setExpressionEvaluationClassLoader(Thread.currentThread().getContextClassLoader());
79              try
80              {
81                  argument.validate();
82              }
83              catch (Exception e)
84              {
85                  throw new InitialisationException(e, this);
86              }
87          }
88      }
89  
90      public List<ExpressionArgument> getArguments()
91      {
92          return arguments;
93      }
94  
95      public void setArguments(List<ExpressionArgument> arguments)
96      {
97          this.arguments = arguments;
98      }
99  }