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 org.apache.tiles.TilesException; 020import org.apache.tiles.preparer.ViewPreparer; 021import org.apache.tiles.preparer.factory.PreparerFactory; 022import org.apache.tiles.request.Request; 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 * @author Juergen Hoeller 033 * @since 3.2 034 * @see #getPreparer(String, org.springframework.web.context.WebApplicationContext) 035 * @see SimpleSpringPreparerFactory 036 * @see SpringBeanPreparerFactory 037 */ 038public abstract class AbstractSpringPreparerFactory implements PreparerFactory { 039 040 @Override 041 public ViewPreparer getPreparer(String name, Request context) { 042 WebApplicationContext webApplicationContext = (WebApplicationContext) context.getContext("request").get( 043 DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE); 044 if (webApplicationContext == null) { 045 webApplicationContext = (WebApplicationContext) context.getContext("application").get( 046 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); 047 if (webApplicationContext == null) { 048 throw new IllegalStateException("No WebApplicationContext found: no ContextLoaderListener registered?"); 049 } 050 } 051 return getPreparer(name, webApplicationContext); 052 } 053 054 /** 055 * Obtain a preparer instance for the given preparer name, 056 * based on the given Spring WebApplicationContext. 057 * @param name the name of the preparer 058 * @param context the current Spring WebApplicationContext 059 * @return the preparer instance 060 * @throws TilesException in case of failure 061 */ 062 protected abstract ViewPreparer getPreparer(String name, WebApplicationContext context) throws TilesException; 063 064}