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