KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > context > support > WebApplicationObjectSupport


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

16
17 package org.springframework.web.context.support;
18
19 import java.io.File JavaDoc;
20
21 import javax.servlet.ServletContext JavaDoc;
22
23 import org.springframework.context.ApplicationContext;
24 import org.springframework.context.support.ApplicationObjectSupport;
25 import org.springframework.web.context.ServletContextAware;
26 import org.springframework.web.context.WebApplicationContext;
27 import org.springframework.web.util.WebUtils;
28
29 /**
30  * Convenient superclass for application objects running in a WebApplicationContext.
31  * Provides getWebApplicationContext, getServletContext, and getTempDir methods.
32  *
33  * @author Juergen Hoeller
34  * @since 28.08.2003
35  */

36 public abstract class WebApplicationObjectSupport extends ApplicationObjectSupport
37         implements ServletContextAware {
38
39     private ServletContext JavaDoc servletContext;
40
41
42     public final void setServletContext(ServletContext JavaDoc servletContext) {
43         this.servletContext = servletContext;
44     }
45
46     /**
47      * Overrides the base class behavior to enforce running in an ApplicationContext.
48      * All accessors will throw IllegalStateException if not running in a context.
49      * @see #getApplicationContext()
50      * @see #getMessageSourceAccessor()
51      * @see #getWebApplicationContext()
52      * @see #getServletContext()
53      * @see #getTempDir()
54      */

55     protected boolean isContextRequired() {
56         return true;
57     }
58
59     /**
60      * Return the current application context as WebApplicationContext.
61      * <p><b>NOTE:</b> Only use this if you actually need to access
62      * WebApplicationContext-specific functionality. Preferably use
63      * <code>getApplicationContext()</code> or <code>getServletContext()</code>
64      * else, to be able to run in non-WebApplicationContext environments as well.
65      * @throws IllegalStateException if not running in a WebApplicationContext
66      * @see #getApplicationContext()
67      */

68     protected final WebApplicationContext getWebApplicationContext() throws IllegalStateException JavaDoc {
69         ApplicationContext ctx = getApplicationContext();
70         if (!(ctx instanceof WebApplicationContext)) {
71             throw new IllegalStateException JavaDoc(
72                     "WebApplicationObjectSupport instance [" + this +
73                     "] does not run in a WebApplicationContext but in: " + ctx);
74         }
75         return (WebApplicationContext) getApplicationContext();
76     }
77
78     /**
79      * Return the current ServletContext.
80      * @throws IllegalStateException if not running within a ServletContext
81      */

82     protected final ServletContext JavaDoc getServletContext() throws IllegalStateException JavaDoc {
83         if (this.servletContext != null) {
84             return this.servletContext;
85         }
86         return getWebApplicationContext().getServletContext();
87     }
88
89     /**
90      * Return the temporary directory for the current web application,
91      * as provided by the servlet container.
92      * @return the File representing the temporary directory
93      * @throws IllegalStateException if not running within a ServletContext
94      * @see org.springframework.web.util.WebUtils#getTempDir(javax.servlet.ServletContext)
95      */

96     protected final File JavaDoc getTempDir() throws IllegalStateException JavaDoc {
97         return WebUtils.getTempDir(getServletContext());
98     }
99
100 }
101
Popular Tags