Search in sources :

Example 1 with RestrictedApplicationContext

use of org.craftercms.engine.util.spring.context.RestrictedApplicationContext in project engine by craftercms.

the class SiteContextFactory method getApplicationContext.

protected ConfigurableApplicationContext getApplicationContext(SiteContext siteContext, URLClassLoader classLoader, HierarchicalConfiguration config, String[] applicationContextPaths, ResourceLoader resourceLoader) {
    String siteName = siteContext.getSiteName();
    logger.info("--------------------------------------------------");
    logger.info("<Loading application context for site: " + siteName + ">");
    logger.info("--------------------------------------------------");
    try {
        Resource appContextResource = null;
        for (int i = applicationContextPaths.length - 1; i >= 0; i--) {
            Resource resource = resourceLoader.getResource(applicationContextPaths[i]);
            if (resource.exists()) {
                appContextResource = resource;
                break;
            }
        }
        if (appContextResource != null) {
            GenericApplicationContext appContext;
            if (disableVariableRestrictions) {
                appContext = new GenericApplicationContext(globalApplicationContext);
            } else {
                appContext = new RestrictedApplicationContext(globalApplicationContext, defaultPublicBeans);
            }
            appContext.setClassLoader(classLoader);
            if (!enableExpressions) {
                appContext.addBeanFactoryPostProcessor(factory -> factory.setBeanExpressionResolver(null));
            }
            if (config != null) {
                MutablePropertySources propertySources = appContext.getEnvironment().getPropertySources();
                propertySources.addFirst(new ApacheCommonsConfiguration2PropertySource(CONFIG_BEAN_NAME, config));
                appContext.getBeanFactory().registerSingleton(CONFIG_BEAN_NAME, config);
            }
            XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(appContext);
            reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD);
            reader.loadBeanDefinitions(appContextResource);
            appContext.refresh();
            return appContext;
        } else {
            return null;
        }
    } catch (Exception e) {
        throw new SiteContextCreationException("Unable to load application context for site '" + siteName + "'", e);
    } finally {
        logger.info("--------------------------------------------------");
        logger.info("</Loading application context for site: " + siteName + ">");
        logger.info("--------------------------------------------------");
    }
}
Also used : SiteContextCreationException(org.craftercms.engine.exception.SiteContextCreationException) RestrictedApplicationContext(org.craftercms.engine.util.spring.context.RestrictedApplicationContext) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) Resource(org.springframework.core.io.Resource) MutablePropertySources(org.springframework.core.env.MutablePropertySources) SiteContextCreationException(org.craftercms.engine.exception.SiteContextCreationException) ConfigurationException(org.craftercms.commons.config.ConfigurationException) BeansException(org.springframework.beans.BeansException) IOException(java.io.IOException) ApacheCommonsConfiguration2PropertySource(org.craftercms.commons.spring.ApacheCommonsConfiguration2PropertySource)

Aggregations

IOException (java.io.IOException)1 ConfigurationException (org.craftercms.commons.config.ConfigurationException)1 ApacheCommonsConfiguration2PropertySource (org.craftercms.commons.spring.ApacheCommonsConfiguration2PropertySource)1 SiteContextCreationException (org.craftercms.engine.exception.SiteContextCreationException)1 RestrictedApplicationContext (org.craftercms.engine.util.spring.context.RestrictedApplicationContext)1 BeansException (org.springframework.beans.BeansException)1 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)1 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)1 MutablePropertySources (org.springframework.core.env.MutablePropertySources)1 Resource (org.springframework.core.io.Resource)1