View | Details | Raw Unified | Return to bug 48813
Collapse All | Expand All

(-)java/org/apache/el/parser/AstValue.java (-5 / +2 lines)
Lines 29-34 Link Here
29
29
30
import org.apache.el.lang.ELSupport;
30
import org.apache.el.lang.ELSupport;
31
import org.apache.el.lang.EvaluationContext;
31
import org.apache.el.lang.EvaluationContext;
32
import org.apache.el.parser.parameters.ELParameters;
32
import org.apache.el.util.MessageFactory;
33
import org.apache.el.util.MessageFactory;
33
import org.apache.el.util.ReflectionUtil;
34
import org.apache.el.util.ReflectionUtil;
34
35
Lines 39-48 Link Here
39
 */
40
 */
40
public final class AstValue extends SimpleNode {
41
public final class AstValue extends SimpleNode {
41
42
42
    protected static final boolean COERCE_TO_ZERO =
43
        Boolean.valueOf(System.getProperty(
44
                "org.apache.el.parser.COERCE_TO_ZERO", "true")).booleanValue();
45
    
46
    protected static class Target {
43
    protected static class Target {
47
        protected Object base;
44
        protected Object base;
48
45
Lines 175-181 Link Here
175
172
176
        // coerce to the expected type
173
        // coerce to the expected type
177
        Class<?> targetClass = resolver.getType(ctx, t.base, t.property);
174
        Class<?> targetClass = resolver.getType(ctx, t.base, t.property);
178
        if (COERCE_TO_ZERO == true
175
        if (ELParameters.getCurrentInstance().isCoerceToZero()
179
                || !isAssignable(value, targetClass)) {
176
                || !isAssignable(value, targetClass)) {
180
            resolver.setValue(ctx, t.base, t.property,
177
            resolver.setValue(ctx, t.base, t.property,
181
                    ELSupport.coerceToType(value, targetClass));
178
                    ELSupport.coerceToType(value, targetClass));
(-)java/org/apache/el/parser/parameters/ELParameters.java (+66 lines)
Line 0 Link Here
1
package org.apache.el.parser.parameters;
2
3
import java.util.Map;
4
import java.util.concurrent.ConcurrentHashMap;
5
6
import javax.servlet.ServletContext;
7
8
/**
9
 * This class holds parameters that can be used by the Expression Language
10
 * parser while evaluating expressions.
11
 * 
12
 * It takes its defaults from system properties but those values may be
13
 * overriden during a {@link ServletContext} (web application) initialization.
14
 * 
15
 * @see ExpressionLanguageConfigurer
16
 * @author Ramiro Pereira de Magalhães [rpm_mailbox@yahoo.com.br]
17
 */
18
public class ELParameters {
19
	private static final String COERCE_TO_ZERO =
20
		"org.apache.el.parser.COERCE_TO_ZERO";
21
22
	private final boolean coerceToZero;
23
24
	private ELParameters() {
25
		this.coerceToZero = Boolean.valueOf(
26
			System.getProperty(COERCE_TO_ZERO, Boolean.TRUE.toString()))
27
			.booleanValue();
28
	}
29
30
	public ELParameters(ServletContext context) {
31
		this.coerceToZero =
32
			Boolean.valueOf(context.getInitParameter(COERCE_TO_ZERO))
33
			.booleanValue();
34
35
		context.log("Parameter " + COERCE_TO_ZERO + " set to " + this.coerceToZero);
36
37
		currentThreadParams.put(
38
			Thread.currentThread().getContextClassLoader(), this);
39
	}
40
41
	public boolean isCoerceToZero() {
42
		return this.coerceToZero;
43
	}
44
45
	public void destroy() {
46
		currentThreadParams.remove(Thread.currentThread()
47
			.getContextClassLoader());
48
	}
49
50
	private final static ELParameters defaultParameters = new ELParameters();
51
52
	private final static Map<ClassLoader, ELParameters> currentThreadParams =
53
		new ConcurrentHashMap<ClassLoader, ELParameters>();
54
55
	public static ELParameters getCurrentInstance() {
56
		final ClassLoader contextClassloader = Thread.currentThread()
57
			.getContextClassLoader();
58
59
		if (!currentThreadParams.containsKey(contextClassloader)) {
60
			return defaultParameters;
61
		}
62
63
		return currentThreadParams.get(contextClassloader);
64
	}
65
66
}
(-)java/org/apache/el/parser/parameters/ExpressionLanguageConfigurer.java (+23 lines)
Line 0 Link Here
1
package org.apache.el.parser.parameters;
2
3
import javax.servlet.ServletContextEvent;
4
import javax.servlet.ServletContextListener;
5
6
/**
7
 * Allows an application to set Expression Language parameters.
8
 * 
9
 * @author Ramiro Pereira de Magalhães [rpm_mailbox@yahoo.com.br]
10
 */
11
public class ExpressionLanguageConfigurer implements ServletContextListener {
12
13
	private ELParameters parameters;
14
15
	public void contextInitialized(ServletContextEvent context) {
16
		this.parameters = new ELParameters(context.getServletContext());
17
	}
18
19
	public void contextDestroyed(ServletContextEvent context) {
20
		this.parameters.destroy();
21
	}
22
23
}

Return to bug 48813