RunnableImpl.java revision 67:5c2ed5d89524
11590Srgrimes/* 294589Sobrien * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 394589Sobrien * 45814Sjkh * Redistribution and use in source and binary forms, with or without 51590Srgrimes * modification, are permitted provided that the following conditions 61590Srgrimes * are met: 71590Srgrimes * 81590Srgrimes * - Redistributions of source code must retain the above copyright 91590Srgrimes * notice, this list of conditions and the following disclaimer. 101590Srgrimes * 111590Srgrimes * - Redistributions in binary form must reproduce the above copyright 121590Srgrimes * notice, this list of conditions and the following disclaimer in the 131590Srgrimes * documentation and/or other materials provided with the distribution. 141590Srgrimes * 151590Srgrimes * - Neither the name of Oracle nor the names of its 161590Srgrimes * contributors may be used to endorse or promote products derived 171590Srgrimes * from this software without specific prior written permission. 181590Srgrimes * 191590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 201590Srgrimes * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 211590Srgrimes * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 221590Srgrimes * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 231590Srgrimes * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 241590Srgrimes * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 251590Srgrimes * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 261590Srgrimes * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 271590Srgrimes * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 281590Srgrimes * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 291590Srgrimes * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 301590Srgrimes */ 311590Srgrimes 321590Srgrimesimport javax.script.*; 331590Srgrimes 341590Srgrimespublic class RunnableImpl { 351590Srgrimes public static void main(String[] args) throws Exception { 361590Srgrimes ScriptEngineManager manager = new ScriptEngineManager(); 371590Srgrimes ScriptEngine engine = manager.getEngineByName("nashorn"); 3862833Swsanchez 3962833Swsanchez // JavaScript code in a String 401590Srgrimes String script = "function run() { print('run called'); }"; 411590Srgrimes 4262833Swsanchez // evaluate script 4394587Sobrien engine.eval(script); 441590Srgrimes 451590Srgrimes Invocable inv = (Invocable) engine; 461590Srgrimes 471590Srgrimes // get Runnable interface object from engine. This interface methods 481590Srgrimes // are implemented by script functions with the matching name. 491590Srgrimes Runnable r = inv.getInterface(Runnable.class); 501590Srgrimes 511590Srgrimes // start a new thread that runs the script implemented 5280381Ssheldonh // runnable interface 531590Srgrimes Thread th = new Thread(r); 541590Srgrimes th.start(); 551590Srgrimes th.join(); 561590Srgrimes } 571590Srgrimes} 581590Srgrimes