File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\ExpressionFactory2.java.html

1 package hudson;
2
3 import org.acegisecurity.AcegiSecurityException;
4 import org.apache.commons.jelly.JellyContext;
5 import org.apache.commons.jelly.JellyException;
6 import org.apache.commons.jelly.expression.Expression;
7 import org.apache.commons.jelly.expression.ExpressionFactory;
8 import org.apache.commons.jelly.expression.ExpressionSupport;
9 import org.apache.commons.jexl.JexlContext;
10
11 import java.util.Collection;
12 import java.util.Map;
13 import java.util.Set;
14 import java.util.logging.Level;
15 import java.util.logging.Logger;
16 import org.kohsuke.stapler.Stapler;
17 import org.kohsuke.stapler.StaplerRequest;
18
19 /**
20 * {@link ExpressionFactory} so that security exception aborts the page rendering.
21 *
22 * @author Kohsuke Kawaguchi
23 */
24 final class ExpressionFactory2 implements ExpressionFactory {
25 public Expression createExpression(String text) throws JellyException {
26 try {
27 return new JexlExpression(
28 org.apache.commons.jexl.ExpressionFactory.createExpression(text)
29 );
30 } catch (Exception e) {
31 throw new JellyException("Unable to create expression: " + text, e);
32 }
33 }
34
35 /*
36 * Copyright 2002,2004 The Apache Software Foundation.
37 *
38 * Licensed under the Apache License, Version 2.0 (the "License");
39 * you may not use this file except in compliance with the License.
40 * You may obtain a copy of the License at
41 *
42 * http://www.apache.org/licenses/LICENSE-2.0
43 *
44 * Unless required by applicable law or agreed to in writing, software
45 * distributed under the License is distributed on an "AS IS" BASIS,
46 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
47 * See the License for the specific language governing permissions and
48 * limitations under the License.
49 */
50 static final class JexlExpression extends ExpressionSupport {
51
52 /** The Jexl expression object */
53 private org.apache.commons.jexl.Expression expression;
54
55 public JexlExpression(org.apache.commons.jexl.Expression expression) {
56 this.expression = expression;
57 }
58
59 @Override
60 public String toString() {
61 return super.toString() + "[" + expression.getExpression() + "]";
62 }
63
64 // Expression interface
65 //-------------------------------------------------------------------------
66 public String getExpressionText() {
67 return "${" + expression.getExpression() + "}";
68 }
69
70 public Object evaluate(JellyContext context) {
71 try {
72 CURRENT_CONTEXT.set(context);
73 JexlContext jexlContext = new JellyJexlContext( context );
74 return expression.evaluate(jexlContext);
75 } catch (AcegiSecurityException e) {
76 // let the security exception pass through
77 throw e;
78 } catch (Exception e) {
79 StaplerRequest currentRequest = Stapler.getCurrentRequest();
80 LOGGER.log(Level.WARNING,"Caught exception evaluating: " + expression + " in " + (currentRequest != null ? currentRequest.getOriginalRequestURI() : "?") + ". Reason: " + e, e);
81 return null;
82 } finally {
83 CURRENT_CONTEXT.set(null);
84 }
85 }
86
87 private static final Logger LOGGER = Logger.getLogger(JexlExpression.class.getName());
88 }
89
90 static final class JellyJexlContext implements JexlContext {
91 private Map vars;
92
93 JellyJexlContext(JellyContext context) {
94 this.vars = new JellyMap( context );
95 }
96
97 public void setVars(Map vars) {
98 this.vars.clear();
99 this.vars.putAll( vars );
100 }
101
102 public Map getVars() {
103 return this.vars;
104 }
105 }
106
107
108 static final class JellyMap implements Map {
109
110 private JellyContext context;
111
112 JellyMap(JellyContext context) {
113 this.context = context;
114 }
115
116 public Object get(Object key) {
117 return context.getVariable( (String) key );
118 }
119
120 public void clear() {
121 // not implemented
122 }
123
124 public boolean containsKey(Object key) {
125 return ( get( key ) != null );
126 }
127
128 public boolean containsValue(Object value) {
129 return false;
130 }
131
132 public Set entrySet() {
133 return null;
134 }
135
136 public boolean isEmpty() {
137 return false;
138 }
139
140 public Set keySet() {
141 return null;
142 }
143
144 public Object put(Object key, Object value) {
145 return null;
146 }
147
148 public void putAll(Map t) {
149 // not implemented
150 }
151
152 public Object remove(Object key) {
153 return null;
154 }
155
156 public int size() {
157 return -1;
158 }
159
160 public Collection values() {
161 return null;
162 }
163 }
164
165 /**
166 * When called from within the JEXL expression evaluation,
167 * retains the current {@link JellyContext}.
168 *
169 * @see Functions#getCurrentJellyContext()
170 */
171 protected static final ThreadLocal<JellyContext> CURRENT_CONTEXT = new ThreadLocal<JellyContext>();
172 }
173