1 /* 2 * $Id: MuleExpressionEvaluator.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; 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 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 this.muleContext = context; 60 } 61 62 public Object evaluate(String expression, MuleMessage message) 63 { 64 if(expression==null) 65 { 66 return message; 67 } 68 int i = expression.indexOf("."); 69 70 ExpressionConfig config = getExpressionConfig(expression.substring(0, i), expression.substring(i+1)); 71 String fullExpression = config.getFullExpression(muleContext.getExpressionManager()); 72 return muleContext.getExpressionManager().evaluate(fullExpression, message); 73 } 74 75 76 protected ExpressionConfig getExpressionConfig(String eval, String expression) 77 { 78 79 int i = expression.indexOf("("); 80 int x = expression.indexOf("."); 81 if(x > 0 && x < i) 82 { 83 eval = expression.substring(0, x); 84 expression = expression.substring(x+1); 85 } 86 87 if(i > 0) 88 { 89 eval = expression.substring(0, i); 90 expression=expression.substring(i+1, expression.length()-1); 91 } 92 return new ExpressionConfig(expression, eval, null); 93 } 94 95 public void setName(String name) 96 { 97 throw new UnsupportedOperationException(); 98 } 99 100 public String getName() 101 { 102 return NAME; 103 } 104 }