protected Collection<Map<String,String>> getAvailableScriptEngines() { List<Map<String,String>> engines = new LinkedList<Map<String,String>>(); for (ScriptEngineFactory f: FACTORIES) { Map<String,String> engine = new LinkedHashMap<String, String>(); engine.put("engine", f.getNames().get(0)); engine.put("language", f.getLanguageName()); engines.add(engine); } return engines; }
String langName = factory.getLanguageName(); String langVersion = factory.getLanguageVersion();
public static ScriptMethodSelector getScriptSelector(XmlScript script) { if (script == null || script.getLanguage() == null) { throw new IllegalArgumentException("Language name must not be null"); } String languageName = script.getLanguage().toLowerCase(); ScriptEngineFactory engineFactory = ENGINE_FACTORIES.get(languageName); if (engineFactory == null) { ServiceLoader<ScriptEngineFactory> loader = ServiceLoader.load(ScriptEngineFactory.class); for (ScriptEngineFactory factory : loader) { ENGINE_FACTORIES.put(factory.getLanguageName().toLowerCase(), factory); } engineFactory = ENGINE_FACTORIES.get(languageName); } if (engineFactory == null) { throw new TestNGException("No engine found for language: " + script.getLanguage() + ". " + "Please check your dependencies and have a look at " + "https://github.com/cbeust/testng/wiki/Supported-script-engines"); } return new ScriptMethodSelector(engineFactory.getScriptEngine(), script.getExpression()); } }
List<AllowableValue> engineList = new LinkedList<>(); for (ScriptEngineFactory factory : scriptEngineFactories) { engineList.add(new AllowableValue(factory.getLanguageName())); scriptEngineFactoryMap.put(factory.getLanguageName(), factory);
boolean includeMethodFromExpression(ITestNGMethod tm) { Map<String, String> groups = Maps.newHashMap(); for (String group : tm.getGroups()) { groups.put(group, group); } try { setContext(engine, groups, tm); Object evalResult = engine.eval(expression); if (evalResult == null) { String msg = String.format("The " + engine.getFactory().getLanguageName() + " expression [%s] evaluated to null.", expression); throw new TestNGException(msg); } return (Boolean) evalResult; } catch (ScriptException e) { throw new TestNGException(e); } finally { resetContext(engine); } }
@Test public void testBase() { ScriptEngineManager factory = new ScriptEngineManager(); ScriptEngine engine = factory.getEngineByName("jphp"); ScriptEngineFactory f = engine.getFactory(); Assert.assertNotNull(engine); Assert.assertTrue(engine instanceof Compilable); Assert.assertNotNull(f); Assert.assertEquals(Information.NAME, f.getEngineName()); Assert.assertEquals("php", f.getLanguageName()); }
private String getLanguageFromFileExtension(String scriptLocation, ParserContext parserContext, Element element) { ScriptEngineManager engineManager = new ScriptEngineManager(); ScriptEngine engine = null; int index = scriptLocation.lastIndexOf(".") + 1; if (index < 1) { return null; } String extension = scriptLocation.substring(index); engine = engineManager.getEngineByExtension(extension); if (engine == null) { parserContext.getReaderContext().error( "No suitable scripting engine found for extension '" + extension + "'", element); } return engine.getFactory().getLanguageName(); } }
import javax.script.ScriptEngineFactory; import javax.script.ScriptEngineManager; public class ScriptEngineTest { public static void main(String[] args) { final ScriptEngineManager mgr = new ScriptEngineManager(); for(ScriptEngineFactory fac: mgr.getEngineFactories()) { System.out.println(String.format("%s (%s), %s (%s), %s", fac.getEngineName(), fac.getEngineVersion(), fac.getLanguageName(), fac.getLanguageVersion(), fac.getParameter("THREADING"))); } } }
private void listJSREngineInfo(ScriptEngineManager mgr, Shell shellState) throws IOException { List<ScriptEngineFactory> factories = mgr.getEngineFactories(); Set<String> lines = new TreeSet<>(); for (ScriptEngineFactory factory : factories) { lines.add("ScriptEngineFactory Info"); String engName = factory.getEngineName(); String engVersion = factory.getEngineVersion(); String langName = factory.getLanguageName(); String langVersion = factory.getLanguageVersion(); lines.add("\tScript Engine: " + engName + " (" + engVersion + ")"); List<String> engNames = factory.getNames(); for (String name : engNames) { lines.add("\tEngine Alias: " + name); } lines.add("\tLanguage: " + langName + " (" + langVersion + ")"); } shellState.printLines(lines.iterator(), true); }
public CompiledScript compile(ScriptEngine scriptEngine, String language, String src) { if(scriptEngine instanceof Compilable && !scriptEngine.getFactory().getLanguageName().equalsIgnoreCase("ecmascript")) { Compilable compilingEngine = (Compilable) scriptEngine; try { CompiledScript compiledScript = compilingEngine.compile(src); LOG.debugCompiledScriptUsing(language); return compiledScript; } catch (ScriptException e) { throw new ScriptCompilationException("Unable to compile script: " + e.getMessage(), e); } } else { // engine does not support compilation return null; } }
public CompiledScript compile(ScriptEngine scriptEngine, String language, String src) { if(scriptEngine instanceof Compilable && !scriptEngine.getFactory().getLanguageName().equalsIgnoreCase("ecmascript")) { Compilable compilingEngine = (Compilable) scriptEngine; try { CompiledScript compiledScript = compilingEngine.compile(src); LOG.debugCompiledScriptUsing(language); return compiledScript; } catch (ScriptException e) { throw new ScriptCompilationException("Unable to compile script: " + e.getMessage(), e); } } else { // engine does not support compilation return null; } }
private void logScriptEngineDetails(final Level level, final ScriptEngineFactory scriptEngineFactory) { if (!LOGGER.isLoggable(level)) { return; } LOGGER .log(level, String .format("Using script engine%n%s %s (%s %s)%nScript engine names: %s%nSupported file extensions: %s", scriptEngineFactory.getEngineName(), scriptEngineFactory.getEngineVersion(), scriptEngineFactory.getLanguageName(), scriptEngineFactory.getLanguageVersion(), ObjectToString.toString(scriptEngineFactory.getNames()), ObjectToString .toString(scriptEngineFactory.getExtensions()))); }
protected Object evaluateScriptExpression(String expressionLanguage, VariableContext variableContext, String expressionText, CachedCompiledScriptSupport cachedCompiledScriptSupport) { ScriptEngine scriptEngine = getScriptEngineForName(expressionLanguage); // wrap script engine bindings + variable context and pass enhanced // bindings to the script engine. Bindings bindings = VariableContextScriptBindings.wrap(scriptEngine.createBindings(), variableContext); bindings.put("variableContext", variableContext); try { if (scriptEngine instanceof Compilable) { CompiledScript compiledScript = cachedCompiledScriptSupport.getCachedCompiledScript(); if (compiledScript == null) { synchronized (cachedCompiledScriptSupport) { compiledScript = cachedCompiledScriptSupport.getCachedCompiledScript(); if(compiledScript == null) { Compilable compilableScriptEngine = (Compilable) scriptEngine; compiledScript = compilableScriptEngine.compile(expressionText); cachedCompiledScriptSupport.cacheCompiledScript(compiledScript); } } } return compiledScript.eval(bindings); } else { return scriptEngine.eval(expressionText, bindings); } } catch (ScriptException e) { throw LOG.unableToEvaluateExpression(expressionText, scriptEngine.getFactory().getLanguageName(), e); } }
protected Collection<Map<String,String>> getAvailableScriptEngines() { List<Map<String,String>> engines = new LinkedList<Map<String,String>>(); for (ScriptEngineFactory f: FACTORIES) { Map<String,String> engine = new LinkedHashMap<String, String>(); engine.put("engine", f.getNames().get(0)); engine.put("language", f.getLanguageName()); engines.add(engine); } return engines; }
/** * <p> Gets the name of the language being used by the environment. </p> * * @return A String representing the language name */ public String getLanguageName() { return engine.getFactory().getLanguageName(); }
@Override public String getEngineLanguageName() { return engine.getFactory().getLanguageName(); }
private Map<String, List<String>> getEnginesAndExtensions(ScriptEngineManager scriptEngineManager) { return scriptEngineManager.getEngineFactories().stream() .collect(Collectors.toMap( f -> f.getLanguageName() + " (" + f.getLanguageVersion() + ")", ScriptEngineFactory::getExtensions)); }
@Override public String getLanguageName() { final PluginInfo<?> info = getInfo(); final String name = info == null ? null : info.getName(); return name != null && !name.isEmpty() ? name : base.getLanguageName(); }
public GenericScriptEngineFactory() { for (javax.script.ScriptEngineFactory f : engineManager.getEngineFactories()) { logger.info("Activated scripting support for {}", f.getLanguageName()); logger.debug( "Activated scripting support with engine {}({}) for {}({}) with mimetypes {} and file extensions {}", f.getEngineName(), f.getEngineVersion(), f.getLanguageName(), f.getLanguageVersion(), f.getMimeTypes(), f.getExtensions()); } }
/** * Gets the scripting environment name given the script extension. * * @param extension the scripting environment extension * @return the scripting environment name */ public String getEnvironmentNameForExtension(String extension) { Preconditions.checkArgument(StringUtils.isNotNullOrBlank(extension), "Extension name cannot be null or empty"); ScriptEngine engine = engineManager.getEngineByExtension(extension); Preconditions.checkArgument(engine != null, extension + " is not a recognized scripting extension."); return engine.getFactory().getLanguageName(); }