001/*
002 * Copyright 2002-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.expression.spel;
018
019import org.springframework.core.SpringProperties;
020
021/**
022 * Configuration object for the SpEL expression parser.
023 *
024 * @author Juergen Hoeller
025 * @author Phillip Webb
026 * @author Andy Clement
027 * @since 3.0
028 * @see org.springframework.expression.spel.standard.SpelExpressionParser#SpelExpressionParser(SpelParserConfiguration)
029 */
030public class SpelParserConfiguration {
031
032        private static final SpelCompilerMode defaultCompilerMode;
033
034        static {
035                String compilerMode = SpringProperties.getProperty("spring.expression.compiler.mode");
036                defaultCompilerMode = (compilerMode != null ?
037                                SpelCompilerMode.valueOf(compilerMode.toUpperCase()) : SpelCompilerMode.OFF);
038        }
039
040
041        private final SpelCompilerMode compilerMode;
042
043        private final ClassLoader compilerClassLoader;
044
045        private final boolean autoGrowNullReferences;
046
047        private final boolean autoGrowCollections;
048
049        private final int maximumAutoGrowSize;
050
051
052        /**
053         * Create a new {@code SpelParserConfiguration} instance with default settings.
054         */
055        public SpelParserConfiguration() {
056                this(null, null, false, false, Integer.MAX_VALUE);
057        }
058
059        /**
060         * Create a new {@code SpelParserConfiguration} instance.
061         * @param compilerMode the compiler mode for the parser
062         * @param compilerClassLoader the ClassLoader to use as the basis for expression compilation
063         */
064        public SpelParserConfiguration(SpelCompilerMode compilerMode, ClassLoader compilerClassLoader) {
065                this(compilerMode, compilerClassLoader, false, false, Integer.MAX_VALUE);
066        }
067
068        /**
069         * Create a new {@code SpelParserConfiguration} instance.
070         * @param autoGrowNullReferences if null references should automatically grow
071         * @param autoGrowCollections if collections should automatically grow
072         * @see #SpelParserConfiguration(boolean, boolean, int)
073         */
074        public SpelParserConfiguration(boolean autoGrowNullReferences, boolean autoGrowCollections) {
075                this(null, null, autoGrowNullReferences, autoGrowCollections, Integer.MAX_VALUE);
076        }
077
078        /**
079         * Create a new {@code SpelParserConfiguration} instance.
080         * @param autoGrowNullReferences if null references should automatically grow
081         * @param autoGrowCollections if collections should automatically grow
082         * @param maximumAutoGrowSize the maximum size that the collection can auto grow
083         */
084        public SpelParserConfiguration(boolean autoGrowNullReferences, boolean autoGrowCollections, int maximumAutoGrowSize) {
085                this(null, null, autoGrowNullReferences, autoGrowCollections, maximumAutoGrowSize);
086        }
087
088        /**
089         * Create a new {@code SpelParserConfiguration} instance.
090         * @param compilerMode the compiler mode that parsers using this configuration object should use
091         * @param compilerClassLoader the ClassLoader to use as the basis for expression compilation
092         * @param autoGrowNullReferences if null references should automatically grow
093         * @param autoGrowCollections if collections should automatically grow
094         * @param maximumAutoGrowSize the maximum size that the collection can auto grow
095         */
096        public SpelParserConfiguration(SpelCompilerMode compilerMode, ClassLoader compilerClassLoader,
097                        boolean autoGrowNullReferences, boolean autoGrowCollections, int maximumAutoGrowSize) {
098
099                this.compilerMode = (compilerMode != null ? compilerMode : defaultCompilerMode);
100                this.compilerClassLoader = compilerClassLoader;
101                this.autoGrowNullReferences = autoGrowNullReferences;
102                this.autoGrowCollections = autoGrowCollections;
103                this.maximumAutoGrowSize = maximumAutoGrowSize;
104        }
105
106
107        /**
108         * Return the configuration mode for parsers using this configuration object.
109         */
110        public SpelCompilerMode getCompilerMode() {
111                return this.compilerMode;
112        }
113
114        /**
115         * Return the ClassLoader to use as the basis for expression compilation.
116         */
117        public ClassLoader getCompilerClassLoader() {
118                return this.compilerClassLoader;
119        }
120
121        /**
122         * Return {@code true} if {@code null} references should be automatically grown.
123         */
124        public boolean isAutoGrowNullReferences() {
125                return this.autoGrowNullReferences;
126        }
127
128        /**
129         * Return {@code true} if collections should be automatically grown.
130         */
131        public boolean isAutoGrowCollections() {
132                return this.autoGrowCollections;
133        }
134
135        /**
136         * Return the maximum size that a collection can auto grow.
137         */
138        public int getMaximumAutoGrowSize() {
139                return this.maximumAutoGrowSize;
140        }
141
142}