001/* 002 * Copyright 2012-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 * http://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.boot.web.servlet.context; 018 019import javax.servlet.ServletConfig; 020import javax.servlet.ServletContext; 021 022import org.springframework.util.Assert; 023import org.springframework.web.context.ConfigurableWebApplicationContext; 024import org.springframework.web.context.support.ServletContextAwareProcessor; 025 026/** 027 * Variant of {@link ServletContextAwareProcessor} for use with a 028 * {@link ConfigurableWebApplicationContext}. Can be used when registering the processor 029 * can occur before the {@link ServletContext} or {@link ServletConfig} have been 030 * initialized. 031 * 032 * @author Phillip Webb 033 */ 034public class WebApplicationContextServletContextAwareProcessor 035 extends ServletContextAwareProcessor { 036 037 private final ConfigurableWebApplicationContext webApplicationContext; 038 039 public WebApplicationContextServletContextAwareProcessor( 040 ConfigurableWebApplicationContext webApplicationContext) { 041 Assert.notNull(webApplicationContext, "WebApplicationContext must not be null"); 042 this.webApplicationContext = webApplicationContext; 043 } 044 045 @Override 046 protected ServletContext getServletContext() { 047 ServletContext servletContext = this.webApplicationContext.getServletContext(); 048 return (servletContext != null) ? servletContext : super.getServletContext(); 049 } 050 051 @Override 052 protected ServletConfig getServletConfig() { 053 ServletConfig servletConfig = this.webApplicationContext.getServletConfig(); 054 return (servletConfig != null) ? servletConfig : super.getServletConfig(); 055 } 056 057}