1 16 package org.directwebremoting.servlet; 17 18 import java.io.IOException ; 19 20 import javax.servlet.ServletConfig ; 21 import javax.servlet.ServletContext ; 22 import javax.servlet.ServletException ; 23 import javax.servlet.http.HttpServlet ; 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpServletResponse ; 26 27 import org.directwebremoting.Container; 28 import org.directwebremoting.WebContextFactory.WebContextBuilder; 29 import org.directwebremoting.extend.ServerLoadMonitor; 30 import org.directwebremoting.impl.ContainerUtil; 31 import org.directwebremoting.impl.DefaultContainer; 32 import org.directwebremoting.impl.StartupUtil; 33 import org.directwebremoting.util.Logger; 34 import org.directwebremoting.util.ServletLoggingOutput; 35 36 54 public class DwrServlet extends HttpServlet 55 { 56 59 public void init(ServletConfig servletConfig) throws ServletException 60 { 61 super.init(servletConfig); 62 ServletContext servletContext = servletConfig.getServletContext(); 63 64 try 65 { 66 StartupUtil.setupLogging(servletConfig, this); 69 StartupUtil.logStartup(servletConfig); 70 71 container = ContainerUtil.createDefaultContainer(servletConfig); 73 ContainerUtil.setupDefaultContainer(container, servletConfig); 74 75 webContextBuilder = StartupUtil.initWebContext(servletConfig, servletContext, container); 76 StartupUtil.initServerContext(servletConfig, servletContext, container); 77 78 ContainerUtil.prepareForWebContextFilter(servletContext, servletConfig, container, webContextBuilder, this); 79 ContainerUtil.configureContainerFully(container, servletConfig); 80 ContainerUtil.publishContainer(container, servletConfig); 81 } 82 catch (ExceptionInInitializerError ex) 83 { 84 log.fatal("ExceptionInInitializerError. Nested exception:", ex.getException()); 85 throw new ServletException (ex); 86 } 87 catch (Exception ex) 88 { 89 log.fatal("DwrServlet.init() failed", ex); 90 throw new ServletException (ex); 91 } 92 finally 93 { 94 if (webContextBuilder != null) 95 { 96 webContextBuilder.unset(); 97 } 98 99 ServletLoggingOutput.unsetExecutionContext(); 100 } 101 } 102 103 106 public void destroy() 107 { 108 shutdown(); 109 super.destroy(); 110 } 111 112 121 public void shutdown() 122 { 123 ServerLoadMonitor monitor = (ServerLoadMonitor) container.getBean(ServerLoadMonitor.class.getName()); 124 monitor.shutdown(); 125 } 126 127 130 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException , ServletException 131 { 132 doPost(req, resp); 133 } 134 135 138 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException 139 { 140 try 141 { 142 webContextBuilder.set(request, response, getServletConfig(), getServletContext(), container); 143 ServletLoggingOutput.setExecutionContext(this); 144 145 UrlProcessor processor = (UrlProcessor) container.getBean(UrlProcessor.class.getName()); 146 processor.handle(request, response); 147 } 148 finally 149 { 150 webContextBuilder.unset(); 151 ServletLoggingOutput.unsetExecutionContext(); 152 } 153 } 154 155 159 public Container getContainer() 160 { 161 return container; 162 } 163 164 167 private DefaultContainer container; 168 169 172 private WebContextBuilder webContextBuilder; 173 174 177 public static final Logger log = Logger.getLogger(DwrServlet.class); 178 } 179 | Popular Tags |