javax.script.ScriptEngineFactory Java Examples

The following examples show how to use javax.script.ScriptEngineFactory. 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: ModulesTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Basic test of ServiceLoader.load using a custom Layer as the context.
 */
@Test
public void testWithCustomLayer2() {
    ModuleLayer layer = createCustomLayer("bananascript");

    List<ScriptEngineFactory> factories
        = collectAll(ServiceLoader.load(layer, ScriptEngineFactory.class));

    // should have at least 2 x bananascript
    assertTrue(factories.size() >= 2);

    // first element should be the provider in the custom layer
    ScriptEngineFactory factory = factories.get(0);
    assertTrue(factory.getClass().getModule().getLayer() == layer);
    assertTrue(factory.getEngineName().equals("BananaScriptEngine"));

    // remainder should be the boot layer
    factories.remove(0);
    Set<String> names = factories.stream()
            .map(ScriptEngineFactory::getEngineName)
            .collect(Collectors.toSet());
    assertTrue(names.contains("BananaScriptEngine"));
    assertFalse(names.contains("PearScriptEngine"));
}
 
Example #2
Source File: TrustedScriptEngineTest.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void factoryOptionsTest() {
    final ScriptEngineManager sm = new ScriptEngineManager();
    for (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 ScriptException se) {
            }
            return;
        }
    }

    fail("Cannot find nashorn factory!");
}
 
Example #3
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 #4
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 #5
Source File: TrustedScriptEngineTest.java    From openjdk-jdk8u-backup 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 #6
Source File: TrustedScriptEngineTest.java    From openjdk-jdk8u-backup 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-backup 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 #8
Source File: MondrianDataSourceEditor.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void updateComponents() {
  final ScriptEngineFactory globalLanguage = (ScriptEngineFactory) globalLanguageField.getSelectedItem();
  globalScriptTextArea.setSyntaxEditingStyle
    ( DataFactoryEditorSupport.mapLanguageToSyntaxHighlighting( globalLanguage ) );
  queryLanguageListCellRenderer.setDefaultValue( globalLanguage );

  final ScriptEngineFactory queryScriptLanguage = (ScriptEngineFactory) queryLanguageField.getSelectedItem();
  if ( queryScriptLanguage == null ) {
    queryScriptTextArea.setSyntaxEditingStyle( globalScriptTextArea.getSyntaxEditingStyle() );
  } else {
    queryScriptTextArea
      .setSyntaxEditingStyle( DataFactoryEditorSupport.mapLanguageToSyntaxHighlighting( queryScriptLanguage ) );
  }

  final boolean querySelected = dialogModel.isQuerySelected();
  queryScriptTextArea.setEnabled( querySelected );
  queryLanguageField.setEnabled( querySelected );
  queryTemplateAction.update();
  if ( querySelected == false ) {
    queryTemplateAction.setEnabled( false );
  }

  globalTemplateAction.update();

}
 
Example #9
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 #10
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 #11
Source File: MondrianDataSourceEditor.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
protected void setScriptingLanguage( final String lang, final JComboBox languageField ) {
  if ( lang == null ) {
    languageField.setSelectedItem( null );
    return;
  }

  final ListModel model = languageField.getModel();
  for ( int i = 0; i < model.getSize(); i++ ) {
    final ScriptEngineFactory elementAt = (ScriptEngineFactory) model.getElementAt( i );
    if ( elementAt == null ) {
      continue;
    }
    if ( elementAt.getNames().contains( lang ) ) {
      languageField.setSelectedItem( elementAt );
      return;
    }
  }
}
 
Example #12
Source File: TrustedScriptEngineTest.java    From jdk8u60 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 #13
Source File: LanguageBox.java    From binnavi with Apache License 2.0 6 votes vote down vote up
private void fillLanguageBox(final ScriptEngineManager manager) {
  final List<ScriptEngineFactory> factories = manager.getEngineFactories();

  final List<ScriptingLanguage> languages = new ArrayList<ScriptingLanguage>();

  for (final ScriptEngineFactory factory : factories) {
    // Disable Rhino scripting engine for JavaScript / ECMAScript.
    if (factory.getLanguageName().equals("python")) {
      languages
          .add(new ScriptingLanguage(factory.getLanguageName(), factory.getLanguageVersion()));
    }
  }

  Collections.sort(languages);

  for (final ScriptingLanguage language : languages) {
    addItem(language);
  }
}
 
Example #14
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 #15
Source File: ScriptOptionsBackend.java    From unitime with Apache License 2.0 6 votes vote down vote up
@Override
@PreAuthorize("checkPermission('Scripts')")
public ScriptOptionsInterface execute(GetScriptOptionsRpcRequest request, SessionContext context) {
	ScriptOptionsInterface options = new ScriptOptionsInterface();
	
	for (ScriptEngineFactory factory: new ScriptEngineManager().getEngineFactories())
		options.addEngine(factory.getLanguageName());
	
	for (Right right: Right.values()) {
		if (!right.hasType() || right.type().equals(Session.class) || right.type().equals(Department.class) || right.type().equals(SubjectArea.class))
			options.addPermission(right.toString());
	}
	
	options.setCanAdd(context.hasPermission(Right.ScriptEdit));
	options.setEmail(context.getUser().getEmail());
	
	return options;
}
 
Example #16
Source File: JdbcDataSourceDialog.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void updateComponents() {
  final ScriptEngineFactory globalLanguage = (ScriptEngineFactory) globalLanguageField.getSelectedItem();
  globalScriptTextArea.setSyntaxEditingStyle( DataFactoryEditorSupport.mapLanguageToSyntaxHighlighting( globalLanguage ) );
  queryLanguageListCellRenderer.setDefaultValue( globalLanguage );

  final ScriptEngineFactory queryScriptLanguage = (ScriptEngineFactory) queryLanguageField.getSelectedItem();
  if ( queryScriptLanguage == null ) {
    queryScriptTextArea.setSyntaxEditingStyle( globalScriptTextArea.getSyntaxEditingStyle() );
  } else {
    queryScriptTextArea.setSyntaxEditingStyle( DataFactoryEditorSupport.mapLanguageToSyntaxHighlighting( queryScriptLanguage ) );
  }

  final boolean querySelected = dialogModel.isQuerySelected();
  queryScriptTextArea.setEnabled( querySelected );
  queryLanguageField.setEnabled( querySelected );
  queryTemplateAction.update();
  if ( querySelected == false ) {
    queryTemplateAction.setEnabled( false );
  }

  globalTemplateAction.update();

}
 
Example #17
Source File: PmdDataSourceEditor.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
private String mapLanguageToSyntaxHighlighting( final ScriptEngineFactory script ) {
  if ( script == null ) {
    return SyntaxConstants.SYNTAX_STYLE_NONE;
  }

  final String language = script.getLanguageName();
  if ( "ECMAScript".equalsIgnoreCase( language )
     || "js".equalsIgnoreCase( language )
     || "rhino".equalsIgnoreCase( language )
     || "javascript".equalsIgnoreCase( language ) ) {
    return SyntaxConstants.SYNTAX_STYLE_JAVASCRIPT;
  }
  if ( "groovy".equalsIgnoreCase( language ) ) {
    return SyntaxConstants.SYNTAX_STYLE_GROOVY;
  }
  return SyntaxConstants.SYNTAX_STYLE_NONE;
}
 
Example #18
Source File: Scripting.java    From netbeans with Apache License 2.0 6 votes vote down vote up
EngineManager(boolean allowAllAccess, ClassLoader loader) {
    super(loader);
    this.allowAllAccess = allowAllAccess;
    if (allowAllAccess) {
        getBindings().put("allowAllAccess", true); // NOI18N
    }
    this.extra = populateExtras(this);
    for (ScriptEngineFactory f : extra) {
        registerEngineName(f.getEngineName(), f);
        for (String ext : f.getExtensions()) {
            registerEngineExtension(ext, f);
        }
        for (String mime : f.getMimeTypes()) {
            registerEngineMimeType(mime, f);
        }
    }
}
 
Example #19
Source File: ModulesTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Basic test of stream() to ensure that elements for providers in named
 * modules come before elements for providers in unnamed modules.
 */
@Test
public void testStreamOrder() {
    List<Class<?>> types = ServiceLoader.load(ScriptEngineFactory.class)
            .stream()
            .map(Provider::type)
            .collect(Collectors.toList());

    boolean foundUnnamed = false;
    for (Class<?> factoryClass : types) {
        if (factoryClass.getModule().isNamed()) {
            if (foundUnnamed) {
                assertTrue(false, "Named module element after unnamed");
            }
        } else {
            foundUnnamed = true;
        }
    }
}
 
Example #20
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 #21
Source File: PmdDataSourceEditor.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
protected void setScriptingLanguage( final String lang, final JComboBox languageField ) {
  if ( lang == null ) {
    languageField.setSelectedItem( null );
    return;
  }

  final ListModel model = languageField.getModel();
  for ( int i = 0; i < model.getSize(); i++ ) {
    final ScriptEngineFactory elementAt = (ScriptEngineFactory) model.getElementAt( i );
    if ( elementAt == null ) {
      continue;
    }
    if ( elementAt.getNames().contains( lang ) ) {
      languageField.setSelectedItem( elementAt );
      return;
    }
  }
}
 
Example #22
Source File: StatelessScriptUpdateProcessorFactory.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * For error messages - returns null if there are any exceptions of any 
 * kind building the string (or of the list is empty for some unknown reason).
 * @param ext - if true, list of extensions, otherwise a list of engine names
 */
private static String getSupportedEngines(ScriptEngineManager mgr,
                                          boolean ext) {
  String result = null;
  try {
    List<ScriptEngineFactory> factories = mgr.getEngineFactories();
    if (null == factories) return result;

    Set<String> engines = new LinkedHashSet<>(factories.size());
    for (ScriptEngineFactory f : factories) {
      if (ext) {
        engines.addAll(f.getExtensions());
      } else {
        engines.addAll(f.getNames());
      }
    }
    result = String.join(", ", engines);
  } catch (RuntimeException e) {
    /* :NOOP: */
  }
  return result;
}
 
Example #23
Source File: TrustedScriptEngineTest.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void factoryOptionsTest() {
    final ScriptEngineManager sm = new ScriptEngineManager();
    for (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 ScriptException se) {
            }
            return;
        }
    }

    fail("Cannot find nashorn factory!");
}
 
Example #24
Source File: ScriptsInfoPage.java    From GreasySpoon with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
   * @return HTML string with ScriptEngines description
   */
  public static String getEnginesParameters(){
      StringBuilder stb = new StringBuilder();
ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
for (ScriptEngineFactory factories : scriptEngineManager.getEngineFactories()){
       stb.append("\t").append(factories.getEngineName())
       	.append(" - language:\t").append(factories.getLanguageName())
       	.append(" - version:\t").append(factories.getLanguageVersion())
       	.append("<br>\r\n");			
}
//Add GS Java scripts
if (javax.tools.ToolProvider.getSystemJavaCompiler()!=null){
	stb.append("\t").append("Native Gs Scripts")
   	.append(" - language:\t").append("Java")
   	.append(" - version:\t").append(System.getProperty("java.vm.version"))
   	.append("<br />\r\n");
}
      return stb.toString();
  }
 
Example #25
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 #26
Source File: NoPersistenceCachingTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@BeforeTest
public void setupTest() {
   stderr = new ByteArrayOutputStream();
   prevStderr = System.err;
   System.setErr(new PrintStream(stderr));
   NashornScriptEngineFactory nashornFactory = null;
   final ScriptEngineManager sm = new ScriptEngineManager();
   for (final ScriptEngineFactory fac : sm.getEngineFactories()) {
      if (fac instanceof NashornScriptEngineFactory) {
         nashornFactory = (NashornScriptEngineFactory) fac;
         break;
      }
   }
   if (nashornFactory == null) {
      fail("Cannot find nashorn factory!");
   }
   // fine is enough for cache hits, finest produces way too much information
   // TODO this should be ported to use the RuntimeEvents instead of screen scraping
   // logs, as obviously this is very brittle
   final String[] options = new String[]{"--log=compiler:fine"};
   engine = nashornFactory.getScriptEngine(options);
   context1 = engine.getContext();
   context2 = new SimpleScriptContext();
   context2.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE);
   context3 = new SimpleScriptContext();
   context3.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE);
}
 
Example #27
Source File: JavaScriptEnginesTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void fillArray(final ScriptEngineManager man, boolean allowAllAccess, List<Object[]> arr) {
    for (ScriptEngineFactory f : man.getEngineFactories()) {
        final String name = f.getEngineName();
        if (
                f.getMimeTypes().contains("text/javascript") ||
                name.contains("Nashorn")
                ) {
            final ScriptEngine eng = f.getScriptEngine();
            arr.add(new Object[] { name, "engineFactories", implName(eng), eng, allowAllAccess });
            for (String n : eng.getFactory().getNames()) {
                ScriptEngine byName = n == null ? null : man.getEngineByName(n);
                if (byName != null && eng.getClass() == byName.getClass()) {
                    arr.add(new Object[] { n, "name", implName(byName), byName, allowAllAccess });
                }
            }
            for (String t : eng.getFactory().getMimeTypes()) {
                ScriptEngine byType = t == null ? null : man.getEngineByMimeType(t);
                if (byType != null && eng.getClass() == byType.getClass()) {
                    arr.add(new Object[] { t, "type", implName(byType), byType, allowAllAccess });
                }
            }
            for (String e : eng.getFactory().getExtensions()) {
                ScriptEngine byExt = e == null ? null : man.getEngineByExtension(e);
                if (byExt != null && eng.getClass() == byExt.getClass()) {
                    arr.add(new Object[] { e, "ext", implName(byExt), byExt, allowAllAccess });
                }
            }
        }
    }
}
 
Example #28
Source File: Main.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) {
    Module thisModule = Main.class.getModule();
    assertTrue(thisModule.isNamed());

    // this module does not declare that it uses ScriptEngineFactory
    assertFalse(thisModule.canUse(ScriptEngineFactory.class));
    try {
        ServiceLoader.load(ScriptEngineFactory.class);
        assertTrue(false);
    } catch (ServiceConfigurationError expected) { }

    // invoke addUses and retry
    thisModule.addUses(ScriptEngineFactory.class);
    ServiceLoader.load(ScriptEngineFactory.class).findFirst();
}
 
Example #29
Source File: ModulesTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Basic test of Provider::type
 */
@Test
public void testProviderType() {
    Set<String> types = ServiceLoader.load(ScriptEngineFactory.class)
            .stream()
            .map(Provider::type)
            .map(Class::getName)
            .collect(Collectors.toSet());
    assertTrue(types.contains("org.banana.BananaScriptEngineFactory"));
    assertTrue(types.contains("org.pear.PearScriptEngineFactory"));
}
 
Example #30
Source File: Olap4JDataSourceEditor.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void actionPerformed( final ActionEvent e ) {
  final DataSetQuery<String> query = dialogModel.getQueries().getSelectedQuery();
  if ( query != null ) {
    final ScriptEngineFactory selectedItem = (ScriptEngineFactory) queryLanguageField.getSelectedItem();
    if ( selectedItem != null ) {
      query.setScriptLanguage( selectedItem.getLanguageName() );
    } else {
      query.setScriptLanguage( null );
    }
  }
  updateComponents();
}