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.Map;
021    import java.util.HashMap;
022    import java.util.Collections;
023    
024    import org.apache.commons.logging.Log;
025    import org.apache.commons.logging.LogFactory;
026    
027    import ognl.Ognl;
028    import ognl.OgnlContext;
029    
030    /**
031     * Provides a simple facility to evaluate condition expressions using the
032     * <a href="http://ognl.org">OGNL</a> language.
033     *
034     * <p>
035     * This class is thread-safe.
036     * </p>
037     *
038     * @version $Rev: 470597 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $
039     */
040    public class OgnlConditionParser
041        implements ConditionParser
042    {
043        private static final Log log = LogFactory.getLog(OgnlConditionParser.class);
044        
045        private final Map vars;
046        
047        public OgnlConditionParser() {
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 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            // Object root;
102            OgnlContext context = new OgnlContext(vars);
103    
104            Object expr = Ognl.parseExpression(expression);
105            Object result = Ognl.getValue(expr, context);
106            
107            if (debug) {
108                log.debug("Result: " + result);
109            }
110    
111            return result;
112        }
113    }