Search in sources :

Example 1 with ScriptFactory

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;
    }
}
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 2 with ScriptFactory

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();
    }
}
Also used : JobDataMap(org.quartz.JobDataMap) JobExecutionException(org.quartz.JobExecutionException) HashMap(java.util.HashMap) SiteContext(org.craftercms.engine.service.context.SiteContext) ServletContext(javax.servlet.ServletContext) ScriptFactory(org.craftercms.engine.scripting.ScriptFactory) JobExecutionException(org.quartz.JobExecutionException)

Example 3 with ScriptFactory

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");
    }
}
Also used : Script(org.craftercms.engine.scripting.Script) TemplateException(freemarker.template.TemplateException) SiteContext(org.craftercms.engine.service.context.SiteContext) ScriptFactory(org.craftercms.engine.scripting.ScriptFactory) TemplateModelException(freemarker.template.TemplateModelException) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException)

Example 4 with ScriptFactory

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;
}
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) GroovyScriptFactory(org.craftercms.engine.scripting.impl.GroovyScriptFactory) ScriptFactory(org.craftercms.engine.scripting.ScriptFactory)

Example 5 with ScriptFactory

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;
}
Also used : CrafterException(org.craftercms.core.exception.CrafterException) ScriptFactory(org.craftercms.engine.scripting.ScriptFactory)

Aggregations

ScriptFactory (org.craftercms.engine.scripting.ScriptFactory)10 SiteContext (org.craftercms.engine.service.context.SiteContext)8 ServletContext (javax.servlet.ServletContext)4 Context (org.craftercms.core.service.Context)4 TemplateException (freemarker.template.TemplateException)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 HierarchicalConfiguration (org.apache.commons.configuration.HierarchicalConfiguration)2 RequestContext (org.craftercms.commons.http.RequestContext)2 ContentStoreService (org.craftercms.core.service.ContentStoreService)2 Script (org.craftercms.engine.scripting.Script)2 GroovyScriptFactory (org.craftercms.engine.scripting.impl.GroovyScriptFactory)2 TemplateModelException (freemarker.template.TemplateModelException)1 URLClassLoader (java.net.URLClassLoader)1 List (java.util.List)1 Matcher (java.util.regex.Matcher)1 ConfigurationException (org.apache.commons.configuration.ConfigurationException)1 XMLConfiguration (org.apache.commons.configuration.XMLConfiguration)1 Callback (org.craftercms.commons.lang.Callback)1