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;
}
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;
}
}
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());
}
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);
}
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;
}
Aggregations