public CompiledScript compile(String s) throws ScriptException { return engine.compile(s); }
public CompiledScript compileUnchecked(String s) { try { return engine.compile(s); }catch (ScriptException e) { throw new IllegalStateException(e.getMessage(), e); } }
ScriptBasedUDFunction(FunctionName name, List<ColumnIdentifier> argNames, List<AbstractType<?>> argTypes, AbstractType<?> returnType, boolean calledOnNullInput, String language, String body) { super(name, argNames, argTypes, returnType, calledOnNullInput, language, body); if (!"JavaScript".equalsIgnoreCase(language) || scriptEngine == null) throw new InvalidRequestException(String.format("Invalid language '%s' for function '%s'", language, name)); // execute compilation with no-permissions to prevent evil code e.g. via "static code blocks" / "class initialization" try { this.script = AccessController.doPrivileged((PrivilegedExceptionAction<CompiledScript>) () -> scriptEngine.compile(body), accessControlContext); } catch (PrivilegedActionException x) { Throwable e = x.getCause(); logger.info("Failed to compile function '{}' for language {}: ", name, language, e); throw new InvalidRequestException( String.format("Failed to compile function '%s' for language %s: %s", name, language, e)); } // It's not always possible to simply pass a plain Java object as a binding to Nashorn and // let the script execute methods on it. udfContextBinding = new UDFContextWrapper(); }
ScriptBasedUDFunction(FunctionName name, List<ColumnIdentifier> argNames, List<AbstractType<?>> argTypes, AbstractType<?> returnType, boolean calledOnNullInput, String language, String body) { super(name, argNames, argTypes, returnType, calledOnNullInput, language, body); if (!"JavaScript".equalsIgnoreCase(language) || scriptEngine == null) throw new InvalidRequestException(String.format("Invalid language '%s' for function '%s'", language, name)); // execute compilation with no-permissions to prevent evil code e.g. via "static code blocks" / "class initialization" try { this.script = AccessController.doPrivileged((PrivilegedExceptionAction<CompiledScript>) () -> scriptEngine.compile(body), accessControlContext); } catch (PrivilegedActionException x) { Throwable e = x.getCause(); logger.info("Failed to compile function '{}' for language {}: ", name, language, e); throw new InvalidRequestException( String.format("Failed to compile function '%s' for language %s: %s", name, language, e)); } // It's not always possible to simply pass a plain Java object as a binding to Nashorn and // let the script execute methods on it. udfContextBinding = new UDFContextWrapper(); }
ScriptBasedUDFunction(FunctionName name, List<ColumnIdentifier> argNames, List<AbstractType<?>> argTypes, AbstractType<?> returnType, boolean calledOnNullInput, String language, String body) { super(name, argNames, argTypes, returnType, calledOnNullInput, language, body); if (!"JavaScript".equalsIgnoreCase(language) || scriptEngine == null) throw new InvalidRequestException(String.format("Invalid language '%s' for function '%s'", language, name)); // execute compilation with no-permissions to prevent evil code e.g. via "static code blocks" / "class initialization" try { this.script = AccessController.doPrivileged((PrivilegedExceptionAction<CompiledScript>) () -> scriptEngine.compile(body), accessControlContext); } catch (PrivilegedActionException x) { Throwable e = x.getCause(); logger.info("Failed to compile function '{}' for language {}: ", name, language, e); throw new InvalidRequestException( String.format("Failed to compile function '%s' for language %s: %s", name, language, e)); } // It's not always possible to simply pass a plain Java object as a binding to Nashorn and // let the script execute methods on it. udfContextBinding = new UDFContextWrapper(); }
public ScriptExecutor(Collection<Pair<String, String>> libraries, String scriptName, String scriptCode, Map<String,Object> globalObjects) { scriptEngine = (NashornScriptEngine)new NashornScriptEngineFactory().getScriptEngine(); bindings = new SimpleBindings(); bindings.putAll(globalObjects); scriptEngine.setBindings(bindings, ScriptContext.ENGINE_SCOPE); libraries.forEach(p->{ bindings.put(NASHORN_SCRIPT_FILE_NAME, p.getKey()); try { scriptEngine.eval(p.getValue()); } catch (ScriptException e) { throw new RuntimeException(e); } }); try { bindings.put(NASHORN_SCRIPT_FILE_NAME, scriptName); compiledScript = scriptEngine.compile(scriptCode); } catch (ScriptException e) { throw new RuntimeException(e); } }