Search in sources :

Example 1 with ConfigurableWebApplicationContext

use of org.springframework.web.context.ConfigurableWebApplicationContext in project spring-framework by spring-projects.

the class FrameworkServlet method initWebApplicationContext.

/**
	 * Initialize and publish the WebApplicationContext for this servlet.
	 * <p>Delegates to {@link #createWebApplicationContext} for actual creation
	 * of the context. Can be overridden in subclasses.
	 * @return the WebApplicationContext instance
	 * @see #FrameworkServlet(WebApplicationContext)
	 * @see #setContextClass
	 * @see #setContextConfigLocation
	 */
protected WebApplicationContext initWebApplicationContext() {
    WebApplicationContext rootContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
    WebApplicationContext wac = null;
    if (this.webApplicationContext != null) {
        // A context instance was injected at construction time -> use it
        wac = this.webApplicationContext;
        if (wac instanceof ConfigurableWebApplicationContext) {
            ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) wac;
            if (!cwac.isActive()) {
                // setting the parent context, setting the application context id, etc
                if (cwac.getParent() == null) {
                    // The context instance was injected without an explicit parent -> set
                    // the root application context (if any; may be null) as the parent
                    cwac.setParent(rootContext);
                }
                configureAndRefreshWebApplicationContext(cwac);
            }
        }
    }
    if (wac == null) {
        // No context instance was injected at construction time -> see if one
        // has been registered in the servlet context. If one exists, it is assumed
        // that the parent context (if any) has already been set and that the
        // user has performed any initialization such as setting the context id
        wac = findWebApplicationContext();
    }
    if (wac == null) {
        // No context instance is defined for this servlet -> create a local one
        wac = createWebApplicationContext(rootContext);
    }
    if (!this.refreshEventReceived) {
        // Either the context is not a ConfigurableApplicationContext with refresh
        // support or the context injected at construction time had already been
        // refreshed -> trigger initial onRefresh manually here.
        onRefresh(wac);
    }
    if (this.publishContext) {
        // Publish the context as a servlet context attribute.
        String attrName = getServletContextAttributeName();
        getServletContext().setAttribute(attrName, wac);
        if (this.logger.isDebugEnabled()) {
            this.logger.debug("Published WebApplicationContext of servlet '" + getServletName() + "' as ServletContext attribute with name [" + attrName + "]");
        }
    }
    return wac;
}
Also used : ConfigurableWebApplicationContext(org.springframework.web.context.ConfigurableWebApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) ConfigurableWebApplicationContext(org.springframework.web.context.ConfigurableWebApplicationContext) XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext)

Example 2 with ConfigurableWebApplicationContext

use of org.springframework.web.context.ConfigurableWebApplicationContext in project cuba by cuba-platform.

the class SingleAppRestApiServlet method createWebApplicationContext.

@Override
protected WebApplicationContext createWebApplicationContext(ApplicationContext parent) {
    if (this.logger.isDebugEnabled()) {
        this.logger.debug("Servlet with name '" + getServletName() + "' will try to create custom WebApplicationContext context of class '" + CubaXmlWebApplicationContext.class.getName() + "'" + ", using parent context [" + parent + "]");
    }
    ConfigurableWebApplicationContext wac = new CubaXmlWebApplicationContext() {

        @Override
        protected ResourcePatternResolver getResourcePatternResolver() {
            if (dependencyJars == null || dependencyJars.isEmpty()) {
                throw new RuntimeException("No JARs defined for the 'web' block. " + "Please check that web.dependencies file exists in WEB-INF directory.");
            }
            return new SingleAppResourcePatternResolver(this, dependencyJars);
        }
    };
    wac.setEnvironment(getEnvironment());
    wac.setParent(parent);
    wac.setConfigLocation(getContextConfigLocation());
    configureAndRefreshWebApplicationContext(wac);
    return wac;
}
Also used : ConfigurableWebApplicationContext(org.springframework.web.context.ConfigurableWebApplicationContext) CubaXmlWebApplicationContext(com.haulmont.cuba.core.sys.CubaXmlWebApplicationContext) SingleAppResourcePatternResolver(com.haulmont.cuba.core.sys.SingleAppResourcePatternResolver)

Example 3 with ConfigurableWebApplicationContext

use of org.springframework.web.context.ConfigurableWebApplicationContext in project cuba by cuba-platform.

the class SingleAppIdpServlet method createWebApplicationContext.

@Override
protected WebApplicationContext createWebApplicationContext(ApplicationContext parent) {
    if (this.logger.isDebugEnabled()) {
        this.logger.debug("Servlet with name '" + getServletName() + "' will try to create custom WebApplicationContext context of class '" + CubaXmlWebApplicationContext.class.getName() + "'" + ", using parent context [" + parent + "]");
    }
    ConfigurableWebApplicationContext wac = new CubaXmlWebApplicationContext() {

        @Override
        protected ResourcePatternResolver getResourcePatternResolver() {
            if (dependencyJars == null || dependencyJars.isEmpty()) {
                throw new RuntimeException("No JARs defined for the 'web' block. " + "Please check that web.dependencies file exists in WEB-INF directory.");
            }
            return new SingleAppResourcePatternResolver(this, dependencyJars);
        }
    };
    wac.setEnvironment(getEnvironment());
    wac.setParent(parent);
    wac.setConfigLocation(getContextConfigLocation());
    configureAndRefreshWebApplicationContext(wac);
    return wac;
}
Also used : ConfigurableWebApplicationContext(org.springframework.web.context.ConfigurableWebApplicationContext) CubaXmlWebApplicationContext(com.haulmont.cuba.core.sys.CubaXmlWebApplicationContext) SingleAppResourcePatternResolver(com.haulmont.cuba.core.sys.SingleAppResourcePatternResolver)

Example 4 with ConfigurableWebApplicationContext

use of org.springframework.web.context.ConfigurableWebApplicationContext in project pentaho-platform by pentaho.

the class PentahoWSSpringServlet method getAppContext.

protected ApplicationContext getAppContext() {
    ConfigurableWebApplicationContext wac = new XmlWebApplicationContext() {

        @Override
        protected Resource getResourceByPath(String path) {
            return new FileSystemResource(new File(path));
        }
    };
    wac.setServletContext(getServletContext());
    wac.setServletConfig(getServletConfig());
    wac.setNamespace(getServletName());
    String springFile = PentahoSystem.getApplicationContext().getSolutionPath(// $NON-NLS-1$ //$NON-NLS-2$
    "system" + File.separator + "pentahoServices.spring.xml");
    wac.setConfigLocations(new String[] { springFile });
    wac.refresh();
    return wac;
}
Also used : ConfigurableWebApplicationContext(org.springframework.web.context.ConfigurableWebApplicationContext) XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) FileSystemResource(org.springframework.core.io.FileSystemResource) File(java.io.File)

Example 5 with ConfigurableWebApplicationContext

use of org.springframework.web.context.ConfigurableWebApplicationContext in project pentaho-platform by pentaho.

the class GwtRpcProxyServlet method getAppContext.

protected ApplicationContext getAppContext() {
    WebApplicationContext parent = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
    ConfigurableWebApplicationContext wac = new XmlWebApplicationContext() {

        @Override
        protected Resource getResourceByPath(String path) {
            return new FileSystemResource(new File(path));
        }
    };
    wac.setParent(parent);
    wac.setServletContext(getServletContext());
    wac.setServletConfig(getServletConfig());
    wac.setNamespace(getServletName());
    String springFile = PentahoSystem.getApplicationContext().getSolutionPath(// $NON-NLS-1$ //$NON-NLS-2$
    "system" + File.separator + "pentahoServices.spring.xml");
    wac.setConfigLocations(new String[] { springFile });
    wac.refresh();
    return wac;
}
Also used : ConfigurableWebApplicationContext(org.springframework.web.context.ConfigurableWebApplicationContext) XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) FileSystemResource(org.springframework.core.io.FileSystemResource) File(java.io.File) XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) ConfigurableWebApplicationContext(org.springframework.web.context.ConfigurableWebApplicationContext)

Aggregations

ConfigurableWebApplicationContext (org.springframework.web.context.ConfigurableWebApplicationContext)19 Test (org.junit.jupiter.api.Test)6 XmlWebApplicationContext (org.springframework.web.context.support.XmlWebApplicationContext)6 CubaXmlWebApplicationContext (com.haulmont.cuba.core.sys.CubaXmlWebApplicationContext)3 File (java.io.File)3 List (java.util.List)3 ContextMappings (org.springframework.boot.actuate.web.mappings.MappingsEndpoint.ContextMappings)3 ReactiveWebApplicationContextRunner (org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner)3 WebApplicationContextRunner (org.springframework.boot.test.context.runner.WebApplicationContextRunner)3 FileSystemResource (org.springframework.core.io.FileSystemResource)3 MockServletContext (org.springframework.mock.web.MockServletContext)3 SingleAppResourcePatternResolver (com.haulmont.cuba.core.sys.SingleAppResourcePatternResolver)2 DispatcherServletMappingDescription (org.springframework.boot.actuate.web.mappings.servlet.DispatcherServletMappingDescription)2 FilterRegistrationMappingDescription (org.springframework.boot.actuate.web.mappings.servlet.FilterRegistrationMappingDescription)2 ServletRegistrationMappingDescription (org.springframework.boot.actuate.web.mappings.servlet.ServletRegistrationMappingDescription)2 WebApplicationContext (org.springframework.web.context.WebApplicationContext)2 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)1 PageContext (jakarta.servlet.jsp.PageContext)1 Nonnull (javax.annotation.Nonnull)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1