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