-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
/
ExpressionFactory2.java
172 lines (141 loc) · 5.11 KB
/
ExpressionFactory2.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
package hudson;
import org.acegisecurity.AcegiSecurityException;
import org.apache.commons.jelly.JellyContext;
import org.apache.commons.jelly.JellyException;
import org.apache.commons.jelly.expression.Expression;
import org.apache.commons.jelly.expression.ExpressionFactory;
import org.apache.commons.jelly.expression.ExpressionSupport;
import org.apache.commons.jexl.JexlContext;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerRequest;
/**
* {@link ExpressionFactory} so that security exception aborts the page rendering.
*
* @author Kohsuke Kawaguchi
*/
final class ExpressionFactory2 implements ExpressionFactory {
public Expression createExpression(String text) throws JellyException {
try {
return new JexlExpression(
org.apache.commons.jexl.ExpressionFactory.createExpression(text)
);
} catch (Exception e) {
throw new JellyException("Unable to create expression: " + text, e);
}
}
/*
* Copyright 2002,2004 The Apache Software Foundation.
*
* Licensed 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.
*/
static final class JexlExpression extends ExpressionSupport {
/** The Jexl expression object */
private org.apache.commons.jexl.Expression expression;
public JexlExpression(org.apache.commons.jexl.Expression expression) {
this.expression = expression;
}
@Override
public String toString() {
return super.toString() + "[" + expression.getExpression() + "]";
}
// Expression interface
//-------------------------------------------------------------------------
public String getExpressionText() {
return "${" + expression.getExpression() + "}";
}
public Object evaluate(JellyContext context) {
try {
CURRENT_CONTEXT.set(context);
JexlContext jexlContext = new JellyJexlContext( context );
return expression.evaluate(jexlContext);
} catch (AcegiSecurityException e) {
// let the security exception pass through
throw e;
} catch (Exception e) {
StaplerRequest currentRequest = Stapler.getCurrentRequest();
LOGGER.log(Level.WARNING,"Caught exception evaluating: " + expression + " in " + (currentRequest != null ? currentRequest.getOriginalRequestURI() : "?") + ". Reason: " + e, e);
return null;
} finally {
CURRENT_CONTEXT.set(null);
}
}
private static final Logger LOGGER = Logger.getLogger(JexlExpression.class.getName());
}
static final class JellyJexlContext implements JexlContext {
private Map vars;
JellyJexlContext(JellyContext context) {
this.vars = new JellyMap( context );
}
public void setVars(Map vars) {
this.vars.clear();
this.vars.putAll( vars );
}
public Map getVars() {
return this.vars;
}
}
static final class JellyMap implements Map {
private JellyContext context;
JellyMap(JellyContext context) {
this.context = context;
}
public Object get(Object key) {
return context.getVariable( (String) key );
}
public void clear() {
// not implemented
}
public boolean containsKey(Object key) {
return ( get( key ) != null );
}
public boolean containsValue(Object value) {
return false;
}
public Set entrySet() {
return null;
}
public boolean isEmpty() {
return false;
}
public Set keySet() {
return null;
}
public Object put(Object key, Object value) {
return null;
}
public void putAll(Map t) {
// not implemented
}
public Object remove(Object key) {
return null;
}
public int size() {
return -1;
}
public Collection values() {
return null;
}
}
/**
* When called from within the JEXL expression evaluation,
* retains the current {@link JellyContext}.
*
* @see Functions#getCurrentJellyContext()
*/
protected static final ThreadLocal<JellyContext> CURRENT_CONTEXT = new ThreadLocal<>();
}