/************************************************************************** /* This class loads a script and executes various functions. /* /* Copyright (c) 2007 by Bernhard Bablok (mail@bablokb.de) /* /* This program is free software; you can redistribute it and/or modify /* it under the terms of the GNU General Public License as published /* by the Free Software Foundation; either version 2 of the License or /* (at your option) any later version. /* /* This program is distributed in the hope that it will be useful, but /* WITHOUT ANY WARRANTY; without even the implied warranty of /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /* GNU General Public License for more details. /* /* You should have received a copy of the GNU General Public License /* along with this program; see the file COPYING. If not, write to /* the Free Software Foundation Inc., 59 Temple Place - Suite 330, /* Boston, MA 02111-1307 USA /**************************************************************************/ import java.io.*; import javax.script.*; /** This class loads a script and executes various functions. @version $Revision: 1.3 $ @author $Author: bablokb $ */ public class ExecFuncs { private static final String SCRIPT_NAME1 = "execfuncs.py", SCRIPT_NAME2 = "execfuncs2.py"; private ScriptEngine iEngine = null; ////////////////////////////////////////////////////////////////////////////// /** Constructor. */ public ExecFuncs() throws Exception { ScriptEngineManager manager = new ScriptEngineManager(); iEngine = manager.getEngineByName("jython"); if (iEngine == null) { throw new IllegalStateException("no jython script-engine"); } } ////////////////////////////////////////////////////////////////////////////// /** Return engine */ public ScriptEngine getEngine() { return iEngine; } ////////////////////////////////////////////////////////////////////////////// /** Read and evaluate script. @param pName script name */ public void readScript(String pName) throws Exception { FileReader reader = new FileReader(pName); iEngine.eval(reader); reader.close(); } ////////////////////////////////////////////////////////////////////////////// /** Execute function. @param pName function name @param pArgs function arguments */ public void execFunc(String pName,Object... pArgs) throws Exception { ((Invocable) iEngine).invokeFunction(pName,pArgs); } ////////////////////////////////////////////////////////////////////////////// /** Main method. @param args Program arguments (ignored). */ public static void main(String[] args) { try { ExecFuncs obj = new ExecFuncs(); obj.readScript(SCRIPT_NAME1); obj.execFunc("printX"); obj.execFunc("setX","5"); obj.execFunc("printX"); obj.readScript(SCRIPT_NAME2); obj.execFunc("setX",Math.PI); obj.execFunc("printX"); obj.execFunc("printY"); obj.execFunc("setY","42"); obj.execFunc("printY"); } catch (Exception e) { e.printStackTrace(); } } }