Search in sources :

Example 1 with Context

use of org.craftercms.core.service.Context in project engine by craftercms.

the class ConfigurationScriptJobResolver method getJobsUnderFolder.

protected List<JobContext> getJobsUnderFolder(SiteContext siteContext, HierarchicalConfiguration jobFolderConfig) {
    List<JobContext> jobContexts = null;
    String folderPath = jobFolderConfig.getString(PATH_KEY);
    String cronExpression = jobFolderConfig.getString(CRON_EXPRESSION_KEY);
    ContentStoreService storeService = siteContext.getStoreService();
    Context context = siteContext.getContext();
    if (StringUtils.isNotEmpty(folderPath) && StringUtils.isNotEmpty(cronExpression)) {
        List<String> scriptPaths = ContentStoreUtils.findChildrenUrl(storeService, context, folderPath);
        if (CollectionUtils.isNotEmpty(scriptPaths)) {
            for (String scriptPath : scriptPaths) {
                if (scriptPath.endsWith(scriptSuffix)) {
                    if (jobContexts == null) {
                        jobContexts = new ArrayList<>();
                    }
                    jobContexts.add(SchedulingUtils.createJobContext(siteContext, scriptPath, cronExpression, servletContext));
                }
            }
        }
    }
    return jobContexts;
}
Also used : SiteContext(org.craftercms.engine.service.context.SiteContext) Context(org.craftercms.core.service.Context) JobContext(org.craftercms.engine.util.quartz.JobContext) ServletContext(javax.servlet.ServletContext) ContentStoreService(org.craftercms.core.service.ContentStoreService) JobContext(org.craftercms.engine.util.quartz.JobContext)

Example 2 with Context

use of org.craftercms.core.service.Context in project engine by craftercms.

the class SiteContextFactory method createContext.

public SiteContext createContext(String siteName) {
    Map<String, String> macroValues = Collections.singletonMap(siteNameMacroName, siteName);
    String resolvedRootFolderPath = macroResolver.resolveMacros(rootFolderPath, macroValues);
    Context context = storeService.createContext(storeType, storeServerUrl, username, password, resolvedRootFolderPath, mergingOn, cacheOn, maxAllowedItemsInCache, ignoreHiddenFiles);
    try {
        SiteContext siteContext = new SiteContext();
        siteContext.setStoreService(storeService);
        siteContext.setSiteName(siteName);
        siteContext.setContext(context);
        siteContext.setStaticAssetsPath(staticAssetsPath);
        siteContext.setTemplatesPath(templatesPath);
        siteContext.setFreeMarkerConfig(freeMarkerConfigFactory.getObject());
        siteContext.setUrlTransformationEngine(urlTransformationEngine);
        siteContext.setOverlayCallback(overlayCallback);
        siteContext.setRestScriptsPath(restScriptsPath);
        siteContext.setControllerScriptsPath(controllerScriptsPath);
        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);
        }
        ResourceLoader resourceLoader = new ContentStoreResourceLoader(siteContext);
        HierarchicalConfiguration config = getConfig(siteContext, resolvedConfigPaths, resourceLoader);
        URLClassLoader classLoader = getClassLoader(siteContext);
        ScriptFactory scriptFactory = getScriptFactory(siteContext, classLoader);
        ConfigurableApplicationContext appContext = getApplicationContext(siteContext, classLoader, config, resolvedAppContextPaths, resourceLoader);
        siteContext.setConfigPaths(resolvedConfigPaths);
        siteContext.setApplicationContextPaths(resolvedAppContextPaths);
        siteContext.setGroovyClassesPath(groovyClassesPath);
        siteContext.setScriptFactory(scriptFactory);
        siteContext.setConfig(config);
        siteContext.setApplicationContext(appContext);
        siteContext.setClassLoader(classLoader);
        executeInitScript(siteContext, scriptFactory);
        Scheduler scheduler = scheduleJobs(siteContext);
        siteContext.setScheduler(scheduler);
        return siteContext;
    } catch (Exception e) {
        // Destroy context if the site context creation failed
        storeService.destroyContext(context);
        throw e;
    }
}
Also used : JobContext(org.craftercms.engine.util.quartz.JobContext) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) Context(org.craftercms.core.service.Context) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) ServletContext(javax.servlet.ServletContext) 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.configuration.HierarchicalConfiguration) SiteContextCreationException(org.craftercms.engine.exception.SiteContextCreationException) BeansException(org.springframework.beans.BeansException) ConfigurationException(org.apache.commons.configuration.ConfigurationException) URLClassLoader(java.net.URLClassLoader) GroovyScriptFactory(org.craftercms.engine.scripting.impl.GroovyScriptFactory) ScriptFactory(org.craftercms.engine.scripting.ScriptFactory)

Example 3 with Context

use of org.craftercms.core.service.Context in project engine by craftercms.

the class TargetedContentDescriptorMergeStrategyTest method testGetDescriptors.

@Test
public void testGetDescriptors() throws Exception {
    Item item = mock(Item.class);
    when(item.getDescriptorDom()).thenReturn(mock(Document.class));
    ContentStoreAdapter storeAdapter = mock(ContentStoreAdapter.class);
    when(storeAdapter.findItem(any(Context.class), any(CachingOptions.class), anyString(), anyBoolean())).thenReturn(item);
    Context context = mock(Context.class);
    when(context.getStoreAdapter()).thenReturn(storeAdapter);
    List<MergeableDescriptor> descriptors = mergeStrategy.getDescriptors(context, CachingOptions.DEFAULT_CACHING_OPTIONS, "/site/website/es_CR/products/index.xml", mock(Document.class), false);
    assertNotNull(descriptors);
    assertEquals(12, descriptors.size());
    assertEquals("/crafter-level-descriptor.level.xml", descriptors.get(0).getUrl());
    assertEquals("/site/crafter-level-descriptor.level.xml", descriptors.get(1).getUrl());
    assertEquals("/site/website/crafter-level-descriptor.level.xml", descriptors.get(2).getUrl());
    assertEquals("/site/website/en/crafter-level-descriptor.level.xml", descriptors.get(3).getUrl());
    assertEquals("/site/website/en/products/crafter-level-descriptor.level.xml", descriptors.get(4).getUrl());
    assertEquals("/site/website/en/products/index.xml", descriptors.get(5).getUrl());
    assertEquals("/site/website/es/crafter-level-descriptor.level.xml", descriptors.get(6).getUrl());
    assertEquals("/site/website/es/products/crafter-level-descriptor.level.xml", descriptors.get(7).getUrl());
    assertEquals("/site/website/es/products/index.xml", descriptors.get(8).getUrl());
    assertEquals("/site/website/es_CR/crafter-level-descriptor.level.xml", descriptors.get(9).getUrl());
    assertEquals("/site/website/es_CR/products/crafter-level-descriptor.level.xml", descriptors.get(10).getUrl());
    assertEquals("/site/website/es_CR/products/index.xml", descriptors.get(11).getUrl());
}
Also used : SiteContext(org.craftercms.engine.service.context.SiteContext) Context(org.craftercms.core.service.Context) Item(org.craftercms.core.service.Item) CachingOptions(org.craftercms.core.service.CachingOptions) ContentStoreAdapter(org.craftercms.core.store.ContentStoreAdapter) MergeableDescriptor(org.craftercms.core.xml.mergers.MergeableDescriptor) Document(org.dom4j.Document) Test(org.junit.Test)

Example 4 with Context

use of org.craftercms.core.service.Context in project engine by craftercms.

the class TargetedContentStoreAdapterTest method testFindContent.

@Test
public void testFindContent() throws Exception {
    Context context = new TargetedContentStoreAdapter.ContextWrapper(storeAdapter, mock(Context.class));
    CachingOptions cachingOptions = CachingOptions.DEFAULT_CACHING_OPTIONS;
    Content content = storeAdapter.findContent(context, cachingOptions, "/site/website/en/index.xml");
    assertNotNull(content);
    content = storeAdapter.findContent(context, cachingOptions, "/site/website/ja_JP_JP/index.xml");
    assertNotNull(content);
    content = storeAdapter.findContent(context, cachingOptions, "/site/website/index.xml");
    assertNotNull(content);
    content = storeAdapter.findContent(context, cachingOptions, "/static-assets/css/main.css");
    assertNotNull(content);
}
Also used : Context(org.craftercms.core.service.Context) CachingOptions(org.craftercms.core.service.CachingOptions) Content(org.craftercms.core.service.Content) Test(org.junit.Test)

Example 5 with Context

use of org.craftercms.core.service.Context in project engine by craftercms.

the class ScriptUrlTemplateScannerImpl method scan.

@Override
public List<UriTemplate> scan(SiteContext siteContext) {
    Context context = siteContext.getContext();
    ContentStoreService storeService = siteContext.getStoreService();
    ScriptFactory scriptFactory = siteContext.getScriptFactory();
    List<String> scriptUrls = new ArrayList<>();
    List<UriTemplate> urlTemplates = new ArrayList<>();
    findScripts(context, storeService, scriptFactory, scriptsFolder, scriptUrls);
    if (CollectionUtils.isNotEmpty(scriptUrls)) {
        for (String scriptUrl : scriptUrls) {
            Matcher matcher = urlVariablePlaceholderPattern.matcher(scriptUrl);
            if (matcher.find()) {
                urlTemplates.add(new UriTemplate(scriptUrl));
            }
        }
    }
    return urlTemplates;
}
Also used : SiteContext(org.craftercms.engine.service.context.SiteContext) Context(org.craftercms.core.service.Context) ContentStoreService(org.craftercms.core.service.ContentStoreService) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) UriTemplate(org.springframework.web.util.UriTemplate) ScriptFactory(org.craftercms.engine.scripting.ScriptFactory)

Aggregations

Context (org.craftercms.core.service.Context)8 Test (org.junit.Test)5 CachingOptions (org.craftercms.core.service.CachingOptions)4 Item (org.craftercms.core.service.Item)3 SiteContext (org.craftercms.engine.service.context.SiteContext)3 ServletContext (javax.servlet.ServletContext)2 ContentStoreService (org.craftercms.core.service.ContentStoreService)2 ScriptFactory (org.craftercms.engine.scripting.ScriptFactory)2 JobContext (org.craftercms.engine.util.quartz.JobContext)2 URLClassLoader (java.net.URLClassLoader)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 ConfigurationException (org.apache.commons.configuration.ConfigurationException)1 HierarchicalConfiguration (org.apache.commons.configuration.HierarchicalConfiguration)1 Content (org.craftercms.core.service.Content)1 ContentStoreAdapter (org.craftercms.core.store.ContentStoreAdapter)1 MergeableDescriptor (org.craftercms.core.xml.mergers.MergeableDescriptor)1 SiteContextCreationException (org.craftercms.engine.exception.SiteContextCreationException)1 GroovyScriptFactory (org.craftercms.engine.scripting.impl.GroovyScriptFactory)1 ContentStoreGroovyResourceLoader (org.craftercms.engine.util.groovy.ContentStoreGroovyResourceLoader)1