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;
8   
9   import org.mule.api.MuleContext;
10  import org.mule.api.MuleMessage;
11  import org.mule.api.context.MuleContextAware;
12  import org.mule.api.expression.ExpressionEvaluator;
13  
14  /**
15   * This evaluator provide a powerful expression language for querying mule information
16   * at runtime.  It provides a unified language for querying message properties, attachments
17   * payload, Mule context information such as the current service or endpoint and access to
18   * the registry. Here are some examples:
19   *
20   * #[mule:message.headers(foo, bar)] - retrieves two headers 'foo' and 'bar' and returns a Map.
21   *
22   * #[mule:message.attachments-list(attach1, attach2*)] - retrieves two named attachments in a List.  The star on 'attach2'
23   * indicates that it is optional
24   *
25   * #[mule:message.headers(all)] - retrieves all headers and returns as a Map.
26   *
27   * #[mule:message.payload(org.dom4j.Document)] - return the payload and convert it to a org.dom4j.Document.
28   *
29   * #[mule:message.correlationId] - return the the correlationId on the message
30   *
31   * #[mule:message.map-payload(foo)] - expects a Map payload object and will retrive a property 'foo' from the map.
32   *
33   * #[mule.context.serviceName] - returns the current service Name
34   *
35   * #[mule.context.modelName] - returns the current model Name
36   *
37   * #[mule.context.workingDir] - returns the working Directory
38   *
39   * #[mule.context.serverId] - returns the current server ID
40   *
41   * #[mule.registry.apple] - returns an object called 'apple' from the registry
42   *
43   * #[mule.registry.apple*] - returns an object called 'apple' from the registry but is optional
44   *
45   * #[mule.registry.apple.washed] - returns the property 'washed on an object called 'apple' in the registry
46   *
47   */
48  public class MuleExpressionEvaluator implements ExpressionEvaluator, MuleContextAware
49  {
50      public static final String NAME = "mule";
51  
52      private MuleContext muleContext;
53  
54      public void setMuleContext(MuleContext context)
55      {
56          this.muleContext = context;
57      }
58  
59      public Object evaluate(String expression, MuleMessage message)
60      {
61          if(expression==null)
62          {
63              return message;
64          }
65          int i = expression.indexOf(".");
66  
67          ExpressionConfig config = getExpressionConfig(expression.substring(0, i), expression.substring(i+1));
68          String fullExpression = config.getFullExpression(muleContext.getExpressionManager());
69          return muleContext.getExpressionManager().evaluate(fullExpression, message);
70      }
71  
72  
73      protected ExpressionConfig getExpressionConfig(String eval, String expression)
74      {
75  
76          int i = expression.indexOf("(");
77          int x = expression.indexOf(".");
78          if(x > 0 && x < i)
79          {
80              eval = expression.substring(0, x);
81              expression = expression.substring(x+1);
82          }
83  
84          if(i > 0)
85          {
86              eval = expression.substring(0, i);
87              expression=expression.substring(i+1, expression.length()-1);
88          }
89          return new ExpressionConfig(expression, eval,  null);
90      }
91  
92      public void setName(String name)
93      {
94          throw new UnsupportedOperationException();
95      }
96  
97      public String getName()
98      {
99          return NAME;
100     }
101 }