List of usage examples for javax.script Invocable getInterface
public <T> T getInterface(Class<T> clasz);
From source file:InterfaceTest.java
public static void main(String args[]) throws Exception { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("javascript"); engine.eval("function run() {print('www.java2s.com');}"); Invocable invokeEngine = (Invocable) engine; Runnable runner = invokeEngine.getInterface(Runnable.class); Thread t = new Thread(runner); t.start();// w ww. j a v a 2s. c o m t.join(); }
From source file:Main.java
public static void main(String[] args) throws Exception { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("JavaScript"); if (!(engine instanceof Invocable)) { System.out.println("Interface implementation in script" + "is not supported."); return;//from w w w . j a va2 s . co m } Invocable inv = (Invocable) engine; String scriptPath = "c:/Java_Dev/cal.js"; engine.eval("load('" + scriptPath + "')"); Calculator calc = inv.getInterface(Calculator.class); if (calc == null) { System.err.println("Calculator interface " + "implementation not found."); return; } double x = 2.0; double y = 1.0; double addResult = calc.add(x, y); System.out.println(addResult); }
From source file:Main.java
public static void main(String[] args) throws Exception { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("js"); engine.eval("function add (a, b) {c = a + b; return c; }"); Invocable jsInvoke = (Invocable) engine; Object result1 = jsInvoke.invokeFunction("add", new Object[] { 10, 5 }); System.out.println(result1);//from w w w . j a v a 2s . co m Adder adder = jsInvoke.getInterface(Adder.class); int result2 = adder.add(10, 5); System.out.println(result2); }
From source file:mondrian.util.UtilCompatibleJdk16.java
public <T> T compileScript(Class<T> iface, String script, String engineName) { ScriptEngineManager factory = new ScriptEngineManager(); ScriptEngine engine = factory.getEngineByName(engineName); try {// w w w . java2 s. c o m engine.eval(script); Invocable inv = (Invocable) engine; return inv.getInterface(iface); } catch (ScriptException e) { throw Util.newError(e, "Error while compiling script to implement " + iface + " SPI"); } }
From source file:net.landora.video.filerenaming.RenameScriptManager.java
RenamingScript createRenamingScript(String folderScript, String fileScript, boolean reportExceptions) { try {/*from ww w . ja v a 2 s. c o m*/ StringBuilder str = new StringBuilder(); str.append(getClassScript("RenameScript.py")); str.append("\n"); str.append(createScript("findFolderName", folderScript)); str.append("\n"); str.append(createScript("findFilename", fileScript)); ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("jython"); engine.eval(str.toString()); Invocable inv = (Invocable) engine; return inv.getInterface(RenamingScript.class); } catch (Exception ex) { if (reportExceptions) { LoggerFactory.getLogger(getClass()).error("Error getting rename script.", ex); } return null; } }
From source file:org.jgentleframework.integration.scripting.ScriptingInstantiationInterceptor.java
@Override public Object instantiate(ObjectInstantiation oi) throws Throwable { Object result = null;/*from www . jav a2 s . c o m*/ Class<?> target = oi.getTargetClass(); Definition definition = this.definitionManager.getDefinition(target); if (definition.isAnnotationPresent(ScriptingInject.class)) { if (!target.isInterface()) { if (log.isFatalEnabled()) { log.fatal("Could not binding to Scripting service", new ScriptException("Target class must be a interface!")); } } final ScriptingInject scriptingInject = definition.getAnnotation(ScriptingInject.class); Object previous = oi.getPreviousResult(); if (previous == null) { ScriptEngineManager engineManager = new ScriptEngineManager(); ScriptEngine engine = engineManager.getEngineByName(scriptingInject.lang().getType()); if (engine == null) { if (log.isFatalEnabled()) { log.fatal( "Script engine with name : " + scriptingInject.lang().getType() + " is not found!", new ScriptException("Script engine with name : " + scriptingInject.lang().getType() + " is not found!")); } throw new ScriptException( "Script engine with name : " + scriptingInject.lang().getType() + " is not found!"); } File parentFile = null; if (!(PathType.CLASSPATH.equals(scriptingInject.pathType()) || PathType.CLASSPATH.equals(scriptingInject.pathType()))) { parentFile = new File(scriptingInject.pathType().getType()); } File sourceFile = null; if (parentFile != null) { sourceFile = new File(parentFile, scriptingInject.scriptFile()); } else { if (PathType.CLASSPATH.equals(scriptingInject.pathType())) { sourceFile = new File(ScriptingInstantiationInterceptor.class .getResource(scriptingInject.scriptFile()).toURI()); } else { sourceFile = new File(scriptingInject.scriptFile()); } } engine.eval(new FileReader(sourceFile)); Invocable inv = (Invocable) engine; final Object scriptObject = inv.getInterface(target); result = scriptObject; final Enhancer enhancer = new Enhancer(); enhancer.setInterfaces(new Class<?>[] { target }); enhancer.setCallback(new MethodInterceptor() { @Override public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { if (method.getName().equals("hashCode")) { final int prime = 31; int result = 1; result = prime * result + super.hashCode(); result = prime * result + enhancer.hashCode(); return result; } else { MethodInvocation invocation = new BasicMethodInvocation(obj, method, args); return invoke(invocation, scriptObject); } } protected Object invoke(MethodInvocation invocation, Object stub) throws NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { Object result = null; Method method = null; Method methodType = invocation.getMethod(); Class<?> clazz = stub.getClass(); Class<?>[] classType = ReflectUtils.getClassTypeOf(invocation.getArguments()); method = ReflectUtils.getSupportedMethod(clazz, methodType.getName(), classType); result = method.invoke(stub, invocation.getArguments()); return result; } }); if (oi.args() != null && oi.argTypes() != null) result = enhancer.create(oi.argTypes(), oi.args()); else result = enhancer.create(); oi.setPreviousResult(result); } else { if (log.isFatalEnabled()) { log.fatal( "Does not support multible Instantiation Interceptor " + "conjointly with Scripting Instantiation", new ScriptException("Does not support multible Instantiation Interceptor!")); } } return oi.proceed(); } else { if (log.isWarnEnabled()) { log.warn("The target interface is not annotated with [" + ScriptingInject.class + "]"); } } return result; }
From source file:org.nuxeo.automation.scripting.internals.AutomationScriptingServiceImpl.java
@Override public <T> T getInterface(Class<T> scriptingOperationInterface, String script, CoreSession session) throws ScriptException, OperationException { run(script, session);/*from w w w . j a v a 2 s .co m*/ Invocable inv = (Invocable) engines.get(); return inv.getInterface(scriptingOperationInterface); }