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
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 /**
31 * Provides a simple facility to evaluate condition expressions using the
32 * <a href="http://ognl.org">OGNL</a> language.
33 *
34 * <p>
35 * This class is thread-safe.
36 * </p>
37 *
38 * @version $Rev: 470597 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $
39 */
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
49 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 /**
57 * Evaluate a condition expression.
58 *
59 * @param expression The condition expression to evaluate; must not be null
60 * @return True if the condition is satisfied
61 *
62 * @throws ConditionParserException Failed to evaluate condition expression
63 */
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
70 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
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 }