Search in sources :

Example 1 with SiteContextCreationException

use of org.craftercms.engine.exception.SiteContextCreationException 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)

Example 2 with SiteContextCreationException

use of org.craftercms.engine.exception.SiteContextCreationException in project engine by craftercms.

the class SiteContextFactory method getConfig.

protected HierarchicalConfiguration getConfig(SiteContext siteContext, String[] configPaths, ResourceLoader resourceLoader) {
    String siteName = siteContext.getSiteName();
    try {
        logger.info("--------------------------------------------------");
        logger.info("<Loading configuration for site: " + siteName + ">");
        logger.info("--------------------------------------------------");
        ConfigurationBuilder builder = new MultiConfigurationBuilder(configPaths, resourceLoader);
        HierarchicalConfiguration config = (HierarchicalConfiguration) builder.getConfiguration();
        logger.info("--------------------------------------------------");
        logger.info("</Loading configuration for site: " + siteName + ">");
        logger.info("--------------------------------------------------");
        return config;
    } catch (ConfigurationException e) {
        throw new SiteContextCreationException("Unable to load configuration for site '" + siteContext.getSiteName() + "'", e);
    }
}
Also used : SiteContextCreationException(org.craftercms.engine.exception.SiteContextCreationException) MultiConfigurationBuilder(org.craftercms.engine.util.config.impl.MultiConfigurationBuilder) ConfigurationBuilder(org.apache.commons.configuration.ConfigurationBuilder) ConfigurationException(org.apache.commons.configuration.ConfigurationException) MultiConfigurationBuilder(org.craftercms.engine.util.config.impl.MultiConfigurationBuilder) HierarchicalConfiguration(org.apache.commons.configuration.HierarchicalConfiguration)

Aggregations

ConfigurationException (org.apache.commons.configuration.ConfigurationException)2 SiteContextCreationException (org.craftercms.engine.exception.SiteContextCreationException)2 ArrayList (java.util.ArrayList)1 ConfigurationBuilder (org.apache.commons.configuration.ConfigurationBuilder)1 HierarchicalConfiguration (org.apache.commons.configuration.HierarchicalConfiguration)1 MultiConfigurationBuilder (org.craftercms.engine.util.config.impl.MultiConfigurationBuilder)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