本文整理汇总了Java中javax.script.ScriptEngineManager.getEngineFactories方法的典型用法代码示例。如果您正苦于以下问题:Java ScriptEngineManager.getEngineFactories方法的具体用法?Java ScriptEngineManager.getEngineFactories怎么用?Java ScriptEngineManager.getEngineFactories使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.script.ScriptEngineManager
的用法示例。
在下文中一共展示了ScriptEngineManager.getEngineFactories方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: isJavaScriptAvailable
import javax.script.ScriptEngineManager; //导入方法依赖的package包/类
private boolean isJavaScriptAvailable() {
if(isJSAvailableChecked) {
return isJSAvailable;
}
ScriptEngineManager mgr = new ScriptEngineManager();
List<ScriptEngineFactory> factories = mgr.getEngineFactories();
for (ScriptEngineFactory factory: factories) {
List<String> engNames = factory.getNames();
for(String name: engNames) {
if(name.equalsIgnoreCase("js") || name.equalsIgnoreCase("javascript")) { //NOI18N
isJSAvailableChecked = true;
isJSAvailable = true;
return isJSAvailable;
}
}
}
isJSAvailableChecked = true;
isJSAvailable = false;
return isJSAvailable;
}
示例2: factoryClassLoaderTest
import javax.script.ScriptEngineManager; //导入方法依赖的package包/类
@Test
public void factoryClassLoaderTest() {
final ScriptEngineManager sm = new ScriptEngineManager();
for (final ScriptEngineFactory fac : sm.getEngineFactories()) {
if (fac instanceof NashornScriptEngineFactory) {
final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac;
final MyClassLoader loader = new MyClassLoader();
// set the classloader as app class loader
final ScriptEngine e = nfac.getScriptEngine(loader);
try {
e.eval("Packages.foo");
// check that the class loader was attempted
assertTrue(loader.reached(), "did not reach class loader!");
} catch (final ScriptException se) {
se.printStackTrace();
fail(se.getMessage());
}
return;
}
}
fail("Cannot find nashorn factory!");
}
示例3: factoryOptionsTest
import javax.script.ScriptEngineManager; //导入方法依赖的package包/类
@Test
public void factoryOptionsTest() {
final ScriptEngineManager sm = new ScriptEngineManager();
for (final ScriptEngineFactory fac : sm.getEngineFactories()) {
if (fac instanceof NashornScriptEngineFactory) {
final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac;
// specify --no-syntax-extensions flag
final String[] options = new String[] { "--no-syntax-extensions" };
final ScriptEngine e = nfac.getScriptEngine(options);
try {
// try nashorn specific extension
e.eval("var f = funtion(x) 2*x;");
fail("should have thrown exception!");
} catch (final Exception ex) {
//empty
}
return;
}
}
fail("Cannot find nashorn factory!");
}
示例4: noLoaderPerCompilerTest
import javax.script.ScriptEngineManager; //导入方法依赖的package包/类
@Test
/**
* Test repeated evals with --loader-per-compile=false
* We used to get "class redefinition error".
*/
public void noLoaderPerCompilerTest() {
final ScriptEngineManager sm = new ScriptEngineManager();
for (final ScriptEngineFactory fac : sm.getEngineFactories()) {
if (fac instanceof NashornScriptEngineFactory) {
final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac;
final String[] options = new String[] { "--loader-per-compile=false" };
final ScriptEngine e = nfac.getScriptEngine(options);
try {
e.eval("2 + 3");
e.eval("4 + 4");
} catch (final ScriptException se) {
se.printStackTrace();
fail(se.getMessage());
}
return;
}
}
fail("Cannot find nashorn factory!");
}
示例5: noLoaderPerCompilerWithSameNameTest
import javax.script.ScriptEngineManager; //导入方法依赖的package包/类
@Test
/**
* Test that we can use same script name in repeated evals with --loader-per-compile=false
* We used to get "class redefinition error" as name was derived from script name.
*/
public void noLoaderPerCompilerWithSameNameTest() {
final ScriptEngineManager sm = new ScriptEngineManager();
for (final ScriptEngineFactory fac : sm.getEngineFactories()) {
if (fac instanceof NashornScriptEngineFactory) {
final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac;
final String[] options = new String[] { "--loader-per-compile=false" };
final ScriptEngine e = nfac.getScriptEngine(options);
e.put(ScriptEngine.FILENAME, "test.js");
try {
e.eval("2 + 3");
e.eval("4 + 4");
} catch (final ScriptException se) {
se.printStackTrace();
fail(se.getMessage());
}
return;
}
}
fail("Cannot find nashorn factory!");
}
示例6: L2ScriptEngineManager
import javax.script.ScriptEngineManager; //导入方法依赖的package包/类
private L2ScriptEngineManager()
{
ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
List<ScriptEngineFactory> factories = scriptEngineManager.getEngineFactories();
if (USE_COMPILED_CACHE)
_cache = this.loadCompiledScriptCache();
else
_cache = null;
_log.info("Initializing Script Engine Manager");
for (ScriptEngineFactory factory : factories)
try
{
ScriptEngine engine = factory.getScriptEngine();
boolean reg = false;
for (String name : factory.getNames())
{
ScriptEngine existentEngine = _nameEngines.get(name);
if (existentEngine != null)
{
double engineVer = Double.parseDouble(factory.getEngineVersion());
double existentEngVer = Double.parseDouble(existentEngine.getFactory().getEngineVersion());
if (engineVer <= existentEngVer)
continue;
}
reg = true;
_nameEngines.put(name, engine);
}
if (reg)
_log.info("Script Engine: " + factory.getEngineName() + " " + factory.getEngineVersion() + " - Language: " + factory.getLanguageName() + " - Language Version: " + factory.getLanguageVersion());
for (String ext : factory.getExtensions())
if (!ext.equals("java") || factory.getLanguageName().equals("java"))
_extEngines.put(ext, engine);
}
catch (Exception e)
{
_log.warning("Failed initializing factory. ");
e.printStackTrace();
}
this.preConfigure();
}
示例7: main
import javax.script.ScriptEngineManager; //导入方法依赖的package包/类
public static void main(String ... args) throws ScriptException {
ScriptEngineManager manager = new ScriptEngineManager();
for (ScriptEngineFactory f : manager.getEngineFactories()) {
System.out.println(f.getEngineName() + " -> " + f.getLanguageName() + " (" + String.join(", ", f.getExtensions()) + ")");
}
System.out.println("");
ScriptEngine python_engine;
ScriptEngine javascript_engine;
ScriptEngine lua_engine;
long start_time;
long end_time;
System.out.println("Testing Python interpreter...");
python_engine = manager.getEngineByExtension("py");
start_time = System.currentTimeMillis();
python_engine.eval("print(\"Hello, world!\")");
end_time = System.currentTimeMillis();
System.out.println("Time taken: " + String.valueOf((end_time - start_time)) + "ms\n");
System.out.println("Testing JavaScript interpreter...");
javascript_engine = manager.getEngineByExtension("js");
start_time = System.currentTimeMillis();
javascript_engine.eval("print(\"Hello, world!\")");
end_time = System.currentTimeMillis();
System.out.println("Time taken: " + String.valueOf((end_time - start_time)) + "ms\n");
System.out.println("Testing Lua interpreter...");
lua_engine = manager.getEngineByExtension("lua");
start_time = System.currentTimeMillis();
lua_engine.eval("print(\"Hello, world!\")");
end_time = System.currentTimeMillis();
System.out.println("Time taken: " + String.valueOf((end_time - start_time)) + "ms\n");
}