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.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.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 * @author Juergen Hoeller
036 * @since 3.2
037 * @see SpringBeanPreparerFactory
038 */
039public class SimpleSpringPreparerFactory extends AbstractSpringPreparerFactory {
040
041        /** Cache of shared ViewPreparer instances: bean name -> bean instance */
042        private final Map<String, ViewPreparer> sharedPreparers = new ConcurrentHashMap<String, ViewPreparer>(16);
043
044
045        @Override
046        protected ViewPreparer getPreparer(String name, WebApplicationContext context) throws TilesException {
047                // Quick check on the concurrent map first, with minimal locking.
048                ViewPreparer preparer = this.sharedPreparers.get(name);
049                if (preparer == null) {
050                        synchronized (this.sharedPreparers) {
051                                preparer = this.sharedPreparers.get(name);
052                                if (preparer == null) {
053                                        try {
054                                                Class<?> beanClass = context.getClassLoader().loadClass(name);
055                                                if (!ViewPreparer.class.isAssignableFrom(beanClass)) {
056                                                        throw new PreparerException(
057                                                                        "Invalid preparer class [" + name + "]: does not implement ViewPreparer interface");
058                                                }
059                                                preparer = (ViewPreparer) context.getAutowireCapableBeanFactory().createBean(beanClass);
060                                                this.sharedPreparers.put(name, preparer);
061                                        }
062                                        catch (ClassNotFoundException ex) {
063                                                throw new NoSuchPreparerException("Preparer class [" + name + "] not found", ex);
064                                        }
065                                }
066                        }
067                }
068                return preparer;
069        }
070
071}