/*
 * Copyright 2002-2005 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.web.context.support;

import java.io.File;

import javax.servlet.ServletContext;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ApplicationObjectSupport;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.util.WebUtils;

/**
 * Convenient superclass for application objects running in a WebApplicationContext.
 * Provides getWebApplicationContext, getServletContext, and getTempDir methods.
 *
 * @author Juergen Hoeller
 * @since 28.08.2003
 */
public abstract class WebApplicationObjectSupport extends ApplicationObjectSupport {

	/**
	 * Overrides the base class behavior to enforce running in an ApplicationContext.
	 * All accessors will throw IllegalStateException if not running in a context.
	 * @see #getApplicationContext
	 * @see #getMessageSourceAccessor
	 * @see #getWebApplicationContext
	 * @see #getServletContext
	 * @see #getTempDir
	 */
	protected boolean isContextRequired() {
		return true;
	}

	/**
	 * Return the current application context as WebApplicationContext.
	 * @throws IllegalStateException if not running in a WebApplicationContext
	 */
	protected final WebApplicationContext getWebApplicationContext() throws IllegalStateException {
		ApplicationContext ctx = getApplicationContext();
		if (!(ctx instanceof WebApplicationContext)) {
			throw new IllegalStateException(
					"WebApplicationObjectSupport instance [" + this +
					"] does not run in a WebApplicationContext but in: " + ctx);
		}
		return (WebApplicationContext) getApplicationContext();
	}

	/**
	 * Return the current ServletContext.
	 * @throws IllegalStateException if not running in a WebApplicationContext
	 */
	protected final ServletContext getServletContext() {
		return getWebApplicationContext().getServletContext();
	}

	/**
	 * Return the temporary directory for the current web application,
	 * as provided by the servlet container.
	 * @return the File representing the temporary directory
	 * @throws IllegalStateException if not running in a WebApplicationContext
	 */
	protected final File getTempDir() {
		return WebUtils.getTempDir(getServletContext());
	}

}

@@ -852,16 +852,76 @@
 upport;%0A
+import org.springframework.web.context.ServletContextAware;%0A
 import o
@@ -1328,18 +1328,202 @@
 tSupport
- %7B
+%0A%09%09implements ServletContextAware %7B%0A%0A%09private ServletContext servletContext;%0A%0A%0A%09public void setServletContext(ServletContext servletContext) %7B%0A%09%09this.servletContext = servletContext;%0A%09%7D%0A
 %0A%0A%09/**%0A%09
@@ -1707,32 +1707,34 @@
 plicationContext
+()
 %0A%09 * @see #getMe
@@ -1752,16 +1752,18 @@
 Accessor
+()
 %0A%09 * @se
@@ -1785,24 +1785,26 @@
 ationContext
+()
 %0A%09 * @see #g
@@ -1815,24 +1815,26 @@
 rvletContext
+()
 %0A%09 * @see #g
@@ -1842,16 +1842,18 @@
 tTempDir
+()
 %0A%09 */%0A%09p
@@ -1976,32 +1976,318 @@
 icationContext.%0A
+%09 * %3Cp%3E%3Cb%3ENOTE:%3C/b%3E Only use this if you actually need to access%0A%09 * WebApplicationContext-specific functionality. Preferably use%0A%09 * %3Ccode%3EgetApplicationContext()%3C/code%3E or %3Ccode%3EgetServletContext()%3C/code%3E%0A%09 * else, to be able to run in non-WebApplicationContext environments as well.%0A
 %09 * @throws Ille
@@ -2338,32 +2338,66 @@
 licationContext%0A
+%09 * @see #getApplicationContext()%0A
 %09 */%0A%09protected 
@@ -2899,35 +2899,32 @@
 running 
+with
 in a 
-WebApplication
+Servlet
 Context%0A
@@ -2980,17 +2980,118 @@
 ntext() 
-%7B
+throws IllegalStateException %7B%0A%09%09if (this.servletContext != null) %7B%0A%09%09%09return this.servletContext;%0A%09%09%7D
 %0A%09%09retur

implement ServletContextAware to work with non-WebApplicationContext too, as long as ServletContextAware is satisfied