Search in sources :

Example 1 with CacheTemplate

use of org.craftercms.core.util.cache.CacheTemplate 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)

Example 2 with CacheTemplate

use of org.craftercms.core.util.cache.CacheTemplate in project engine by craftercms.

the class Dom4jExtensionTest method createSiteContext.

private SiteContext createSiteContext(ContentStoreService storeService) {
    CacheTemplate cacheTemplate = CacheTemplateMockUtils.createCacheTemplate();
    SiteContext siteContext = spy(new SiteContext());
    when(siteContext.getSiteName()).thenReturn("default");
    when(siteContext.getStoreService()).thenReturn(storeService);
    when(siteContext.getCacheTemplate()).thenReturn(cacheTemplate);
    return siteContext;
}
Also used : SiteContext(org.craftercms.engine.service.context.SiteContext) CacheTemplate(org.craftercms.core.util.cache.CacheTemplate)

Example 3 with CacheTemplate

use of org.craftercms.core.util.cache.CacheTemplate in project engine by craftercms.

the class RestScriptsControllerTest method createSiteContext.

private SiteContext createSiteContext(ContentStoreService storeService) {
    SiteContext siteContext = spy(new SiteContext());
    CacheTemplate cacheTemplate = CacheTemplateMockUtils.createCacheTemplate();
    ContentStoreResourceConnector resourceConnector = new ContentStoreResourceConnector(siteContext);
    ScriptFactory scriptFactory = new GroovyScriptFactory(siteContext, resourceConnector, Collections.emptyMap(), false);
    when(siteContext.getSiteName()).thenReturn("test");
    when(siteContext.getContext()).thenReturn(mock(Context.class));
    when(siteContext.getRestScriptsPath()).thenReturn("/scripts");
    when(siteContext.getStoreService()).thenReturn(storeService);
    when(siteContext.getScriptFactory()).thenReturn(scriptFactory);
    when(siteContext.getCacheTemplate()).thenReturn(cacheTemplate);
    return siteContext;
}
Also used : RequestContext(org.craftercms.commons.http.RequestContext) SiteContext(org.craftercms.engine.service.context.SiteContext) Context(org.craftercms.core.service.Context) ServletContext(javax.servlet.ServletContext) ContentStoreResourceConnector(org.craftercms.engine.util.groovy.ContentStoreResourceConnector) SiteContext(org.craftercms.engine.service.context.SiteContext) GroovyScriptFactory(org.craftercms.engine.scripting.impl.GroovyScriptFactory) CacheTemplate(org.craftercms.core.util.cache.CacheTemplate) GroovyScriptFactory(org.craftercms.engine.scripting.impl.GroovyScriptFactory) ScriptFactory(org.craftercms.engine.scripting.ScriptFactory)

Example 4 with CacheTemplate

use of org.craftercms.core.util.cache.CacheTemplate in project engine by craftercms.

the class GroovyScriptFactoryTest method createSiteContext.

private SiteContext createSiteContext(ContentStoreService storeService) {
    CacheTemplate cacheTemplate = CacheTemplateMockUtils.createCacheTemplate();
    SiteContext siteContext = spy(new SiteContext());
    when(siteContext.getSiteName()).thenReturn("default");
    when(siteContext.getContext()).thenReturn(mock(Context.class));
    when(siteContext.getStoreService()).thenReturn(storeService);
    when(siteContext.getCacheTemplate()).thenReturn(cacheTemplate);
    return siteContext;
}
Also used : SiteContext(org.craftercms.engine.service.context.SiteContext) RequestContext(org.craftercms.commons.http.RequestContext) Context(org.craftercms.core.service.Context) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) SiteContext(org.craftercms.engine.service.context.SiteContext) CacheTemplate(org.craftercms.core.util.cache.CacheTemplate)

Example 5 with CacheTemplate

use of org.craftercms.core.util.cache.CacheTemplate in project engine by craftercms.

the class CacheTemplateMockUtils method createCacheTemplate.

public static CacheTemplate createCacheTemplate() {
    CacheTemplate mock = mock(CacheTemplate.class);
    setUpWithNoCaching(mock);
    return mock;
}
Also used : CacheTemplate(org.craftercms.core.util.cache.CacheTemplate)

Aggregations

CacheTemplate (org.craftercms.core.util.cache.CacheTemplate)5 Context (org.craftercms.core.service.Context)3 SiteContext (org.craftercms.engine.service.context.SiteContext)3 ServletContext (javax.servlet.ServletContext)2 RequestContext (org.craftercms.commons.http.RequestContext)2 ScriptFactory (org.craftercms.engine.scripting.ScriptFactory)2 GroovyScriptFactory (org.craftercms.engine.scripting.impl.GroovyScriptFactory)2 ContentStoreResourceConnector (org.craftercms.engine.util.groovy.ContentStoreResourceConnector)2 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)2 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 CollectionUtils (org.apache.commons.collections4.CollectionUtils)1