001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.system.configuration.condition;
018    
019    import java.util.HashMap;
020    import java.util.Map;
021    import java.util.Properties;
022    
023    import org.apache.commons.jexl.Expression;
024    import org.apache.commons.jexl.ExpressionFactory;
025    import org.apache.commons.jexl.JexlContext;
026    import org.apache.commons.jexl.JexlHelper;
027    import org.apache.commons.jexl.context.HashMapContext;
028    import org.apache.commons.logging.Log;
029    import org.apache.commons.logging.LogFactory;
030    
031    /**
032     * Provides a simple facility to evaluate condition expressions using the
033     * <a href="http://jakarta.apache.org/commons/jexl">Jexl</a> language.
034     *
035     * <p>
036     * This class is thread-safe.
037     * </p>
038     *
039     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
040     */
041    public class JexlConditionParser
042        implements ConditionParser
043    {
044        private static final Log log = LogFactory.getLog(JexlConditionParser.class);
045    
046        private final Map<String, Object> vars;
047    
048        public JexlConditionParser(final Map vars) {
049            if (vars == null) {
050                throw new IllegalArgumentException("vars");
051            }
052            this.vars = vars;
053        }
054        
055        public JexlConditionParser() {
056            // Setup the default vars
057            vars = new HashMap<String, Object>();
058            ParserUtils.addDefaultVariables(vars);
059        }
060        
061        /**
062         * Evaluate a condition expression.
063         *
064         * @param expression    The condition expression to evaluate; must not be null
065         * @return              True if the condition is satisfied
066         *
067         * @throws org.apache.geronimo.system.configuration.condition.ConditionParserException     Failed to evaluate condition expression
068         */
069        public boolean evaluate(final String expression) throws ConditionParserException {
070            if (expression == null) {
071                throw new IllegalArgumentException("Expression must not be null");
072            }
073    
074            // Empty expressions are true
075            if (expression.trim().length() == 0) {
076                log.debug("Expression is empty; skipping evaluation");
077    
078                return true;
079            }
080    
081            Object result;
082            try {
083                result = doEvaluate(expression);
084            }
085            catch (Exception e) {
086                throw new ConditionParserException("Failed to evaluate expression: " + expression, e);
087            }
088    
089            if (result instanceof Boolean) {
090                return (Boolean) result;
091            }
092            else {
093                throw new ConditionParserException("Expression '" + expression + "' did not evaluate to a boolean value; found: " + result);
094            }
095        }
096    
097        private Object doEvaluate(final String expression) throws Exception {
098            assert expression != null;
099    
100            boolean debug = log.isDebugEnabled();
101    
102            if (debug) {
103                log.debug("Evaluating expression: " + expression);
104            }
105    
106            Expression expr = ExpressionFactory.createExpression(expression);
107    
108            JexlContext ctx = JexlHelper.createContext();
109            ctx.setVars(vars);
110    
111            Object result = expr.evaluate(ctx);
112            if (debug) {
113                log.debug("Result: " + result);
114            }
115    
116            return result;
117        }    
118    }