/
ELEvaluator.java
257 lines (234 loc) · 8.76 KB
/
ELEvaluator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/**
* 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.oozie.util;
import org.apache.commons.el.ExpressionEvaluatorImpl;
import org.apache.commons.el.ExpressionString;
import javax.servlet.jsp.el.ELException;
import javax.servlet.jsp.el.ExpressionEvaluator;
import javax.servlet.jsp.el.FunctionMapper;
import javax.servlet.jsp.el.VariableResolver;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;
/**
* JSP Expression Language Evaluator. <p> It provides a more convenient way of using the JSP EL Evaluator.
*/
public class ELEvaluator {
/**
* Provides functions and variables for the EL evaluator. <p> All functions and variables in the context of an EL
* evaluator are accessible from EL expressions.
*/
public static class Context implements VariableResolver, FunctionMapper {
private Map<String, Object> vars;
private Map<String, Method> functions;
/**
* Create an empty context.
*/
public Context() {
vars = new HashMap<String, Object>();
functions = new HashMap<String, Method>();
}
/**
* Add variables to the context. <p>
*
* @param vars variables to add to the context.
*/
public void setVariables(Map<String, Object> vars) {
this.vars.putAll(vars);
}
/**
* Add a variable to the context. <p>
*
* @param name variable name.
* @param value variable value.
*/
public void setVariable(String name, Object value) {
vars.put(name, value);
}
/**
* Return a variable from the context. <p>
*
* @param name variable name.
* @return the variable value.
*/
public Object getVariable(String name) {
return vars.get(name);
}
/**
* Add a function to the context. <p>
*
* @param prefix function prefix.
* @param functionName function name.
* @param method method that will be invoked for the function, it must be a static and public method.
*/
public void addFunction(String prefix, String functionName, Method method) {
if ((method.getModifiers() & (Modifier.PUBLIC | Modifier.STATIC)) != (Modifier.PUBLIC | Modifier.STATIC)) {
throw new IllegalArgumentException(XLog.format("Method[{0}] must be public and static", method));
}
prefix = (prefix.length() > 0) ? prefix + ":" : "";
functions.put(prefix + functionName, method);
}
/**
* Resolve a variable name. Used by the EL evaluator implemenation. <p>
*
* @param name variable name.
* @return the variable value.
* @throws ELException thrown if the variable is not defined in the context.
*/
public Object resolveVariable(String name) throws ELException {
if (!vars.containsKey(name)) {
throw new ELException(XLog.format("variable [{0}] cannot be resolved", name));
}
return vars.get(name);
}
/**
* Resolve a function prefix:name. Used by the EL evaluator implementation. <p>
*
* @param prefix function prefix.
* @param name function name.
* @return the method associated to the function.
*/
public Method resolveFunction(String prefix, String name) {
if (prefix.length() > 0) {
name = prefix + ":" + name;
}
return functions.get(name);
}
}
private static ThreadLocal<ELEvaluator> current = new ThreadLocal<ELEvaluator>();
/**
* If within the scope of a EL evaluation call, it gives access to the ELEvaluator instance performing the EL
* evaluation. <p> This is useful for EL function methods to get access to the variables of the Evaluator. Because
* of this, ELEvaluator variables can be used to pass context to EL function methods (which must be static methods).
* <p>
*
* @return the ELEvaluator in scope, or <code>null</code> if none.
*/
public static ELEvaluator getCurrent() {
return current.get();
}
private Context context;
private ExpressionEvaluatorImpl evaluator = new ExpressionEvaluatorImpl();
/**
* Creates an ELEvaluator with no functions and no variables defined.
*/
public ELEvaluator() {
this(new Context());
}
/**
* Creates an ELEvaluator with the functions and variables defined in the given {@link ELEvaluator.Context}. <p>
*
* @param context the ELSupport with functions and variables to be available for EL evalution.
*/
public ELEvaluator(Context context) {
this.context = context;
}
/**
* Return the context with the functions and variables of the EL evaluator. <p>
*
* @return the context.
*/
public Context getContext() {
return context;
}
/**
* Convenience method that sets a variable in the EL evaluator context. <p>
*
* @param name variable name.
* @param value variable value.
*/
public void setVariable(String name, Object value) {
context.setVariable(name, value);
}
/**
* Convenience method that returns a variable from the EL evaluator context. <p>
*
* @param name variable name.
* @return the variable value, <code>null</code> if not defined.
*/
public Object getVariable(String name) {
return context.getVariable(name);
}
/**
* Evaluate an EL expression. <p>
*
* @param <T> the return type of the expression
* @param expr EL expression to evaluate.
* @param clazz return type of the EL expression.
* @return the object the EL expression evaluated to.
* @throws Exception thrown if an EL function failed due to a transient error or EL expression could not be
* evaluated.
*/
@SuppressWarnings({"unchecked", "deprecation"})
public <T> T evaluate(String expr, Class<T> clazz) throws Exception {
ELEvaluator existing = current.get();
try {
current.set(this);
return (T) evaluator.evaluate(expr, clazz, context, context);
}
catch (ELException ex) {
if (ex.getRootCause() instanceof Exception) {
throw (Exception) ex.getRootCause();
}
else {
throw ex;
}
}
finally {
current.set(existing);
}
}
/**
* Check if the input expression contains sequence statically. for example
* identify if "," is present outside of a function invocation in the given
* expression. Ex "${func('abc')},${func('def'}",
*
* @param expr - Expression string
* @param sequence - char sequence to check in the input expression
* @return true if present
* @throws Exception Exception thrown if an EL function failed due to a
* transient error or EL expression could not be parsed
*/
public boolean checkForExistence(String expr, String sequence)
throws Exception {
try {
Object exprString = evaluator.parseExpressionString(expr);
if (exprString instanceof ExpressionString) {
for (Object element : ((ExpressionString)exprString).getElements()) {
if (element instanceof String &&
element.toString().contains(sequence)) {
return true;
}
}
} else if (exprString instanceof String) {
if (((String)exprString).contains(sequence)) {
return true;
}
}
return false;
} catch (ELException ex) {
if (ex.getRootCause() instanceof Exception) {
throw (Exception) ex.getRootCause();
}
else {
throw ex;
}
}
}
}