001/*
002 * Copyright 2002-2018 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.web.servlet.view;
018
019import java.util.HashMap;
020import java.util.LinkedList;
021import java.util.List;
022import java.util.Locale;
023import java.util.Map;
024import java.util.MissingResourceException;
025import java.util.ResourceBundle;
026
027import org.springframework.beans.BeansException;
028import org.springframework.beans.factory.BeanFactory;
029import org.springframework.beans.factory.DisposableBean;
030import org.springframework.beans.factory.InitializingBean;
031import org.springframework.beans.factory.NoSuchBeanDefinitionException;
032import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;
033import org.springframework.context.ConfigurableApplicationContext;
034import org.springframework.core.Ordered;
035import org.springframework.lang.Nullable;
036import org.springframework.web.context.support.GenericWebApplicationContext;
037import org.springframework.web.servlet.View;
038
039/**
040 * A {@link org.springframework.web.servlet.ViewResolver} implementation that uses
041 * bean definitions in a {@link ResourceBundle}, specified by the bundle basename.
042 *
043 * <p>The bundle is typically defined in a properties file, located in the classpath.
044 * The default bundle basename is "views".
045 *
046 * <p>This {@code ViewResolver} supports localized view definitions, using the
047 * default support of {@link java.util.PropertyResourceBundle}. For example, the
048 * basename "views" will be resolved as class path resources "views_de_AT.properties",
049 * "views_de.properties", "views.properties" - for a given Locale "de_AT".
050 *
051 * <p>Note: This {@code ViewResolver} implements the {@link Ordered} interface
052 * in order to allow for flexible participation in {@code ViewResolver} chaining.
053 * For example, some special views could be defined via this {@code ViewResolver}
054 * (giving it 0 as "order" value), while all remaining views could be resolved by
055 * a {@link UrlBasedViewResolver}.
056 *
057 * @author Rod Johnson
058 * @author Juergen Hoeller
059 * @see java.util.ResourceBundle#getBundle
060 * @see java.util.PropertyResourceBundle
061 * @see UrlBasedViewResolver
062 */
063public class ResourceBundleViewResolver extends AbstractCachingViewResolver
064                implements Ordered, InitializingBean, DisposableBean {
065
066        /** The default basename if no other basename is supplied. */
067        public static final String DEFAULT_BASENAME = "views";
068
069
070        private String[] basenames = new String[] {DEFAULT_BASENAME};
071
072        private ClassLoader bundleClassLoader = Thread.currentThread().getContextClassLoader();
073
074        @Nullable
075        private String defaultParentView;
076
077        @Nullable
078        private Locale[] localesToInitialize;
079
080        private int order = Ordered.LOWEST_PRECEDENCE;  // default: same as non-Ordered
081
082        /* Locale -> BeanFactory */
083        private final Map<Locale, BeanFactory> localeCache = new HashMap<>();
084
085        /* List of ResourceBundle -> BeanFactory */
086        private final Map<List<ResourceBundle>, ConfigurableApplicationContext> bundleCache = new HashMap<>();
087
088
089        /**
090         * Set a single basename, following {@link java.util.ResourceBundle} conventions.
091         * The default is "views".
092         * <p>{@code ResourceBundle} supports different locale suffixes. For example,
093         * a base name of "views" might map to {@code ResourceBundle} files
094         * "views", "views_en_au" and "views_de".
095         * <p>Note that ResourceBundle names are effectively classpath locations: As a
096         * consequence, the JDK's standard ResourceBundle treats dots as package separators.
097         * This means that "test.theme" is effectively equivalent to "test/theme",
098         * just like it is for programmatic {@code java.util.ResourceBundle} usage.
099         * @see #setBasenames
100         * @see ResourceBundle#getBundle(String)
101         * @see ResourceBundle#getBundle(String, Locale)
102         */
103        public void setBasename(String basename) {
104                setBasenames(basename);
105        }
106
107        /**
108         * Set an array of basenames, each following {@link java.util.ResourceBundle}
109         * conventions. The default is a single basename "views".
110         * <p>{@code ResourceBundle} supports different locale suffixes. For example,
111         * a base name of "views" might map to {@code ResourceBundle} files
112         * "views", "views_en_au" and "views_de".
113         * <p>The associated resource bundles will be checked sequentially when resolving
114         * a message code. Note that message definitions in a <i>previous</i> resource
115         * bundle will override ones in a later bundle, due to the sequential lookup.
116         * <p>Note that ResourceBundle names are effectively classpath locations: As a
117         * consequence, the JDK's standard ResourceBundle treats dots as package separators.
118         * This means that "test.theme" is effectively equivalent to "test/theme",
119         * just like it is for programmatic {@code java.util.ResourceBundle} usage.
120         * @see #setBasename
121         * @see ResourceBundle#getBundle(String)
122         * @see ResourceBundle#getBundle(String, Locale)
123         */
124        public void setBasenames(String... basenames) {
125                this.basenames = basenames;
126        }
127
128        /**
129         * Set the {@link ClassLoader} to load resource bundles with.
130         * Default is the thread context {@code ClassLoader}.
131         */
132        public void setBundleClassLoader(ClassLoader classLoader) {
133                this.bundleClassLoader = classLoader;
134        }
135
136        /**
137         * Return the {@link ClassLoader} to load resource bundles with.
138         * <p>Default is the specified bundle {@code ClassLoader},
139         * usually the thread context {@code ClassLoader}.
140         */
141        protected ClassLoader getBundleClassLoader() {
142                return this.bundleClassLoader;
143        }
144
145        /**
146         * Set the default parent for views defined in the {@code ResourceBundle}.
147         * <p>This avoids repeated "yyy1.(parent)=xxx", "yyy2.(parent)=xxx" definitions
148         * in the bundle, especially if all defined views share the same parent.
149         * <p>The parent will typically define the view class and common attributes.
150         * Concrete views might simply consist of a URL definition then:
151         * a la "yyy1.url=/my.jsp", "yyy2.url=/your.jsp".
152         * <p>View definitions that define their own parent or carry their own
153         * class can still override this. Strictly speaking, the rule that a
154         * default parent setting does not apply to a bean definition that
155         * carries a class is there for backwards compatibility reasons.
156         * It still matches the typical use case.
157         */
158        public void setDefaultParentView(String defaultParentView) {
159                this.defaultParentView = defaultParentView;
160        }
161
162        /**
163         * Specify Locales to initialize eagerly, rather than lazily when actually accessed.
164         * <p>Allows for pre-initialization of common Locales, eagerly checking
165         * the view configuration for those Locales.
166         */
167        public void setLocalesToInitialize(Locale... localesToInitialize) {
168                this.localesToInitialize = localesToInitialize;
169        }
170
171        /**
172         * Specify the order value for this ViewResolver bean.
173         * <p>The default value is {@code Ordered.LOWEST_PRECEDENCE}, meaning non-ordered.
174         * @see org.springframework.core.Ordered#getOrder()
175         */
176        public void setOrder(int order) {
177                this.order = order;
178        }
179
180        @Override
181        public int getOrder() {
182                return this.order;
183        }
184
185        /**
186         * Eagerly initialize Locales if necessary.
187         * @see #setLocalesToInitialize
188         */
189        @Override
190        public void afterPropertiesSet() throws BeansException {
191                if (this.localesToInitialize != null) {
192                        for (Locale locale : this.localesToInitialize) {
193                                initFactory(locale);
194                        }
195                }
196        }
197
198
199        @Override
200        protected View loadView(String viewName, Locale locale) throws Exception {
201                BeanFactory factory = initFactory(locale);
202                try {
203                        return factory.getBean(viewName, View.class);
204                }
205                catch (NoSuchBeanDefinitionException ex) {
206                        // Allow for ViewResolver chaining...
207                        return null;
208                }
209        }
210
211        /**
212         * Initialize the View {@link BeanFactory} from the {@code ResourceBundle},
213         * for the given {@link Locale locale}.
214         * <p>Synchronized because of access by parallel threads.
215         * @param locale the target {@code Locale}
216         * @return the View factory for the given Locale
217         * @throws BeansException in case of initialization errors
218         */
219        protected synchronized BeanFactory initFactory(Locale locale) throws BeansException {
220                // Try to find cached factory for Locale:
221                // Have we already encountered that Locale before?
222                if (isCache()) {
223                        BeanFactory cachedFactory = this.localeCache.get(locale);
224                        if (cachedFactory != null) {
225                                return cachedFactory;
226                        }
227                }
228
229                // Build list of ResourceBundle references for Locale.
230                List<ResourceBundle> bundles = new LinkedList<>();
231                for (String basename : this.basenames) {
232                        ResourceBundle bundle = getBundle(basename, locale);
233                        bundles.add(bundle);
234                }
235
236                // Try to find cached factory for ResourceBundle list:
237                // even if Locale was different, same bundles might have been found.
238                if (isCache()) {
239                        BeanFactory cachedFactory = this.bundleCache.get(bundles);
240                        if (cachedFactory != null) {
241                                this.localeCache.put(locale, cachedFactory);
242                                return cachedFactory;
243                        }
244                }
245
246                // Create child ApplicationContext for views.
247                GenericWebApplicationContext factory = new GenericWebApplicationContext();
248                factory.setParent(getApplicationContext());
249                factory.setServletContext(getServletContext());
250
251                // Load bean definitions from resource bundle.
252                PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(factory);
253                reader.setDefaultParentBean(this.defaultParentView);
254                for (ResourceBundle bundle : bundles) {
255                        reader.registerBeanDefinitions(bundle);
256                }
257
258                factory.refresh();
259
260                // Cache factory for both Locale and ResourceBundle list.
261                if (isCache()) {
262                        this.localeCache.put(locale, factory);
263                        this.bundleCache.put(bundles, factory);
264                }
265
266                return factory;
267        }
268
269        /**
270         * Obtain the resource bundle for the given basename and {@link Locale}.
271         * @param basename the basename to look for
272         * @param locale the {@code Locale} to look for
273         * @return the corresponding {@code ResourceBundle}
274         * @throws MissingResourceException if no matching bundle could be found
275         * @see ResourceBundle#getBundle(String, Locale, ClassLoader)
276         */
277        protected ResourceBundle getBundle(String basename, Locale locale) throws MissingResourceException {
278                return ResourceBundle.getBundle(basename, locale, getBundleClassLoader());
279        }
280
281
282        /**
283         * Close the bundle View factories on context shutdown.
284         */
285        @Override
286        public void destroy() throws BeansException {
287                for (ConfigurableApplicationContext factory : this.bundleCache.values()) {
288                        factory.close();
289                }
290                this.localeCache.clear();
291                this.bundleCache.clear();
292        }
293
294}