Search in sources :

Example 1 with ContentStoreResourceLoader

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

the class SiteContextFactory method createContext.

public SiteContext createContext(String siteName) {
    Map<String, String> macroValues = new HashMap<>();
    macroValues.put(siteNameMacroName, siteName);
    if (publishingTargetResolver instanceof SiteAwarePublishingTargetResolver) {
        String target = ((SiteAwarePublishingTargetResolver) publishingTargetResolver).getPublishingTarget(siteName);
        macroValues.put(publishingTargetMacroName, target);
    }
    String resolvedRootFolderPath = macroResolver.resolveMacros(rootFolderPath, macroValues);
    Context context = storeService.getContext(UUID.randomUUID().toString(), storeType, resolvedRootFolderPath, mergingOn, cacheOn, maxAllowedItemsInCache, ignoreHiddenFiles);
    try {
        SiteContext siteContext = new SiteContext();
        siteContext.setInitTimeout(initTimeout);
        siteContext.setStoreService(storeService);
        siteContext.setCacheTemplate(cacheTemplate);
        siteContext.setSiteName(siteName);
        siteContext.setContext(context);
        siteContext.setStaticAssetsPath(staticAssetsPath);
        siteContext.setTemplatesPath(templatesPath);
        siteContext.setInitScriptPath(initScriptPath);
        siteContext.setFreeMarkerConfig(freeMarkerConfigFactory.getObject());
        siteContext.setUrlTransformationEngine(urlTransformationEngine);
        siteContext.setRestScriptsPath(restScriptsPath);
        siteContext.setControllerScriptsPath(controllerScriptsPath);
        siteContext.setGraphQLFactory(graphQLFactory);
        siteContext.setShutdownTimeout(shutdownTimeout);
        if (disableVariableRestrictions) {
            siteContext.setServletContext(servletContext);
        }
        if (cacheWarmUpEnabled) {
            siteContext.setCacheWarmer(cacheWarmer);
        }
        String[] resolvedConfigPaths = new String[configPaths.length];
        for (int i = 0; i < configPaths.length; i++) {
            resolvedConfigPaths[i] = macroResolver.resolveMacros(configPaths[i], macroValues);
        }
        String[] resolvedAppContextPaths = new String[applicationContextPaths.length];
        for (int i = 0; i < applicationContextPaths.length; i++) {
            resolvedAppContextPaths[i] = macroResolver.resolveMacros(applicationContextPaths[i], macroValues);
        }
        String[] resolvedUrlRewriteConfPaths = new String[urlRewriteConfPaths.length];
        for (int i = 0; i < urlRewriteConfPaths.length; i++) {
            resolvedUrlRewriteConfPaths[i] = macroResolver.resolveMacros(urlRewriteConfPaths[i], macroValues);
        }
        List<String> resolvedProxyConfPaths = Stream.of(proxyConfigPaths).map(path -> macroResolver.resolveMacros(path, macroValues)).collect(toList());
        ResourceLoader resourceLoader = new ContentStoreResourceLoader(siteContext);
        HierarchicalConfiguration<?> config = getConfig(siteContext, resolvedConfigPaths, resourceLoader);
        configureScriptSandbox(siteContext, resourceLoader);
        URLClassLoader classLoader = getClassLoader(siteContext);
        ScriptFactory scriptFactory = getScriptFactory(siteContext, classLoader);
        ConfigurableApplicationContext appContext = getApplicationContext(siteContext, classLoader, config, resolvedAppContextPaths, resourceLoader);
        UrlRewriter urlRewriter = getUrlRewriter(siteContext, resolvedUrlRewriteConfPaths, resourceLoader);
        HierarchicalConfiguration proxyConfig = getProxyConfig(siteContext, resolvedProxyConfPaths, resourceLoader);
        siteContext.setScriptFactory(scriptFactory);
        siteContext.setConfig(config);
        siteContext.setGlobalApplicationContext(globalApplicationContext);
        siteContext.setApplicationContext(appContext);
        siteContext.setClassLoader(classLoader);
        siteContext.setUrlRewriter(urlRewriter);
        siteContext.setProxyConfig(proxyConfig);
        if (config != null) {
            siteContext.setAllowedTemplatePaths(config.getStringArray(CONFIG_KEY_ALLOWED_TEMPLATE_PATHS));
        }
        Scheduler scheduler = scheduleJobs(siteContext);
        siteContext.setScheduler(scheduler);
        return siteContext;
    } catch (Exception e) {
        logger.error("Error creating context for site '" + siteName + "'", e);
        // Destroy context if the site context creation failed
        storeService.destroyContext(context);
        throw e;
    }
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Context(org.craftercms.core.service.Context) JobContext(org.craftercms.engine.util.quartz.JobContext) ApplicationContext(org.springframework.context.ApplicationContext) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) RestrictedApplicationContext(org.craftercms.engine.util.spring.context.RestrictedApplicationContext) ServletContext(javax.servlet.ServletContext) ServletContextAware(org.springframework.web.context.ServletContextAware) FreeMarkerConfig(org.springframework.web.servlet.view.freemarker.FreeMarkerConfig) PublishingTargetResolver(org.craftercms.commons.config.PublishingTargetResolver) Scheduler(org.quartz.Scheduler) Collections.singletonList(java.util.Collections.singletonList) ContentStoreService(org.craftercms.core.service.ContentStoreService) URLClassLoader(java.net.URLClassLoader) SiteContextCreationException(org.craftercms.engine.exception.SiteContextCreationException) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) UrlRewriter(org.tuckey.web.filters.urlrewrite.UrlRewriter) ContentStoreGroovyResourceLoader(org.craftercms.engine.util.groovy.ContentStoreGroovyResourceLoader) Resource(org.springframework.core.io.Resource) MacroResolver(org.craftercms.engine.macro.MacroResolver) ResourceLoader(org.springframework.core.io.ResourceLoader) HierarchicalConfiguration(org.apache.commons.configuration2.HierarchicalConfiguration) Context(org.craftercms.core.service.Context) CacheTemplate(org.craftercms.core.util.cache.CacheTemplate) ConfigurationException(org.craftercms.commons.config.ConfigurationException) Stream(java.util.stream.Stream) ContentStoreResourceConnector(org.craftercms.engine.util.groovy.ContentStoreResourceConnector) GroovyScriptUtils.getCompilerConfiguration(org.craftercms.engine.util.GroovyScriptUtils.getCompilerConfiguration) Dom4jExtension(org.craftercms.engine.util.groovy.Dom4jExtension) SiteCacheWarmer(org.craftercms.engine.cache.SiteCacheWarmer) PermitAllWhitelist(org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.PermitAllWhitelist) ContentStoreResourceLoader(org.craftercms.engine.util.spring.ContentStoreResourceLoader) LogFactory(org.apache.commons.logging.LogFactory) MutablePropertySources(org.springframework.core.env.MutablePropertySources) GroovyClassLoader(groovy.lang.GroovyClassLoader) ScriptJobResolver(org.craftercms.engine.scripting.ScriptJobResolver) ApplicationContextAware(org.springframework.context.ApplicationContextAware) java.util(java.util) GroovyScriptFactory(org.craftercms.engine.scripting.impl.GroovyScriptFactory) CollectionUtils(org.apache.commons.collections4.CollectionUtils) GraphQLFactory(org.craftercms.engine.graphql.GraphQLFactory) JobContext(org.craftercms.engine.util.quartz.JobContext) ApacheCommonsConfiguration2PropertySource(org.craftercms.commons.spring.ApacheCommonsConfiguration2PropertySource) SiteAwarePublishingTargetResolver(org.craftercms.engine.util.config.SiteAwarePublishingTargetResolver) EncryptionAwareConfigurationReader(org.craftercms.commons.config.EncryptionAwareConfigurationReader) ScriptFactory(org.craftercms.engine.scripting.ScriptFactory) SchedulingUtils(org.craftercms.engine.util.SchedulingUtils) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) Executor(java.util.concurrent.Executor) Conf(org.tuckey.web.filters.urlrewrite.Conf) BeansException(org.springframework.beans.BeansException) IOException(java.io.IOException) Blacklist(org.jenkinsci.plugins.scriptsecurity.sandbox.blacklists.Blacklist) ApplicationContext(org.springframework.context.ApplicationContext) InputStreamReader(java.io.InputStreamReader) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) ObjectFactory(org.springframework.beans.factory.ObjectFactory) Whitelist(org.jenkinsci.plugins.scriptsecurity.sandbox.Whitelist) Collectors.toList(java.util.stream.Collectors.toList) UrlTransformationEngine(org.craftercms.core.url.UrlTransformationEngine) RestrictedApplicationContext(org.craftercms.engine.util.spring.context.RestrictedApplicationContext) Log(org.apache.commons.logging.Log) SandboxInterceptor(org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor) ServletContext(javax.servlet.ServletContext) Required(org.springframework.beans.factory.annotation.Required) InputStream(java.io.InputStream) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ContentStoreGroovyResourceLoader(org.craftercms.engine.util.groovy.ContentStoreGroovyResourceLoader) ResourceLoader(org.springframework.core.io.ResourceLoader) ContentStoreResourceLoader(org.craftercms.engine.util.spring.ContentStoreResourceLoader) Scheduler(org.quartz.Scheduler) ContentStoreResourceLoader(org.craftercms.engine.util.spring.ContentStoreResourceLoader) HierarchicalConfiguration(org.apache.commons.configuration2.HierarchicalConfiguration) SiteContextCreationException(org.craftercms.engine.exception.SiteContextCreationException) ConfigurationException(org.craftercms.commons.config.ConfigurationException) BeansException(org.springframework.beans.BeansException) IOException(java.io.IOException) SiteAwarePublishingTargetResolver(org.craftercms.engine.util.config.SiteAwarePublishingTargetResolver) UrlRewriter(org.tuckey.web.filters.urlrewrite.UrlRewriter) URLClassLoader(java.net.URLClassLoader) GroovyScriptFactory(org.craftercms.engine.scripting.impl.GroovyScriptFactory) ScriptFactory(org.craftercms.engine.scripting.ScriptFactory)

Aggregations

GroovyClassLoader (groovy.lang.GroovyClassLoader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 URLClassLoader (java.net.URLClassLoader)1 java.util (java.util)1 Collections.singletonList (java.util.Collections.singletonList)1 Executor (java.util.concurrent.Executor)1 Collectors.toList (java.util.stream.Collectors.toList)1 Stream (java.util.stream.Stream)1 ServletContext (javax.servlet.ServletContext)1 CollectionUtils (org.apache.commons.collections4.CollectionUtils)1 HierarchicalConfiguration (org.apache.commons.configuration2.HierarchicalConfiguration)1 Log (org.apache.commons.logging.Log)1 LogFactory (org.apache.commons.logging.LogFactory)1 ConfigurationException (org.craftercms.commons.config.ConfigurationException)1 EncryptionAwareConfigurationReader (org.craftercms.commons.config.EncryptionAwareConfigurationReader)1 PublishingTargetResolver (org.craftercms.commons.config.PublishingTargetResolver)1 ApacheCommonsConfiguration2PropertySource (org.craftercms.commons.spring.ApacheCommonsConfiguration2PropertySource)1 ContentStoreService (org.craftercms.core.service.ContentStoreService)1