Author: sebb Date: Sun Aug 2 18:25:01 2009 New Revision: 800124 URL: http://svn.apache.org/viewvc?rev=800124&view=rev Log: Update test engine for 3.0 Add -show option to display details of factories and engine
Modified: jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/Main.java Modified: jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/Main.java URL: http://svn.apache.org/viewvc/jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/Main.java?rev=800124&r1=800123&r2=800124&view=diff ============================================================================== --- jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/Main.java (original) +++ jakarta/bsf/trunk/bsf3/bsf-utils/src/main/java/org/apache/bsf/Main.java Sun Aug 2 18:25:01 2009 @@ -23,28 +23,34 @@ import java.io.InputStreamReader; import java.io.Reader; import java.util.Hashtable; +import java.util.Iterator; import java.util.List; import javax.script.Bindings; +import javax.script.Compilable; +import javax.script.Invocable; import javax.script.ScriptContext; import javax.script.ScriptEngine; +import javax.script.ScriptEngineFactory; import javax.script.ScriptEngineManager; import javax.script.ScriptException; /** * This is the main driver for BSF to be run on the command line - * to eval/exec/compile scripts directly. + * to eval scripts directly. * * @author Sanjiva Weerawarana * @author Matthew J. Duftler * @author Sam Ruby */ public class Main { + private static final String SHOW_ENGINE = "engine"; + private static final String SHOW_FACTORIES = "factories"; private static final String ARG_IN = "-in"; private static final String ARG_LANG = "-lang"; - private static final String ARG_OUT = "-out"; + private static final String ARG_EXT = "-ext"; + private static final String ARG_SHOW = "-show"; private static final String DEFAULT_IN_FILE_NAME = "<STDIN>"; - private static final String DEFAULT_CLASS_NAME = "Test"; /** * Static driver to be able to run BSF scripts from the command line. @@ -59,30 +65,39 @@ Hashtable argsTable = new Hashtable(); - argsTable.put(ARG_OUT, DEFAULT_CLASS_NAME); - for (int i = 0; i < args.length; i += 2) { argsTable.put(args[i], args[i + 1]); } String inFileName = (String) argsTable.get(ARG_IN); + String extension = (String) argsTable.get(ARG_EXT); String language = (String) argsTable.get(ARG_LANG); + String show = (String) argsTable.get(ARG_SHOW); + ScriptEngineManager mgr = new ScriptEngineManager(); + final List engineFactories = mgr.getEngineFactories(); + if (engineFactories.isEmpty()){ + throw new RuntimeException("Could not find any engine factories"); + } + + final int engineCount = engineFactories.size(); + if (SHOW_FACTORIES.equalsIgnoreCase(show)) { + System.err.println("Found "+engineCount+ " engine factories"); + for (Iterator iter = engineFactories.iterator(); iter.hasNext();){ + ScriptEngineFactory fac = (ScriptEngineFactory) iter.next(); + showFactory(fac, false); + } + return; + } - if (language == null && inFileName != null) { + if (language == null && extension == null && inFileName != null) { int i = inFileName.lastIndexOf('.'); if (i > 0) { - language = inFileName.substring(i+1); - } - if (language == null) { - throw new IllegalArgumentException("unable to determine language"); + extension = inFileName.substring(i+1); } } - - ScriptEngineManager mgr = new ScriptEngineManager(); - final List engineFactories = mgr.getEngineFactories(); - if (engineFactories.isEmpty()){ - throw new RuntimeException("Could not find any engine factories"); + if (extension == null && language == null) { + throw new IllegalArgumentException("unable to determine language"); } Reader in; @@ -95,9 +110,29 @@ } try { - ScriptEngine engine = mgr.getEngineByExtension(language); - if (engine == null){ - throw new IllegalArgumentException("unable to find engine using Extension: "+language); + ScriptEngine engine; + if (language != null) { + engine = mgr.getEngineByName(language); + if (engine == null){ + throw new IllegalArgumentException("unable to find engine using Language: "+language); + } + } else { + engine = mgr.getEngineByExtension(extension); + if (engine == null){ + throw new IllegalArgumentException("unable to find engine using Extension: "+extension); + } + } + if (SHOW_ENGINE.equalsIgnoreCase(show)){ + ScriptEngineFactory fac = engine.getFactory(); + showFactory(fac, true); + System.err.println("Engine="+engine.getClass().getName()); + if (engine instanceof Compilable){ + System.err.println("Engine supports Compilable interface."); + } + if (engine instanceof Invocable){ + System.err.println("Engine supports Invocable interface."); + } + System.err.println(); } Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE); bindings.put("args", args); @@ -108,32 +143,124 @@ } } + private static void showFactory(ScriptEngineFactory fac, boolean full) { + StringBuffer sb = new StringBuffer(); + sb.append('['); + sb.append(fac.getClass().getName()); + sb.append("]\n"); + + try { + sb.append(fac.getEngineName()); + sb.append(" "); + try { + sb.append(fac.getEngineVersion()); + } catch (NoClassDefFoundError e) { + sb.append("N/A"); + } + sb.append(" for "); + sb.append(fac.getLanguageName()); + sb.append(" "); + try { + sb.append(fac.getLanguageVersion()); + } catch (NoClassDefFoundError e) { + sb.append("N/A"); + } + sb.append("\n"); + + Iterator iter=fac.getNames().iterator(); + char sep = '{'; + while(iter.hasNext()){ + sb.append(sep); + sb.append(iter.next()); + sep=','; + } + if (sep == ','){ + sb.append("} "); + } + iter=fac.getExtensions().iterator(); + sep='['; + while(iter.hasNext()){ + sb.append(sep); + sb.append('.'); + sb.append(iter.next()); + sep=','; + } + if (sep == ','){ + sb.append("] "); + } + iter=fac.getMimeTypes().iterator(); + sep='('; + while(iter.hasNext()){ + sb.append(sep); + sb.append(iter.next()); + sep=','; + } + if (sep == ','){ + sb.append(")"); + } + sb.append("\n"); + if (full){ + sb.append("OutputStatement: "); + try { + sb.append(fac.getOutputStatement("String")); + } catch (Exception e) { + sb.append(e.toString()); + } + sb.append("\n"); + sb.append("Program: "); + try { + sb.append(fac.getProgram(new String[]{"Line1","Line2"})); + } catch (Exception e) { + sb.append(e.toString()); + } + sb.append("\n"); + sb.append("Method call: "); + try { + sb.append(fac.getMethodCallSyntax("object", "method", new String[]{"Arg1","Arg2"})); + } catch (Exception e) { + sb.append(e.toString()); + } + sb.append("\n"); + } + } catch (Throwable t) { + sb.append(t.toString()); + } finally { + System.err.println(sb.toString()); + } + } + private static void printHelp() { System.err.println("Usage:"); System.err.println(); - System.err.println(" java " + Main.class.getName() + " [args]"); + System.err.println(" java " + Main.class.getName() + " [opts] [args]"); System.err.println(); - System.err.println(" args:"); + System.err.println(" opts:"); System.err.println(); System.err.println( - " [-in fileName] default: " + DEFAULT_IN_FILE_NAME); + " [-lang shortname] (e.g. jexl, jython) Overrides -ext."); System.err.println( - " [-lang languageName] default: " - + "<If -in is specified and -lang"); + " [-in fileName] default: " + DEFAULT_IN_FILE_NAME); System.err.println( - " " - + " is not, attempt to determine"); + " [-ext extension] default: " + + "<If -in is specified and neither -ext nor -lang"); System.err.println( - " " - + " language from file extension;"); + " " + + " are, attempt to determine"); System.err.println( - " " - + " otherwise, -lang is required.>"); - System.err.println(); + " " + + " extension from file extension;"); System.err.println( - " Additional args used only if -mode is " + "set to \"compile\":"); + " " + + " otherwise, -ext or -lang is required.>"); + System.err.println( + " [-show item]"); + System.err.println( + " " + SHOW_FACTORIES + " - list all engine factories and exit"); + System.err.println( + " " +SHOW_ENGINE + " - list details of selected factory and engine"); + System.err.println(); System.err.println(); System.err.println( - " [-out className] default: " + DEFAULT_CLASS_NAME); + " The command-line arguments are stored in the array 'args'"); } } \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: bsf-dev-unsubscr...@jakarta.apache.org For additional commands, e-mail: bsf-dev-h...@jakarta.apache.org