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