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 groovy.lang.Binding;
16  import groovy.lang.GroovyShell;
17  import groovy.lang.Script;
18  
19  import java.util.Map;
20  import java.util.Map.Entry;
21  
22  import net.sf.oval.exception.ExpressionEvaluationException;
23  import net.sf.oval.internal.Log;
24  import net.sf.oval.internal.util.ObjectCache;
25  import net.sf.oval.internal.util.ThreadLocalObjectCache;
26  
27  /**
28   * @author Sebastian Thomschke
29   */
30  public class ExpressionLanguageGroovyImpl extends AbstractExpressionLanguage
31  {
32  	private static final Log LOG = Log.getLog(ExpressionLanguageGroovyImpl.class);
33  
34  	private static final GroovyShell GROOVY_SHELL = new GroovyShell();
35  
36  	private final ThreadLocalObjectCache<String, Script> threadScriptCache = new ThreadLocalObjectCache<String, Script>();
37  
38  	public Object evaluate(final String expression, final Map<String, ? > values) throws ExpressionEvaluationException
39  	{
40  		LOG.debug("Evaluating Groovy expression: {1}", expression);
41  		try
42  		{
43  			final ObjectCache<String, Script> scriptCache = threadScriptCache.get();
44  			Script script = scriptCache.get(expression);
45  			if (script == null)
46  			{
47  				script = GROOVY_SHELL.parse(expression);
48  				scriptCache.put(expression, script);
49  			}
50  
51  			final Binding binding = new Binding();
52  			for (final Entry<String, ? > entry : values.entrySet())
53  			{
54  				binding.setVariable(entry.getKey(), entry.getValue());
55  			}
56  			script.setBinding(binding);
57  			return script.run();
58  		}
59  		catch (final Exception ex)
60  		{
61  			throw new ExpressionEvaluationException("Evaluating script with Groovy failed.", ex);
62  		}
63  	}
64  }