Package org.apache.geronimo.system.configuration.condition

Source Code of org.apache.geronimo.system.configuration.condition.OgnlConditionParser

/**
*   Licensed to the Apache Software Foundation (ASF) under one or more
*  contributor license agreements.  See the NOTICE file distributed with
*  this work for additional information regarding copyright ownership.
*  The ASF licenses this file to You under the Apache License, Version 2.0
*  (the "License"); you may not use this file except in compliance with
*  the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/

package org.apache.geronimo.system.configuration.condition;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import ognl.Ognl;
import ognl.OgnlContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* Provides a simple facility to evaluate condition expressions using the
* <a href="http://ognl.org">OGNL</a> language.
*
* <p>
* This class is thread-safe.
* </p>
*
* @version $Rev: 593629 $ $Date: 2007-11-10 03:29:01 +0800 (Sat, 10 Nov 2007) $
*/
public class OgnlConditionParser
    implements ConditionParser
{
    private static final Log log = LogFactory.getLog(OgnlConditionParser.class);
   
    private final Map<String, Object> vars;
   
    public OgnlConditionParser(final Map vars) {
        if (vars == null) {
            throw new IllegalArgumentException("vars");
        }
        this.vars = vars;
    }
   
    public OgnlConditionParser() {
        // Setup the default vars
        vars = new HashMap<String, Object>();
        ParserUtils.addDefaultVariables(vars);
    }
   
    /**
     * Evaluate a condition expression.
     *
     * @param expression    The condition expression to evaluate; must not be null
     * @return              True if the condition is satisfied
     *
     * @throws ConditionParserException     Failed to evaluate condition expression
     */
    public boolean evaluate(final String expression) throws ConditionParserException {
        if (expression == null) {
            throw new IllegalArgumentException("Expression must not be null");
        }
       
        // Empty expressions are true
        if (expression.trim().length() == 0) {
            log.debug("Expression is empty; skipping evaluation");
           
            return true;
        }

        Object result;
        try {
            result = doEvaluate(expression);
        }
        catch (Exception e) {
            throw new ConditionParserException("Failed to evaluate expression: " + expression, e);
        }

        if (result instanceof Boolean) {
            return (Boolean) result;
        }
        else {
            throw new ConditionParserException("Expression '" + expression + "' did not evaluate to a boolean value; found: " + result);
        }
    }
   
    private Object doEvaluate(final String expression) throws Exception {
        assert expression != null;

        boolean debug = log.isDebugEnabled();

        if (debug) {
            log.debug("Evaluating expression: " + expression);
        }

        // Object root;
        OgnlContext context = new OgnlContext(vars);

        Object expr = Ognl.parseExpression(expression);
        Object result = Ognl.getValue(expr, context);
       
        if (debug) {
            log.debug("Result: " + result);
        }

        return result;
    }
}
TOP

Related Classes of org.apache.geronimo.system.configuration.condition.OgnlConditionParser

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.