001    /*******************************************************************************
002     * Portions created by Sebastian Thomschke are copyright (c) 2005-2012 Sebastian
003     * Thomschke.
004     * 
005     * All Rights Reserved. This program and the accompanying materials
006     * are made available under the terms of the Eclipse Public License v1.0
007     * which accompanies this distribution, and is available at
008     * http://www.eclipse.org/legal/epl-v10.html
009     * 
010     * Contributors:
011     *     Sebastian Thomschke - initial implementation.
012     *******************************************************************************/
013    package net.sf.oval.expression;
014    
015    import java.util.Map;
016    import java.util.Map.Entry;
017    
018    import net.sf.oval.exception.ExpressionEvaluationException;
019    import net.sf.oval.internal.Log;
020    import net.sf.oval.internal.util.ObjectCache;
021    import ognl.Ognl;
022    import ognl.OgnlContext;
023    import ognl.OgnlException;
024    
025    /**
026     * @author Sebastian Thomschke
027     *
028     */
029    public class ExpressionLanguageOGNLImpl implements ExpressionLanguage
030    {
031            private static final Log LOG = Log.getLog(ExpressionLanguageOGNLImpl.class);
032    
033            private final ObjectCache<String, Object> expressionCache = new ObjectCache<String, Object>();
034    
035            /**
036             * {@inheritDoc}
037             */
038            public Object evaluate(final String expression, final Map<String, ? > values) throws ExpressionEvaluationException
039            {
040                    LOG.debug("Evaluating OGNL expression: {1}", expression);
041                    try
042                    {
043                            final OgnlContext ctx = (OgnlContext) Ognl.createDefaultContext(null);
044    
045                            for (final Entry<String, ? > entry : values.entrySet())
046                                    ctx.put(entry.getKey(), entry.getValue());
047    
048                            Object expr = expressionCache.get(expression);
049                            if (expr == null)
050                            {
051                                    expr = Ognl.parseExpression(expression);
052                                    expressionCache.put(expression, expr);
053                            }
054                            return Ognl.getValue(expr, ctx);
055                    }
056                    catch (final OgnlException ex)
057                    {
058                            throw new ExpressionEvaluationException("Evaluating MVEL expression failed: " + expression, ex);
059                    }
060            }
061    
062            /**
063             * {@inheritDoc}
064             */
065            public boolean evaluateAsBoolean(final String expression, final Map<String, ? > values)
066                            throws ExpressionEvaluationException
067            {
068                    final Object result = evaluate(expression, values);
069                    if (!(result instanceof Boolean))
070                            throw new ExpressionEvaluationException("The script must return a boolean value.");
071                    return (Boolean) result;
072            }
073    }