View Javadoc
1   /*******************************************************************************
2    * Portions created by Sebastian Thomschke are copyright (c) 2005-2015 Sebastian
3    * Thomschke.
4    *
5    * All Rights Reserved. This program and the accompanying materials
6    * are made available under the terms of the Eclipse Public License v1.0
7    * which accompanies this distribution, and is available at
8    * http://www.eclipse.org/legal/epl-v10.html
9    *
10   * Contributors:
11   *     Sebastian Thomschke - initial implementation.
12   *******************************************************************************/
13  package net.sf.oval.expression;
14  
15  import java.util.Map;
16  
17  import net.sf.oval.exception.ExpressionEvaluationException;
18  import net.sf.oval.internal.Log;
19  import net.sf.oval.internal.util.ObjectCache;
20  
21  import org.mvel2.MVEL;
22  
23  /**
24   * @author Sebastian Thomschke
25   */
26  public class ExpressionLanguageMVELImpl extends AbstractExpressionLanguage
27  {
28  	private static final Log LOG = Log.getLog(ExpressionLanguageMVELImpl.class);
29  
30  	private final ObjectCache<String, Object> expressionCache = new ObjectCache<String, Object>();
31  
32  	public Object evaluate(final String expression, final Map<String, ? > values) throws ExpressionEvaluationException
33  	{
34  		LOG.debug("Evaluating MVEL expression: {1}", expression);
35  		try
36  		{
37  			Object expr = expressionCache.get(expression);
38  			if (expr == null)
39  			{
40  				expr = MVEL.compileExpression(expression);
41  				expressionCache.put(expression, expr);
42  			}
43  			return MVEL.executeExpression(expr, values);
44  		}
45  		catch (final Exception ex)
46  		{
47  			throw new ExpressionEvaluationException("Evaluating MVEL expression failed: " + expression, ex);
48  		}
49  	}
50  }