package org.springframework.web.servlet.view.tiles;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.tiles.ComponentContext;
import org.apache.struts.tiles.Controller;

import org.springframework.web.context.support.WebApplicationObjectSupport;
import org.springframework.web.servlet.support.RequestContextUtils;

/**
 * Convenience class for Spring-aware Tiles component controllers.
 * Provides a reference to the current Spring application context,
 * e.g. for bean lookup or resource loading.
 * @author Juergen Hoeller
 * @author Alef Arendsen
 * @since 22.08.2003
 */
public abstract class ComponentControllerSupport extends WebApplicationObjectSupport implements Controller {

	/**
	 * This implementation delegates to the simplified doPerform,
	 * as the ServletContext is provided by getServletContext anyway.
	 * @see #doPerform
	 * @see #getServletContext
	 */
	public final void perform(ComponentContext componentContext, HttpServletRequest request,
	                          HttpServletResponse response, ServletContext servletContext)
	    throws ServletException, IOException {
		// TODO the following is inserted because ComponentControllerSupport does NOT
		// work when inserting tiles directly with the tiles:insert tag in JSPs, since
		// the org.apache.struts.taglib.tiles.InsertTag (line 869) manually creates
		// and executes controllers. For now, we'll check for the applicationcontext and
		// set it if necessary!
		if (getWebApplicationContext() == null) {
			setApplicationContext(RequestContextUtils.getWebApplicationContext(request));
		}
		doPerform(componentContext, request, response);
	}

	/**
	 * Perform the actual preparation for the component.
	 * The ServletContext can be retrieved via getServletContext, if necessary.
	 * @param componentContext current Tiles component context
	 * @param request current HTTP request
	 * @param response current HTTP response
	 * @throws ServletException in case of execution errors
	 * @throws IOException in case of I/O errors
	 * @see org.apache.struts.tiles.Controller#perform
	 * @see #getServletContext
	 */
	protected abstract void doPerform(ComponentContext componentContext, HttpServletRequest request,
	                                  HttpServletResponse response) throws ServletException, IOException;

}

@@ -899,148 +899,170 @@
  to 
-the simplified doPerform,%0A%09 * as the ServletContext is provided by getServletContext anyway.%0A%09 * @see #doPerform%0A%09 * @see #getServletContext
+doPerform, lazy-initializing the application context%0A%09 * reference if necessary, and converting non-Servlet/IO Exceptions to ServletException.%0A%09 * @see #doPerform
 %0A%09 *
@@ -1364,21 +1364,16 @@
 does NOT
-%0A%09%09//
  work wh
@@ -1374,16 +1374,21 @@
 ork when
+%0A%09%09//
  inserti
@@ -1449,17 +1449,38 @@
 ince
+ the %3Ctiles:insert%3E tag
 %0A%09%09// 
-the
+-
  org
@@ -1527,16 +1527,18 @@
 ine 869)
+ -
  manuall
@@ -1546,21 +1546,16 @@
  creates
-%0A%09%09//
  and exe
@@ -1559,16 +1559,21 @@
 executes
+%0A%09%09//
  control
@@ -1618,16 +1618,17 @@
 lication
+ 
 context 
@@ -1630,21 +1630,16 @@
 text and
-%0A%09%09//
  set it 
@@ -1652,16 +1652,41 @@
 essary!%0A
+%09%09synchronized (this) %7B%0A%09
 %09%09if (ge
@@ -1721,16 +1721,17 @@
 null) %7B%0A
+%09
 %09%09%09setAp
@@ -1805,18 +1805,32 @@
 st));%0A%09%09
+%09
 %7D%0A
+%09%09%7D%0A%09%09try %7B%0A%09
 %09%09doPerf
@@ -1871,16 +1871,194 @@
 ponse);%0A
+%09%09%7D%0A%09%09catch (ServletException ex) %7B%0A%09%09%09throw ex;%0A%09%09%7D%0A%09%09catch (IOException ex) %7B%0A%09%09%09throw ex;%0A%09%09%7D%0A%09%09catch (Exception ex) %7B%0A%09%09%09throw new ServletException(ex.getMessage(), ex);%0A%09%09%7D%0A
 %09%7D%0A%0A%09/**
@@ -2078,15 +2078,8 @@
 the 
-actual 
 prep
@@ -2103,16 +2103,57 @@
 omponent
+, allowing for any Exception to be thrown
 .%0A%09 * Th
@@ -2223,16 +2223,99 @@
 essary.%0A
+%09 * The Spring WebApplicationContext can be accessed via getWebApplicationContext.%0A
 %09 * @par
@@ -2456,31 +2456,24 @@
 %09 * @throws 
-Servlet
 Exception in
@@ -2485,64 +2485,8 @@
  of 
-execution errors%0A%09 * @throws IOException in case of I/O 
 erro
@@ -2568,16 +2568,51 @@
 Context%0A
+%09 * @see #getWebApplicationContext%0A
 %09 */%0A%09pr
@@ -2778,36 +2778,16 @@
  throws 
-ServletException, IO
 Exceptio

allow any Exception to be thrown on doPerform