01: /*
02: * Created on 01-Feb-2006
03: */
04: package uk.org.ponder.rsf.servlet;
05:
06: import javax.servlet.ServletConfig;
07: import javax.servlet.ServletContext;
08: import javax.servlet.http.HttpServlet;
09: import javax.servlet.http.HttpServletRequest;
10: import javax.servlet.http.HttpServletResponse;
11:
12: import org.springframework.web.context.ContextLoader;
13: import org.springframework.web.context.WebApplicationContext;
14:
15: import uk.org.ponder.rsac.RSACBeanLocator;
16: import uk.org.ponder.rsac.servlet.RSACUtils;
17: import uk.org.ponder.util.Logger;
18:
19: /**
20: * An alternative "all-in-one" main servlet that also performs Spring context
21: * loading, for environments where a listener is "too early". Use this servlet
22: * if you want to keep web.xml free of clutter (no need for Spring
23: * ContextLoaderListener OR RSAC filter).
24: *
25: * <p>
26: * The main servlet for the RSF system. Hands off immediately to the
27: * RootHandlerBean for all logic.
28: *
29: * @author Antranig Basman (antranig@caret.cam.ac.uk)
30: *
31: */
32: public class ReasonableSpringServlet extends HttpServlet {
33: private ContextLoader contextLoader;
34: private RSACBeanLocator rsacbeanlocator;
35:
36: private ServletContext sc;
37:
38: public void init(ServletConfig config) {
39: sc = config.getServletContext();
40: Logger.log
41: .warn("ReasonableSpringServlet starting up for context "
42: + sc.getRealPath(""));
43: contextLoader = new ContextLoader();
44: WebApplicationContext wac = contextLoader
45: .initWebApplicationContext(sc);
46: rsacbeanlocator = (RSACBeanLocator) wac
47: .getBean(RSACBeanLocator.RSAC_BEAN_LOCATOR_NAME);
48: }
49:
50: public void destroy() {
51: this .contextLoader.closeWebApplicationContext(sc);
52: }
53:
54: protected void service(HttpServletRequest request,
55: HttpServletResponse response) {
56: RSACUtils.startServletRequest(request, response,
57: rsacbeanlocator, RSACUtils.HTTP_SERVLET_FACTORY);
58: try {
59: rsacbeanlocator.getBeanLocator().locateBean(
60: "rootHandlerBean");
61: } catch (Throwable t) {
62: // Catch and log this here because Tomcat's stack rendering is
63: // non-standard and crummy.
64: Logger.log.error("Error servicing RSAC request: ", t);
65: if (t instanceof Error) {
66: throw ((Error) t);
67: }
68: } finally {
69: rsacbeanlocator.endRequest();
70: }
71: }
72: }
|