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;
020import org.springframework.lang.Nullable;
021
022/**
023 * Configuration object for the SpEL expression parser.
024 *
025 * @author Juergen Hoeller
026 * @author Phillip Webb
027 * @author Andy Clement
028 * @since 3.0
029 * @see org.springframework.expression.spel.standard.SpelExpressionParser#SpelExpressionParser(SpelParserConfiguration)
030 */
031public class SpelParserConfiguration {
032
033        private static final SpelCompilerMode defaultCompilerMode;
034
035        static {
036                String compilerMode = SpringProperties.getProperty("spring.expression.compiler.mode");
037                defaultCompilerMode = (compilerMode != null ?
038                                SpelCompilerMode.valueOf(compilerMode.toUpperCase()) : SpelCompilerMode.OFF);
039        }
040
041
042        private final SpelCompilerMode compilerMode;
043
044        @Nullable
045        private final ClassLoader compilerClassLoader;
046
047        private final boolean autoGrowNullReferences;
048
049        private final boolean autoGrowCollections;
050
051        private final int maximumAutoGrowSize;
052
053
054        /**
055         * Create a new {@code SpelParserConfiguration} instance with default settings.
056         */
057        public SpelParserConfiguration() {
058                this(null, null, false, false, Integer.MAX_VALUE);
059        }
060
061        /**
062         * Create a new {@code SpelParserConfiguration} instance.
063         * @param compilerMode the compiler mode for the parser
064         * @param compilerClassLoader the ClassLoader to use as the basis for expression compilation
065         */
066        public SpelParserConfiguration(@Nullable SpelCompilerMode compilerMode, @Nullable ClassLoader compilerClassLoader) {
067                this(compilerMode, compilerClassLoader, false, false, Integer.MAX_VALUE);
068        }
069
070        /**
071         * Create a new {@code SpelParserConfiguration} instance.
072         * @param autoGrowNullReferences if null references should automatically grow
073         * @param autoGrowCollections if collections should automatically grow
074         * @see #SpelParserConfiguration(boolean, boolean, int)
075         */
076        public SpelParserConfiguration(boolean autoGrowNullReferences, boolean autoGrowCollections) {
077                this(null, null, autoGrowNullReferences, autoGrowCollections, Integer.MAX_VALUE);
078        }
079
080        /**
081         * Create a new {@code SpelParserConfiguration} instance.
082         * @param autoGrowNullReferences if null references should automatically grow
083         * @param autoGrowCollections if collections should automatically grow
084         * @param maximumAutoGrowSize the maximum size that the collection can auto grow
085         */
086        public SpelParserConfiguration(boolean autoGrowNullReferences, boolean autoGrowCollections, int maximumAutoGrowSize) {
087                this(null, null, autoGrowNullReferences, autoGrowCollections, maximumAutoGrowSize);
088        }
089
090        /**
091         * Create a new {@code SpelParserConfiguration} instance.
092         * @param compilerMode the compiler mode that parsers using this configuration object should use
093         * @param compilerClassLoader the ClassLoader to use as the basis for expression compilation
094         * @param autoGrowNullReferences if null references should automatically grow
095         * @param autoGrowCollections if collections should automatically grow
096         * @param maximumAutoGrowSize the maximum size that the collection can auto grow
097         */
098        public SpelParserConfiguration(@Nullable SpelCompilerMode compilerMode, @Nullable ClassLoader compilerClassLoader,
099                        boolean autoGrowNullReferences, boolean autoGrowCollections, int maximumAutoGrowSize) {
100
101                this.compilerMode = (compilerMode != null ? compilerMode : defaultCompilerMode);
102                this.compilerClassLoader = compilerClassLoader;
103                this.autoGrowNullReferences = autoGrowNullReferences;
104                this.autoGrowCollections = autoGrowCollections;
105                this.maximumAutoGrowSize = maximumAutoGrowSize;
106        }
107
108
109        /**
110         * Return the configuration mode for parsers using this configuration object.
111         */
112        public SpelCompilerMode getCompilerMode() {
113                return this.compilerMode;
114        }
115
116        /**
117         * Return the ClassLoader to use as the basis for expression compilation.
118         */
119        @Nullable
120        public ClassLoader getCompilerClassLoader() {
121                return this.compilerClassLoader;
122        }
123
124        /**
125         * Return {@code true} if {@code null} references should be automatically grown.
126         */
127        public boolean isAutoGrowNullReferences() {
128                return this.autoGrowNullReferences;
129        }
130
131        /**
132         * Return {@code true} if collections should be automatically grown.
133         */
134        public boolean isAutoGrowCollections() {
135                return this.autoGrowCollections;
136        }
137
138        /**
139         * Return the maximum size that a collection can auto grow.
140         */
141        public int getMaximumAutoGrowSize() {
142                return this.maximumAutoGrowSize;
143        }
144
145}