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 org.apache.commons.logging.Log;
020    import org.apache.commons.logging.LogFactory;
021    import org.apache.commons.jexl.Expression;
022    import org.apache.commons.jexl.ExpressionFactory;
023    import org.apache.commons.jexl.JexlContext;
024    import org.apache.commons.jexl.JexlHelper;
025    
026    import java.util.Map;
027    import java.util.HashMap;
028    import java.util.Collections;
029    
030    /**
031     * Provides a simple facility to evaluate condition expressions using the
032     * <a href="http://jakarta.apache.org/commons/jexl">Jexl</a> language.
033     *
034     * <p>
035     * This class is thread-safe.
036     * </p>
037     *
038     * @version $Rev: 487175 $ $Date: 2006-12-14 03:10:31 -0800 (Thu, 14 Dec 2006) $
039     */
040    public class JexlConditionParser
041        implements ConditionParser
042    {
043        private static final Log log = LogFactory.getLog(JexlConditionParser.class);
044    
045        private final Map vars;
046    
047        public JexlConditionParser() {
048            // Setup the default vars
049            vars = new HashMap();
050    
051            vars.put("props", Collections.unmodifiableMap(System.getProperties()));
052            vars.put("java", new JavaVariable());
053            vars.put("os", new OsVariable());
054        }
055    
056        /**
057         * Evaluate a condition expression.
058         *
059         * @param expression    The condition expression to evaluate; must not be null
060         * @return              True if the condition is satisfied
061         *
062         * @throws org.apache.geronimo.system.configuration.condition.ConditionParserException     Failed to evaluate condition expression
063         */
064        public boolean evaluate(final String expression) throws ConditionParserException {
065            if (expression == null) {
066                throw new IllegalArgumentException("Expression must not be null");
067            }
068    
069            // Empty expressions are true
070            if (expression.trim().length() == 0) {
071                log.debug("Expression is empty; skipping evaluation");
072    
073                return true;
074            }
075    
076            Object result;
077            try {
078                result = doEvaluate(expression);
079            }
080            catch (Exception e) {
081                throw new ConditionParserException("Failed to evaluate expression: " + expression, e);
082            }
083    
084            if (result instanceof Boolean) {
085                return ((Boolean)result).booleanValue();
086            }
087            else {
088                throw new ConditionParserException("Expression '" + expression + "' did not evaluate to a boolean value; found: " + result);
089            }
090        }
091    
092        private Object doEvaluate(final String expression) throws Exception {
093            assert expression != null;
094    
095            boolean debug = log.isDebugEnabled();
096    
097            if (debug) {
098                log.debug("Evaluating expression: " + expression);
099            }
100    
101            Expression expr = ExpressionFactory.createExpression(expression);
102    
103            JexlContext ctx = JexlHelper.createContext();
104            ctx.setVars(vars);
105    
106            Object result = expr.evaluate(ctx);
107            if (debug) {
108                log.debug("Result: " + result);
109            }
110    
111            return result;
112        }
113    }