View Javadoc

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