1 16 17 package org.springframework.web.context; 18 19 import java.io.IOException ; 20 import java.util.Properties ; 21 22 import javax.servlet.ServletContext ; 23 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 27 import org.springframework.beans.BeanUtils; 28 import org.springframework.beans.BeansException; 29 import org.springframework.beans.factory.access.BeanFactoryLocator; 30 import org.springframework.beans.factory.access.BeanFactoryReference; 31 import org.springframework.context.ApplicationContext; 32 import org.springframework.context.ApplicationContextException; 33 import org.springframework.context.access.ContextSingletonBeanFactoryLocator; 34 import org.springframework.core.io.ClassPathResource; 35 import org.springframework.core.io.support.PropertiesLoaderUtils; 36 import org.springframework.util.ClassUtils; 37 import org.springframework.util.StringUtils; 38 import org.springframework.web.context.support.XmlWebApplicationContext; 39 40 75 public class ContextLoader { 76 77 81 public static final String CONTEXT_CLASS_PARAM = "contextClass"; 82 83 89 public static final String CONFIG_LOCATION_PARAM = "contextConfigLocation"; 90 91 103 public static final String LOCATOR_FACTORY_SELECTOR_PARAM = "locatorFactorySelector"; 104 105 114 public static final String LOCATOR_FACTORY_KEY_PARAM = "parentContextKey"; 115 116 120 private static final String DEFAULT_STRATEGIES_PATH = "ContextLoader.properties"; 121 122 123 private static final Properties defaultStrategies; 124 125 static { 126 try { 130 ClassPathResource resource = new ClassPathResource(DEFAULT_STRATEGIES_PATH, ContextLoader.class); 131 defaultStrategies = PropertiesLoaderUtils.loadProperties(resource); 132 } 133 catch (IOException ex) { 134 throw new IllegalStateException ("Could not load 'ContextLoader.properties': " + ex.getMessage()); 135 } 136 } 137 138 139 private final Log logger = LogFactory.getLog(ContextLoader.class); 140 141 144 private WebApplicationContext context; 145 146 150 private BeanFactoryReference parentContextRef; 151 152 153 163 public WebApplicationContext initWebApplicationContext(ServletContext servletContext) 164 throws IllegalStateException , BeansException { 165 166 if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) { 167 throw new IllegalStateException ( 168 "Cannot initialize context because there is already a root application context present - " + 169 "check whether you have multiple ContextLoader* definitions in your web.xml!"); 170 } 171 172 servletContext.log("Initializing Spring root WebApplicationContext"); 173 if (logger.isInfoEnabled()) { 174 logger.info("Root WebApplicationContext: initialization started"); 175 } 176 long startTime = System.currentTimeMillis(); 177 178 try { 179 ApplicationContext parent = loadParentContext(servletContext); 181 182 this.context = createWebApplicationContext(servletContext, parent); 185 servletContext.setAttribute( 186 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context); 187 188 if (logger.isDebugEnabled()) { 189 logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" + 190 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]"); 191 } 192 if (logger.isInfoEnabled()) { 193 long elapsedTime = System.currentTimeMillis() - startTime; 194 logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms"); 195 } 196 197 return this.context; 198 } 199 catch (RuntimeException ex) { 200 logger.error("Context initialization failed", ex); 201 servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex); 202 throw ex; 203 } 204 catch (Error err) { 205 logger.error("Context initialization failed", err); 206 servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err); 207 throw err; 208 } 209 } 210 211 222 protected WebApplicationContext createWebApplicationContext( 223 ServletContext servletContext, ApplicationContext parent) throws BeansException { 224 225 Class contextClass = determineContextClass(servletContext); 226 if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) { 227 throw new ApplicationContextException("Custom context class [" + contextClass.getName() + 228 "] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]"); 229 } 230 231 ConfigurableWebApplicationContext wac = 232 (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass); 233 wac.setParent(parent); 234 wac.setServletContext(servletContext); 235 String configLocation = servletContext.getInitParameter(CONFIG_LOCATION_PARAM); 236 if (configLocation != null) { 237 wac.setConfigLocations(StringUtils.tokenizeToStringArray(configLocation, 238 ConfigurableWebApplicationContext.CONFIG_LOCATION_DELIMITERS)); 239 } 240 241 wac.refresh(); 242 return wac; 243 } 244 245 254 protected Class determineContextClass(ServletContext servletContext) throws ApplicationContextException { 255 String contextClassName = servletContext.getInitParameter(CONTEXT_CLASS_PARAM); 256 if (contextClassName != null) { 257 try { 258 return ClassUtils.forName(contextClassName); 259 } 260 catch (ClassNotFoundException ex) { 261 throw new ApplicationContextException( 262 "Failed to load custom context class [" + contextClassName + "]", ex); 263 } 264 } 265 else { 266 contextClassName = defaultStrategies.getProperty(WebApplicationContext.class.getName()); 267 try { 268 return ClassUtils.forName(contextClassName); 269 } 270 catch (ClassNotFoundException ex) { 271 throw new ApplicationContextException( 272 "Failed to load default context class [" + contextClassName + "]", ex); 273 } 274 } 275 } 276 277 298 protected ApplicationContext loadParentContext(ServletContext servletContext) 299 throws BeansException { 300 301 ApplicationContext parentContext = null; 302 String locatorFactorySelector = servletContext.getInitParameter(LOCATOR_FACTORY_SELECTOR_PARAM); 303 String parentContextKey = servletContext.getInitParameter(LOCATOR_FACTORY_KEY_PARAM); 304 305 if (locatorFactorySelector != null) { 306 BeanFactoryLocator locator = ContextSingletonBeanFactoryLocator.getInstance(locatorFactorySelector); 307 if (logger.isDebugEnabled()) { 308 logger.debug("Getting parent context definition: using parent context key of '" + 309 parentContextKey + "' with BeanFactoryLocator"); 310 } 311 this.parentContextRef = locator.useBeanFactory(parentContextKey); 312 parentContext = (ApplicationContext) this.parentContextRef.getFactory(); 313 } 314 315 return parentContext; 316 } 317 318 319 327 public void closeWebApplicationContext(ServletContext servletContext) { 328 servletContext.log("Closing Spring root WebApplicationContext"); 329 try { 330 if (this.context instanceof ConfigurableWebApplicationContext) { 331 ((ConfigurableWebApplicationContext) this.context).close(); 332 } 333 } 334 finally { 335 if (this.parentContextRef != null) { 336 this.parentContextRef.release(); 337 } 338 } 339 } 340 341 } 342 | Popular Tags |