JDK 1.6におけるスクリプト言語のサポート


package cn.itcast.js;

import java.io.File;
import java.io.FileNotFoundException;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

/**
 * @author handw
 * @version     :2009-10-20   09:49:31
 *    :JDK1.6    Script(JSR223)   。        ,           Java     。               ,             。    API          Java  。  ,Web Scripting Framework          Servlet  (  Tomcat)   Web  。
 * 
 */

public class JdkJs {

	/**
	 * @author handw
	 * @time 2009-10-20  09:49:31
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		//JdkJs.test1();
		//--------------------------
		ScriptEngineManager mgr = new ScriptEngineManager();     
    	ScriptEngine engine = mgr.getEngineByExtension("js"); 
    	//-------------------------------
    	 //JdkJs.testUsingJDKClasses(engine);
    	//--------------------------
    	JdkJs.testScriptInterface(engine);
	}
	/**
	 *   ScriptEngine ScriptEngineManager   :
	 *   js    ScriptEngine
	 *  jdk    3     :
	 * @author handw
	 * @throws ScriptException 
	 * @throws FileNotFoundException 
	 * @throws NoSuchMethodException 
	 * @time 2009-10-20  09:51:06
	 */
    public static void test1() throws FileNotFoundException, ScriptException, NoSuchMethodException
    {
    	ScriptEngineManager mgr = new ScriptEngineManager();     
    	ScriptEngine engine = mgr.getEngineByExtension("js");  

//    	mgr.getEngineByExtension(String extension) //getEngineByExtension    :js 
//    	mgr.getEngineByMimeType(String mimeType)   // getEngineByMimeType    :application/javascript;application/ecmascript;text/javascript;text/ecmascript;
//    	mgr.getEngineByName(String shortName)      //getEngineByName    :js;rhino;JavaScript;javascript;ECMAScript--js javascript     ;
    	//    js     js    js      :
    	//    js  :
    	String jsfile="";
    	//engine.eval(new FileReader(jsfile));  
    	//  
    	String jsstring="";
    	//engine.eval(jsstring);
//    	  js  Invocable  :  ScriptEngine   js       (  ):
//    	      Invocable           js  :
//    	eg:
    	engine.eval("function test(){return 'this is test js in java'}");   
    	Invocable inv = (Invocable) engine;   
    	String value = String.valueOf(inv.invokeFunction("test"));
    	System.out.println(value);
    }
    
		/**
		 *           JDK     ,    JDK   Swing  
		 * @param engine ScriptEngine  
		 * @return void
		 * */	
	private static void testUsingJDKClasses(ScriptEngine engine)	throws Exception {
		// Packages             ,     JDK package
		String js = "function doSwing(t){var f=new Packages.javax.swing.JFrame(t);f.setSize(400,300);f.setVisible(true);}";
		engine.eval(js);
		// Invocable   :   java               
		Invocable inv = (Invocable) engine;
		// invokeFunction()                     ,                 ;
		inv.invokeFunction("doSwing", "Scripting Swing");
	}
	
	/**
	 *           Java   ,       script     
	 *
	 * @param engine ScriptEngine  
	 * @return void
	 * */	
	private static void testScriptInterface(ScriptEngine engine)	throws ScriptException {
		String script = "var obj = new Object(); obj.run = function() { println('run method called'); }";
		engine.eval(script);
		Object obj = engine.get("obj");
		Invocable inv = (Invocable) engine;
		
		Runnable r = inv.getInterface(obj, Runnable.class);
		Thread th = new Thread(r);
		th.start();
	}
	/**
	 *      Java          ,  JDK   script         
	 *
	 * @param engine ScriptEngine  
	 * @return void
	 * */		
	private static void testInvokeScriptMethod(ScriptEngine engine)	throws Exception {
		String script = "function helloFunction(name) { return 'Hello everybody,' + name;}";
		engine.eval(script);
		Invocable inv = (Invocable) engine;
		String res = (String) inv.invokeFunction("helloFunction", "Scripting");
		System.out.println("res:" + res);
	}
	/**
	 *       Java            ,     File        ,                
	 *
	 * @param engine ScriptEngine  
	 * @return void
	 * */		
	private static void testScriptVariables(ScriptEngine engine)	throws ScriptException {
		File file = new File("test.txt");
		engine.put("f", file);
		engine.eval("println('Total Space:'+f.getTotalSpace())");
		engine.eval("println('Path:'+f.getPath())");
	}
}
 出典:http://sharong.iteye.com/blog/474476 
  トマトを食べる人に感謝します.