1. /*
  2. * Copyright 2002-2004 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. package org.springframework.web.servlet.view.tiles;
  17. import java.io.File;
  18. import java.io.IOException;
  19. import javax.servlet.ServletContext;
  20. import javax.servlet.ServletException;
  21. import javax.servlet.http.HttpServletRequest;
  22. import javax.servlet.http.HttpServletResponse;
  23. import org.apache.struts.tiles.ComponentContext;
  24. import org.apache.struts.tiles.ControllerSupport;
  25. import org.springframework.beans.BeansException;
  26. import org.springframework.context.ApplicationContext;
  27. import org.springframework.context.support.MessageSourceAccessor;
  28. import org.springframework.web.context.WebApplicationContext;
  29. import org.springframework.web.servlet.support.RequestContextUtils;
  30. import org.springframework.web.util.WebUtils;
  31. /**
  32. * Convenience class for Spring-aware Tiles component controllers.
  33. * Provides a reference to the current Spring application context,
  34. * e.g. for bean lookup or resource loading.
  35. *
  36. * <p>Derives from Tiles' ControllerSupport class rather than
  37. * implementing Tiles' Controller interface to be compatible with
  38. * Struts 1.1 and 1.2. Implements both Struts 1.1's <code>perform</code>
  39. * and Struts 1.2's <code>execute</code> method accordingly.
  40. *
  41. * @author Juergen Hoeller
  42. * @author Alef Arendsen
  43. * @since 22.08.2003
  44. * @see org.springframework.web.context.support.WebApplicationObjectSupport
  45. */
  46. public abstract class ComponentControllerSupport extends ControllerSupport {
  47. private WebApplicationContext webApplicationContext;
  48. private MessageSourceAccessor messageSourceAccessor;
  49. /**
  50. * This implementation delegates to <code>execute</code>,
  51. * converting non-Servlet/IO Exceptions to ServletException.
  52. * <p>This is the only execution method available in Struts 1.1.
  53. * @see #execute
  54. */
  55. public final void perform(ComponentContext componentContext, HttpServletRequest request,
  56. HttpServletResponse response, ServletContext servletContext)
  57. throws ServletException, IOException {
  58. try {
  59. execute(componentContext, request, response, servletContext);
  60. }
  61. catch (ServletException ex) {
  62. throw ex;
  63. }
  64. catch (IOException ex) {
  65. throw ex;
  66. }
  67. catch (Exception ex) {
  68. throw new ServletException(ex.getMessage(), ex);
  69. }
  70. }
  71. /**
  72. * This implementation delegates to <code>doPerform</code>,
  73. * lazy-initializing the application context reference if necessary.
  74. * <p>This is the preferred execution method in Struts 1.2.
  75. * When running with Struts 1.1, it will be called by <code>perform</code>.
  76. * @see #perform
  77. * @see #doPerform
  78. */
  79. public final void execute(ComponentContext componentContext, HttpServletRequest request,
  80. HttpServletResponse response, ServletContext servletContext)
  81. throws Exception {
  82. synchronized (this) {
  83. if (this.webApplicationContext == null) {
  84. this.webApplicationContext = RequestContextUtils.getWebApplicationContext(request, servletContext);
  85. this.messageSourceAccessor = new MessageSourceAccessor(this.webApplicationContext);
  86. }
  87. }
  88. doPerform(componentContext, request, response);
  89. }
  90. /**
  91. * Subclasses can override this for custom initialization behavior.
  92. * Gets called on initialization of the context for this controller.
  93. * @throws org.springframework.context.ApplicationContextException in case of initialization errors
  94. * @throws org.springframework.beans.BeansException if thrown by application context methods
  95. */
  96. protected void initApplicationContext() throws BeansException {
  97. }
  98. /**
  99. * Return the current Spring ApplicationContext.
  100. */
  101. protected final ApplicationContext getApplicationContext() {
  102. return this.webApplicationContext;
  103. }
  104. /**
  105. * Return the current Spring WebApplicationContext.
  106. */
  107. protected final WebApplicationContext getWebApplicationContext() {
  108. return this.webApplicationContext;
  109. }
  110. /**
  111. * Return a MessageSourceAccessor for the application context
  112. * used by this object, for easy message access.
  113. */
  114. protected final MessageSourceAccessor getMessageSourceAccessor() {
  115. return this.messageSourceAccessor;
  116. }
  117. /**
  118. * Return the current ServletContext.
  119. */
  120. protected final ServletContext getServletContext() {
  121. return this.webApplicationContext.getServletContext();
  122. }
  123. /**
  124. * Return the temporary directory for the current web application,
  125. * as provided by the servlet container.
  126. * @return the File representing the temporary directory
  127. */
  128. protected final File getTempDir() {
  129. return WebUtils.getTempDir(getServletContext());
  130. }
  131. /**
  132. * Perform the preparation for the component, allowing for any Exception to be thrown.
  133. * The ServletContext can be retrieved via getServletContext, if necessary.
  134. * The Spring WebApplicationContext can be accessed via getWebApplicationContext.
  135. * <p>This method will be called both in the Struts 1.1 and Struts 1.2 case,
  136. * by <code>perform</code> respectively <code>execute</code>.
  137. * @param componentContext current Tiles component context
  138. * @param request current HTTP request
  139. * @param response current HTTP response
  140. * @throws Exception in case of errors
  141. * @see org.apache.struts.tiles.Controller#perform
  142. * @see #getServletContext
  143. * @see #getWebApplicationContext
  144. * @see #perform
  145. * @see #execute
  146. */
  147. protected abstract void doPerform(ComponentContext componentContext, HttpServletRequest request,
  148. HttpServletResponse response) throws Exception;
  149. }