Example usage for javax.script SimpleScriptContext SimpleScriptContext

List of usage examples for javax.script SimpleScriptContext SimpleScriptContext

Introduction

In this page you can find the example usage for javax.script SimpleScriptContext SimpleScriptContext.

Prototype

public SimpleScriptContext() 

Source Link

Document

Create a SimpleScriptContext .

Usage

From source file:org.pentaho.reporting.engine.classic.core.modules.misc.datafactory.DataFactoryScriptingSupport.java

public void initialize(final DataFactory dataFactory, final DataFactoryContext dataFactoryContext)
        throws ReportDataFactoryException {
    if (globalScriptContext != null) {
        return;/*from  w ww . j  a va  2s  .  c  o m*/
    }

    this.dataFactory = dataFactory;
    this.resourceManager = dataFactoryContext.getResourceManager();
    this.contextKey = dataFactoryContext.getContextKey();
    this.configuration = dataFactoryContext.getConfiguration();
    this.resourceBundleFactory = dataFactoryContext.getResourceBundleFactory();
    this.dataFactoryContext = dataFactoryContext;

    globalScriptContext = new SimpleScriptContext();
    globalScriptContext.setAttribute("dataFactory", dataFactory, ScriptContext.ENGINE_SCOPE);
    globalScriptContext.setAttribute("configuration", configuration, ScriptContext.ENGINE_SCOPE);
    globalScriptContext.setAttribute("resourceManager", resourceManager, ScriptContext.ENGINE_SCOPE);
    globalScriptContext.setAttribute("contextKey", contextKey, ScriptContext.ENGINE_SCOPE);
    globalScriptContext.setAttribute("resourceBundleFactory", resourceBundleFactory,
            ScriptContext.ENGINE_SCOPE);

    if (StringUtils.isEmpty(globalScriptLanguage)) {
        return;
    }

    globalScriptContext.setAttribute("scriptHelper",
            new ScriptHelper(globalScriptContext, globalScriptLanguage, resourceManager, contextKey),
            ScriptContext.ENGINE_SCOPE);

    final ScriptEngine maybeInvocableEngine = new ScriptEngineManager().getEngineByName(globalScriptLanguage);
    if (maybeInvocableEngine == null) {
        throw new ReportDataFactoryException(String.format(
                "DataFactoryScriptingSupport: Failed to locate scripting engine for language '%s'.",
                globalScriptLanguage));
    }
    if (maybeInvocableEngine instanceof Invocable == false) {
        return;
    }
    this.globalScriptEngine = (Invocable) maybeInvocableEngine;

    maybeInvocableEngine.setContext(globalScriptContext);
    try {
        maybeInvocableEngine.eval(globalScript);
    } catch (ScriptException e) {
        throw new ReportDataFactoryException(
                "DataFactoryScriptingSupport: Failed to execute datafactory init script.", e);
    }
}

From source file:org.jahia.services.workflow.jbpm.custom.email.JBPMMailProducer.java

protected String evaluateExpression(WorkItem workItem, String scriptToExecute, JCRSessionWrapper session)
        throws RepositoryException, ScriptException {
    ScriptContext scriptContext = new SimpleScriptContext();
    if (bindings == null) {
        bindings = getBindings(workItem, session);
    }/*from www .ja  v a 2 s . c  o  m*/
    scriptContext.setWriter(new StringWriter());
    scriptContext.setErrorWriter(new StringWriter());
    scriptContext.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
    scriptContext.setBindings(scriptContext.getBindings(ScriptContext.GLOBAL_SCOPE),
            ScriptContext.GLOBAL_SCOPE);
    scriptEngine.eval(scriptToExecute, scriptContext);
    String error = scriptContext.getErrorWriter().toString();
    if (error.length() > 0) {
        logger.error("Scripting error : " + error);
    }
    return scriptContext.getWriter().toString().trim();
}

From source file:org.wso2.carbon.business.rules.core.util.TemplateManagerHelper.java

/**
 * Runs the script that is given as a string, and gives all the variables specified in the script
 *
 * @param script//from  w ww.j  a  va  2  s . c  o  m
 * @return Map of Strings
 * @throws TemplateManagerHelperException
 */
public static Map<String, String> getScriptGeneratedVariables(String script)
        throws RuleTemplateScriptException {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");

    ScriptContext scriptContext = new SimpleScriptContext();
    scriptContext.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE);

    try {
        // Run script
        engine.eval(script);
        Map<String, Object> returnedScriptContextBindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);

        // Variable names and their values as strings, from the script context binding
        Map<String, String> scriptVariables = new HashMap<>();
        for (Map.Entry scriptVariable : returnedScriptContextBindings.entrySet()) {
            if (scriptVariable.getValue() == null) {
                scriptVariables.put(scriptVariable.getKey().toString(), null);
            } else {
                scriptVariables.put(scriptVariable.getKey().toString(), scriptVariable.getValue().toString());
            }
        }
        return scriptVariables;
    } catch (ScriptException e) {
        throw new RuleTemplateScriptException(e.getCause().getMessage(), e);
    }
}

From source file:org.jahia.ajax.gwt.helper.UIConfigHelper.java

/**
 * Get resources//  w  w w .j a va  2  s .co m
 *
 * @param key
 * @param locale
 * @param site
 * @param jahiaUser
 * @return
 */
private String getResources(String key, Locale locale, JCRSiteNode site, JahiaUser jahiaUser) {
    if (key == null || key.length() == 0) {
        return key;
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Resources key: " + key);
    }
    String baseName = null;
    String value = null;
    if (key.contains("@")) {
        baseName = StringUtils.substringAfter(key, "@");
        key = StringUtils.substringBefore(key, "@");
    }

    value = Messages.get(baseName, site != null ? site.getTemplatePackage() : null, key, locale, null);
    if (value == null || value.length() == 0) {
        value = Messages.getInternal(key, locale);
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Resources value: " + value);
    }
    if (value.contains("${")) {
        try {
            ScriptEngine scriptEngine = scriptEngineUtils.getEngineByName("velocity");
            ScriptContext scriptContext = new SimpleScriptContext();
            final Bindings bindings = new SimpleBindings();
            bindings.put("currentSite", site);
            bindings.put("currentUser", jahiaUser);
            bindings.put("currentLocale", locale);
            bindings.put("PrincipalViewHelper", PrincipalViewHelper.class);
            scriptContext.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
            scriptContext.setBindings(scriptEngine.getContext().getBindings(ScriptContext.GLOBAL_SCOPE),
                    ScriptContext.GLOBAL_SCOPE);
            scriptContext.setWriter(new StringWriter());
            scriptContext.setErrorWriter(new StringWriter());
            scriptEngine.eval(value, scriptContext);
            //String error = scriptContext.getErrorWriter().toString();
            return scriptContext.getWriter().toString().trim();
        } catch (ScriptException e) {
            logger.error("Error while executing script [" + value + "]", e);
        }
    }
    return value;
}