use of org.craftercms.engine.scripting.ScriptFactory 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.engine.scripting.ScriptFactory in project engine by craftercms.
the class ScriptJob method execute.
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
JobDataMap dataMap = context.getJobDetail().getJobDataMap();
String scriptUrl = dataMap.getString(SCRIPT_URL_DATA_KEY);
SiteContext siteContext = (SiteContext) dataMap.get(SITE_CONTEXT_DATA_KEY);
ServletContext servletContext = (ServletContext) dataMap.get(SERVLET_CONTEXT_DATA_KEY);
ScriptFactory scriptFactory = siteContext.getScriptFactory();
if (scriptFactory == null) {
throw new JobExecutionException("No script factory associate to site context '" + siteContext.getSiteName() + "'");
}
SiteContext.setCurrent(siteContext);
try {
Map<String, Object> variables = new HashMap<>();
GroovyScriptUtils.addJobScriptVariables(variables, servletContext);
scriptFactory.getScript(scriptUrl).execute(variables);
} catch (Exception e) {
throw new JobExecutionException("Error executing script job at " + scriptUrl, e);
} finally {
SiteContext.clear();
}
}
use of org.craftercms.engine.scripting.ScriptFactory in project engine by craftercms.
the class ExecuteControllerDirective method executeController.
protected void executeController(String path, Environment env) throws TemplateException {
Map<String, Object> scriptVariables = createScriptVariables(env);
SiteContext siteContext = SiteContext.getCurrent();
if (siteContext != null) {
ScriptFactory scriptFactory = siteContext.getScriptFactory();
if (scriptFactory == null) {
throw new IllegalStateException("No script factory associate to current site context '" + siteContext.getSiteName() + "'");
}
Script script;
try {
script = scriptFactory.getScript(path);
} catch (Exception e) {
throw new TemplateException("Unable to load controller at '" + path + "'", e, env);
}
executeController(script, scriptVariables, env);
} else {
throw new IllegalStateException("No current site context found");
}
}
use of org.craftercms.engine.scripting.ScriptFactory in project engine by craftercms.
the class RestScriptsControllerTest method createSiteContext.
private SiteContext createSiteContext(ContentStoreService storeService) {
SiteContext siteContext = mock(SiteContext.class);
ContentStoreResourceConnector resourceConnector = new ContentStoreResourceConnector(siteContext);
ScriptFactory scriptFactory = new GroovyScriptFactory(resourceConnector, Collections.<String, Object>emptyMap());
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);
return siteContext;
}
use of org.craftercms.engine.scripting.ScriptFactory in project engine by craftercms.
the class PageRenderController method getControllerScript.
protected Script getControllerScript(SiteContext siteContext, HttpServletRequest request, String pageUrl) {
ScriptFactory scriptFactory = siteContext.getScriptFactory();
if (scriptFactory == null) {
throw new IllegalStateException("No script factory associated to current site context '" + siteContext.getSiteName() + "'");
}
String scriptUrl = getScriptUrl(siteContext, scriptFactory, request, pageUrl);
try {
// Check controller script exists
if (storeService.exists(siteContext.getContext(), scriptUrl)) {
if (logger.isDebugEnabled()) {
logger.debug("Controller script found for page " + pageUrl + " at " + scriptUrl);
}
return scriptFactory.getScript(scriptUrl);
} else if (logger.isDebugEnabled()) {
logger.debug("No controller script for page " + pageUrl + " at " + scriptUrl);
}
} catch (CrafterException e) {
logger.error("Error while trying to retrieve controller script at " + scriptUrl, e);
}
return null;
}
Aggregations