KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > system > configuration > condition > JexlConditionParser


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 org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.apache.commons.jexl.Expression;
22 import org.apache.commons.jexl.ExpressionFactory;
23 import org.apache.commons.jexl.JexlContext;
24 import org.apache.commons.jexl.JexlHelper;
25
26 import java.util.Map JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Collections JavaDoc;
29
30 /**
31  * Provides a simple facility to evaluate condition expressions using the
32  * <a HREF="http://jakarta.apache.org/commons/jexl">Jexl</a> language.
33  *
34  * <p>
35  * This class is thread-safe.
36  * </p>
37  *
38  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
39  */

40 public class JexlConditionParser
41     implements ConditionParser
42 {
43     private static final Log log = LogFactory.getLog(JexlConditionParser.class);
44
45     private final Map JavaDoc vars;
46
47     public JexlConditionParser() {
48         // Setup the default vars
49
vars = new HashMap JavaDoc();
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 org.apache.geronimo.system.configuration.condition.ConditionParserException Failed to evaluate condition expression
63      */

64     public boolean evaluate(final String JavaDoc expression) throws ConditionParserException {
65         if (expression == null) {
66             throw new IllegalArgumentException JavaDoc("Expression must not be null");
67         }
68
69         // Empty expressions are true
70
if (expression.trim().length() == 0) {
71             log.debug("Expression is empty; skipping evaluation");
72
73             return true;
74         }
75
76         Object JavaDoc result;
77         try {
78             result = doEvaluate(expression);
79         }
80         catch (Exception JavaDoc e) {
81             throw new ConditionParserException("Failed to evaluate expression: " + expression, e);
82         }
83
84         if (result instanceof Boolean JavaDoc) {
85             return ((Boolean JavaDoc)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 JavaDoc doEvaluate(final String JavaDoc expression) throws Exception JavaDoc {
93         assert expression != null;
94
95         boolean debug = log.isDebugEnabled();
96
97         if (debug) {
98             log.debug("Evaluating expression: " + expression);
99         }
100
101         Expression expr = ExpressionFactory.createExpression(expression);
102
103         JexlContext ctx = JexlHelper.createContext();
104         ctx.setVars(vars);
105
106         Object JavaDoc result = expr.evaluate(ctx);
107         if (debug) {
108             log.debug("Result: " + result);
109         }
110
111         return result;
112     }
113 }
114
Popular Tags