1 16 17 package org.springframework.web.servlet; 18 19 import java.io.IOException ; 20 import java.security.Principal ; 21 22 import javax.servlet.ServletException ; 23 import javax.servlet.http.HttpServletRequest ; 24 import javax.servlet.http.HttpServletResponse ; 25 26 import org.springframework.beans.BeanUtils; 27 import org.springframework.beans.BeansException; 28 import org.springframework.context.ApplicationContextException; 29 import org.springframework.context.ConfigurableApplicationContext; 30 import org.springframework.util.StringUtils; 31 import org.springframework.web.context.ConfigurableWebApplicationContext; 32 import org.springframework.web.context.WebApplicationContext; 33 import org.springframework.web.context.support.ServletRequestHandledEvent; 34 import org.springframework.web.context.support.WebApplicationContextUtils; 35 import org.springframework.web.context.support.XmlWebApplicationContext; 36 import org.springframework.web.util.NestedServletException; 37 import org.springframework.web.util.WebUtils; 38 39 88 public abstract class FrameworkServlet extends HttpServletBean { 89 90 95 public static final String DEFAULT_NAMESPACE_SUFFIX = "-servlet"; 96 97 101 public static final Class DEFAULT_CONTEXT_CLASS = XmlWebApplicationContext.class; 102 103 107 public static final String SERVLET_CONTEXT_PREFIX = FrameworkServlet.class.getName() + ".CONTEXT."; 108 109 110 111 private Class contextClass = DEFAULT_CONTEXT_CLASS; 112 113 114 private String namespace; 115 116 117 private String contextConfigLocation; 118 119 120 private boolean publishContext = true; 121 122 123 private boolean publishEvents = true; 124 125 126 private WebApplicationContext webApplicationContext; 127 128 129 138 public void setContextClass(Class contextClass) { 139 this.contextClass = contextClass; 140 } 141 142 145 public Class getContextClass() { 146 return this.contextClass; 147 } 148 149 153 public void setNamespace(String namespace) { 154 this.namespace = namespace; 155 } 156 157 161 public String getNamespace() { 162 return (this.namespace != null) ? this.namespace : getServletName() + DEFAULT_NAMESPACE_SUFFIX; 163 } 164 165 170 public void setContextConfigLocation(String contextConfigLocation) { 171 this.contextConfigLocation = contextConfigLocation; 172 } 173 174 177 public String getContextConfigLocation() { 178 return this.contextConfigLocation; 179 } 180 181 187 public void setPublishContext(boolean publishContext) { 188 this.publishContext = publishContext; 189 } 190 191 194 public boolean isPublishContext() { 195 return this.publishContext; 196 } 197 198 204 public void setPublishEvents(boolean publishEvents) { 205 this.publishEvents = publishEvents; 206 } 207 208 212 public boolean isPublishEvents() { 213 return this.publishEvents; 214 } 215 216 217 221 protected final void initServletBean() throws ServletException , BeansException { 222 getServletContext().log("Initializing Spring FrameworkServlet '" + getServletName() + "'"); 223 if (logger.isInfoEnabled()) { 224 logger.info("FrameworkServlet '" + getServletName() + "': initialization started"); 225 } 226 long startTime = System.currentTimeMillis(); 227 228 try { 229 this.webApplicationContext = initWebApplicationContext(); 230 initFrameworkServlet(); 231 } 232 catch (ServletException ex) { 233 logger.error("Context initialization failed", ex); 234 throw ex; 235 } 236 catch (BeansException ex) { 237 logger.error("Context initialization failed", ex); 238 throw ex; 239 } 240 241 if (logger.isInfoEnabled()) { 242 long elapsedTime = System.currentTimeMillis() - startTime; 243 logger.info("FrameworkServlet '" + getServletName() + "': initialization completed in " + 244 elapsedTime + " ms"); 245 } 246 } 247 248 256 protected WebApplicationContext initWebApplicationContext() throws BeansException { 257 WebApplicationContext parent = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); 258 WebApplicationContext wac = createWebApplicationContext(parent); 259 260 if (isPublishContext()) { 261 String attrName = getServletContextAttributeName(); 263 getServletContext().setAttribute(attrName, wac); 264 if (logger.isDebugEnabled()) { 265 logger.debug("Published WebApplicationContext of servlet '" + getServletName() + 266 "' as ServletContext attribute with name [" + attrName + "]"); 267 } 268 } 269 270 return wac; 271 } 272 273 285 protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) 286 throws BeansException { 287 288 if (logger.isDebugEnabled()) { 289 logger.debug("Servlet with name '" + getServletName() + 290 "' will try to create custom WebApplicationContext context of class '" + 291 getContextClass().getName() + "'" + ", using parent context [" + parent + "]"); 292 } 293 if (!ConfigurableWebApplicationContext.class.isAssignableFrom(getContextClass())) { 294 throw new ApplicationContextException( 295 "Fatal initialization error in servlet with name '" + getServletName() + 296 "': custom WebApplicationContext class [" + getContextClass().getName() + 297 "] is not of type ConfigurableWebApplicationContext"); 298 } 299 300 ConfigurableWebApplicationContext wac = 301 (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(getContextClass()); 302 wac.setParent(parent); 303 wac.setServletContext(getServletContext()); 304 wac.setServletConfig(getServletConfig()); 305 wac.setNamespace(getNamespace()); 306 if (getContextConfigLocation() != null) { 307 wac.setConfigLocations( 308 StringUtils.tokenizeToStringArray( 309 getContextConfigLocation(), ConfigurableWebApplicationContext.CONFIG_LOCATION_DELIMITERS)); 310 } 311 wac.refresh(); 312 return wac; 313 } 314 315 322 public String getServletContextAttributeName() { 323 return SERVLET_CONTEXT_PREFIX + getServletName(); 324 } 325 326 329 public final WebApplicationContext getWebApplicationContext() { 330 return this.webApplicationContext; 331 } 332 333 340 protected void initFrameworkServlet() throws ServletException , BeansException { 341 } 342 343 344 351 protected final void doGet(HttpServletRequest request, HttpServletResponse response) 352 throws ServletException , IOException { 353 354 processRequest(request, response); 355 } 356 357 361 protected final void doPost(HttpServletRequest request, HttpServletResponse response) 362 throws ServletException , IOException { 363 364 processRequest(request, response); 365 } 366 367 371 protected final void doPut(HttpServletRequest request, HttpServletResponse response) 372 throws ServletException , IOException { 373 374 processRequest(request, response); 375 } 376 377 381 protected final void doDelete(HttpServletRequest request, HttpServletResponse response) 382 throws ServletException , IOException { 383 384 processRequest(request, response); 385 } 386 387 392 protected final void processRequest(HttpServletRequest request, HttpServletResponse response) 393 throws ServletException , IOException { 394 395 long startTime = System.currentTimeMillis(); 396 Throwable failureCause = null; 397 398 try { 399 doService(request, response); 400 } 401 catch (ServletException ex) { 402 failureCause = ex; 403 throw ex; 404 } 405 catch (IOException ex) { 406 failureCause = ex; 407 throw ex; 408 } 409 catch (Throwable ex) { 410 failureCause = ex; 411 throw new NestedServletException("Request processing failed", ex); 412 } 413 414 finally { 415 if (failureCause != null) { 416 logger.debug("Could not complete request", failureCause); 417 } 418 else { 419 logger.debug("Successfully completed request"); 420 } 421 if (isPublishEvents()) { 422 long processingTime = System.currentTimeMillis() - startTime; 424 this.webApplicationContext.publishEvent( 425 new ServletRequestHandledEvent(this, 426 request.getRequestURI(), request.getRemoteAddr(), 427 request.getMethod(), getServletConfig().getServletName(), 428 WebUtils.getSessionId(request), getUsernameForRequest(request), 429 processingTime, failureCause)); 430 } 431 } 432 } 433 434 442 protected String getUsernameForRequest(HttpServletRequest request) { 443 Principal userPrincipal = request.getUserPrincipal(); 444 return (userPrincipal != null ? userPrincipal.getName() : null); 445 } 446 447 460 protected abstract void doService(HttpServletRequest request, HttpServletResponse response) 461 throws Exception ; 462 463 464 468 public void destroy() { 469 getServletContext().log( 470 "Destroying Spring FrameworkServlet '" + getServletName() + "'"); 471 if (this.webApplicationContext instanceof ConfigurableApplicationContext) { 472 ((ConfigurableApplicationContext) this.webApplicationContext).close(); 473 } 474 } 475 476 } 477 | Popular Tags |