當前位置: 首頁>>代碼示例>>Java>>正文


Java ScriptContext.setAttribute方法代碼示例

本文整理匯總了Java中javax.script.ScriptContext.setAttribute方法的典型用法代碼示例。如果您正苦於以下問題:Java ScriptContext.setAttribute方法的具體用法?Java ScriptContext.setAttribute怎麽用?Java ScriptContext.setAttribute使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在javax.script.ScriptContext的用法示例。


在下文中一共展示了ScriptContext.setAttribute方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: processInterceptItem

import javax.script.ScriptContext; //導入方法依賴的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

示例2: invokeFunctionWithCustomScriptContextTest

import javax.script.ScriptContext; //導入方法依賴的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

示例3: applyVars

import javax.script.ScriptContext; //導入方法依賴的package包/類
protected Map<String, Object> applyVars ( final ScriptContext context, final Map<String, Object> scriptObjects )
{
    if ( scriptObjects == null || scriptObjects.isEmpty () )
    {
        return null;
    }

    final Map<String, Object> replaced = new HashMap<String, Object> ();
    for ( final Map.Entry<String, Object> entry : scriptObjects.entrySet () )
    {
        final Object original = context.getAttribute ( entry.getKey (), ScriptContext.ENGINE_SCOPE );
        replaced.put ( entry.getKey (), original );
        context.setAttribute ( entry.getKey (), entry.getValue (), ScriptContext.ENGINE_SCOPE );
    }
    return replaced;
}
 
開發者ID:eclipse,項目名稱:neoscada,代碼行數:17,代碼來源:ScriptExecutor.java

示例4: restoreVars

import javax.script.ScriptContext; //導入方法依賴的package包/類
protected void restoreVars ( final ScriptContext context, final Map<String, Object> vars )
{
    if ( vars == null )
    {
        return;
    }

    for ( final Map.Entry<String, Object> entry : vars.entrySet () )
    {
        if ( entry.getValue () == null )
        {
            context.removeAttribute ( entry.getKey (), ScriptContext.ENGINE_SCOPE );
        }
        else
        {
            context.setAttribute ( entry.getKey (), entry.getValue (), ScriptContext.ENGINE_SCOPE );
        }
    }
}
 
開發者ID:eclipse,項目名稱:neoscada,代碼行數:20,代碼來源:ScriptExecutor.java

示例5: processInterceptItem

import javax.script.ScriptContext; //導入方法依賴的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: handleEvent

import javax.script.ScriptContext; //導入方法依賴的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

示例7: evalFileNameWithSpecialCharsTest

import javax.script.ScriptContext; //導入方法依賴的package包/類
@Test
public void evalFileNameWithSpecialCharsTest() throws ScriptException {
    final NashornScriptEngineFactory fac = new NashornScriptEngineFactory();
    final ScriptEngine engine = fac.getScriptEngine(new String[] { "--verify-code=true" });
    final ScriptContext ctxt = new SimpleScriptContext();
    // use file name with "dangerous" chars.
    ctxt.setAttribute(ScriptEngine.FILENAME, "<myscript>", ScriptContext.ENGINE_SCOPE);
    engine.eval("var a = 3;");
    ctxt.setAttribute(ScriptEngine.FILENAME, "[myscript]", ScriptContext.ENGINE_SCOPE);
    engine.eval("var h = 'hello';");
    ctxt.setAttribute(ScriptEngine.FILENAME, ";/\\$.", ScriptContext.ENGINE_SCOPE);
    engine.eval("var foo = 'world';");
    // name used by jjs shell tool for the interactive mode
    ctxt.setAttribute(ScriptEngine.FILENAME, "<shell>", ScriptContext.ENGINE_SCOPE);
    engine.eval("var foo = 'world';");
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:17,代碼來源:TrustedScriptEngineTest.java

示例8: executeScript

import javax.script.ScriptContext; //導入方法依賴的package包/類
/**
 * @native ts
 * var print = console.log;
 * var println = console.log;
 * var ctx = context;
 * return eval(script);
 */
private static boolean executeScript(String script, TaskContext context) {
    ScriptContext scriptCtx = new SimpleScriptContext();
    scriptCtx.setAttribute("ctx", context, ScriptContext.ENGINE_SCOPE);
    try {
        return (boolean) TaskHelper.SCRIPT_ENGINE.eval(script, scriptCtx);
    } catch (ScriptException | ClassCastException e) {
        e.printStackTrace();
        return false;
    }
}
 
開發者ID:datathings,項目名稱:greycat,代碼行數:18,代碼來源:CoreTask.java

示例9: callScript

import javax.script.ScriptContext; //導入方法依賴的package包/類
/**
 * @native ts
 * var print = console.log;
 * return eval(this._script);
 */
private boolean callScript(Node node, TaskContext context) {
    ScriptContext scriptCtx = new SimpleScriptContext();
    scriptCtx.setAttribute("node", node, ScriptContext.ENGINE_SCOPE);
    scriptCtx.setAttribute("context", context, ScriptContext.ENGINE_SCOPE);
    try {
        return (boolean) TaskHelper.SCRIPT_ENGINE.eval(_script, scriptCtx);
    } catch (ScriptException | ClassCastException e) {
        throw new RuntimeException(e);
    }
}
 
開發者ID:datathings,項目名稱:greycat,代碼行數:16,代碼來源:ActionSelect.java

示例10: runScript

import javax.script.ScriptContext; //導入方法依賴的package包/類
public static void runScript(Network network, Reader reader, Writer out) {
    ScriptEngineManager mgr = new ScriptEngineManager();
    ScriptEngine jsEngine = mgr.getEngineByName("js");
    try {
        ScriptContext context = new SimpleScriptContext();
        context.setAttribute("network", network, ScriptContext.ENGINE_SCOPE);
        if (out != null) {
            context.setWriter(out);
        }
        jsEngine.eval(reader, context);
    } catch (ScriptException e) {
        throw new PowsyblException(e);
    }
}
 
開發者ID:powsybl,項目名稱:powsybl-core,代碼行數:15,代碼來源:Networks.java

示例11: handleEval

import javax.script.ScriptContext; //導入方法依賴的package包/類
protected Double handleEval ( final long timestamp )
{
    if ( this.scriptExecutor == null )
    {
        return null;
    }

    final ScriptContext context = new SimpleScriptContext ();
    context.setAttribute ( "timestamp", timestamp, ScriptContext.GLOBAL_SCOPE ); //$NON-NLS-1$
    try
    {
        final Map<String, Object> var = new HashMap<String, Object> ( 1 );
        var.put ( "timestamp", timestamp ); //$NON-NLS-1$
        final Object result = this.scriptExecutor.execute ( context, var );
        if ( result == null )
        {
            return null;
        }
        if ( result instanceof Number )
        {
            return ( (Number)result ).doubleValue ();
        }
        else
        {
            return Double.parseDouble ( result.toString () );
        }
    }
    catch ( final Exception e )
    {
        return null;
    }
}
 
開發者ID:eclipse,項目名稱:neoscada,代碼行數:33,代碼來源:ScriptInput.java

示例12: main

import javax.script.ScriptContext; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
    ScriptEngineManager m = new ScriptEngineManager();
    ScriptContext ctx = new MyContext();
    ctx.setAttribute("x", "hello", MyContext.APP_SCOPE);
    ScriptEngine e = Helper.getJsEngine(m);
    if (e == null) {
        System.out.println("Warning: No js engine found; test vacuously passes.");
        return;
    }
    // the following reference to 'x' throws exception
    // if APP_SCOPE is not searched.
    e.eval("x", ctx);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:14,代碼來源:PluggableContextTest.java

示例13: testGlobalScopeSearch

import javax.script.ScriptContext; //導入方法依賴的package包/類
@Test
public void testGlobalScopeSearch() throws Exception {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    final ScriptContext c = e.getContext();
    c.setAttribute("name1234", "value", ScriptContext.GLOBAL_SCOPE);
    assertEquals(c.getAttribute("name1234"), "value");
    assertEquals(c.getAttributesScope("name1234"),
        ScriptContext.GLOBAL_SCOPE);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:11,代碼來源:ScopeTest.java

示例14: compileAndEvalInDiffContextTest

import javax.script.ScriptContext; //導入方法依賴的package包/類
@Test
public void compileAndEvalInDiffContextTest() throws ScriptException {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine engine = m.getEngineByName("js");
    final Compilable compilable = (Compilable) engine;
    final CompiledScript compiledScript = compilable.compile("foo");
    final ScriptContext ctxt = new SimpleScriptContext();
    ctxt.setAttribute("foo", "hello", ScriptContext.ENGINE_SCOPE);
    assertEquals(compiledScript.eval(ctxt), "hello");
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:11,代碼來源:ScriptEngineTest.java

示例15: init

import javax.script.ScriptContext; //導入方法依賴的package包/類
@Override
public void init ( final Composite parent, final Map<String, String> properties )
{
    try
    {
        final ScriptEngineManager engineManager = Scripts.createManager ( DetailViewImpl.class.getClassLoader () );
        final ScriptContext scriptContext = new SimpleScriptContext ();
        scriptContext.setBindings ( new SimpleBindings (), ScriptContext.GLOBAL_SCOPE );
        scriptContext.setAttribute ( "properties", properties, ScriptContext.GLOBAL_SCOPE ); //$NON-NLS-1$

        this.visibleFactory = new VisibilityProviderFactory ( engineManager, scriptContext, properties );

        load ();

        // load script modules
        for ( final ScriptModule module : this.scriptModules )
        {
            loadScriptModule ( engineManager, scriptContext, module );
        }

        final Composite box = new Composite ( parent, SWT.NONE );
        box.setLayout ( new GridLayout ( 1, false ) );

        final List<IObservableSet> lists = new LinkedList<IObservableSet> ();

        final WritableSet hiddenItems = new WritableSet ();
        lists.add ( hiddenItems );

        // add hidden
        for ( final HiddenComponent hidden : this.hiddenItems )
        {
            final String item = DetailComponentImpl.resolve ( hidden.getDescriptor (), properties );
            hiddenItems.add ( DataItemDescriptor.create ( item ) );
        }

        // init
        if ( this.header != null )
        {
            final Composite wrapper = new Composite ( box, SWT.NONE );
            wrapper.setLayout ( new FillLayout () );
            wrapper.setLayoutData ( new GridData ( SWT.FILL, SWT.FILL, true, false ) );
            this.header.init ( this.visibleFactory, wrapper, properties );
            lists.add ( this.header.getDescriptors () );
            this.header.start ();
        }

        final TabProvider tabProvider = createTabProvider ( box );

        int i = 0;
        for ( final GroupTab group : this.groups )
        {

            final Folder folder = tabProvider.createFolder ( i, group.getLabel () );

            group.init ( this.visibleFactory, folder.getContainer (), properties );

            final VisibilityProvider provider = this.visibleFactory.createProvider ( group.getVisibility () );
            final TabVisibleComponent component = new TabVisibleComponent ( folder, i, group, folder.getContainer () );

            folder.setSummaryProvider ( component.getSummaryProvider () );

            final ComponentVisibility visibility = new ComponentVisibility ( provider, component );
            addVisibility ( visibility );

            lists.add ( visibility.getDescriptors () );
            i++;
        }

        // hook up realtime list to observable list
        this.realTimeTab.setInput ( new UnionSet ( lists.toArray ( new IObservableSet[lists.size ()] ) ) );

    }
    catch ( final Exception e )
    {
        logger.warn ( "Failed to load view", e ); //$NON-NLS-1$
        throw new RuntimeException ( e );
    }
}
 
開發者ID:eclipse,項目名稱:neoscada,代碼行數:79,代碼來源:DetailViewImpl.java


注:本文中的javax.script.ScriptContext.setAttribute方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。