1 16 package org.directwebremoting.spring; 17 18 import java.io.IOException ; 19 import java.util.ArrayList ; 20 import java.util.List ; 21 22 import javax.servlet.ServletConfig ; 23 import javax.servlet.ServletContext ; 24 import javax.servlet.ServletException ; 25 import javax.servlet.http.HttpServlet ; 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpServletResponse ; 28 29 import org.directwebremoting.WebContextFactory.WebContextBuilder; 30 import org.directwebremoting.impl.ContainerUtil; 31 import org.directwebremoting.impl.StartupUtil; 32 import org.directwebremoting.servlet.UrlProcessor; 33 import org.directwebremoting.util.Logger; 34 import org.springframework.beans.factory.BeanCreationException; 35 import org.springframework.beans.factory.NoSuchBeanDefinitionException; 36 import org.springframework.web.context.WebApplicationContext; 37 import org.springframework.web.context.support.WebApplicationContextUtils; 38 39 56 public class DwrSpringServlet extends HttpServlet  57 { 58 63 public void setConfigurators(List configurators) 64 { 65 this.configurators = configurators; 66 } 67 68 73 public void setIncludeDefaultConfig(boolean includeDefaultConfig) 74 { 75 this.includeDefaultConfig = includeDefaultConfig; 76 } 77 78 81 public void init(ServletConfig servletConfig) throws ServletException  82 { 83 super.init(servletConfig); 84 ServletContext servletContext = servletConfig.getServletContext(); 85 86 try 87 { 88 WebApplicationContext webappContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext); 89 90 container = new SpringContainer(); 91 container.setBeanFactory(webappContext); 92 ContainerUtil.setupDefaults(container, servletConfig); 93 ContainerUtil.setupFromServletConfig(container, servletConfig); 94 95 container.setupFinished(); 96 97 webContextBuilder = StartupUtil.initWebContext(servletConfig, servletContext, container); 98 StartupUtil.initServerContext(servletConfig, servletContext, container); 99 100 ContainerUtil.prepareForWebContextFilter(servletContext, servletConfig, container, webContextBuilder, this); 101 try 103 { 104 configurators.add(webappContext.getBean(DwrNamespaceHandler.DEFAULT_SPRING_CONFIGURATOR_ID)); 105 } 106 catch (NoSuchBeanDefinitionException ex) 107 { 108 throw new ServletException ("No DWR configuration was found in your application context, make sure to define one", ex); 109 } 110 111 if (includeDefaultConfig) 112 { 113 ContainerUtil.configureFromSystemDwrXml(container); 114 } 115 116 ContainerUtil.configureFromInitParams(container, servletConfig); 117 ContainerUtil.configure(container, configurators); 118 119 ContainerUtil.publishContainer(container, servletConfig); 120 } 121 catch (InstantiationException ex) 122 { 123 throw new BeanCreationException("Failed to instansiate", ex); 124 } 125 catch (IllegalAccessException ex) 126 { 127 throw new BeanCreationException("Access error", ex); 128 } 129 catch (Exception ex) 130 { 131 log.fatal("init failed", ex); 132 throw new ServletException (ex); 133 } 134 finally 135 { 136 webContextBuilder.unset(); 137 } 138 } 139 140 143 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException , ServletException  144 { 145 doPost(req, resp); 146 } 147 148 151 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException  152 { 153 try 154 { 155 webContextBuilder.set(request, response, getServletConfig(), getServletContext(), container); 156 157 UrlProcessor processor = (UrlProcessor) container.getBean(UrlProcessor.class.getName()); 158 processor.handle(request, response); 159 } 160 finally 161 { 162 webContextBuilder.unset(); 163 } 164 } 165 166 169 private SpringContainer container; 170 171 174 protected WebContextBuilder webContextBuilder; 175 176 180 private boolean includeDefaultConfig = true; 181 182 185 private List configurators = new ArrayList (); 186 187 190 private static final Logger log = Logger.getLogger(DwrSpringServlet.class); 191 } 192
| Popular Tags
|