Search in sources :

Example 91 with Scriptable

use of org.mozilla.javascript.Scriptable in project backstage by zepheira.

the class Context method generateLens.

public Scriptable generateLens(String itemID) {
    _logger.debug("> generateLens");
    String key = "lens-rendering:" + itemID;
    _logger.debug("itemID: " + key);
    Scriptable result = (Scriptable) getDatabase().cacheAndRun(key, new LensRenderingCacheableQuery(itemID));
    _logger.debug("< generateLens");
    return result;
}
Also used : Scriptable(org.mozilla.javascript.Scriptable)

Example 92 with Scriptable

use of org.mozilla.javascript.Scriptable in project OpenAM by OpenRock.

the class RhinoScriptEngine method getScope.

/**
     * Builds a Rhino variable scope that includes all of the scopes defined in the given script context as well as
     * the standard Rhino top-level environment. Also binds the variable {@code context} to point to the JSR 223
     * ScriptContext object, as per the JSR 223 spec.
     *
     * @param context the Rhino context to build the scope for.
     * @param scriptContext the JSR 223 script context.
     * @return a Rhino scope containing the given ScriptContext bindings and standard Rhino top-level bindings.
     */
private Scriptable getScope(final Context context, final ScriptContext scriptContext) {
    final Scriptable scope = new ScriptContextScope(scriptContext);
    final ScriptableObject topLevel = context.initStandardObjects();
    scope.setPrototype(topLevel);
    scope.put("context", scope, scriptContext);
    return scope;
}
Also used : ScriptableObject(org.mozilla.javascript.ScriptableObject) Scriptable(org.mozilla.javascript.Scriptable)

Example 93 with Scriptable

use of org.mozilla.javascript.Scriptable in project Activiti by Activiti.

the class SecureJavascriptUtil method evaluateScript.

// exposes beans
public static Object evaluateScript(VariableScope variableScope, String script, Map<Object, Object> beans) {
    Context context = Context.enter();
    try {
        Scriptable scope = context.initStandardObjects();
        SecureScriptScope secureScriptScope = new SecureScriptScope(variableScope, beans);
        scope.setPrototype(secureScriptScope);
        return context.evaluateString(scope, script, "<script>", 0, null);
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) Scriptable(org.mozilla.javascript.Scriptable)

Example 94 with Scriptable

use of org.mozilla.javascript.Scriptable in project jmeter by apache.

the class JavaScript method executeWithRhino.

/**
     * @param previousResult {@link SampleResult}
     * @param currentSampler {@link Sampler}
     * @param jmctx {@link JMeterContext}
     * @param vars {@link JMeterVariables}
     * @param script Javascript code
     * @param varName variable name
     * @return result as String
     * @throws InvalidVariableException
     */
private String executeWithRhino(SampleResult previousResult, Sampler currentSampler, JMeterContext jmctx, JMeterVariables vars, String script, String varName) throws InvalidVariableException {
    Context cx = Context.enter();
    String resultStr = null;
    try {
        Scriptable scope = cx.initStandardObjects(null);
        // Set up some objects for the script to play with
        //$NON-NLS-1$
        scope.put("log", scope, log);
        //$NON-NLS-1$
        scope.put("ctx", scope, jmctx);
        //$NON-NLS-1$
        scope.put("vars", scope, vars);
        //$NON-NLS-1$
        scope.put("props", scope, JMeterUtils.getJMeterProperties());
        // Previously mis-spelt as theadName
        //$NON-NLS-1$
        scope.put("threadName", scope, Thread.currentThread().getName());
        //$NON-NLS-1$
        scope.put("sampler", scope, currentSampler);
        //$NON-NLS-1$
        scope.put("sampleResult", scope, previousResult);
        //$NON-NLS-1$
        Object result = cx.evaluateString(scope, script, "<cmd>", 1, null);
        resultStr = Context.toString(result);
        if (varName != null && vars != null) {
            // vars can be null if run from TestPlan
            vars.put(varName, resultStr);
        }
    } catch (RhinoException e) {
        log.error("Error processing Javascript: [" + script + "]\n", e);
        throw new InvalidVariableException("Error processing Javascript: [" + script + "]", e);
    } finally {
        Context.exit();
    }
    return resultStr;
}
Also used : Context(org.mozilla.javascript.Context) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) JMeterContext(org.apache.jmeter.threads.JMeterContext) RhinoException(org.mozilla.javascript.RhinoException) Scriptable(org.mozilla.javascript.Scriptable)

Example 95 with Scriptable

use of org.mozilla.javascript.Scriptable in project jmeter by apache.

the class BSFJavaScriptEngine method initialize.

/**
     * Initialize the engine.
     * Put the manager into the context-manager
     * map hashtable too.
     */
@Override
public void initialize(BSFManager mgr, String lang, // superclass does not support types
@SuppressWarnings("rawtypes") Vector declaredBeans) throws BSFException {
    super.initialize(mgr, lang, declaredBeans);
    // Initialize context and global scope object
    try {
        Context cx = Context.enter();
        global = new ImporterTopLevel(cx);
        Scriptable bsf = Context.toObject(new BSFFunctions(mgr, this), global);
        global.put("bsf", global, bsf);
        // superclass does not support types
        @SuppressWarnings("unchecked") final Vector<BSFDeclaredBean> beans = declaredBeans;
        for (BSFDeclaredBean declaredBean : beans) {
            declareBean(declaredBean);
        }
    } catch (Throwable t) {
        // NOSONAR We handle correctly Error case in function
        handleError(t);
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) ImporterTopLevel(org.mozilla.javascript.ImporterTopLevel) Scriptable(org.mozilla.javascript.Scriptable) BSFFunctions(org.apache.bsf.util.BSFFunctions) BSFDeclaredBean(org.apache.bsf.BSFDeclaredBean)

Aggregations

Scriptable (org.mozilla.javascript.Scriptable)106 Context (org.mozilla.javascript.Context)49 ScriptableObject (org.mozilla.javascript.ScriptableObject)30 ContextAction (org.mozilla.javascript.ContextAction)12 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)7 NativeObject (org.mozilla.javascript.NativeObject)7 Function (org.mozilla.javascript.Function)6 Map (java.util.Map)5 Notifier (org.apache.cxf.javascript.JavascriptTestUtilities.Notifier)5 Test (org.junit.Test)5 InputStreamReader (java.io.InputStreamReader)4 RhinoException (org.mozilla.javascript.RhinoException)4 Wrapper (org.mozilla.javascript.Wrapper)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 IdentityHashMap (java.util.IdentityHashMap)3 ScriptContext (javax.script.ScriptContext)3 Callable (org.mozilla.javascript.Callable)3 ContextFactory (org.mozilla.javascript.ContextFactory)3 NativeJavaClass (org.mozilla.javascript.NativeJavaClass)3