1 16 17 package org.springframework.web.struts; 18 19 import javax.servlet.ServletContext ; 20 import javax.servlet.ServletException ; 21 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 import org.apache.struts.action.ActionServlet; 25 import org.apache.struts.action.PlugIn; 26 import org.apache.struts.config.ModuleConfig; 27 28 import org.springframework.beans.BeanUtils; 29 import org.springframework.beans.BeansException; 30 import org.springframework.beans.factory.config.BeanFactoryPostProcessor; 31 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; 32 import org.springframework.context.ApplicationContextException; 33 import org.springframework.context.ConfigurableApplicationContext; 34 import org.springframework.util.ClassUtils; 35 import org.springframework.util.StringUtils; 36 import org.springframework.web.context.ConfigurableWebApplicationContext; 37 import org.springframework.web.context.WebApplicationContext; 38 import org.springframework.web.context.support.WebApplicationContextUtils; 39 import org.springframework.web.context.support.XmlWebApplicationContext; 40 41 103 public class ContextLoaderPlugIn implements PlugIn { 104 105 110 public static final String DEFAULT_NAMESPACE_SUFFIX = "-servlet"; 111 112 116 public static final Class DEFAULT_CONTEXT_CLASS = XmlWebApplicationContext.class; 117 118 122 public static final String SERVLET_CONTEXT_PREFIX = ContextLoaderPlugIn.class.getName() + ".CONTEXT."; 123 124 125 protected final Log logger = LogFactory.getLog(getClass()); 126 127 128 private Class contextClass = DEFAULT_CONTEXT_CLASS; 129 130 131 private String namespace; 132 133 134 private String contextConfigLocation; 135 136 137 private ActionServlet actionServlet; 138 139 140 private ModuleConfig moduleConfig; 141 142 143 private WebApplicationContext webApplicationContext; 144 145 146 152 public void setContextClassName(String contextClassName) throws ClassNotFoundException { 153 this.contextClass = ClassUtils.forName(contextClassName); 154 } 155 156 162 public void setContextClass(Class contextClass) { 163 this.contextClass = contextClass; 164 } 165 166 169 public Class getContextClass() { 170 return contextClass; 171 } 172 173 177 public void setNamespace(String namespace) { 178 this.namespace = namespace; 179 } 180 181 185 public String getNamespace() { 186 if (this.namespace != null) { 187 return this.namespace; 188 } 189 if (this.actionServlet != null) { 190 return this.actionServlet.getServletName() + DEFAULT_NAMESPACE_SUFFIX; 191 } 192 return null; 193 } 194 195 200 public void setContextConfigLocation(String contextConfigLocation) { 201 this.contextConfigLocation = contextConfigLocation; 202 } 203 204 207 public String getContextConfigLocation() { 208 return contextConfigLocation; 209 } 210 211 212 215 public final void init(ActionServlet actionServlet, ModuleConfig moduleConfig) throws ServletException { 216 long startTime = System.currentTimeMillis(); 217 if (logger.isInfoEnabled()) { 218 logger.info("ContextLoaderPlugIn for Struts ActionServlet '" + actionServlet.getServletName() + 219 ", module '" + moduleConfig.getPrefix() + "': initialization started"); 220 } 221 222 this.actionServlet = actionServlet; 223 this.moduleConfig = moduleConfig; 224 try { 225 this.webApplicationContext = initWebApplicationContext(); 226 onInit(); 227 } 228 catch (RuntimeException ex) { 229 logger.error("Context initialization failed", ex); 230 throw ex; 231 } 232 233 if (logger.isInfoEnabled()) { 234 long elapsedTime = System.currentTimeMillis() - startTime; 235 logger.info("ContextLoaderPlugIn for Struts ActionServlet '" + actionServlet.getServletName() + 236 "', module '" + moduleConfig.getPrefix() + "': initialization completed in " + elapsedTime + " ms"); 237 } 238 } 239 240 243 public final ActionServlet getActionServlet() { 244 return actionServlet; 245 } 246 247 250 public final String getServletName() { 251 return this.actionServlet.getServletName(); 252 } 253 254 257 public final ServletContext getServletContext() { 258 return this.actionServlet.getServletContext(); 259 } 260 261 264 public final ModuleConfig getModuleConfig() { 265 return moduleConfig; 266 } 267 268 272 public final String getModulePrefix() { 273 return this.moduleConfig.getPrefix(); 274 } 275 276 291 protected WebApplicationContext initWebApplicationContext() throws BeansException, IllegalStateException { 292 getServletContext().log("Initializing WebApplicationContext for Struts ActionServlet '" + 293 getServletName() + "', module '" + getModulePrefix() + "'"); 294 WebApplicationContext parent = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); 295 296 WebApplicationContext wac = createWebApplicationContext(parent); 297 if (logger.isInfoEnabled()) { 298 logger.info("Using context class '" + wac.getClass().getName() + "' for servlet '" + getServletName() + "'"); 299 } 300 301 String attrName = getServletContextAttributeName(); 303 getServletContext().setAttribute(attrName, wac); 304 if (logger.isDebugEnabled()) { 305 logger.debug("Published WebApplicationContext of Struts ActionServlet '" + getServletName() + 306 "', module '" + getModulePrefix() + "' as ServletContext attribute with name [" + attrName + "]"); 307 } 308 309 return wac; 310 } 311 312 321 protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) 322 throws BeansException { 323 324 if (logger.isDebugEnabled()) { 325 logger.debug("ContextLoaderPlugIn for Struts ActionServlet '" + getServletName() + 326 "', module '" + getModulePrefix() + "' will try to create custom WebApplicationContext " + 327 "context of class '" + getContextClass().getName() + "', using parent context [" + parent + "]"); 328 } 329 if (!ConfigurableWebApplicationContext.class.isAssignableFrom(getContextClass())) { 330 throw new ApplicationContextException( 331 "Fatal initialization error in ContextLoaderPlugIn for Struts ActionServlet '" + getServletName() + 332 "', module '" + getModulePrefix() + "': custom WebApplicationContext class [" + 333 getContextClass().getName() + "] is not of type ConfigurableWebApplicationContext"); 334 } 335 336 ConfigurableWebApplicationContext wac = 337 (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(getContextClass()); 338 wac.setParent(parent); 339 wac.setServletContext(getServletContext()); 340 wac.setNamespace(getNamespace()); 341 if (getContextConfigLocation() != null) { 342 wac.setConfigLocations( 343 StringUtils.tokenizeToStringArray( 344 getContextConfigLocation(), ConfigurableWebApplicationContext.CONFIG_LOCATION_DELIMITERS)); 345 } 346 wac.addBeanFactoryPostProcessor( 347 new BeanFactoryPostProcessor() { 348 public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { 349 beanFactory.addBeanPostProcessor(new ActionServletAwareProcessor(getActionServlet())); 350 beanFactory.ignoreDependencyType(ActionServlet.class); 351 } 352 } 353 ); 354 355 wac.refresh(); 356 return wac; 357 } 358 359 365 public String getServletContextAttributeName() { 366 return SERVLET_CONTEXT_PREFIX + getModulePrefix(); 367 } 368 369 372 public final WebApplicationContext getWebApplicationContext() { 373 return webApplicationContext; 374 } 375 376 380 protected void onInit() throws ServletException { 381 } 382 383 384 388 public void destroy() { 389 getServletContext().log("Closing WebApplicationContext of Struts ActionServlet '" + 390 getServletName() + "', module '" + getModulePrefix() + "'"); 391 if (getWebApplicationContext() instanceof ConfigurableApplicationContext) { 392 ((ConfigurableApplicationContext) getWebApplicationContext()).close(); 393 } 394 } 395 396 } 397 | Popular Tags |