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.module.ibeans.config;
8   
9   import org.mule.api.MuleMessage;
10  import org.mule.api.expression.ExpressionEvaluator;
11  import org.mule.api.expression.ExpressionRuntimeException;
12  import org.mule.config.i18n.CoreMessages;
13  
14  import java.util.Map;
15  
16  import org.apache.commons.beanutils.MethodUtils;
17  
18  /**
19   * TODO
20   */
21  public class InvokeExpressionEvaluator implements ExpressionEvaluator
22  {
23      public Object evaluate(String expression, MuleMessage message)
24      {
25          int i = expression.indexOf(".");
26          String property;
27          String method;
28          if(i > -1)
29          {
30              property = expression.substring(0, i);
31              method = expression.substring(i+1);
32          }
33          else
34          {
35              throw new IllegalArgumentException();
36          }
37          Object[] args;
38  
39          if(message.getPayload() instanceof Map)
40          {
41              args = ((Map)message.getPayload()).values().toArray(new Object[]{});
42          }
43          else if(message.getPayload().getClass().isArray())
44          {
45              args = (Object[]) message.getPayload();
46          }
47          else
48          {
49              args = new Object[]{message.getPayload()};
50          }
51          Object o = message.getInvocationProperty(property,null);
52          if(o!=null)
53          {
54              try
55              {
56                  return MethodUtils.invokeMethod(o, method, args);
57              }
58              catch (Exception e)
59              {
60                  throw new ExpressionRuntimeException(CoreMessages.failedToInvoke(expression), e);
61              }
62          }
63          else
64          {
65              throw new ExpressionRuntimeException(CoreMessages.expressionMalformed(expression, getName()));
66          }
67      }
68  
69      public void setName(String name)
70      {
71          throw new UnsupportedOperationException("setName");
72      }
73  
74      public String getName()
75      {
76          return "invoke";
77      }
78  }