View Javadoc

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