Save This Page
Home » geronimo-2.2-source-release » org.apache.geronimo.system.configuration.condition » [javadoc | source]
    1   /**
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   package org.apache.geronimo.system.configuration.condition;
   18   
   19   import java.util.HashMap;
   20   import java.util.Map;
   21   import java.util.Properties;
   22   
   23   import org.apache.commons.jexl.Expression;
   24   import org.apache.commons.jexl.ExpressionFactory;
   25   import org.apache.commons.jexl.JexlContext;
   26   import org.apache.commons.jexl.JexlHelper;
   27   import org.apache.commons.jexl.context.HashMapContext;
   28   import org.slf4j.Logger;
   29   import org.slf4j.LoggerFactory;
   30   
   31   /**
   32    * Provides a simple facility to evaluate condition expressions using the
   33    * <a href="http://jakarta.apache.org/commons/jexl">Jexl</a> language.
   34    *
   35    * <p>
   36    * This class is thread-safe.
   37    * </p>
   38    *
   39    * @version $Rev: 653740 $ $Date: 2008-05-06 03:44:18 -0700 (Tue, 06 May 2008) $
   40    */
   41   public class JexlConditionParser
   42       implements ConditionParser
   43   {
   44       private static final Logger log = LoggerFactory.getLogger(JexlConditionParser.class);
   45   
   46       private final Map<String, Object> vars;
   47   
   48       public JexlConditionParser(final Map vars) {
   49           if (vars == null) {
   50               throw new IllegalArgumentException("vars");
   51           }
   52           this.vars = vars;
   53       }
   54       
   55       public JexlConditionParser() {
   56           // Setup the default vars
   57           vars = new HashMap<String, Object>();
   58           ParserUtils.addDefaultVariables(vars);
   59       }
   60       
   61       /**
   62        * Evaluate a condition expression.
   63        *
   64        * @param expression    The condition expression to evaluate; must not be null
   65        * @return              True if the condition is satisfied
   66        *
   67        * @throws org.apache.geronimo.system.configuration.condition.ConditionParserException     Failed to evaluate condition expression
   68        */
   69       public boolean evaluate(final String expression) throws ConditionParserException {
   70           if (expression == null) {
   71               throw new IllegalArgumentException("Expression must not be null");
   72           }
   73   
   74           // Empty expressions are true
   75           if (expression.trim().length() == 0) {
   76               log.debug("Expression is empty; skipping evaluation");
   77   
   78               return true;
   79           }
   80   
   81           Object result;
   82           try {
   83               result = doEvaluate(expression);
   84           }
   85           catch (Exception e) {
   86               throw new ConditionParserException("Failed to evaluate expression: " + expression, e);
   87           }
   88   
   89           if (result instanceof Boolean) {
   90               return (Boolean) result;
   91           }
   92           else {
   93               throw new ConditionParserException("Expression '" + expression + "' did not evaluate to a boolean value; found: " + result);
   94           }
   95       }
   96   
   97       private Object doEvaluate(final String expression) throws Exception {
   98           assert expression != null;
   99   
  100           log.debug("Evaluating expression: {}", expression);
  101           
  102           Expression expr = ExpressionFactory.createExpression(expression);
  103   
  104           JexlContext ctx = JexlHelper.createContext();
  105           ctx.setVars(vars);
  106   
  107           Object result = expr.evaluate(ctx);
  108           log.debug("Result: {}", result);
  109   
  110           return result;
  111       }    
  112   }

Save This Page
Home » geronimo-2.2-source-release » org.apache.geronimo.system.configuration.condition » [javadoc | source]