Java Code Examples for javax.script.ScriptEngineManager#getEngineFactories()

The following examples show how to use javax.script.ScriptEngineManager#getEngineFactories() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TrustedScriptEngineTest.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@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!");
}
 
Example 2
Source File: TrustedScriptEngineTest.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@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!");
}
 
Example 3
Source File: TrustedScriptEngineTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@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!");
}
 
Example 4
Source File: TrustedScriptEngineTest.java    From jdk8u_nashorn with GNU General Public License v2.0 6 votes vote down vote up
@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!");
}
 
Example 5
Source File: TrustedScriptEngineTest.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
@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 (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!");
}
 
Example 6
Source File: TrustedScriptEngineTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@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!");
}
 
Example 7
Source File: TrustedScriptEngineTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@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!");
}
 
Example 8
Source File: RunScript.java    From common_gui_tools with Apache License 2.0 6 votes vote down vote up
/**
 * 支持的脚本类型.
 */
private String[] getScriptTypes() {
    ScriptEngineManager sem = new ScriptEngineManager();
    List<ScriptEngineFactory> factorys = sem.getEngineFactories();
    Set<String> languages = new LinkedHashSet<String>();
    for (ScriptEngineFactory factory : factorys) {
        languages.add(factory.getLanguageName());
    }
    String[] types = new String[languages.size()];
    int i = 0;
    for (String name : languages) {
        types[i] = name;
        i++;
    }
    return types;
}
 
Example 9
Source File: TrustedScriptEngineTest.java    From jdk8u_nashorn with GNU General Public License v2.0 6 votes vote down vote up
@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!");
}
 
Example 10
Source File: TrustedScriptEngineTest.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@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!");
}
 
Example 11
Source File: TrustedScriptEngineTest.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@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!");
}
 
Example 12
Source File: TrustedScriptEngineTest.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
@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 (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!");
}
 
Example 13
Source File: JFXActionProvider.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private boolean isJavaScriptAvailable() {
    if(isJSAvailableChecked) {
        return isJSAvailable;
    }
    ScriptEngineManager mgr = Scripting.createManager();
    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;
}
 
Example 14
Source File: TrustedScriptEngineTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@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!");
}
 
Example 15
Source File: TrustedScriptEngineTest.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
@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 (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!");
}
 
Example 16
Source File: GraalEnginesTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Test
public void invokeEngineViaGeneratedScriptEngine() {
    ScriptEngineManager man = Scripting.createManager();
    ScriptEngine llvm = man.getEngineByName("GraalVM:llvm");
    assertNotNull("llvm engine found: " + man.getEngineFactories(), llvm);

    ScriptEngineFactory jsFactory = null;
    ScriptEngineFactory graalvmJsFactory = null;
    ScriptEngineFactory llvmFactory = null;

    StringBuilder log = new StringBuilder();
    for (ScriptEngineFactory factory : man.getEngineFactories()) {
        final List<String> engineNames = factory.getNames();
        final List<String> types = factory.getMimeTypes();

        log.append("\nclass: ").append(factory.getClass().getSimpleName())
            .append("\nnames: ").append(engineNames)
            .append("\ntypes: ").append(types);

        if (engineNames.contains("LLVM")) {
            llvmFactory = factory;
        }
        if (types.contains("text/javascript")) {
            if (factory.getEngineName().startsWith("GraalVM:")) {
                assertNull("No previous generic GraalVM javascript factory: " + graalvmJsFactory, graalvmJsFactory);
                graalvmJsFactory = factory;
            } else {
                assertNull("No previous javascript factory: " + jsFactory, jsFactory);
                jsFactory = factory;
            }
        }
    }

    assertNotNull("llvm factory found: " + log, llvmFactory);
    assertNotNull("js factory found: " + log, jsFactory);
    assertNotNull("Generic GraalVM js factory found: " + log, graalvmJsFactory);
}
 
Example 17
Source File: StandardScriptUtils.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieve a {@link ScriptEngine} from the given {@link ScriptEngineManager}
 * by name, delegating to {@link ScriptEngineManager#getEngineByName} but
 * throwing a descriptive exception if not found or if initialization failed.
 * @param scriptEngineManager the ScriptEngineManager to use
 * @param engineName the name of the engine
 * @return a corresponding ScriptEngine (never {@code null})
 * @throws IllegalArgumentException if no matching engine has been found
 * @throws IllegalStateException if the desired engine failed to initialize
 */
public static ScriptEngine retrieveEngineByName(ScriptEngineManager scriptEngineManager, String engineName) {
	ScriptEngine engine = scriptEngineManager.getEngineByName(engineName);
	if (engine == null) {
		Set<String> engineNames = new LinkedHashSet<String>();
		for (ScriptEngineFactory engineFactory : scriptEngineManager.getEngineFactories()) {
			List<String> factoryNames = engineFactory.getNames();
			if (factoryNames.contains(engineName)) {
				// Special case: getEngineByName returned null but engine is present...
				// Let's assume it failed to initialize (which ScriptEngineManager silently swallows).
				// If it happens to initialize fine now, alright, but we really expect an exception.
				try {
					engine = engineFactory.getScriptEngine();
					engine.setBindings(scriptEngineManager.getBindings(), ScriptContext.GLOBAL_SCOPE);
				}
				catch (Throwable ex) {
					throw new IllegalStateException("Script engine with name '" + engineName +
							"' failed to initialize", ex);
				}
			}
			engineNames.addAll(factoryNames);
		}
		throw new IllegalArgumentException("Script engine with name '" + engineName +
				"' not found; registered engine names: " + engineNames);
	}
	return engine;
}
 
Example 18
Source File: ScriptConnection.java    From scriptella-etl with Apache License 2.0 5 votes vote down vote up
/**
 * Returns available script engine names including all aliases.
 *
 * @param manager script manager instance to use.
 * @return list of languages each of them represented by a list of names..
 */
static List<List<String>> getAvailableEngines(ScriptEngineManager manager) {
    List<List<String>> list = new ArrayList<List<String>>();
    for (ScriptEngineFactory scriptEngineFactory : manager.getEngineFactories()) {
        list.add(scriptEngineFactory.getNames());
    }
    return list;
}
 
Example 19
Source File: SpagoBIScriptManager.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
private ScriptEngine getScriptEngineByLanguage(String language) {
	ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
	List<ScriptEngineFactory> scriptEngineFactories = scriptEngineManager.getEngineFactories();

	for (ScriptEngineFactory scriptEngineFactory : scriptEngineFactories) {
		if (scriptEngineFactory.getLanguageName().equals(language)) {
			return scriptEngineFactory.getScriptEngine();
		}
	}
	return null;
}
 
Example 20
Source File: RangerPluginClassLoader.java    From ranger with Apache License 2.0 4 votes vote down vote up
public ScriptEngine getScriptEngine(String engineName) {

	    final ScriptEngine ret;

        ClassLoader classLoader = preActivateClassLoader.get();

        if (classLoader == null) {
            MyClassLoader savedClassLoader = getComponentClassLoader();
            if (savedClassLoader != null && savedClassLoader.getParent() != null) {
                classLoader = savedClassLoader.getParent();
            }
        }

        ScriptEngineManager manager = classLoader != null ? new ScriptEngineManager(classLoader) : new ScriptEngineManager();

        if (LOG.isDebugEnabled()) {

            List<ScriptEngineFactory> factories = manager.getEngineFactories();

            if (factories == null || factories.size() == 0) {
                LOG.debug("List of scriptEngineFactories is empty!!");

            } else {
                for (ScriptEngineFactory factory : factories) {
                    LOG.debug("engineName=" + factory.getEngineName() + ", language=" + factory.getLanguageName());

                }
            }
        }

        ret = manager.getEngineByName(engineName);

        if (ret == null) {
            LOG.error("scriptEngine for JavaScript is null!!");
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("scriptEngine for JavaScript:[" + ret + "]");
            }
        }

        return ret;
    }