KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > view > tiles > ComponentControllerSupport


1 /*
2  * Copyright 2002-2005 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.servlet.view.tiles;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21
22 import javax.servlet.ServletContext JavaDoc;
23 import javax.servlet.ServletException JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26
27 import org.apache.struts.tiles.ComponentContext;
28 import org.apache.struts.tiles.ControllerSupport;
29
30 import org.springframework.beans.BeansException;
31 import org.springframework.context.ApplicationContext;
32 import org.springframework.context.support.MessageSourceAccessor;
33 import org.springframework.web.context.WebApplicationContext;
34 import org.springframework.web.servlet.support.RequestContextUtils;
35 import org.springframework.web.util.NestedServletException;
36 import org.springframework.web.util.WebUtils;
37
38 /**
39  * Convenience class for Spring-aware Tiles component controllers.
40  * Provides a reference to the current Spring application context,
41  * e.g. for bean lookup or resource loading.
42  *
43  * <p>Derives from Tiles' ControllerSupport class rather than
44  * implementing Tiles' Controller interface to be compatible with
45  * Struts 1.1 and 1.2. Implements both Struts 1.1's <code>perform</code>
46  * and Struts 1.2's <code>execute</code> method accordingly.
47  *
48  * @author Juergen Hoeller
49  * @author Alef Arendsen
50  * @since 22.08.2003
51  * @see org.springframework.web.context.support.WebApplicationObjectSupport
52  */

53 public abstract class ComponentControllerSupport extends ControllerSupport {
54
55     private WebApplicationContext webApplicationContext;
56
57     private MessageSourceAccessor messageSourceAccessor;
58
59
60     /**
61      * This implementation delegates to <code>execute</code>,
62      * converting non-Servlet/IO Exceptions to ServletException.
63      * <p>This is the only execution method available in Struts 1.1.
64      * @see #execute
65      */

66     public final void perform(
67             ComponentContext componentContext, HttpServletRequest JavaDoc request,
68             HttpServletResponse JavaDoc response, ServletContext JavaDoc servletContext)
69         throws ServletException JavaDoc, IOException JavaDoc {
70
71         try {
72             execute(componentContext, request, response, servletContext);
73         }
74         catch (ServletException JavaDoc ex) {
75             throw ex;
76         }
77         catch (IOException JavaDoc ex) {
78             throw ex;
79         }
80         catch (Throwable JavaDoc ex) {
81             throw new NestedServletException("Execution of component controller failed", ex);
82         }
83     }
84
85     /**
86      * This implementation delegates to <code>doPerform</code>,
87      * lazy-initializing the application context reference if necessary.
88      * <p>This is the preferred execution method in Struts 1.2.
89      * When running with Struts 1.1, it will be called by <code>perform</code>.
90      * @see #perform
91      * @see #doPerform
92      */

93     public final void execute(
94             ComponentContext componentContext, HttpServletRequest JavaDoc request,
95             HttpServletResponse JavaDoc response, ServletContext JavaDoc servletContext)
96         throws Exception JavaDoc {
97
98         synchronized (this) {
99             if (this.webApplicationContext == null) {
100                 this.webApplicationContext = RequestContextUtils.getWebApplicationContext(request, servletContext);
101                 this.messageSourceAccessor = new MessageSourceAccessor(this.webApplicationContext);
102             }
103         }
104         doPerform(componentContext, request, response);
105     }
106
107
108     /**
109      * Subclasses can override this for custom initialization behavior.
110      * Gets called on initialization of the context for this controller.
111      * @throws org.springframework.context.ApplicationContextException in case of initialization errors
112      * @throws org.springframework.beans.BeansException if thrown by application context methods
113      */

114     protected void initApplicationContext() throws BeansException {
115     }
116
117     /**
118      * Return the current Spring ApplicationContext.
119      */

120     protected final ApplicationContext getApplicationContext() {
121         return this.webApplicationContext;
122     }
123
124     /**
125      * Return the current Spring WebApplicationContext.
126      */

127     protected final WebApplicationContext getWebApplicationContext() {
128         return this.webApplicationContext;
129     }
130
131     /**
132      * Return a MessageSourceAccessor for the application context
133      * used by this object, for easy message access.
134      */

135     protected final MessageSourceAccessor getMessageSourceAccessor() {
136         return this.messageSourceAccessor;
137     }
138
139     /**
140      * Return the current ServletContext.
141      */

142     protected final ServletContext JavaDoc getServletContext() {
143         return this.webApplicationContext.getServletContext();
144     }
145
146     /**
147      * Return the temporary directory for the current web application,
148      * as provided by the servlet container.
149      * @return the File representing the temporary directory
150      */

151     protected final File JavaDoc getTempDir() {
152         return WebUtils.getTempDir(getServletContext());
153     }
154
155
156     /**
157      * Perform the preparation for the component, allowing for any Exception to be thrown.
158      * The ServletContext can be retrieved via getServletContext, if necessary.
159      * The Spring WebApplicationContext can be accessed via getWebApplicationContext.
160      * <p>This method will be called both in the Struts 1.1 and Struts 1.2 case,
161      * by <code>perform</code> or <code>execute</code>, respectively.
162      * @param componentContext current Tiles component context
163      * @param request current HTTP request
164      * @param response current HTTP response
165      * @throws Exception in case of errors
166      * @see org.apache.struts.tiles.Controller#perform
167      * @see #getServletContext
168      * @see #getWebApplicationContext
169      * @see #perform
170      * @see #execute
171      */

172     protected abstract void doPerform(
173             ComponentContext componentContext, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
174             throws Exception JavaDoc;
175
176 }
177
Popular Tags