View Javadoc
1   /*
2    * Copyright 2002-2014 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.expression.spel;
18  
19  import org.springframework.core.SpringProperties;
20  
21  
22  /**
23   * Configuration object for the SpEL expression parser.
24   *
25   * @author Juergen Hoeller
26   * @author Phillip Webb
27   * @author Andy Clement
28   * @since 3.0
29   * @see org.springframework.expression.spel.standard.SpelExpressionParser#SpelExpressionParser(SpelParserConfiguration)
30   */
31  public class SpelParserConfiguration {
32  
33  	private static final SpelCompilerMode defaultCompilerMode;
34  
35  	static {
36  		String compilerMode = SpringProperties.getProperty("spring.expression.compiler.mode");
37  		defaultCompilerMode = (compilerMode != null ?
38  				SpelCompilerMode.valueOf(compilerMode.toUpperCase()) : SpelCompilerMode.OFF);
39  	}
40  
41  
42  	private final SpelCompilerMode compilerMode;
43  
44  	private final ClassLoader compilerClassLoader;
45  
46  	private final boolean autoGrowNullReferences;
47  
48  	private final boolean autoGrowCollections;
49  
50  	private final int maximumAutoGrowSize;
51  
52  
53  	/**
54  	 * Create a new {@code SpelParserConfiguration} instance with default settings.
55  	 */
56  	public SpelParserConfiguration() {
57  		this(null, null, false, false, Integer.MAX_VALUE);
58  	}
59  
60  	/**
61  	 * Create a new {@code SpelParserConfiguration} instance.
62  	 * @param compilerMode the compiler mode for the parser
63  	 * @param compilerClassLoader the ClassLoader to use as the basis for expression compilation
64  	 */
65  	public SpelParserConfiguration(SpelCompilerMode compilerMode, ClassLoader compilerClassLoader) {
66  		this(compilerMode, compilerClassLoader, false, false, Integer.MAX_VALUE);
67  	}
68  
69  	/**
70  	 * Create a new {@code SpelParserConfiguration} instance.
71  	 * @param autoGrowNullReferences if null references should automatically grow
72  	 * @param autoGrowCollections if collections should automatically grow
73  	 * @see #SpelParserConfiguration(boolean, boolean, int)
74  	 */
75  	public SpelParserConfiguration(boolean autoGrowNullReferences, boolean autoGrowCollections) {
76  		this(null, null, autoGrowNullReferences, autoGrowCollections, Integer.MAX_VALUE);
77  	}
78  
79  	/**
80  	 * Create a new {@code SpelParserConfiguration} instance.
81  	 * @param autoGrowNullReferences if null references should automatically grow
82  	 * @param autoGrowCollections if collections should automatically grow
83  	 * @param maximumAutoGrowSize the maximum size that the collection can auto grow
84  	 */
85  	public SpelParserConfiguration(boolean autoGrowNullReferences, boolean autoGrowCollections, int maximumAutoGrowSize) {
86  		this(null, null, autoGrowNullReferences, autoGrowCollections, maximumAutoGrowSize);
87  	}
88  
89  	/**
90  	 * Create a new {@code SpelParserConfiguration} instance.
91  	 * @param compilerMode the compiler mode that parsers using this configuration object should use
92  	 * @param compilerClassLoader the ClassLoader to use as the basis for expression compilation
93  	 * @param autoGrowNullReferences if null references should automatically grow
94  	 * @param autoGrowCollections if collections should automatically grow
95  	 * @param maximumAutoGrowSize the maximum size that the collection can auto grow
96  	 */
97  	public SpelParserConfiguration(SpelCompilerMode compilerMode, ClassLoader compilerClassLoader,
98  			boolean autoGrowNullReferences, boolean autoGrowCollections, int maximumAutoGrowSize) {
99  
100 		this.compilerMode = (compilerMode != null ? compilerMode : defaultCompilerMode);
101 		this.compilerClassLoader = compilerClassLoader;
102 		this.autoGrowNullReferences = autoGrowNullReferences;
103 		this.autoGrowCollections = autoGrowCollections;
104 		this.maximumAutoGrowSize = maximumAutoGrowSize;
105 	}
106 
107 
108 	/**
109 	 * @return the configuration mode for parsers using this configuration object
110 	 */
111 	public SpelCompilerMode getCompilerMode() {
112 		return this.compilerMode;
113 	}
114 
115 	/**
116 	 * @return the ClassLoader to use as the basis for expression compilation
117 	 */
118 	public ClassLoader getCompilerClassLoader() {
119 		return this.compilerClassLoader;
120 	}
121 
122 	/**
123 	 * @return {@code true} if {@code null} references should be automatically grown
124 	 */
125 	public boolean isAutoGrowNullReferences() {
126 		return this.autoGrowNullReferences;
127 	}
128 
129 	/**
130 	 * @return {@code true} if collections should be automatically grown
131 	 */
132 	public boolean isAutoGrowCollections() {
133 		return this.autoGrowCollections;
134 	}
135 
136 	/**
137 	 * @return the maximum size that a collection can auto grow
138 	 */
139 	public int getMaximumAutoGrowSize() {
140 		return this.maximumAutoGrowSize;
141 	}
142 
143 }