当前位置: 首页>>代码示例>>Java>>正文


Java SimpleScriptContext类代码示例

本文整理汇总了Java中javax.script.SimpleScriptContext的典型用法代码示例。如果您正苦于以下问题:Java SimpleScriptContext类的具体用法?Java SimpleScriptContext怎么用?Java SimpleScriptContext使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


SimpleScriptContext类属于javax.script包,在下文中一共展示了SimpleScriptContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: invokeFunctionWithCustomScriptContextTest

import javax.script.SimpleScriptContext; //导入依赖的package包/类
@Test
public void invokeFunctionWithCustomScriptContextTest() throws Exception {
    final ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");

    // create an engine and a ScriptContext, but don't set it as default
    final ScriptContext scriptContext = new SimpleScriptContext();

    // Set some value in the context
    scriptContext.setAttribute("myString", "foo", ScriptContext.ENGINE_SCOPE);

    // Evaluate script with custom context and get back a function
    final String script = "function (c) { return myString.indexOf(c); }";
    final CompiledScript compiledScript = ((Compilable)engine).compile(script);
    final Object func = compiledScript.eval(scriptContext);

    // Invoked function should be able to see context it was evaluated with
    final Object result = ((Invocable) engine).invokeMethod(func, "call", func, "o", null);
    assertTrue(((Number)result).intValue() == 1);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:20,代码来源:ScopeTest.java

示例2: processInterceptItem

import javax.script.SimpleScriptContext; //导入依赖的package包/类
@Override
protected boolean processInterceptItem ( final Item item, final ItemInterceptor interceptorElement, final MasterContext masterContext, final Properties properties )
{
    final ExporterItemInterceptor interceptor = (ExporterItemInterceptor)interceptorElement;

    final Script script = interceptor.getScript ();

    final ScriptEngineManager manager = new ScriptEngineManager ();
    try
    {
        final ScriptExecutor executor = new ScriptExecutor ( manager, script.getLanguage (), script.getSource (), null );
        final ScriptContext context = new SimpleScriptContext ();
        final IEC60870Processor modbus = new IEC60870Processor ( this, interceptor, masterContext, item );
        context.setAttribute ( "IEC60870", modbus, ScriptContext.ENGINE_SCOPE );
        context.setAttribute ( "item", item, ScriptContext.ENGINE_SCOPE );
        context.setAttribute ( "properties", properties, ScriptContext.ENGINE_SCOPE );
        executor.execute ( context );
    }
    catch ( final Exception e )
    {
        throw new RuntimeException ( "Failed to process script", e );
    }
    return true;
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:25,代码来源:ExporterInterceptorHandler.java

示例3: customize

import javax.script.SimpleScriptContext; //导入依赖的package包/类
/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 *
 * @generated NOT
 */
@Override
public void customize ( final CustomizationRequest request )
{
    // FIXME: we should somehow take this out of here
    try
    {
        if ( this.executor == null )
        {
            final String resource = eResource ().getURI ().toString ();
            this.executor = new ScriptExecutor ( getScriptEngine (), this.code, ScriptCustomizationPipelineImpl.class.getClassLoader (), resource );
        }

        final SimpleScriptContext ctx = new SimpleScriptContext ();
        ctx.setAttribute ( "request", request, ScriptContext.ENGINE_SCOPE );

        this.executor.execute ( ctx );
    }
    catch ( final Exception e )
    {
        throw new RuntimeException ( e );
    }
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:29,代码来源:ScriptCustomizationPipelineImpl.java

示例4: selected

import javax.script.SimpleScriptContext; //导入依赖的package包/类
/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 *
 * @generated NOT
 */
@Override
public Boolean selected ( final CustomizationRequest request )
{
    try
    {
        if ( this.executor == null )
        {
            this.executor = new ScriptExecutor ( getScriptEngine (), this.code, ScriptCustomizationPipelineImpl.class.getClassLoader () );
        }

        final SimpleScriptContext ctx = new SimpleScriptContext ();
        ctx.setAttribute ( "request", request, ScriptContext.ENGINE_SCOPE );

        return (Boolean)this.executor.execute ( ctx );
    }
    catch ( final Exception e )
    {
        throw new RuntimeException ( e );
    }
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:27,代码来源:ScriptSelectorImpl.java

示例5: processInterceptItem

import javax.script.SimpleScriptContext; //导入依赖的package包/类
@Override
protected boolean processInterceptItem ( final Item item, final ItemInterceptor interceptorElement, final MasterContext masterContext, final Properties properties )
{
    final ModbusExporterInterceptor interceptor = (ModbusExporterInterceptor)interceptorElement;

    final Script script = interceptor.getScript ();

    final ScriptEngineManager manager = new ScriptEngineManager ();
    try
    {
        final ScriptExecutor executor = new ScriptExecutor ( manager, script.getLanguage (), script.getSource (), null );
        final ScriptContext context = new SimpleScriptContext ();
        final ModbusProcessor modbus = new ModbusProcessor ( this, interceptor, masterContext, item );
        context.setAttribute ( "MODBUS", modbus, ScriptContext.ENGINE_SCOPE );
        context.setAttribute ( "item", item, ScriptContext.ENGINE_SCOPE );
        context.setAttribute ( "properties", properties, ScriptContext.ENGINE_SCOPE );
        executor.execute ( context );
    }
    catch ( final Exception e )
    {
        throw new RuntimeException ( "Failed to process script", e );
    }
    return true;
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:25,代码来源:ModbusExporterInterceptorHandler.java

示例6: evalName

import javax.script.SimpleScriptContext; //导入依赖的package包/类
public static String evalName ( final CompiledScript script, final ExternalValue v ) throws Exception
{
    final SimpleScriptContext ctx = new SimpleScriptContext ();

    ctx.setAttribute ( "item", v, ScriptContext.ENGINE_SCOPE ); //$NON-NLS-1$

    final Object result = Scripts.executeWithClassLoader ( Activator.class.getClassLoader (), new Callable<Object> () {

        @Override
        public Object call () throws Exception
        {
            return script.eval ( ctx );
        }
    } );

    if ( result == null )
    {
        return null;
    }

    return result.toString ();
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:23,代码来源:SetExternalNameWizard.java

示例7: handleEvent

import javax.script.SimpleScriptContext; //导入依赖的package包/类
@Override
public Event handleEvent ( final Event event, final InjectionContext context )
{
    final ScriptContext scriptContext = new SimpleScriptContext ();
    try
    {
        scriptContext.setAttribute ( "event", event, ScriptContext.GLOBAL_SCOPE );
        scriptContext.setAttribute ( "logger", logger, ScriptContext.GLOBAL_SCOPE );
        scriptContext.setAttribute ( "injector", this.injector, ScriptContext.GLOBAL_SCOPE );

        final Object result = this.script.execute ( scriptContext );
        final Event resultEvent = convert ( result, event );
        logger.debug ( "Result: {}", resultEvent );
        return resultEvent;
    }
    catch ( final Exception e )
    {
        return event;
    }
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:21,代码来源:ScriptEventHandler.java

示例8: setScript

import javax.script.SimpleScriptContext; //导入依赖的package包/类
private synchronized void setScript ( final ConfigurationDataHelper cfg ) throws Exception
{
    String engineName = cfg.getString ( "scriptEngine", DEFAULT_ENGINE_NAME ); //$NON-NLS-1$
    if ( "".equals ( engineName ) ) // catches null
    {
        engineName = DEFAULT_ENGINE_NAME;
    }

    final ScriptEngine engine = Scripts.createEngine ( engineName, this.classLoader );
    this.scriptContext = new SimpleScriptContext ();

    // trigger init script
    final String initScript = cfg.getString ( "init" ); //$NON-NLS-1$
    if ( initScript != null )
    {
        new ScriptExecutor ( engine, initScript, this.classLoader ).execute ( this.scriptContext );
    }

    this.updateCommand = makeScript ( engine, cfg.getString ( "updateCommand" ) ); //$NON-NLS-1$
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:21,代码来源:ScriptMonitor.java

示例9: postProcess

import javax.script.SimpleScriptContext; //导入依赖的package包/类
private void postProcess ( final AuthorizationContext context, final Result result ) throws Exception
{
    if ( this.postProcessor == null )
    {
        return;
    }

    logger.debug ( "Running post processor" );

    final ScriptContext scriptContext = new SimpleScriptContext ();

    final Map<String, Object> scriptObjects = new HashMap<String, Object> ();
    scriptObjects.put ( "authorizationContext", context );
    scriptObjects.put ( "authenticator", this.authenticator );

    this.postProcessor.execute ( scriptContext, scriptObjects );
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:18,代码来源:RequestSignatureRuleImpl.java

示例10: main

import javax.script.SimpleScriptContext; //导入依赖的package包/类
public static void main(final String[] args) throws Exception {
    final ScriptEngineManager manager = new ScriptEngineManager();
    final ScriptEngine engine = manager.getEngineByName("nashorn");

    engine.put("x", "hello");
    // print global variable "x"
    engine.eval("print(x);");
    // the above line prints "hello"

    // Now, pass a different script context
    final ScriptContext newContext = new SimpleScriptContext();
    newContext.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE);
    final Bindings engineScope = newContext.getBindings(ScriptContext.ENGINE_SCOPE);

    // add new variable "x" to the new engineScope
    engineScope.put("x", "world");

    // execute the same script - but this time pass a different script context
    engine.eval("print(x);", newContext);
    // the above line prints "world"
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:22,代码来源:MultiScopes.java

示例11: userEngineScopeBindingsRetentionTest

import javax.script.SimpleScriptContext; //导入依赖的package包/类
@Test
public void userEngineScopeBindingsRetentionTest() throws ScriptException {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    final ScriptContext newContext = new SimpleScriptContext();
    newContext.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE);
    e.eval("function foo() {}", newContext);

    // definition retained with user's ENGINE_SCOPE Binding
    assertTrue(e.eval("typeof foo", newContext).equals("function"));

    final Bindings oldBindings = newContext.getBindings(ScriptContext.ENGINE_SCOPE);
    // but not in another ENGINE_SCOPE binding
    newContext.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE);
    assertTrue(e.eval("typeof foo", newContext).equals("undefined"));

    // restore ENGINE_SCOPE and check again
    newContext.setBindings(oldBindings, ScriptContext.ENGINE_SCOPE);
    assertTrue(e.eval("typeof foo", newContext).equals("function"));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:21,代码来源:ScopeTest.java

示例12: multiThreadedPrimitiveTest

import javax.script.SimpleScriptContext; //导入依赖的package包/类
/**
 * Test multi-threaded access to primitive prototype properties for shared script classes with multiple globals.
 */
@Test
public static void multiThreadedPrimitiveTest() throws ScriptException, InterruptedException {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    final Bindings b = e.createBindings();
    final ScriptContext origContext = e.getContext();
    final ScriptContext newCtxt = new SimpleScriptContext();
    newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE);

    final Object obj1 = e.eval("String.prototype.foo = 'original context';", origContext);
    final Object obj2 = e.eval("String.prototype.foo = 'new context';", newCtxt);
    assertEquals(obj1, "original context");
    assertEquals(obj2, "new context");
    final String sharedScript = "''.foo";

    final Thread t1 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000));
    final Thread t2 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "new context", 1000));
    t1.start();
    t2.start();
    t1.join();
    t2.join();

    final Object obj3 = e.eval("delete String.prototype.foo; Object.prototype.foo = 'newer context';", newCtxt);
    assertEquals(obj3, "newer context");
    final Thread t3 = new Thread(new ScriptRunner(e, origContext, sharedScript, "original context", 1000));
    final Thread t4 = new Thread(new ScriptRunner(e, newCtxt, sharedScript, "newer context", 1000));

    t3.start();
    t4.start();
    t3.join();
    t4.join();

    Assert.assertEquals(e.eval(sharedScript), "original context");
    Assert.assertEquals(e.eval(sharedScript, newCtxt), "newer context");
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:39,代码来源:ScopeTest.java

示例13: testSlowScope

import javax.script.SimpleScriptContext; //导入依赖的package包/类
/**
 * Test "slow" scopes involving {@code with} and {@code eval} statements for shared script classes with multiple globals.
 * @throws ScriptException
 * @throws InterruptedException
 */
@Test
public static void testSlowScope() throws ScriptException, InterruptedException {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");

    for (int i = 0; i < 100; i++) {
        final Bindings b = e.createBindings();
        final ScriptContext ctxt = new SimpleScriptContext();
        ctxt.setBindings(b, ScriptContext.ENGINE_SCOPE);

        e.eval(new URLReader(ScopeTest.class.getResource("resources/witheval.js")), ctxt);
        assertEquals(e.eval("a", ctxt), 1);
        assertEquals(b.get("a"), 1);
        assertEquals(e.eval("b", ctxt), 3);
        assertEquals(b.get("b"), 3);
        assertEquals(e.eval("c", ctxt), 10);
        assertEquals(b.get("c"), 10);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:25,代码来源:ScopeTest.java

示例14: invokeMethodDifferentContextTest

import javax.script.SimpleScriptContext; //导入依赖的package包/类
@Test
/**
 * Check that we can call invokeMethod on an object that we got by
 * evaluating script with different Context set.
 */
public void invokeMethodDifferentContextTest() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");

    try {
        // define an object with method on it
        final Object obj = e.eval("({ hello: function() { return 'Hello World!'; } })");

        final ScriptContext ctxt = new SimpleScriptContext();
        ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE);
        e.setContext(ctxt);

        // invoke 'func' on obj - but with current script context changed
        final Object res = ((Invocable) e).invokeMethod(obj, "hello");
        assertEquals(res, "Hello World!");
    } catch (final Exception exp) {
        exp.printStackTrace();
        fail(exp.getMessage());
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:26,代码来源:InvocableTest.java

示例15: getInterfaceDifferentContext

import javax.script.SimpleScriptContext; //导入依赖的package包/类
@Test
/**
 * Check that we can get interface out of a script object even after
 * switching to use different ScriptContext.
 */
public void getInterfaceDifferentContext() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    try {
        final Object obj = e.eval("({ run: function() { } })");

        // change script context
        final ScriptContext ctxt = new SimpleScriptContext();
        ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE);
        e.setContext(ctxt);

        final Runnable r = ((Invocable) e).getInterface(obj, Runnable.class);
        r.run();
    } catch (final Exception exp) {
        exp.printStackTrace();
        fail(exp.getMessage());
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:24,代码来源:InvocableTest.java


注:本文中的javax.script.SimpleScriptContext类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。