List of usage examples for javax.script Invocable invokeMethod
public Object invokeMethod(Object thiz, String name, Object... args) throws ScriptException, NoSuchMethodException;
ScriptEngine
. From source file:org.zols.datastore.validator.TV4.java
public Boolean validate(Object data, String schema) throws ScriptException, NoSuchMethodException, JsonProcessingException { System.out.println("Schema \n--------- \n " + schema); Invocable inv = (Invocable) engine; Object schemaToJavaScript = inv.invokeMethod(json, "parse", schema); Object dataToJavaScript = inv.invokeMethod(json, "parse", mapper.writeValueAsString(data)); System.out.println("Data \n--------- \n " + mapper.writeValueAsString(data)); Object validation = inv.invokeFunction("validate", dataToJavaScript, schemaToJavaScript); Object result = inv.invokeMethod(json, "stringify", validation); return Boolean.valueOf(result.toString()); }
From source file:com.viddu.handlebars.HandlebarsNashorn.java
@Override public String render(String templateString, String contextJson) throws HandlebarsException { logger.debug("Context JSON={}", contextJson); logger.debug("templateString={}", templateString); Invocable render = (Invocable) engine; try {//from w w w .ja v a 2s . c o m Object context = render.invokeMethod(JSON, "parse", contextJson); ScriptObjectMirror obj = (ScriptObjectMirror) render.invokeMethod(handlebars, "compile", templateString); return (String) obj.call(null, context); } catch (NoSuchMethodException | ScriptException e) { throw new HandlebarsException(e); } }
From source file:org.apache.accumulo.core.util.shell.commands.ScriptCommand.java
private void invokeFunctionOrMethod(Shell shellState, ScriptEngine engine, CommandLine cl, Object[] args) { try {//from w ww .j a v a2 s .c o m Invocable inv = (Invocable) engine; if (cl.hasOption(function.getOpt())) { inv.invokeFunction(cl.getOptionValue(function.getOpt()), args); } else if (cl.hasOption(object.getOpt())) { String objectMethod = cl.getOptionValue(object.getOpt()); String[] parts = objectMethod.split(":"); if (!(parts.length == 2)) { shellState.printException(new Exception("Object and Method must be supplied")); return; } String objectName = parts[0]; String methodName = parts[1]; Object obj = engine.get(objectName); inv.invokeMethod(obj, methodName, args); } } catch (Exception e) { shellState.printException(e); } }
From source file:org.apache.nifi.processors.script.InvokeScriptedProcessor.java
/** * Reloads the script Processor. This must be called within the lock. * * @param scriptBody An input stream associated with the script content * @return Whether the script was successfully reloaded *//* w ww . ja va 2s.c o m*/ private boolean reloadScript(final String scriptBody) { // note we are starting here with a fresh listing of validation // results since we are (re)loading a new/updated script. any // existing validation results are not relevant final Collection<ValidationResult> results = new HashSet<>(); try { // get the engine and ensure its invocable if (scriptEngine instanceof Invocable) { final Invocable invocable = (Invocable) scriptEngine; // Find a custom configurator and invoke their eval() method ScriptEngineConfigurator configurator = scriptingComponentHelper.scriptEngineConfiguratorMap .get(scriptingComponentHelper.getScriptEngineName().toLowerCase()); if (configurator != null) { configurator.eval(scriptEngine, scriptBody, scriptingComponentHelper.getModules()); } else { // evaluate the script scriptEngine.eval(scriptBody); } // get configured processor from the script (if it exists) final Object obj = scriptEngine.get("processor"); if (obj != null) { final ComponentLog logger = getLogger(); try { // set the logger if the processor wants it invocable.invokeMethod(obj, "setLogger", logger); } catch (final NoSuchMethodException nsme) { if (logger.isDebugEnabled()) { logger.debug("Configured script Processor does not contain a setLogger method."); } } // record the processor for use later final Processor scriptProcessor = invocable.getInterface(obj, Processor.class); processor.set(scriptProcessor); if (scriptProcessor != null) { try { scriptProcessor.initialize(new ProcessorInitializationContext() { @Override public String getIdentifier() { return InvokeScriptedProcessor.this.getIdentifier(); } @Override public ComponentLog getLogger() { return logger; } @Override public ControllerServiceLookup getControllerServiceLookup() { return InvokeScriptedProcessor.super.getControllerServiceLookup(); } @Override public NodeTypeProvider getNodeTypeProvider() { return InvokeScriptedProcessor.super.getNodeTypeProvider(); } @Override public String getKerberosServicePrincipal() { return InvokeScriptedProcessor.this.kerberosServicePrincipal; } @Override public File getKerberosServiceKeytab() { return InvokeScriptedProcessor.this.kerberosServiceKeytab; } @Override public File getKerberosConfigurationFile() { return InvokeScriptedProcessor.this.kerberosConfigFile; } }); } catch (final Exception e) { logger.error("Unable to initialize scripted Processor: " + e.getLocalizedMessage(), e); throw new ProcessException(e); } } } else { throw new ScriptException("No processor was defined by the script."); } } } catch (final Exception ex) { final ComponentLog logger = getLogger(); final String message = "Unable to load script: " + ex.getLocalizedMessage(); logger.error(message, ex); results.add(new ValidationResult.Builder().subject("ScriptValidation").valid(false) .explanation("Unable to load script due to " + ex.getLocalizedMessage()) .input(scriptingComponentHelper.getScriptPath()).build()); } // store the updated validation results validationResults.set(results); // return whether there was any issues loading the configured script return results.isEmpty(); }
From source file:org.jumpmind.metl.core.runtime.component.Script.java
protected void invoke(String method, Object... args) { if (engine != null) { try {/*from w ww. ja v a 2s . c om*/ Invocable invocable = (Invocable) engine; Object helper = engine.get("helper"); invocable.invokeMethod(helper, method, args); } catch (RuntimeException e) { throw e; } catch (Exception e) { Throwable rootCause = ExceptionUtils.getRootCause(e); if (rootCause != null) { if (rootCause instanceof RuntimeException) { throw (RuntimeException) rootCause; } else { throw new RuntimeException(rootCause); } } else { throw new RuntimeException(e); } } } }
From source file:org.jwebsocket.plugins.scripting.app.js.JavaScriptApp.java
/** * {@inheritDoc }/*from w ww.ja v a 2s . c om*/ * * @param aObjectId * @param aMethod * @param aArgs * @return * @throws java.lang.Exception */ @Override public Object callMethod(final String aObjectId, final String aMethod, final Collection aArgs) throws Exception { final Invocable lInvocable = (Invocable) getScriptApp(); Boolean lExists = (Boolean) lInvocable.invokeMethod(mApp, "isPublished", new Object[] { aObjectId }); Assert.isTrue(lExists, "The object with id ' " + aObjectId + "' is not published for client access!"); // getting scripting plugin settings Settings lSettings = (Settings) JWebSocketBeanFactory.getInstance(ScriptingPlugIn.NS) .getBean("org.jwebsocket.plugins.scripting.settings"); // calling method into a security sandbox return Tools.doPrivileged(lSettings.getAppPermissions(getName(), getPath()), new PrivilegedAction<Object>() { @Override public Object run() { // invoking method try { return lInvocable.invokeMethod(mApp, "invokeObject", new Object[] { aObjectId, aMethod, aArgs }); } catch (Exception lEx) { throw new RuntimeException(lEx); } } }); }
From source file:org.jwebsocket.plugins.scripting.app.js.JavaScriptApp.java
/** * {@inheritDoc }/*from w ww .ja v a2s.c om*/ * * @return * @throws java.lang.Exception */ @Override public String getVersion() throws Exception { Invocable lInvocable = (Invocable) getScriptApp(); String lVersion = (String) lInvocable.invokeMethod(mApp, "getVersion", new Object[0]); return lVersion; }
From source file:org.jwebsocket.plugins.scripting.app.js.JavaScriptApp.java
/** * {@inheritDoc }//from w w w .j a va 2 s . c om * * @return * @throws java.lang.Exception */ @Override public Map getClientAPI() throws Exception { Invocable lInvocable = (Invocable) getScriptApp(); Map lAPI = (Map) lInvocable.invokeMethod(mApp, "getClientAPI", new Object[0]); return lAPI; }
From source file:org.jwebsocket.plugins.scripting.app.js.JavaScriptApp.java
/** * {@inheritDoc }// w ww. j a v a 2 s .co m * * @return * @throws java.lang.Exception */ @Override public String getDescription() throws Exception { Invocable lInvocable = (Invocable) getScriptApp(); String lVersion = (String) lInvocable.invokeMethod(mApp, "getDescription", new Object[0]); return lVersion; }