1 17 18 package org.apache.geronimo.system.configuration.condition; 19 20 import java.util.Map ; 21 import java.util.HashMap ; 22 import java.util.Collections ; 23 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 27 import ognl.Ognl; 28 import ognl.OgnlContext; 29 30 40 public class OgnlConditionParser 41 implements ConditionParser 42 { 43 private static final Log log = LogFactory.getLog(OgnlConditionParser.class); 44 45 private final Map vars; 46 47 public OgnlConditionParser() { 48 vars = new HashMap (); 50 51 vars.put("props", Collections.unmodifiableMap(System.getProperties())); 52 vars.put("java", new JavaVariable()); 53 vars.put("os", new OsVariable()); 54 } 55 56 64 public boolean evaluate(final String expression) throws ConditionParserException { 65 if (expression == null) { 66 throw new IllegalArgumentException ("Expression must not be null"); 67 } 68 69 if (expression.trim().length() == 0) { 71 log.debug("Expression is empty; skipping evaluation"); 72 73 return true; 74 } 75 76 Object result; 77 try { 78 result = doEvaluate(expression); 79 } 80 catch (Exception e) { 81 throw new ConditionParserException("Failed to evaluate expression: " + expression, e); 82 } 83 84 if (result instanceof Boolean ) { 85 return ((Boolean )result).booleanValue(); 86 } 87 else { 88 throw new ConditionParserException("Expression '" + expression + "' did not evaluate to a boolean value; found: " + result); 89 } 90 } 91 92 private Object doEvaluate(final String expression) throws Exception { 93 assert expression != null; 94 95 boolean debug = log.isDebugEnabled(); 96 97 if (debug) { 98 log.debug("Evaluating expression: " + expression); 99 } 100 101 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 } 114 | Popular Tags |