本文整理汇总了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);
}
示例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;
}
示例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 );
}
}
示例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 );
}
}
示例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;
}
示例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 ();
}
示例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;
}
}
示例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$
}
示例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 );
}
示例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"
}
示例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"));
}
示例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");
}
示例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);
}
}
示例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());
}
}
示例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());
}
}