Search in sources :

Example 6 with SiteContext

use of org.craftercms.engine.service.context.SiteContext 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 7 with SiteContext

use of org.craftercms.engine.service.context.SiteContext in project engine by craftercms.

the class GroovyScriptUtils method addSiteConfigVariable.

private static void addSiteConfigVariable(Map<String, Object> variables) {
    SiteContext siteContext = SiteContext.getCurrent();
    Configuration config = null;
    if (siteContext != null) {
        config = siteContext.getConfig();
    }
    variables.put(VARIABLE_SITE_CONFIG, config);
}
Also used : Configuration(org.apache.commons.configuration.Configuration) SiteContext(org.craftercms.engine.service.context.SiteContext)

Example 8 with SiteContext

use of org.craftercms.engine.service.context.SiteContext in project engine by craftercms.

the class GroovyScriptUtils method addJobScriptVariables.

public static void addJobScriptVariables(Map<String, Object> variables, ServletContext servletContext) {
    SiteContext siteContext = SiteContext.getCurrent();
    if (siteContext != null && siteContext.getApplicationContext() != null) {
        ApplicationContextAccessor appContext = new ApplicationContextAccessor();
        appContext.setApplicationContext(siteContext.getApplicationContext());
        variables.put(VARIABLE_APPLICATION_CONTEXT, appContext);
    }
    variables.put(VARIABLE_APPLICATION, servletContext);
    variables.put(VARIABLE_LOGGER, GROOVY_SCRIPT_LOGGER);
    addSiteContextVariable(variables);
    addSiteConfigVariable(variables);
}
Also used : ApplicationContextAccessor(org.craftercms.engine.util.spring.ApplicationContextAccessor) SiteContext(org.craftercms.engine.service.context.SiteContext)

Example 9 with SiteContext

use of org.craftercms.engine.service.context.SiteContext in project engine by craftercms.

the class RejectIndexFilesItemFilterTest method setUpCurrentConfig.

private void setUpCurrentConfig() {
    XMLConfiguration config = mock(XMLConfiguration.class);
    when(config.getString(INDEX_FILE_NAME_CONFIG_KEY, DEFAULT_INDEX_FILE_NAME)).thenReturn(DEFAULT_INDEX_FILE_NAME);
    when(config.getBoolean(TARGETING_ENABLED_CONFIG_KEY, false)).thenReturn(true);
    SiteContext siteContext = mock(SiteContext.class);
    when(siteContext.getSiteName()).thenReturn("test");
    when(siteContext.getConfig()).thenReturn(config);
    SiteContext.setCurrent(siteContext);
}
Also used : XMLConfiguration(org.apache.commons.configuration.XMLConfiguration) SiteContext(org.craftercms.engine.service.context.SiteContext)

Example 10 with SiteContext

use of org.craftercms.engine.service.context.SiteContext in project engine by craftercms.

the class CircularQueueLogAppender method append.

@Override
protected void append(final LoggingEvent event) {
    final SiteContext ctx = SiteContext.getCurrent();
    if (ctx != null) {
        final String siteName = ctx.getSiteName();
        if (StringUtils.isNoneBlank(siteName)) {
            Map<String, Object> mappy = new HashMap<>();
            mappy.put("site", siteName);
            mappy.put("level", event.getLevel().toString());
            mappy.put("message", event.getRenderedMessage());
            mappy.put("thread", event.getThreadName());
            mappy.put("exception", subAppend(event));
            mappy.put("timestamp", dateFormat.format(new Date(event.getTimeStamp())));
            mappy.put("timestampm", event.getTimeStamp());
            buffer.add(mappy);
        }
    }
}
Also used : HashMap(java.util.HashMap) SiteContext(org.craftercms.engine.service.context.SiteContext) Date(java.util.Date)

Aggregations

SiteContext (org.craftercms.engine.service.context.SiteContext)35 ScriptFactory (org.craftercms.engine.scripting.ScriptFactory)8 Context (org.craftercms.core.service.Context)7 HierarchicalConfiguration (org.apache.commons.configuration.HierarchicalConfiguration)6 HashMap (java.util.HashMap)4 ServletContext (javax.servlet.ServletContext)4 XMLConfiguration (org.apache.commons.configuration.XMLConfiguration)4 RequestContext (org.craftercms.commons.http.RequestContext)4 Callback (org.craftercms.commons.lang.Callback)3 ContentStoreService (org.craftercms.core.service.ContentStoreService)3 Script (org.craftercms.engine.scripting.Script)3 TemplateException (freemarker.template.TemplateException)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Configuration (org.apache.commons.configuration.Configuration)2 CrafterException (org.craftercms.core.exception.CrafterException)2 Content (org.craftercms.core.service.Content)2 ConfigurationException (org.craftercms.engine.exception.ConfigurationException)2 JobContext (org.craftercms.engine.util.quartz.JobContext)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2