001: /*
002: * Copyright 2002-2005 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.servlet.view.tiles;
018:
019: import java.io.File;
020: import java.io.IOException;
021:
022: import javax.servlet.ServletContext;
023: import javax.servlet.ServletException;
024: import javax.servlet.http.HttpServletRequest;
025: import javax.servlet.http.HttpServletResponse;
026:
027: import org.apache.struts.tiles.ComponentContext;
028: import org.apache.struts.tiles.ControllerSupport;
029:
030: import org.springframework.beans.BeansException;
031: import org.springframework.context.ApplicationContext;
032: import org.springframework.context.support.MessageSourceAccessor;
033: import org.springframework.web.context.WebApplicationContext;
034: import org.springframework.web.servlet.support.RequestContextUtils;
035: import org.springframework.web.util.NestedServletException;
036: import org.springframework.web.util.WebUtils;
037:
038: /**
039: * Convenience class for Spring-aware Tiles component controllers.
040: * Provides a reference to the current Spring application context,
041: * e.g. for bean lookup or resource loading.
042: *
043: * <p>Derives from Tiles' ControllerSupport class rather than
044: * implementing Tiles' Controller interface to be compatible with
045: * Struts 1.1 and 1.2. Implements both Struts 1.1's <code>perform</code>
046: * and Struts 1.2's <code>execute</code> method accordingly.
047: *
048: * @author Juergen Hoeller
049: * @author Alef Arendsen
050: * @since 22.08.2003
051: * @see org.springframework.web.context.support.WebApplicationObjectSupport
052: */
053: public abstract class ComponentControllerSupport extends
054: ControllerSupport {
055:
056: private WebApplicationContext webApplicationContext;
057:
058: private MessageSourceAccessor messageSourceAccessor;
059:
060: /**
061: * This implementation delegates to <code>execute</code>,
062: * converting non-Servlet/IO Exceptions to ServletException.
063: * <p>This is the only execution method available in Struts 1.1.
064: * @see #execute
065: */
066: public final void perform(ComponentContext componentContext,
067: HttpServletRequest request, HttpServletResponse response,
068: ServletContext servletContext) throws ServletException,
069: IOException {
070:
071: try {
072: execute(componentContext, request, response, servletContext);
073: } catch (ServletException ex) {
074: throw ex;
075: } catch (IOException ex) {
076: throw ex;
077: } catch (Throwable ex) {
078: throw new NestedServletException(
079: "Execution of component controller failed", ex);
080: }
081: }
082:
083: /**
084: * This implementation delegates to <code>doPerform</code>,
085: * lazy-initializing the application context reference if necessary.
086: * <p>This is the preferred execution method in Struts 1.2.
087: * When running with Struts 1.1, it will be called by <code>perform</code>.
088: * @see #perform
089: * @see #doPerform
090: */
091: public final void execute(ComponentContext componentContext,
092: HttpServletRequest request, HttpServletResponse response,
093: ServletContext servletContext) throws Exception {
094:
095: synchronized (this ) {
096: if (this .webApplicationContext == null) {
097: this .webApplicationContext = RequestContextUtils
098: .getWebApplicationContext(request,
099: servletContext);
100: this .messageSourceAccessor = new MessageSourceAccessor(
101: this .webApplicationContext);
102: }
103: }
104: doPerform(componentContext, request, response);
105: }
106:
107: /**
108: * Subclasses can override this for custom initialization behavior.
109: * Gets called on initialization of the context for this controller.
110: * @throws org.springframework.context.ApplicationContextException in case of initialization errors
111: * @throws org.springframework.beans.BeansException if thrown by application context methods
112: */
113: protected void initApplicationContext() throws BeansException {
114: }
115:
116: /**
117: * Return the current Spring ApplicationContext.
118: */
119: protected final ApplicationContext getApplicationContext() {
120: return this .webApplicationContext;
121: }
122:
123: /**
124: * Return the current Spring WebApplicationContext.
125: */
126: protected final WebApplicationContext getWebApplicationContext() {
127: return this .webApplicationContext;
128: }
129:
130: /**
131: * Return a MessageSourceAccessor for the application context
132: * used by this object, for easy message access.
133: */
134: protected final MessageSourceAccessor getMessageSourceAccessor() {
135: return this .messageSourceAccessor;
136: }
137:
138: /**
139: * Return the current ServletContext.
140: */
141: protected final ServletContext getServletContext() {
142: return this .webApplicationContext.getServletContext();
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: */
150: protected final File getTempDir() {
151: return WebUtils.getTempDir(getServletContext());
152: }
153:
154: /**
155: * Perform the preparation for the component, allowing for any Exception to be thrown.
156: * The ServletContext can be retrieved via getServletContext, if necessary.
157: * The Spring WebApplicationContext can be accessed via getWebApplicationContext.
158: * <p>This method will be called both in the Struts 1.1 and Struts 1.2 case,
159: * by <code>perform</code> or <code>execute</code>, respectively.
160: * @param componentContext current Tiles component context
161: * @param request current HTTP request
162: * @param response current HTTP response
163: * @throws Exception in case of errors
164: * @see org.apache.struts.tiles.Controller#perform
165: * @see #getServletContext
166: * @see #getWebApplicationContext
167: * @see #perform
168: * @see #execute
169: */
170: protected abstract void doPerform(
171: ComponentContext componentContext,
172: HttpServletRequest request, HttpServletResponse response)
173: throws Exception;
174:
175: }
|