Java Code Examples for javax.script.ScriptContext#getAttribute()
The following examples show how to use
javax.script.ScriptContext#getAttribute() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GroovyScriptEngineImpl.java From groovy with Apache License 2.0 | 6 votes |
public Object eval(String script, ScriptContext ctx) throws ScriptException { try { String val = (String) ctx.getAttribute("#jsr223.groovy.engine.keep.globals", ScriptContext.ENGINE_SCOPE); ReferenceBundle bundle = ReferenceBundle.getHardBundle(); if (val != null && val.length() > 0) { if (val.equalsIgnoreCase("soft")) { bundle = ReferenceBundle.getSoftBundle(); } else if (val.equalsIgnoreCase("weak")) { bundle = ReferenceBundle.getWeakBundle(); } else if (val.equalsIgnoreCase("phantom")) { bundle = ReferenceBundle.getPhantomBundle(); } } globalClosures.setBundle(bundle); } catch (ClassCastException cce) { /*ignore.*/ } try { Class<?> clazz = getScriptClass(script, ctx); if (clazz == null) throw new ScriptException("Script class is null"); return eval(clazz, ctx); } catch (Exception e) { if (debug) e.printStackTrace(); throw new ScriptException(e); } }
Example 2
Source File: ScriptExecutor.java From neoscada with Eclipse Public License 1.0 | 6 votes |
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; }
Example 3
Source File: JavaExecutable.java From scheduling with GNU Affero General Public License v3.0 | 5 votes |
/** * Initialization of the dataSpaces.<br> * * @param sc the ScriptContext including as bindings the dataSpaces locations. */ public void initDataSpaces(ScriptContext sc) { this.inputSpace = (String) sc.getAttribute(SchedulerConstants.DS_INPUT_BINDING_NAME); this.outputSpace = (String) sc.getAttribute(SchedulerConstants.DS_OUTPUT_BINDING_NAME); this.globalSpace = (String) sc.getAttribute(SchedulerConstants.DS_GLOBAL_BINDING_NAME); this.userSpace = (String) sc.getAttribute(SchedulerConstants.DS_USER_BINDING_NAME); this.localSpace = (String) sc.getAttribute(SchedulerConstants.DS_SCRATCH_BINDING_NAME); }
Example 4
Source File: ScriptEngineImpl.java From es6draft with MIT License | 5 votes |
private Source createSource(ScriptContext context) { Object fileName = context.getAttribute(FILENAME); if (fileName != null) { String file = fileName.toString(); return new Source(new FileSourceIdentifier(Paths.get(file)), file, 1); } return new Source(new FileSourceIdentifier(Paths.get("")), "<eval>", 1); }
Example 5
Source File: GremlinGroovyScriptEngine.java From tinkerpop with Apache License 2.0 | 5 votes |
private Object callGlobal(final String name, final Object args[], final ScriptContext ctx) { final Closure closure = globalClosures.get(name); if (closure != null) { return closure.call(args); } final Object value = ctx.getAttribute(name); if (value instanceof Closure) { return ((Closure) value).call(args); } else { throw new MissingMethodException(name, getClass(), args); } }
Example 6
Source File: JavaScriptEngine.java From kite with Apache License 2.0 | 5 votes |
private static ClassLoader getParentLoader(ScriptContext ctx) { int scope = ctx.getAttributesScope(PARENTLOADER); if (scope != -1) { Object loader = ctx.getAttribute(PARENTLOADER); if (loader instanceof ClassLoader) { return (ClassLoader) loader; } // else fall through.. } return null; }
Example 7
Source File: GroovyScriptEngineImpl.java From groovy with Apache License 2.0 | 5 votes |
private Object callGlobal(String name, Object[] args, ScriptContext ctx) { Closure<?> closure = globalClosures.get(name); if (closure != null) { return closure.call(args); } else { // Look for closure valued variable in the // given ScriptContext. If available, call it. Object value = ctx.getAttribute(name); if (value instanceof Closure) { return ((Closure) value).call(args); } // else fall thru.. } throw new MissingMethodException(name, getClass(), args); }
Example 8
Source File: JuelScriptEngine.java From activiti6-boot2 with Apache License 2.0 | 4 votes |
private ELContext createElContext(final ScriptContext scriptCtx) { // Check if the ELContext is already stored on the ScriptContext Object existingELCtx = scriptCtx.getAttribute("elcontext"); if (existingELCtx instanceof ELContext) { return (ELContext) existingELCtx; } scriptCtx.setAttribute("context", scriptCtx, ScriptContext.ENGINE_SCOPE); // Built-in function are added to ScriptCtx scriptCtx.setAttribute("out:print", getPrintMethod(), ScriptContext.ENGINE_SCOPE); SecurityManager securityManager = System.getSecurityManager(); if (securityManager == null) { scriptCtx.setAttribute("lang:import", getImportMethod(), ScriptContext.ENGINE_SCOPE); } ELContext elContext = new ELContext() { ELResolver resolver = createElResolver(); VariableMapper varMapper = new ScriptContextVariableMapper(scriptCtx); FunctionMapper funcMapper = new ScriptContextFunctionMapper(scriptCtx); @Override public ELResolver getELResolver() { return resolver; } @Override public VariableMapper getVariableMapper() { return varMapper; } @Override public FunctionMapper getFunctionMapper() { return funcMapper; } }; // Store the elcontext in the scriptContext to be able to reuse scriptCtx.setAttribute("elcontext", elContext, ScriptContext.ENGINE_SCOPE); return elContext; }
Example 9
Source File: ScopeTest.java From jdk8u60 with GNU General Public License v2.0 | 4 votes |
@Test public void multiGlobalTest() { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); final Bindings b = e.createBindings(); final ScriptContext newCtxt = new SimpleScriptContext(); newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); try { final Object obj1 = e.eval("Object"); final Object obj2 = e.eval("Object", newCtxt); Assert.assertNotEquals(obj1, obj2); Assert.assertNotNull(obj1); Assert.assertNotNull(obj2); Assert.assertEquals(obj1.toString(), obj2.toString()); e.eval("x = 'hello'"); e.eval("x = 'world'", newCtxt); Object x1 = e.getContext().getAttribute("x"); Object x2 = newCtxt.getAttribute("x"); Assert.assertNotEquals(x1, x2); Assert.assertEquals(x1, "hello"); Assert.assertEquals(x2, "world"); x1 = e.eval("x"); x2 = e.eval("x", newCtxt); Assert.assertNotEquals(x1, x2); Assert.assertEquals(x1, "hello"); Assert.assertEquals(x2, "world"); final ScriptContext origCtxt = e.getContext(); e.setContext(newCtxt); e.eval("y = new Object()"); e.eval("y = new Object()", origCtxt); final Object y1 = origCtxt.getAttribute("y"); final Object y2 = newCtxt.getAttribute("y"); Assert.assertNotEquals(y1, y2); final Object yeval1 = e.eval("y"); final Object yeval2 = e.eval("y", origCtxt); Assert.assertNotEquals(yeval1, yeval2); Assert.assertEquals("[object Object]", y1.toString()); Assert.assertEquals("[object Object]", y2.toString()); } catch (final ScriptException se) { se.printStackTrace(); fail(se.getMessage()); } }
Example 10
Source File: NashornScriptEngine.java From openjdk-jdk8u with GNU General Public License v2.0 | 4 votes |
private static String getScriptName(final ScriptContext ctxt) { final Object val = ctxt.getAttribute(ScriptEngine.FILENAME); return (val != null) ? val.toString() : "<eval>"; }
Example 11
Source File: JuelScriptEngine.java From flowable-engine with Apache License 2.0 | 4 votes |
private ELContext createElContext(final ScriptContext scriptCtx) { // Check if the ELContext is already stored on the ScriptContext Object existingELCtx = scriptCtx.getAttribute("elcontext"); if (existingELCtx instanceof ELContext) { return (ELContext) existingELCtx; } scriptCtx.setAttribute("context", scriptCtx, ScriptContext.ENGINE_SCOPE); // Built-in function are added to ScriptCtx scriptCtx.setAttribute("out:print", getPrintMethod(), ScriptContext.ENGINE_SCOPE); SecurityManager securityManager = System.getSecurityManager(); if (securityManager == null) { scriptCtx.setAttribute("lang:import", getImportMethod(), ScriptContext.ENGINE_SCOPE); } ELContext elContext = new ELContext() { ELResolver resolver = createElResolver(); VariableMapper varMapper = new ScriptContextVariableMapper(scriptCtx); FunctionMapper funcMapper = new ScriptContextFunctionMapper(scriptCtx); @Override public ELResolver getELResolver() { return resolver; } @Override public VariableMapper getVariableMapper() { return varMapper; } @Override public FunctionMapper getFunctionMapper() { return funcMapper; } }; // Store the elcontext in the scriptContext to be able to reuse scriptCtx.setAttribute("elcontext", elContext, ScriptContext.ENGINE_SCOPE); return elContext; }
Example 12
Source File: ScopeTest.java From hottub with GNU General Public License v2.0 | 4 votes |
@Test public void multiGlobalTest() { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); final Bindings b = e.createBindings(); final ScriptContext newCtxt = new SimpleScriptContext(); newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); try { final Object obj1 = e.eval("Object"); final Object obj2 = e.eval("Object", newCtxt); Assert.assertNotEquals(obj1, obj2); Assert.assertNotNull(obj1); Assert.assertNotNull(obj2); Assert.assertEquals(obj1.toString(), obj2.toString()); e.eval("x = 'hello'"); e.eval("x = 'world'", newCtxt); Object x1 = e.getContext().getAttribute("x"); Object x2 = newCtxt.getAttribute("x"); Assert.assertNotEquals(x1, x2); Assert.assertEquals(x1, "hello"); Assert.assertEquals(x2, "world"); x1 = e.eval("x"); x2 = e.eval("x", newCtxt); Assert.assertNotEquals(x1, x2); Assert.assertEquals(x1, "hello"); Assert.assertEquals(x2, "world"); final ScriptContext origCtxt = e.getContext(); e.setContext(newCtxt); e.eval("y = new Object()"); e.eval("y = new Object()", origCtxt); final Object y1 = origCtxt.getAttribute("y"); final Object y2 = newCtxt.getAttribute("y"); Assert.assertNotEquals(y1, y2); final Object yeval1 = e.eval("y"); final Object yeval2 = e.eval("y", origCtxt); Assert.assertNotEquals(yeval1, yeval2); Assert.assertEquals("[object Object]", y1.toString()); Assert.assertEquals("[object Object]", y2.toString()); } catch (final ScriptException se) { se.printStackTrace(); fail(se.getMessage()); } }
Example 13
Source File: ScopeTest.java From openjdk-8-source with GNU General Public License v2.0 | 4 votes |
@Test public void multiGlobalTest() { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); final Bindings b = e.createBindings(); final ScriptContext newCtxt = new SimpleScriptContext(); newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); try { Object obj1 = e.eval("Object"); Object obj2 = e.eval("Object", newCtxt); Assert.assertNotEquals(obj1, obj2); Assert.assertNotNull(obj1); Assert.assertNotNull(obj2); Assert.assertEquals(obj1.toString(), obj2.toString()); e.eval("x = 'hello'"); e.eval("x = 'world'", newCtxt); Object x1 = e.getContext().getAttribute("x"); Object x2 = newCtxt.getAttribute("x"); Assert.assertNotEquals(x1, x2); Assert.assertEquals(x1, "hello"); Assert.assertEquals(x2, "world"); x1 = e.eval("x"); x2 = e.eval("x", newCtxt); Assert.assertNotEquals(x1, x2); Assert.assertEquals(x1, "hello"); Assert.assertEquals(x2, "world"); final ScriptContext origCtxt = e.getContext(); e.setContext(newCtxt); e.eval("y = new Object()"); e.eval("y = new Object()", origCtxt); Object y1 = origCtxt.getAttribute("y"); Object y2 = newCtxt.getAttribute("y"); Assert.assertNotEquals(y1, y2); Assert.assertNotEquals(e.eval("y"), e.eval("y", origCtxt)); Assert.assertEquals("[object Object]", y1.toString()); Assert.assertEquals("[object Object]", y2.toString()); } catch (final ScriptException se) { se.printStackTrace(); fail(se.getMessage()); } }
Example 14
Source File: ScopeTest.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 4 votes |
@Test public void multiGlobalTest() { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); final Bindings b = e.createBindings(); final ScriptContext newCtxt = new SimpleScriptContext(); newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); try { final Object obj1 = e.eval("Object"); final Object obj2 = e.eval("Object", newCtxt); Assert.assertNotEquals(obj1, obj2); Assert.assertNotNull(obj1); Assert.assertNotNull(obj2); Assert.assertEquals(obj1.toString(), obj2.toString()); e.eval("x = 'hello'"); e.eval("x = 'world'", newCtxt); Object x1 = e.getContext().getAttribute("x"); Object x2 = newCtxt.getAttribute("x"); Assert.assertNotEquals(x1, x2); Assert.assertEquals(x1, "hello"); Assert.assertEquals(x2, "world"); x1 = e.eval("x"); x2 = e.eval("x", newCtxt); Assert.assertNotEquals(x1, x2); Assert.assertEquals(x1, "hello"); Assert.assertEquals(x2, "world"); final ScriptContext origCtxt = e.getContext(); e.setContext(newCtxt); e.eval("y = new Object()"); e.eval("y = new Object()", origCtxt); final Object y1 = origCtxt.getAttribute("y"); final Object y2 = newCtxt.getAttribute("y"); Assert.assertNotEquals(y1, y2); final Object yeval1 = e.eval("y"); final Object yeval2 = e.eval("y", origCtxt); Assert.assertNotEquals(yeval1, yeval2); Assert.assertEquals("[object Object]", y1.toString()); Assert.assertEquals("[object Object]", y2.toString()); } catch (final ScriptException se) { se.printStackTrace(); fail(se.getMessage()); } }
Example 15
Source File: ScopeTest.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 4 votes |
@Test public void testScriptContext_NPE_IAE() throws Exception { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); final ScriptContext c = e.getContext(); try { c.getAttribute(""); throw new AssertionError("should have thrown IAE"); } catch (IllegalArgumentException iae1) {} try { c.getAttribute(null); throw new AssertionError("should have thrown NPE"); } catch (NullPointerException npe1) {} try { c.getAttribute("", ScriptContext.ENGINE_SCOPE); throw new AssertionError("should have thrown IAE"); } catch (IllegalArgumentException iae2) {} try { c.getAttribute(null, ScriptContext.ENGINE_SCOPE); throw new AssertionError("should have thrown NPE"); } catch (NullPointerException npe2) {} try { c.removeAttribute("", ScriptContext.ENGINE_SCOPE); throw new AssertionError("should have thrown IAE"); } catch (IllegalArgumentException iae3) {} try { c.removeAttribute(null, ScriptContext.ENGINE_SCOPE); throw new AssertionError("should have thrown NPE"); } catch (NullPointerException npe3) {} try { c.setAttribute("", "value", ScriptContext.ENGINE_SCOPE); throw new AssertionError("should have thrown IAE"); } catch (IllegalArgumentException iae4) {} try { c.setAttribute(null, "value", ScriptContext.ENGINE_SCOPE); throw new AssertionError("should have thrown NPE"); } catch (NullPointerException npe4) {} try { c.getAttributesScope(""); throw new AssertionError("should have thrown IAE"); } catch (IllegalArgumentException iae5) {} try { c.getAttributesScope(null); throw new AssertionError("should have thrown NPE"); } catch (NullPointerException npe5) {} }
Example 16
Source File: VelocityScriptEngine.java From velocity-engine with Apache License 2.0 | 4 votes |
protected static String getFilename(ScriptContext ctx) { Object fileName = ctx.getAttribute(ScriptEngine.FILENAME); return fileName != null? fileName.toString() : "<unknown>"; }
Example 17
Source File: ScopeTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
@Test public void multiGlobalTest() { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); final Bindings b = e.createBindings(); final ScriptContext newCtxt = new SimpleScriptContext(); newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE); try { final Object obj1 = e.eval("Object"); final Object obj2 = e.eval("Object", newCtxt); Assert.assertNotEquals(obj1, obj2); Assert.assertNotNull(obj1); Assert.assertNotNull(obj2); Assert.assertEquals(obj1.toString(), obj2.toString()); e.eval("x = 'hello'"); e.eval("x = 'world'", newCtxt); Object x1 = e.getContext().getAttribute("x"); Object x2 = newCtxt.getAttribute("x"); Assert.assertNotEquals(x1, x2); Assert.assertEquals(x1, "hello"); Assert.assertEquals(x2, "world"); x1 = e.eval("x"); x2 = e.eval("x", newCtxt); Assert.assertNotEquals(x1, x2); Assert.assertEquals(x1, "hello"); Assert.assertEquals(x2, "world"); final ScriptContext origCtxt = e.getContext(); e.setContext(newCtxt); e.eval("y = new Object()"); e.eval("y = new Object()", origCtxt); final Object y1 = origCtxt.getAttribute("y"); final Object y2 = newCtxt.getAttribute("y"); Assert.assertNotEquals(y1, y2); final Object yeval1 = e.eval("y"); final Object yeval2 = e.eval("y", origCtxt); Assert.assertNotEquals(yeval1, yeval2); Assert.assertEquals("[object Object]", y1.toString()); Assert.assertEquals("[object Object]", y2.toString()); } catch (final ScriptException se) { se.printStackTrace(); fail(se.getMessage()); } }
Example 18
Source File: ScopeTest.java From TencentKona-8 with GNU General Public License v2.0 | 4 votes |
@Test public void testScriptContext_NPE_IAE() throws Exception { final ScriptEngineManager m = new ScriptEngineManager(); final ScriptEngine e = m.getEngineByName("nashorn"); final ScriptContext c = e.getContext(); try { c.getAttribute(""); throw new AssertionError("should have thrown IAE"); } catch (IllegalArgumentException iae1) {} try { c.getAttribute(null); throw new AssertionError("should have thrown NPE"); } catch (NullPointerException npe1) {} try { c.getAttribute("", ScriptContext.ENGINE_SCOPE); throw new AssertionError("should have thrown IAE"); } catch (IllegalArgumentException iae2) {} try { c.getAttribute(null, ScriptContext.ENGINE_SCOPE); throw new AssertionError("should have thrown NPE"); } catch (NullPointerException npe2) {} try { c.removeAttribute("", ScriptContext.ENGINE_SCOPE); throw new AssertionError("should have thrown IAE"); } catch (IllegalArgumentException iae3) {} try { c.removeAttribute(null, ScriptContext.ENGINE_SCOPE); throw new AssertionError("should have thrown NPE"); } catch (NullPointerException npe3) {} try { c.setAttribute("", "value", ScriptContext.ENGINE_SCOPE); throw new AssertionError("should have thrown IAE"); } catch (IllegalArgumentException iae4) {} try { c.setAttribute(null, "value", ScriptContext.ENGINE_SCOPE); throw new AssertionError("should have thrown NPE"); } catch (NullPointerException npe4) {} try { c.getAttributesScope(""); throw new AssertionError("should have thrown IAE"); } catch (IllegalArgumentException iae5) {} try { c.getAttributesScope(null); throw new AssertionError("should have thrown NPE"); } catch (NullPointerException npe5) {} }
Example 19
Source File: JavaClassScriptEngine.java From scheduling with GNU Affero General Public License v3.0 | 4 votes |
@Override public Object eval(String userExecutableClassName, ScriptContext context) throws ScriptException { try { JavaExecutable javaExecutable = getExecutable(userExecutableClassName); JavaStandaloneExecutableInitializer execInitializer = new JavaStandaloneExecutableInitializer(); PrintStream output = new PrintStream(new WriterOutputStream(context.getWriter()), true); execInitializer.setOutputSink(output); PrintStream error = new PrintStream(new WriterOutputStream(context.getErrorWriter()), true); execInitializer.setErrorSink(error); Map<String, byte[]> propagatedVariables = null; if (context.getAttribute(SchedulerConstants.VARIABLES_BINDING_NAME) != null) { propagatedVariables = SerializationUtil.serializeVariableMap(((VariablesMap) context.getAttribute(SchedulerConstants.VARIABLES_BINDING_NAME)).getPropagatedVariables()); execInitializer.setPropagatedVariables(propagatedVariables); } else { execInitializer.setPropagatedVariables(Collections.<String, byte[]> emptyMap()); } if (context.getAttribute(Script.ARGUMENTS_NAME) != null) { execInitializer.setSerializedArguments((Map<String, byte[]>) ((Serializable[]) context.getAttribute(Script.ARGUMENTS_NAME))[0]); } else { execInitializer.setSerializedArguments(Collections.<String, byte[]> emptyMap()); } if (context.getAttribute(SchedulerConstants.CREDENTIALS_VARIABLE) != null) { execInitializer.setThirdPartyCredentials((Map<String, String>) context.getAttribute(SchedulerConstants.CREDENTIALS_VARIABLE)); } else { execInitializer.setThirdPartyCredentials(Collections.<String, String> emptyMap()); } if (context.getAttribute(SchedulerConstants.MULTI_NODE_TASK_NODESURL_BINDING_NAME) != null) { List<String> nodesURLs = (List<String>) context.getAttribute(SchedulerConstants.MULTI_NODE_TASK_NODESURL_BINDING_NAME); execInitializer.setNodesURL(nodesURLs); } else { execInitializer.setNodesURL(Collections.<String> emptyList()); } javaExecutable.internalInit(execInitializer, context); Serializable execute = javaExecutable.execute((TaskResult[]) context.getAttribute(SchedulerConstants.RESULTS_VARIABLE)); if (propagatedVariables != null) { ((Map<String, Serializable>) context.getAttribute(SchedulerConstants.VARIABLES_BINDING_NAME)).putAll(javaExecutable.getVariables()); } output.close(); error.close(); return execute; } catch (Throwable e) { throw new ScriptException(new TaskException(getStackTraceAsString(e), e)); } }
Example 20
Source File: JavaExecutable.java From scheduling with GNU Affero General Public License v3.0 | 2 votes |
/** * Initialization of the metadata.<br> * * @param sc the ScriptContext including as bindings the metadata map. */ public void initMetadata(ScriptContext sc) { this.metadata = (Map<String, String>) sc.getAttribute(SchedulerConstants.RESULT_METADATA_VARIABLE); }