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.tiles3;
018
019import java.util.Map;
020import java.util.concurrent.ConcurrentHashMap;
021
022import org.apache.tiles.TilesException;
023import org.apache.tiles.preparer.PreparerException;
024import org.apache.tiles.preparer.ViewPreparer;
025import org.apache.tiles.preparer.factory.NoSuchPreparerException;
026
027import org.springframework.util.ClassUtils;
028import org.springframework.web.context.WebApplicationContext;
029
030/**
031 * Tiles {@link org.apache.tiles.preparer.PreparerFactory} implementation
032 * that expects preparer class names and builds preparer instances for those,
033 * creating them through the Spring ApplicationContext in order to apply
034 * Spring container callbacks and configured Spring BeanPostProcessors.
035 *
036 * @author Juergen Hoeller
037 * @since 3.2
038 * @see SpringBeanPreparerFactory
039 */
040public class SimpleSpringPreparerFactory extends AbstractSpringPreparerFactory {
041
042        /** Cache of shared ViewPreparer instances: bean name -> bean instance. */
043        private final Map<String, ViewPreparer> sharedPreparers = new ConcurrentHashMap<>(16);
044
045
046        @Override
047        protected ViewPreparer getPreparer(String name, WebApplicationContext context) throws TilesException {
048                // Quick check on the concurrent map first, with minimal locking.
049                ViewPreparer preparer = this.sharedPreparers.get(name);
050                if (preparer == null) {
051                        synchronized (this.sharedPreparers) {
052                                preparer = this.sharedPreparers.get(name);
053                                if (preparer == null) {
054                                        try {
055                                                Class<?> beanClass = ClassUtils.forName(name, context.getClassLoader());
056                                                if (!ViewPreparer.class.isAssignableFrom(beanClass)) {
057                                                        throw new PreparerException(
058                                                                        "Invalid preparer class [" + name + "]: does not implement ViewPreparer interface");
059                                                }
060                                                preparer = (ViewPreparer) context.getAutowireCapableBeanFactory().createBean(beanClass);
061                                                this.sharedPreparers.put(name, preparer);
062                                        }
063                                        catch (ClassNotFoundException ex) {
064                                                throw new NoSuchPreparerException("Preparer class [" + name + "] not found", ex);
065                                        }
066                                }
067                        }
068                }
069                return preparer;
070        }
071
072}