Search in sources :

Example 1 with ApacheCommonsConfigPropertySource

use of org.craftercms.engine.util.spring.ApacheCommonsConfigPropertySource in project engine by craftercms.

the class SiteContextFactory method getApplicationContext.

protected ConfigurableApplicationContext getApplicationContext(SiteContext siteContext, URLClassLoader classLoader, HierarchicalConfiguration config, String[] applicationContextPaths, ResourceLoader resourceLoader) {
    try {
        List<Resource> resources = new ArrayList<>();
        for (String path : applicationContextPaths) {
            Resource resource = resourceLoader.getResource(path);
            if (resource.exists()) {
                resources.add(resource);
            }
        }
        if (CollectionUtils.isNotEmpty(resources)) {
            String siteName = siteContext.getSiteName();
            logger.info("--------------------------------------------------");
            logger.info("<Loading application context for site: " + siteName + ">");
            logger.info("--------------------------------------------------");
            GenericApplicationContext appContext = new GenericApplicationContext(mainApplicationContext);
            appContext.setClassLoader(classLoader);
            if (config != null) {
                MutablePropertySources propertySources = appContext.getEnvironment().getPropertySources();
                propertySources.addFirst(new ApacheCommonsConfigPropertySource("siteConfig", config));
            }
            XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(appContext);
            reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD);
            for (Resource resource : resources) {
                reader.loadBeanDefinitions(resource);
            }
            appContext.refresh();
            logger.info("--------------------------------------------------");
            logger.info("</Loading application context for site: " + siteName + ">");
            logger.info("--------------------------------------------------");
            return appContext;
        } else {
            return null;
        }
    } catch (Exception e) {
        throw new SiteContextCreationException("Unable to load application context for site '" + siteContext.getSiteName() + "'", e);
    }
}
Also used : SiteContextCreationException(org.craftercms.engine.exception.SiteContextCreationException) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) ApacheCommonsConfigPropertySource(org.craftercms.engine.util.spring.ApacheCommonsConfigPropertySource) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) ArrayList(java.util.ArrayList) Resource(org.springframework.core.io.Resource) MutablePropertySources(org.springframework.core.env.MutablePropertySources) SiteContextCreationException(org.craftercms.engine.exception.SiteContextCreationException) BeansException(org.springframework.beans.BeansException) ConfigurationException(org.apache.commons.configuration.ConfigurationException)

Aggregations

ArrayList (java.util.ArrayList)1 ConfigurationException (org.apache.commons.configuration.ConfigurationException)1 SiteContextCreationException (org.craftercms.engine.exception.SiteContextCreationException)1 ApacheCommonsConfigPropertySource (org.craftercms.engine.util.spring.ApacheCommonsConfigPropertySource)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