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    
018    package org.apache.geronimo.system.configuration.condition;
019    
020    import java.util.HashMap;
021    import java.util.Map;
022    import java.util.Properties;
023    
024    import ognl.Ognl;
025    import ognl.OgnlContext;
026    import org.apache.commons.logging.Log;
027    import org.apache.commons.logging.LogFactory;
028    
029    /**
030     * Provides a simple facility to evaluate condition expressions using the
031     * <a href="http://ognl.org">OGNL</a> language.
032     *
033     * <p>
034     * This class is thread-safe.
035     * </p>
036     *
037     * @version $Rev: 514355 $ $Date: 2007-03-03 23:29:59 -0500 (Sat, 03 Mar 2007) $
038     */
039    public class OgnlConditionParser
040        implements ConditionParser
041    {
042        private static final Log log = LogFactory.getLog(OgnlConditionParser.class);
043        
044        private final Map<String, Object> vars;
045        
046        public OgnlConditionParser() {
047            // Setup the default vars
048            vars = new HashMap<String, Object>();
049            
050            vars.put("java", new JavaVariable());
051            vars.put("os", new OsVariable());
052            
053            // Install properties (to allow getProperty(x,y) to be used for defaults
054            Properties props = new Properties();
055            props.putAll(System.getProperties());
056            vars.put("props", props);
057        }
058        
059        /**
060         * Evaluate a condition expression.
061         *
062         * @param expression    The condition expression to evaluate; must not be null
063         * @return              True if the condition is satisfied
064         *
065         * @throws ConditionParserException     Failed to evaluate condition expression
066         */
067        public boolean evaluate(final String expression) throws ConditionParserException {
068            if (expression == null) {
069                throw new IllegalArgumentException("Expression must not be null");
070            }
071            
072            // Empty expressions are true
073            if (expression.trim().length() == 0) {
074                log.debug("Expression is empty; skipping evaluation");
075                
076                return true;
077            }
078    
079            Object result;
080            try {
081                result = doEvaluate(expression);
082            }
083            catch (Exception e) {
084                throw new ConditionParserException("Failed to evaluate expression: " + expression, e);
085            }
086    
087            if (result instanceof Boolean) {
088                return (Boolean) result;
089            }
090            else {
091                throw new ConditionParserException("Expression '" + expression + "' did not evaluate to a boolean value; found: " + result);
092            }
093        }
094        
095        private Object doEvaluate(final String expression) throws Exception {
096            assert expression != null;
097    
098            boolean debug = log.isDebugEnabled();
099    
100            if (debug) {
101                log.debug("Evaluating expression: " + expression);
102            }
103    
104            // Object root;
105            OgnlContext context = new OgnlContext(vars);
106    
107            Object expr = Ognl.parseExpression(expression);
108            Object result = Ognl.getValue(expr, context);
109            
110            if (debug) {
111                log.debug("Result: " + result);
112            }
113    
114            return result;
115        }
116    }