001: /*
002: * Copyright 2002-2006 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:
017: package org.springframework.web.context.support;
018:
019: import java.io.File;
020:
021: import javax.servlet.ServletContext;
022:
023: import org.springframework.context.ApplicationContext;
024: import org.springframework.context.support.ApplicationObjectSupport;
025: import org.springframework.web.context.ServletContextAware;
026: import org.springframework.web.context.WebApplicationContext;
027: import org.springframework.web.util.WebUtils;
028:
029: /**
030: * Convenient superclass for application objects running in a WebApplicationContext.
031: * Provides getWebApplicationContext, getServletContext, and getTempDir methods.
032: *
033: * @author Juergen Hoeller
034: * @since 28.08.2003
035: */
036: public abstract class WebApplicationObjectSupport extends
037: ApplicationObjectSupport implements ServletContextAware {
038:
039: private ServletContext servletContext;
040:
041: public final void setServletContext(ServletContext servletContext) {
042: this .servletContext = servletContext;
043: }
044:
045: /**
046: * Overrides the base class behavior to enforce running in an ApplicationContext.
047: * All accessors will throw IllegalStateException if not running in a context.
048: * @see #getApplicationContext()
049: * @see #getMessageSourceAccessor()
050: * @see #getWebApplicationContext()
051: * @see #getServletContext()
052: * @see #getTempDir()
053: */
054: protected boolean isContextRequired() {
055: return true;
056: }
057:
058: /**
059: * Return the current application context as WebApplicationContext.
060: * <p><b>NOTE:</b> Only use this if you actually need to access
061: * WebApplicationContext-specific functionality. Preferably use
062: * <code>getApplicationContext()</code> or <code>getServletContext()</code>
063: * else, to be able to run in non-WebApplicationContext environments as well.
064: * @throws IllegalStateException if not running in a WebApplicationContext
065: * @see #getApplicationContext()
066: */
067: protected final WebApplicationContext getWebApplicationContext()
068: throws IllegalStateException {
069: ApplicationContext ctx = getApplicationContext();
070: if (!(ctx instanceof WebApplicationContext)) {
071: throw new IllegalStateException(
072: "WebApplicationObjectSupport instance ["
073: + this
074: + "] does not run in a WebApplicationContext but in: "
075: + ctx);
076: }
077: return (WebApplicationContext) getApplicationContext();
078: }
079:
080: /**
081: * Return the current ServletContext.
082: * @throws IllegalStateException if not running within a ServletContext
083: */
084: protected final ServletContext getServletContext()
085: throws IllegalStateException {
086: if (this .servletContext != null) {
087: return this .servletContext;
088: }
089: return getWebApplicationContext().getServletContext();
090: }
091:
092: /**
093: * Return the temporary directory for the current web application,
094: * as provided by the servlet container.
095: * @return the File representing the temporary directory
096: * @throws IllegalStateException if not running within a ServletContext
097: * @see org.springframework.web.util.WebUtils#getTempDir(javax.servlet.ServletContext)
098: */
099: protected final File getTempDir() throws IllegalStateException {
100: return WebUtils.getTempDir(getServletContext());
101: }
102:
103: }
|