Java Code Examples for javax.script.ScriptEngineFactory#getNames()

The following examples show how to use javax.script.ScriptEngineFactory#getNames() . 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: ScriptLanguageFixture.java    From hsac-fitnesse-fixtures with Apache License 2.0 6 votes vote down vote up
public Map<String, Map<String, Object>> availableEngines() {
    Map<String, Map<String, Object>> result = new LinkedHashMap<>();

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

    for (ScriptEngineFactory factory : factories) {
        Map<String, Object> f = new LinkedHashMap<>();
        String engName = factory.getEngineName();
        String engVersion = factory.getEngineVersion();
        List<String> engNames = factory.getNames();
        String langName = factory.getLanguageName();
        String langVersion = factory.getLanguageVersion();

        result.put(engName, f);
        f.put("language name", langName);
        f.put("language version", langVersion);
        f.put("aliases", engNames);
        f.put("version", engVersion);
    }
    return result;
}
 
Example 2
Source File: SpagoBIScriptManager.java    From Knowage-Server with GNU Affero General Public License v3.0 6 votes vote down vote up
public void printInfo() {
	ScriptEngineManager mgr = new ScriptEngineManager();
	List<ScriptEngineFactory> factories = mgr.getEngineFactories();

	for (ScriptEngineFactory factory : factories) {
		logger.debug("ScriptEngineFactory Info");
		String engName = factory.getEngineName();
		String engVersion = factory.getEngineVersion();
		String langName = factory.getLanguageName();
		String langVersion = factory.getLanguageVersion();
		System.out.printf("\tScript Engine: %s (%s)\n", engName, engVersion);
		List<String> engNames = factory.getNames();
		for (String name : engNames) {
			System.out.printf("\tEngine Alias: %s\n", name);
		}
		System.out.printf("\tLanguage: %s (%s)\n", langName, langVersion);
	}
}
 
Example 3
Source File: J2SEDeployActionProvider.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 4
Source File: ScriptUtilities.java    From Knowage-Server with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * getEngineFactoriesNames
 *
 * @return a map <name of the engine, alias>
 */
static public Map<String, String> getEngineFactoriesNames(){
	logger.debug("IN");
	ScriptEngineManager mgr = new ScriptEngineManager();
	List<ScriptEngineFactory> factories = mgr.getEngineFactories();

	Map<String,String> names=new HashMap<String,String>();
	for (ScriptEngineFactory factory: factories) {
		String engName1 = factory.getEngineName(); 
		String engVersion=factory.getEngineVersion();
		String engName=engName1+engVersion;
		List<String> engNames = factory.getNames();
		String alias=engName;
		if(engNames.size()>=1){
			alias=engNames.get(0);
		}
		names.put(engName, alias);
	}
	logger.debug("found "+names.keySet().size()+" engines");		
	logger.debug("OUT");
	return names;
}
 
Example 5
Source File: StandardScriptUtils.java    From spring-analysis-note with MIT License 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<>();
		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 6
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 7
Source File: ScriptEngineManagerUtils.java    From tutorials with MIT License 5 votes vote down vote up
public static void listEngines() {
    ScriptEngineManager manager = new ScriptEngineManager();
    List<ScriptEngineFactory> engines = manager.getEngineFactories();

    for (ScriptEngineFactory engine : engines) {
        LOGGER.info("Engine name: {}", engine.getEngineName());
        LOGGER.info("Version: {}", engine.getEngineVersion());
        LOGGER.info("Language: {}", engine.getLanguageName());

        LOGGER.info("Short Names:");
        for (String names : engine.getNames()) {
            LOGGER.info(names);
        }
    }
}
 
Example 8
Source File: Extender.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "rawtypes" })
public ScriptEngine resolveScriptEngine(String name) {
  try {
    BufferedReader in = new BufferedReader(new InputStreamReader(configFile.openStream()));
    String className = in.readLine();
    in.close();
    Class cls = bundle.loadClass(className);
    if (!ScriptEngineFactory.class.isAssignableFrom(cls)) {
      throw new IllegalStateException("Invalid ScriptEngineFactory: " + cls.getName());
    }
    ScriptEngineFactory factory = (ScriptEngineFactory) cls.newInstance();
    List<String> names = factory.getNames();
    for (String test : names) {
      if (test.equals(name)) {
        ClassLoader old = Thread.currentThread().getContextClassLoader();
        ScriptEngine engine;
        try {
          // JRuby seems to require the correct TCCL to call
          // getScriptEngine
          Thread.currentThread().setContextClassLoader(factory.getClass().getClassLoader());
          engine = factory.getScriptEngine();
        } finally {
          Thread.currentThread().setContextClassLoader(old);
        }
        LOGGER.trace("Resolved ScriptEngineFactory: {} for expected name: {}", engine, name);
        return engine;
      }
    }
    LOGGER.debug("ScriptEngineFactory: {} does not match expected name: {}", factory.getEngineName(), name);
    return null;
  } catch (Exception e) {
    LOGGER.warn("Cannot create ScriptEngineFactory: {}", e.getClass().getName(), e);
    return null;
  }
}
 
Example 9
Source File: L2ScriptEngineManager.java    From L2jBrasil with GNU General Public License v3.0 5 votes vote down vote up
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();
}
 
Example 10
Source File: NewAction.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
private void showEngineDetails(JComponent parent, ScriptEngineFactory scriptEngineFactory) {
    StringBuilder out = new StringBuilder();

    out.append(String.format("Engine name: %s\n", scriptEngineFactory.getEngineName()));
    out.append(String.format("Engine version: %s\n", scriptEngineFactory.getEngineVersion()));
    out.append(String.format("Language name: %s\n", scriptEngineFactory.getLanguageName()));
    out.append(String.format("Language version: %s\n", scriptEngineFactory.getLanguageVersion()));

    out.append("File name extension(s):");
    final List<String> extensions = scriptEngineFactory.getExtensions();
    for (String extension : extensions) {
        out.append(" ");
        out.append(extension);
    }
    out.append("\n");

    out.append("File content type(s):");
    final List<String> mimeTypes = scriptEngineFactory.getMimeTypes();
    for (String mimeType : mimeTypes) {
        out.append(" ");
        out.append(mimeType);
    }
    out.append("\n");

    out.append("Engine factory name(s): ");
    final List<String> names = scriptEngineFactory.getNames();
    for (String name : names) {
        out.append(" ");
        out.append(name);
    }
    out.append("\n");

    JOptionPane.showMessageDialog(parent, out.toString(), "Script Engine Details", JOptionPane.INFORMATION_MESSAGE);
}
 
Example 11
Source File: Extender.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"rawtypes"})
@Override
public ScriptEngine resolveScriptEngine(String name) {
    try {
        BufferedReader in = new BufferedReader(new InputStreamReader(configFile.openStream()));
        String className = in.readLine();
        in.close();
        Class cls = bundle.loadClass(className);
        if (!ScriptEngineFactory.class.isAssignableFrom(cls)) {
            throw new IllegalStateException("Invalid ScriptEngineFactory: " + cls.getName());
        }
        ScriptEngineFactory factory = (ScriptEngineFactory) cls.newInstance();
        List<String> names = factory.getNames();
        for (String test : names) {
            if (test.equals(name)) {
                ClassLoader old = Thread.currentThread().getContextClassLoader();
                ScriptEngine engine;
                try {
                    // JRuby seems to require the correct TCCL to call
                    // getScriptEngine
                    Thread.currentThread().setContextClassLoader(factory.getClass().getClassLoader());
                    engine = factory.getScriptEngine();
                } finally {
                    Thread.currentThread().setContextClassLoader(old);
                }
                LOGGER.trace("Resolved ScriptEngineFactory: {} for expected name: {}", engine, name);
                return engine;
            }
        }
        LOGGER.debug("ScriptEngineFactory: {} does not match expected name: {}", factory.getEngineName(), name);
        return null;
    } catch (Exception e) {
        LOGGER.warn("Cannot create ScriptEngineFactory: {}", e.getClass().getName(), e);
        return null;
    }
}
 
Example 12
Source File: StandardScriptUtils.java    From lams with GNU General Public License v2.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 13
Source File: ScriptEngineTest.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void factoryTests() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    assertNotNull(e);

    final ScriptEngineFactory fac = e.getFactory();

    assertEquals(fac.getLanguageName(), "ECMAScript");
    assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript");
    assertEquals(fac.getLanguageVersion(), "ECMA - 262 Edition 5.1");
    assertEquals(fac.getEngineName(), "Oracle Nashorn");
    assertEquals(fac.getOutputStatement("context"), "print(context)");
    assertEquals(fac.getProgram("print('hello')", "print('world')"), "print('hello');print('world');");
    assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript");

    boolean seenJS = false;
    for (String ext : fac.getExtensions()) {
        if (ext.equals("js")) {
            seenJS = true;
        }
    }

    assertEquals(seenJS, true);
    String str = fac.getMethodCallSyntax("obj", "foo", "x");
    assertEquals(str, "obj.foo(x)");

    boolean seenNashorn = false, seenJavaScript = false, seenECMAScript = false;
    for (String name : fac.getNames()) {
        switch (name) {
            case "nashorn": seenNashorn = true; break;
            case "javascript": seenJavaScript = true; break;
            case "ECMAScript": seenECMAScript = true; break;
        }
    }

    assertTrue(seenNashorn);
    assertTrue(seenJavaScript);
    assertTrue(seenECMAScript);

    boolean seenAppJS = false, seenAppECMA = false, seenTextJS = false, seenTextECMA = false;
    for (String mime : fac.getMimeTypes()) {
        switch (mime) {
            case "application/javascript": seenAppJS = true; break;
            case "application/ecmascript": seenAppECMA = true; break;
            case "text/javascript": seenTextJS = true; break;
            case "text/ecmascript": seenTextECMA = true; break;
        }
    }

    assertTrue(seenAppJS);
    assertTrue(seenAppECMA);
    assertTrue(seenTextJS);
    assertTrue(seenTextECMA);
}
 
Example 14
Source File: ScriptEngineTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void factoryTests() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    assertNotNull(e);

    final ScriptEngineFactory fac = e.getFactory();

    assertEquals(fac.getLanguageName(), "ECMAScript");
    assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript");
    assertEquals(fac.getLanguageVersion(), "ECMA - 262 Edition 5.1");
    assertEquals(fac.getEngineName(), "Oracle Nashorn");
    assertEquals(fac.getOutputStatement("context"), "print(context)");
    assertEquals(fac.getProgram("print('hello')", "print('world')"), "print('hello');print('world');");
    assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript");

    boolean seenJS = false;
    for (final String ext : fac.getExtensions()) {
        if (ext.equals("js")) {
            seenJS = true;
        }
    }

    assertEquals(seenJS, true);
    final String str = fac.getMethodCallSyntax("obj", "foo", "x");
    assertEquals(str, "obj.foo(x)");

    boolean seenNashorn = false, seenJavaScript = false, seenECMAScript = false;
    for (final String name : fac.getNames()) {
        switch (name) {
            case "nashorn": seenNashorn = true; break;
            case "javascript": seenJavaScript = true; break;
            case "ECMAScript": seenECMAScript = true; break;
        default:
            break;
        }
    }

    assertTrue(seenNashorn);
    assertTrue(seenJavaScript);
    assertTrue(seenECMAScript);

    boolean seenAppJS = false, seenAppECMA = false, seenTextJS = false, seenTextECMA = false;
    for (final String mime : fac.getMimeTypes()) {
        switch (mime) {
            case "application/javascript": seenAppJS = true; break;
            case "application/ecmascript": seenAppECMA = true; break;
            case "text/javascript": seenTextJS = true; break;
            case "text/ecmascript": seenTextECMA = true; break;
        default:
            break;
        }
    }

    assertTrue(seenAppJS);
    assertTrue(seenAppECMA);
    assertTrue(seenTextJS);
    assertTrue(seenTextECMA);
}
 
Example 15
Source File: ScriptEngineTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void factoryTests() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    assertNotNull(e);

    final ScriptEngineFactory fac = e.getFactory();

    assertEquals(fac.getLanguageName(), "ECMAScript");
    assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript");
    assertEquals(fac.getLanguageVersion(), "ECMA - 262 Edition 5.1");
    assertEquals(fac.getEngineName(), "Oracle Nashorn");
    assertEquals(fac.getOutputStatement("context"), "print(context)");
    assertEquals(fac.getProgram("print('hello')", "print('world')"), "print('hello');print('world');");
    assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript");

    boolean seenJS = false;
    for (final String ext : fac.getExtensions()) {
        if (ext.equals("js")) {
            seenJS = true;
        }
    }

    assertEquals(seenJS, true);
    final String str = fac.getMethodCallSyntax("obj", "foo", "x");
    assertEquals(str, "obj.foo(x)");

    boolean seenNashorn = false, seenJavaScript = false, seenECMAScript = false;
    for (final String name : fac.getNames()) {
        switch (name) {
            case "nashorn": seenNashorn = true; break;
            case "javascript": seenJavaScript = true; break;
            case "ECMAScript": seenECMAScript = true; break;
        default:
            break;
        }
    }

    assertTrue(seenNashorn);
    assertTrue(seenJavaScript);
    assertTrue(seenECMAScript);

    boolean seenAppJS = false, seenAppECMA = false, seenTextJS = false, seenTextECMA = false;
    for (final String mime : fac.getMimeTypes()) {
        switch (mime) {
            case "application/javascript": seenAppJS = true; break;
            case "application/ecmascript": seenAppECMA = true; break;
            case "text/javascript": seenTextJS = true; break;
            case "text/ecmascript": seenTextECMA = true; break;
        default:
            break;
        }
    }

    assertTrue(seenAppJS);
    assertTrue(seenAppECMA);
    assertTrue(seenTextJS);
    assertTrue(seenTextECMA);
}
 
Example 16
Source File: ScriptEngineTest.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void factoryTests() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    assertNotNull(e);

    final ScriptEngineFactory fac = e.getFactory();

    assertEquals(fac.getLanguageName(), "ECMAScript");
    assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript");
    assertEquals(fac.getLanguageVersion(), "ECMA - 262 Edition 5.1");
    assertEquals(fac.getEngineName(), "Oracle Nashorn");
    assertEquals(fac.getOutputStatement("context"), "print(context)");
    assertEquals(fac.getProgram("print('hello')", "print('world')"), "print('hello');print('world');");
    assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript");

    boolean seenJS = false;
    for (final String ext : fac.getExtensions()) {
        if (ext.equals("js")) {
            seenJS = true;
        }
    }

    assertEquals(seenJS, true);
    final String str = fac.getMethodCallSyntax("obj", "foo", "x");
    assertEquals(str, "obj.foo(x)");

    boolean seenNashorn = false, seenJavaScript = false, seenECMAScript = false;
    for (final String name : fac.getNames()) {
        switch (name) {
            case "nashorn": seenNashorn = true; break;
            case "javascript": seenJavaScript = true; break;
            case "ECMAScript": seenECMAScript = true; break;
        default:
            break;
        }
    }

    assertTrue(seenNashorn);
    assertTrue(seenJavaScript);
    assertTrue(seenECMAScript);

    boolean seenAppJS = false, seenAppECMA = false, seenTextJS = false, seenTextECMA = false;
    for (final String mime : fac.getMimeTypes()) {
        switch (mime) {
            case "application/javascript": seenAppJS = true; break;
            case "application/ecmascript": seenAppECMA = true; break;
            case "text/javascript": seenTextJS = true; break;
            case "text/ecmascript": seenTextECMA = true; break;
        default:
            break;
        }
    }

    assertTrue(seenAppJS);
    assertTrue(seenAppECMA);
    assertTrue(seenTextJS);
    assertTrue(seenTextECMA);
}
 
Example 17
Source File: ScriptEngineTest.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void factoryTests() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    assertNotNull(e);

    final ScriptEngineFactory fac = e.getFactory();

    assertEquals(fac.getLanguageName(), "ECMAScript");
    assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript");
    assertEquals(fac.getLanguageVersion(), "ECMA - 262 Edition 5.1");
    assertEquals(fac.getEngineName(), "Oracle Nashorn");
    assertEquals(fac.getOutputStatement("context"), "print(context)");
    assertEquals(fac.getProgram("print('hello')", "print('world')"), "print('hello');print('world');");
    assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript");

    boolean seenJS = false;
    for (final String ext : fac.getExtensions()) {
        if (ext.equals("js")) {
            seenJS = true;
        }
    }

    assertEquals(seenJS, true);
    final String str = fac.getMethodCallSyntax("obj", "foo", "x");
    assertEquals(str, "obj.foo(x)");

    boolean seenNashorn = false, seenJavaScript = false, seenECMAScript = false;
    for (final String name : fac.getNames()) {
        switch (name) {
            case "nashorn": seenNashorn = true; break;
            case "javascript": seenJavaScript = true; break;
            case "ECMAScript": seenECMAScript = true; break;
        default:
            break;
        }
    }

    assertTrue(seenNashorn);
    assertTrue(seenJavaScript);
    assertTrue(seenECMAScript);

    boolean seenAppJS = false, seenAppECMA = false, seenTextJS = false, seenTextECMA = false;
    for (final String mime : fac.getMimeTypes()) {
        switch (mime) {
            case "application/javascript": seenAppJS = true; break;
            case "application/ecmascript": seenAppECMA = true; break;
            case "text/javascript": seenTextJS = true; break;
            case "text/ecmascript": seenTextECMA = true; break;
        default:
            break;
        }
    }

    assertTrue(seenAppJS);
    assertTrue(seenAppECMA);
    assertTrue(seenTextJS);
    assertTrue(seenTextECMA);
}
 
Example 18
Source File: ScriptEngineTest.java    From jdk8u_nashorn with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void factoryTests() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    assertNotNull(e);

    final ScriptEngineFactory fac = e.getFactory();

    assertEquals(fac.getLanguageName(), "ECMAScript");
    assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript");
    assertEquals(fac.getLanguageVersion(), "ECMA - 262 Edition 5.1");
    assertEquals(fac.getEngineName(), "Oracle Nashorn");
    assertEquals(fac.getOutputStatement("context"), "print(context)");
    assertEquals(fac.getProgram("print('hello')", "print('world')"), "print('hello');print('world');");
    assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript");

    boolean seenJS = false;
    for (final String ext : fac.getExtensions()) {
        if (ext.equals("js")) {
            seenJS = true;
        }
    }

    assertEquals(seenJS, true);
    final String str = fac.getMethodCallSyntax("obj", "foo", "x");
    assertEquals(str, "obj.foo(x)");

    boolean seenNashorn = false, seenJavaScript = false, seenECMAScript = false;
    for (final String name : fac.getNames()) {
        switch (name) {
            case "nashorn": seenNashorn = true; break;
            case "javascript": seenJavaScript = true; break;
            case "ECMAScript": seenECMAScript = true; break;
        default:
            break;
        }
    }

    assertTrue(seenNashorn);
    assertTrue(seenJavaScript);
    assertTrue(seenECMAScript);

    boolean seenAppJS = false, seenAppECMA = false, seenTextJS = false, seenTextECMA = false;
    for (final String mime : fac.getMimeTypes()) {
        switch (mime) {
            case "application/javascript": seenAppJS = true; break;
            case "application/ecmascript": seenAppECMA = true; break;
            case "text/javascript": seenTextJS = true; break;
            case "text/ecmascript": seenTextECMA = true; break;
        default:
            break;
        }
    }

    assertTrue(seenAppJS);
    assertTrue(seenAppECMA);
    assertTrue(seenTextJS);
    assertTrue(seenTextECMA);
}
 
Example 19
Source File: BundleScriptEngineResolver.java    From camunda-bpm-platform-osgi with Apache License 2.0 4 votes vote down vote up
@Override
 public ScriptEngine resolveScriptEngine(String name) {
	try {
		BufferedReader in = new BufferedReader(new InputStreamReader(
				configFile.openStream()));
		String className = removeCommentsFromInput(in);
		in.close();
		Class<?> cls = bundle.loadClass(className);
		if (!ScriptEngineFactory.class.isAssignableFrom(cls)) {
			throw new IllegalStateException("Invalid ScriptEngineFactory: "
					+ cls.getName());
		}
		ScriptEngineFactory factory = (ScriptEngineFactory) cls
				.newInstance();
		List<String> names = factory.getNames();
		for (String n : names) {
			if (n.equals(name)) {
				ClassLoader old = Thread.currentThread()
						.getContextClassLoader();
				ScriptEngine engine;
				try {
					// JRuby seems to require the correct TCCL to call
					// getScriptEngine
					Thread.currentThread().setContextClassLoader(
							factory.getClass().getClassLoader());
					engine = factory.getScriptEngine();
				} finally {
					Thread.currentThread().setContextClassLoader(old);
				}
				LOGGER.finest("Resolved ScriptEngineFactory: " + engine
						+ " for expected name: " + name);
				return engine;
			}
		}
		LOGGER.fine("ScriptEngineFactory: " + factory.getEngineName()
				+ " does not match expected name: " + name);
		return null;
	} catch (Exception e) {
		LOGGER.log(Level.WARNING, "Cannot create ScriptEngineFactory: "
				+ e.getClass().getName(), e);
		return null;
	}
}
 
Example 20
Source File: ScriptEngineTest.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void factoryTests() {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    assertNotNull(e);

    final ScriptEngineFactory fac = e.getFactory();

    assertEquals(fac.getLanguageName(), "ECMAScript");
    assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript");
    assertEquals(fac.getLanguageVersion(), "ECMA - 262 Edition 5.1");
    assertEquals(fac.getEngineName(), "Oracle Nashorn");
    assertEquals(fac.getOutputStatement("context"), "print(context)");
    assertEquals(fac.getProgram("print('hello')", "print('world')"), "print('hello');print('world');");
    assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript");

    boolean seenJS = false;
    for (final String ext : fac.getExtensions()) {
        if (ext.equals("js")) {
            seenJS = true;
        }
    }

    assertEquals(seenJS, true);
    final String str = fac.getMethodCallSyntax("obj", "foo", "x");
    assertEquals(str, "obj.foo(x)");

    boolean seenNashorn = false, seenJavaScript = false, seenECMAScript = false;
    for (final String name : fac.getNames()) {
        switch (name) {
            case "nashorn": seenNashorn = true; break;
            case "javascript": seenJavaScript = true; break;
            case "ECMAScript": seenECMAScript = true; break;
        default:
            break;
        }
    }

    assertTrue(seenNashorn);
    assertTrue(seenJavaScript);
    assertTrue(seenECMAScript);

    boolean seenAppJS = false, seenAppECMA = false, seenTextJS = false, seenTextECMA = false;
    for (final String mime : fac.getMimeTypes()) {
        switch (mime) {
            case "application/javascript": seenAppJS = true; break;
            case "application/ecmascript": seenAppECMA = true; break;
            case "text/javascript": seenTextJS = true; break;
            case "text/ecmascript": seenTextECMA = true; break;
        default:
            break;
        }
    }

    assertTrue(seenAppJS);
    assertTrue(seenAppECMA);
    assertTrue(seenTextJS);
    assertTrue(seenTextECMA);
}