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