Search in sources :

Example 81 with ScriptEngineManager

use of javax.script.ScriptEngineManager in project tomee by apache.

the class AbstractTomEEMojo method scriptCustomization.

private void scriptCustomization(final List<String> customizers, final String ext) throws MojoExecutionException {
    if (customizers != null) {
        final ScriptEngine engine = new ScriptEngineManager().getEngineByExtension(ext);
        if (engine == null) {
            throw new IllegalStateException("No engine for " + ext + ". Maybe add the JSR223 implementation as plugin dependency.");
        }
        for (final String js : customizers) {
            try {
                final SimpleBindings bindings = new SimpleBindings();
                bindings.put("catalinaBase", catalinaBase.getAbsolutePath());
                bindings.put("resolver", new Resolver() {

                    @Override
                    public File resolve(final String group, final String artifact, final String version, final String classifier, final String type) {
                        try {
                            return AbstractTomEEMojo.this.resolve(group, artifact, version, classifier, type).resolved;
                        } catch (final ArtifactResolutionException | ArtifactNotFoundException e) {
                            throw new IllegalArgumentException(e);
                        }
                    }

                    @Override
                    public File resolve(final String group, final String artifact, final String version) {
                        try {
                            return AbstractTomEEMojo.this.resolve(group, artifact, version, null, "jar").resolved;
                        } catch (final ArtifactResolutionException | ArtifactNotFoundException e) {
                            throw new IllegalArgumentException(e);
                        }
                    }

                    @Override
                    public File resolve(final String group, final String artifact, final String version, final String type) {
                        try {
                            return AbstractTomEEMojo.this.resolve(group, artifact, version, null, type).resolved;
                        } catch (final ArtifactResolutionException | ArtifactNotFoundException e) {
                            throw new IllegalArgumentException(e);
                        }
                    }
                });
                engine.eval(new StringReader(js), bindings);
            } catch (final ScriptException e) {
                throw new MojoExecutionException(e.getMessage(), e);
            }
        }
    }
}
Also used : ScriptException(javax.script.ScriptException) ArtifactResolver(org.apache.maven.artifact.resolver.ArtifactResolver) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) SimpleBindings(javax.script.SimpleBindings) ScriptEngineManager(javax.script.ScriptEngineManager) StringReader(java.io.StringReader) ZipFile(java.util.zip.ZipFile) File(java.io.File) ScriptEngine(javax.script.ScriptEngine)

Example 82 with ScriptEngineManager

use of javax.script.ScriptEngineManager in project bnd by bndtools.

the class Macro method _js.

public Object _js(String[] args) throws Exception {
    verifyCommand(args, _js, null, 2, Integer.MAX_VALUE);
    StringBuilder sb = new StringBuilder();
    for (int i = 1; i < args.length; i++) sb.append(args[i]).append(';');
    if (context == null) {
        synchronized (this) {
            if (engine == null)
                engine = new ScriptEngineManager().getEngineByName("javascript");
        }
        context = engine.getContext();
        bindings = context.getBindings(ScriptContext.ENGINE_SCOPE);
        bindings.put("domain", domain);
        String javascript = domain.mergeProperties("javascript", ";");
        if (javascript != null && javascript.length() > 0) {
            engine.eval(javascript, context);
        }
        context.setErrorWriter(stderr);
        context.setWriter(stdout);
    }
    Object eval = engine.eval(sb.toString(), context);
    StringBuffer buffer = stdout.getBuffer();
    if (buffer.length() > 0) {
        domain.error("Executing js: %s: %s", sb, buffer);
        buffer.setLength(0);
    }
    if (eval != null) {
        return toString(eval);
    }
    String out = stdout.toString();
    stdout.getBuffer().setLength(0);
    return out;
}
Also used : ScriptEngineManager(javax.script.ScriptEngineManager)

Example 83 with ScriptEngineManager

use of javax.script.ScriptEngineManager in project jgnash by ccavanaugh.

the class ExecuteJavaScriptAction method showAndWait.

public static void showAndWait() {
    final ResourceBundle resources = ResourceUtils.getBundle();
    final FileChooser fileChooser = configureFileChooser();
    fileChooser.setTitle(resources.getString("Title.SelFile"));
    final File file = fileChooser.showOpenDialog(MainView.getPrimaryStage());
    if (file != null) {
        Preferences pref = Preferences.userNodeForPackage(ExecuteJavaScriptAction.class);
        pref.put(LAST_DIR, file.getParentFile().getAbsolutePath());
        Platform.runLater(() -> {
            try (final Reader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) {
                new ScriptEngineManager().getEngineByName("nashorn").eval(reader);
            } catch (IOException | ScriptException ex) {
                Logger.getLogger(ExecuteJavaScriptAction.class.getName()).log(Level.SEVERE, ex.toString(), ex);
            }
        });
    }
}
Also used : ScriptException(javax.script.ScriptException) FileChooser(javafx.stage.FileChooser) ScriptEngineManager(javax.script.ScriptEngineManager) Reader(java.io.Reader) ResourceBundle(java.util.ResourceBundle) IOException(java.io.IOException) Preferences(java.util.prefs.Preferences) File(java.io.File)

Example 84 with ScriptEngineManager

use of javax.script.ScriptEngineManager in project jgnash by ccavanaugh.

the class RunJavaScriptAction method actionPerformed.

@Override
public void actionPerformed(final ActionEvent e) {
    final Preferences pref = Preferences.userNodeForPackage(RunJavaScriptAction.class);
    JFileChooser chooser = new JFileChooser(pref.get(JAVASCRIPT_DIR, null));
    chooser.setMultiSelectionEnabled(false);
    if (chooser.showOpenDialog(UIApplication.getFrame()) == JFileChooser.APPROVE_OPTION) {
        pref.put(JAVASCRIPT_DIR, chooser.getCurrentDirectory().getAbsolutePath());
        final Path path = chooser.getSelectedFile().toPath();
        EventQueue.invokeLater(() -> {
            try (final Reader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
                new ScriptEngineManager().getEngineByName("nashorn").eval(reader);
            } catch (IOException | ScriptException ex) {
                Logger.getLogger(RunJavaScriptAction.class.getName()).log(Level.SEVERE, ex.toString(), ex);
            }
        });
    }
}
Also used : Path(java.nio.file.Path) ScriptException(javax.script.ScriptException) JFileChooser(javax.swing.JFileChooser) ScriptEngineManager(javax.script.ScriptEngineManager) Reader(java.io.Reader) IOException(java.io.IOException) Preferences(java.util.prefs.Preferences)

Aggregations

ScriptEngineManager (javax.script.ScriptEngineManager)84 ScriptEngine (javax.script.ScriptEngine)46 Test (org.junit.Test)19 ScriptException (javax.script.ScriptException)15 ScriptEngineFactory (javax.script.ScriptEngineFactory)11 QuickTest (com.hazelcast.test.annotation.QuickTest)10 Invocable (javax.script.Invocable)10 IOException (java.io.IOException)8 GroovyScriptEngineFactory (org.codehaus.groovy.jsr223.GroovyScriptEngineFactory)8 Before (org.junit.Before)8 FileReader (java.io.FileReader)7 File (java.io.File)6 Reader (java.io.Reader)6 Bindings (javax.script.Bindings)6 Map (java.util.Map)5 HashMap (java.util.HashMap)3 SimpleBindings (javax.script.SimpleBindings)3 NashornScriptEngine (jdk.nashorn.api.scripting.NashornScriptEngine)3 Plugin (the.bytecode.club.bytecodeviewer.api.Plugin)3 Person (com.winterbe.java8.samples.lambda.Person)2