1 16 package org.directwebremoting.spring; 17 18 import java.util.HashMap ; 19 import java.util.List ; 20 import java.util.Map ; 21 22 import javax.servlet.ServletConfig ; 23 import javax.servlet.ServletContext ; 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpServletResponse ; 26 27 import org.directwebremoting.WebContextFactory.WebContextBuilder; 28 import org.directwebremoting.impl.ContainerMap; 29 import org.directwebremoting.impl.ContainerUtil; 30 import org.directwebremoting.impl.StartupUtil; 31 import org.directwebremoting.servlet.UrlProcessor; 32 import org.directwebremoting.util.FakeServletConfig; 33 import org.directwebremoting.util.Logger; 34 import org.springframework.beans.BeansException; 35 import org.springframework.beans.factory.BeanCreationException; 36 import org.springframework.beans.factory.BeanFactory; 37 import org.springframework.beans.factory.BeanFactoryAware; 38 import org.springframework.beans.factory.BeanNameAware; 39 import org.springframework.beans.factory.InitializingBean; 40 import org.springframework.util.Assert; 41 import org.springframework.web.servlet.ModelAndView; 42 import org.springframework.web.servlet.mvc.AbstractController; 43 44 102 public class DwrController extends AbstractController implements BeanNameAware, InitializingBean, BeanFactoryAware 103 { 104 110 public void setBeanFactory(BeanFactory beanFactory) throws BeansException 111 { 112 container = new SpringContainer(); 113 container.setBeanFactory(beanFactory); 114 } 115 116 122 public void setDebug(boolean debug) 123 { 124 this.debug = debug; 125 } 126 127 132 public void setConfigurators(List configurators) 133 { 134 this.configurators = configurators; 135 } 136 137 143 public void setIncludeDefaultConfig(boolean includeDefaultConfig) 144 { 145 this.includeDefaultConfig = includeDefaultConfig; 146 } 147 148 155 public void afterPropertiesSet() throws Exception  156 { 157 ServletContext servletContext = getServletContext(); 158 159 if (logger.isDebugEnabled()) { 160 logger.debug("afterPropertiesSet() called with servletContext '" + servletContext + "'"); 161 } 162 163 Assert.notNull(servletContext, "The servlet context has not been set on the controller"); 164 Assert.notNull(configurators, "The required 'configurators' property should be set"); 165 166 169 configParams.putAll(new ContainerMap(container, true)); 175 servletConfig = new FakeServletConfig(name, servletContext, configParams); 176 177 try 178 { 179 ContainerUtil.setupDefaults(container, servletConfig); 180 ContainerUtil.setupFromServletConfig(container, servletConfig); 181 container.addParameter("debug", "" + debug); 182 container.setupFinished(); 183 184 webContextBuilder = StartupUtil.initWebContext(servletConfig, servletContext, container); 185 StartupUtil.initServerContext(servletConfig, servletContext, container); 186 187 ContainerUtil.prepareForWebContextFilter(servletContext, servletConfig, container, webContextBuilder, null); 188 189 if (includeDefaultConfig) 191 { 192 ContainerUtil.configureFromSystemDwrXml(container); 193 } 194 195 ContainerUtil.configure(container, configurators); 196 ContainerUtil.publishContainer(container, servletConfig); 197 } 198 catch (InstantiationException ex) 199 { 200 throw new BeanCreationException("Failed to instansiate", ex); 201 } 202 catch (IllegalAccessException ex) 203 { 204 throw new BeanCreationException("Access error", ex); 205 } 206 catch (Exception ex) 207 { 208 log.fatal("init failed", ex); 209 throw ex; 210 } 211 finally 212 { 213 webContextBuilder.unset(); 214 } 215 } 216 217 226 protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception  227 { 228 try 229 { 230 webContextBuilder.set(request, response, servletConfig, getServletContext(), container); 232 233 UrlProcessor processor = (UrlProcessor) container.getBean(UrlProcessor.class.getName()); 234 processor.handle(request, response); 235 } 236 finally 237 { 238 webContextBuilder.unset(); 239 } 240 241 return null; 243 } 244 245 250 public void setBeanName(String name) 251 { 252 this.name = name; 253 } 254 255 260 public void setConfigParams(Map configParams) 261 { 262 Assert.notNull(configParams, "configParams cannot be null"); 263 this.configParams = configParams; 264 } 265 266 267 270 private String name; 271 272 275 private boolean debug = false; 276 277 281 protected WebContextBuilder webContextBuilder; 282 283 286 private SpringContainer container; 287 288 291 private ServletConfig servletConfig; 292 293 297 private boolean includeDefaultConfig = true; 298 299 302 private List configurators; 303 304 308 private Map configParams = new HashMap (); 309 310 313 private static final Logger log = Logger.getLogger(DwrController.class); 314 } 315
| Popular Tags
|