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 org.apache.tiles.TilesException;
020import org.apache.tiles.context.TilesRequestContext;
021import org.apache.tiles.preparer.PreparerFactory;
022import org.apache.tiles.preparer.ViewPreparer;
023
024import org.springframework.web.context.WebApplicationContext;
025import org.springframework.web.servlet.DispatcherServlet;
026
027/**
028 * Abstract implementation of the Tiles {@link org.apache.tiles.preparer.PreparerFactory}
029 * interface, obtaining the current Spring WebApplicationContext and delegating to
030 * {@link #getPreparer(String, org.springframework.web.context.WebApplicationContext)}.
031 *
032 * <p><b>NOTE: Tiles 2 support is deprecated in favor of Tiles 3 and will be removed
033 * as of Spring Framework 5.0.</b>.
034 *
035 * @author Juergen Hoeller
036 * @since 2.5
037 * @see #getPreparer(String, org.springframework.web.context.WebApplicationContext)
038 * @see SimpleSpringPreparerFactory
039 * @see SpringBeanPreparerFactory
040 * @deprecated as of Spring 4.2, in favor of Tiles 3
041 */
042@Deprecated
043public abstract class AbstractSpringPreparerFactory implements PreparerFactory {
044
045        @Override
046        public ViewPreparer getPreparer(String name, TilesRequestContext context) throws TilesException {
047                WebApplicationContext webApplicationContext = (WebApplicationContext) context.getRequestScope().get(
048                                DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE);
049                if (webApplicationContext == null) {
050                        webApplicationContext = (WebApplicationContext) context.getApplicationContext().getApplicationScope().get(
051                                        WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
052                        if (webApplicationContext == null) {
053                                throw new IllegalStateException("No WebApplicationContext found: no ContextLoaderListener registered?");
054                        }
055                }
056                return getPreparer(name, webApplicationContext);
057        }
058
059        /**
060         * Obtain a preparer instance for the given preparer name,
061         * based on the given Spring WebApplicationContext.
062         * @param name the name of the preparer
063         * @param context the current Spring WebApplicationContext
064         * @return the preparer instance
065         * @throws TilesException in case of failure
066         */
067        protected abstract ViewPreparer getPreparer(String name, WebApplicationContext context) throws TilesException;
068
069}