/** * Returns the active {@link ApplicationContext}. Be default this method obtains * the context via {@link ContextLoader#getCurrentWebApplicationContext()}, which * finds the ApplicationContext loaded via {@link ContextLoader} typically in a * Servlet container environment. When not running in a Servlet container and * not using {@link ContextLoader}, this method should be overridden. * @return the {@link ApplicationContext} or {@code null} */ @Nullable protected ApplicationContext getApplicationContext() { return ContextLoader.getCurrentWebApplicationContext(); }
@Before public void setup() { this.servletContext = new MockServletContext(); this.webAppContext = new AnnotationConfigWebApplicationContext(); this.webAppContext.register(Config.class); this.contextLoader = new ContextLoader(this.webAppContext); this.contextLoader.initWebApplicationContext(this.servletContext); this.configurator = new SpringConfigurator(); }
@After public void destroy() { this.contextLoader.closeWebApplicationContext(this.servletContext); }
@Test public void updateTargetUrlWithContextLoader() throws Exception { StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.registerSingleton("requestDataValueProcessor", RequestDataValueProcessorWrapper.class); MockServletContext servletContext = new MockServletContext(); ContextLoader contextLoader = new ContextLoader(wac); contextLoader.initWebApplicationContext(servletContext); try { RequestDataValueProcessor mockProcessor = mock(RequestDataValueProcessor.class); wac.getBean(RequestDataValueProcessorWrapper.class).setRequestDataValueProcessor(mockProcessor); RedirectView rv = new RedirectView(); rv.setUrl("/path"); given(mockProcessor.processUrl(request, "/path")).willReturn("/path?key=123"); rv.render(new ModelMap(), request, response); verify(mockProcessor).processUrl(request, "/path"); } finally { contextLoader.closeWebApplicationContext(servletContext); } }
@Override public void contextInitialized(ServletContextEvent sce) { super.initWebApplicationContext(sce.getServletContext()); }
public synchronized void refresh() { ContextLoader loader = new BootstrappedContextLoader(); // if we have an existing spring context, ensure we close it properly ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); if (ctx != null) { loader.closeWebApplicationContext(getServletContext()); } loader.initWebApplicationContext(getServletContext()); if (getApplicationContext() == null) { setApplicationContext(WebApplicationContextUtils.getWebApplicationContext(getServletContext())); } contextReloaded(); } }
/** * Instantiate the root WebApplicationContext for this loader, either the * default context class or a custom context class if specified. * <p>This implementation expects custom contexts to implement the * {@link ConfigurableWebApplicationContext} interface. * Can be overridden in subclasses. * <p>In addition, {@link #customizeContext} gets called prior to refreshing the * context, allowing subclasses to perform custom modifications to the context. * @param sc current servlet context * @return the root WebApplicationContext * @see ConfigurableWebApplicationContext */ protected WebApplicationContext createWebApplicationContext(ServletContext sc) { Class<?> contextClass = determineContextClass(sc); if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) { throw new ApplicationContextException("Custom context class [" + contextClass.getName() + "] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]"); } return (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass); }
/** * Create the ContextLoader to use. Can be overridden in subclasses. * @return the new ContextLoader */ protected ContextLoader createContextLoader() { return new ContextLoader(); }
protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc) { if (ObjectUtils.identityToString(wac).equals(wac.getId())) { // The application context id is still set to its original default value // -> assign a more useful id based on available information String idParam = sc.getInitParameter(CONTEXT_ID_PARAM); if (idParam != null) { wac.setId(idParam); } else { // Generate default id... wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + ObjectUtils.getDisplayString(sc.getContextPath())); } } wac.setServletContext(sc); String configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM); if (configLocationParam != null) { wac.setConfigLocation(configLocationParam); } // The wac environment's #initPropertySources will be called in any case when the context // is refreshed; do it eagerly here to ensure servlet property sources are in place for // use in any post-processing or initialization that occurs below prior to #refresh ConfigurableEnvironment env = wac.getEnvironment(); if (env instanceof ConfigurableWebEnvironment) { ((ConfigurableWebEnvironment) env).initPropertySources(sc, null); } customizeContext(sc, wac); wac.refresh(); }
/**`` * Do everything to get openmrs going. * * @param servletContext the servletContext from the filterconfig * @see Listener#startOpenmrs(ServletContext) */ private void startOpenmrs(ServletContext servletContext) throws Exception { // start spring // after this point, all errors need to also call: contextLoader.closeWebApplicationContext(event.getServletContext()) // logic copied from org.springframework.web.context.ContextLoaderListener ContextLoader contextLoader = new ContextLoader(); contextLoader.initWebApplicationContext(servletContext); try { WebDaemon.startOpenmrs(servletContext); } catch (Exception exception) { contextLoader.closeWebApplicationContext(servletContext); throw exception; } }
@Override public WebApplicationContext initWebApplicationContext(ServletContext servletContext) throws IllegalStateException, BeansException { this.servletContext = servletContext; init(); return super.initWebApplicationContext(servletContext); }
/** * Instantiate the root WebApplicationContext for this loader, either the * default context class or a custom context class if specified. * <p>This implementation expects custom contexts to implement the * {@link ConfigurableWebApplicationContext} interface. * Can be overridden in subclasses. * <p>In addition, {@link #customizeContext} gets called prior to refreshing the * context, allowing subclasses to perform custom modifications to the context. * @param sc current servlet context * @return the root WebApplicationContext * @see ConfigurableWebApplicationContext */ protected WebApplicationContext createWebApplicationContext(ServletContext sc) { Class<?> contextClass = determineContextClass(sc); if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) { throw new ApplicationContextException("Custom context class [" + contextClass.getName() + "] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]"); } return (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass); }
/** * Create the ContextLoader to use. Can be overridden in subclasses. * @return the new ContextLoader */ protected ContextLoader createContextLoader() { return new ContextLoader(); }
protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc) { if (ObjectUtils.identityToString(wac).equals(wac.getId())) { // The application context id is still set to its original default value // -> assign a more useful id based on available information String idParam = sc.getInitParameter(CONTEXT_ID_PARAM); if (idParam != null) { wac.setId(idParam); } else { // Generate default id... wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + ObjectUtils.getDisplayString(sc.getContextPath())); } } wac.setServletContext(sc); String configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM); if (configLocationParam != null) { wac.setConfigLocation(configLocationParam); } // The wac environment's #initPropertySources will be called in any case when the context // is refreshed; do it eagerly here to ensure servlet property sources are in place for // use in any post-processing or initialization that occurs below prior to #refresh ConfigurableEnvironment env = wac.getEnvironment(); if (env instanceof ConfigurableWebEnvironment) { ((ConfigurableWebEnvironment) env).initPropertySources(sc, null); } customizeContext(sc, wac); wac.refresh(); }
/** * Retrieve the Spring {@link WebApplicationContext} to use. * The default implementation returns the current {@link WebApplicationContext} * as registered for the thread context class loader. * @return the current WebApplicationContext (never {@code null}) * @see ContextLoader#getCurrentWebApplicationContext() */ protected WebApplicationContext getWebApplicationContext() { WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext(); if (wac == null) { throw new IllegalStateException("No WebApplicationContext registered for current thread - " + "consider overriding SpringWebConstraintValidatorFactory.getWebApplicationContext()"); } return wac; }
public void registerSubContext(String webAppKey) { // get the sub contexts - servlet context ServletContext ctx = servletContext.getContext(webAppKey); if (ctx == null) { ctx = servletContext; } ContextLoader loader = new ContextLoader(); ConfigurableWebApplicationContext appCtx = (ConfigurableWebApplicationContext) loader.initWebApplicationContext(ctx); appCtx.setParent(applicationContext); appCtx.refresh(); ctx.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, appCtx); ConfigurableBeanFactory appFactory = appCtx.getBeanFactory(); logger.debug("About to grab Webcontext bean for {}", webAppKey); Context webContext = (Context) appCtx.getBean("web.context"); webContext.setCoreBeanFactory(parentFactory); webContext.setClientRegistry(clientRegistry); webContext.setServiceInvoker(globalInvoker); webContext.setScopeResolver(globalResolver); webContext.setMappingStrategy(globalStrategy); WebScope scope = (WebScope) appFactory.getBean("web.scope"); scope.setServer(server); scope.setParent(global); scope.register(); scope.start(); // register the context so we dont try to reinitialize it registeredContexts.add(ctx); }
ContextLoader contextLoader = new ContextLoader(); contextLoader.initWebApplicationContext(filterConfig.getServletContext()); Context.shutdown(); WebModuleUtil.shutdownModules(filterConfig.getServletContext()); contextLoader.closeWebApplicationContext(filterConfig.getServletContext()); return; Context.shutdown(); WebModuleUtil.shutdownModules(filterConfig.getServletContext()); contextLoader.closeWebApplicationContext(filterConfig.getServletContext()); reportError(ErrorMessageConstants.ERROR_COMPLETE_STARTUP, DEFAULT_PAGE, e.getMessage()); log.warn("Unable to complete the startup.", e);
@Override public WebApplicationContext initWebApplicationContext(ServletContext servletContext) throws IllegalStateException, BeansException { this.servletContext = servletContext; init(); return super.initWebApplicationContext(servletContext); }
@Override protected Class determineContextClass(ServletContext servletContext) throws ApplicationContextException { String contextClassName = servletContext.getInitParameter(CONTEXT_CLASS_PARAM); if (InternalUtils.isNonBlank(contextClassName)) { Class result = super.determineContextClass(servletContext); if (!TapestryApplicationContext.class.isAssignableFrom(result)) throw new IllegalArgumentException(String.format( "When using the Tapestry/Spring integration library, you must specifiy a context class that extends from %s. Class %s does not. Update the '%s' servlet context init parameter.", TapestryApplicationContext.class.getName(), result.getName(), CONTEXT_CLASS_PARAM)); return result; } return TapestryApplicationContext.class; } }
@Override public void closeWebApplicationContext(ServletContext servletContext) { super.closeWebApplicationContext(servletContext); }