001/* 002 * Copyright 2002-2015 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.context.support; 018 019import java.io.File; 020import javax.servlet.ServletContext; 021 022import org.springframework.context.ApplicationContext; 023import org.springframework.context.support.ApplicationObjectSupport; 024import org.springframework.web.context.ServletContextAware; 025import org.springframework.web.context.WebApplicationContext; 026import org.springframework.web.util.WebUtils; 027 028/** 029 * Convenient superclass for application objects running in a {@link WebApplicationContext}. 030 * Provides {@code getWebApplicationContext()}, {@code getServletContext()}, and 031 * {@code getTempDir()} accessors. 032 * 033 * <p>Note: It is generally recommended to use individual callback interfaces for the actual 034 * callbacks needed. This broad base class is primarily intended for use within the framework, 035 * in case of {@link ServletContext} access etc typically being needed. 036 * 037 * @author Juergen Hoeller 038 * @since 28.08.2003 039 * @see SpringBeanAutowiringSupport 040 */ 041public abstract class WebApplicationObjectSupport extends ApplicationObjectSupport implements ServletContextAware { 042 043 private ServletContext servletContext; 044 045 046 @Override 047 public final void setServletContext(ServletContext servletContext) { 048 if (servletContext != this.servletContext) { 049 this.servletContext = servletContext; 050 if (servletContext != null) { 051 initServletContext(servletContext); 052 } 053 } 054 } 055 056 /** 057 * Overrides the base class behavior to enforce running in an ApplicationContext. 058 * All accessors will throw IllegalStateException if not running in a context. 059 * @see #getApplicationContext() 060 * @see #getMessageSourceAccessor() 061 * @see #getWebApplicationContext() 062 * @see #getServletContext() 063 * @see #getTempDir() 064 */ 065 @Override 066 protected boolean isContextRequired() { 067 return true; 068 } 069 070 /** 071 * Calls {@link #initServletContext(javax.servlet.ServletContext)} if the 072 * given ApplicationContext is a {@link WebApplicationContext}. 073 */ 074 @Override 075 protected void initApplicationContext(ApplicationContext context) { 076 super.initApplicationContext(context); 077 if (this.servletContext == null && context instanceof WebApplicationContext) { 078 this.servletContext = ((WebApplicationContext) context).getServletContext(); 079 if (this.servletContext != null) { 080 initServletContext(this.servletContext); 081 } 082 } 083 } 084 085 /** 086 * Subclasses may override this for custom initialization based 087 * on the ServletContext that this application object runs in. 088 * <p>The default implementation is empty. Called by 089 * {@link #initApplicationContext(org.springframework.context.ApplicationContext)} 090 * as well as {@link #setServletContext(javax.servlet.ServletContext)}. 091 * @param servletContext the ServletContext that this application object runs in 092 * (never {@code null}) 093 */ 094 protected void initServletContext(ServletContext servletContext) { 095 } 096 097 /** 098 * Return the current application context as WebApplicationContext. 099 * <p><b>NOTE:</b> Only use this if you actually need to access 100 * WebApplicationContext-specific functionality. Preferably use 101 * {@code getApplicationContext()} or {@code getServletContext()} 102 * else, to be able to run in non-WebApplicationContext environments as well. 103 * @throws IllegalStateException if not running in a WebApplicationContext 104 * @see #getApplicationContext() 105 */ 106 protected final WebApplicationContext getWebApplicationContext() throws IllegalStateException { 107 ApplicationContext ctx = getApplicationContext(); 108 if (ctx instanceof WebApplicationContext) { 109 return (WebApplicationContext) getApplicationContext(); 110 } 111 else if (isContextRequired()) { 112 throw new IllegalStateException("WebApplicationObjectSupport instance [" + this + 113 "] does not run in a WebApplicationContext but in: " + ctx); 114 } 115 else { 116 return null; 117 } 118 } 119 120 /** 121 * Return the current ServletContext. 122 * @throws IllegalStateException if not running within a ServletContext 123 */ 124 protected final ServletContext getServletContext() throws IllegalStateException { 125 if (this.servletContext != null) { 126 return this.servletContext; 127 } 128 WebApplicationContext wac = getWebApplicationContext(); 129 if (wac == null) { 130 return null; 131 } 132 ServletContext servletContext = wac.getServletContext(); 133 if (servletContext == null && isContextRequired()) { 134 throw new IllegalStateException("WebApplicationObjectSupport instance [" + this + 135 "] does not run within a ServletContext. Make sure the object is fully configured!"); 136 } 137 return servletContext; 138 } 139 140 /** 141 * Return the temporary directory for the current web application, 142 * as provided by the servlet container. 143 * @return the File representing the temporary directory 144 * @throws IllegalStateException if not running within a ServletContext 145 * @see org.springframework.web.util.WebUtils#getTempDir(javax.servlet.ServletContext) 146 */ 147 protected final File getTempDir() throws IllegalStateException { 148 return WebUtils.getTempDir(getServletContext()); 149 } 150 151}