Search in sources :

Example 61 with ScriptEngineManager

use of javax.script.ScriptEngineManager in project opennms by OpenNMS.

the class OSGiScriptEngineManager method getEngineByMimeType.

public ScriptEngine getEngineByMimeType(String mimeType) {
    // TODO this is a hack to deal with context class loader issues
    ScriptEngine engine = null;
    for (ScriptEngineManager manager : classLoaders.keySet()) {
        ClassLoader old = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader(classLoaders.get(manager));
        engine = manager.getEngineByMimeType(mimeType);
        Thread.currentThread().setContextClassLoader(old);
        if (engine != null)
            break;
    }
    return engine;
}
Also used : ScriptEngineManager(javax.script.ScriptEngineManager) ScriptEngine(javax.script.ScriptEngine)

Example 62 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 63 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)

Example 64 with ScriptEngineManager

use of javax.script.ScriptEngineManager in project hazelcast by hazelcast.

the class OSGiScriptEngineManager method createScriptEngineManagerInfo.

private ScriptEngineManagerInfo createScriptEngineManagerInfo(String factoryName, ClassLoader factoryLoader) {
    try {
        ScriptEngineManager manager = new ScriptEngineManager(factoryLoader);
        manager.setBindings(bindings);
        return new ScriptEngineManagerInfo(manager, factoryLoader);
    } catch (Exception e) {
        // May fail if script implementation is not in environment
        logger.warning("Found ScriptEngineFactory candidate for " + factoryName + ", but could not load ScripEngineManager! -> " + e);
        if (logger.isFinestEnabled()) {
            logger.finest(e);
        }
        return null;
    }
}
Also used : ScriptEngineManager(javax.script.ScriptEngineManager) IOException(java.io.IOException)

Example 65 with ScriptEngineManager

use of javax.script.ScriptEngineManager in project cryptomator by cryptomator.

the class ExitUtil method showTrayNotification.

private void showTrayNotification(TrayIcon trayIcon) {
    int remainingTrayNotification = settings.numTrayNotifications().get();
    if (remainingTrayNotification <= 0) {
        return;
    } else {
        settings.numTrayNotifications().set(remainingTrayNotification - 1);
    }
    final Runnable notificationCmd;
    if (SystemUtils.IS_OS_MAC_OSX) {
        final String title = localization.getString("tray.infoMsg.title");
        final String msg = localization.getString("tray.infoMsg.msg.osx");
        final String notificationCenterAppleScript = String.format("display notification \"%s\" with title \"%s\"", msg, title);
        notificationCmd = () -> {
            try {
                final ScriptEngineManager mgr = new ScriptEngineManager();
                final ScriptEngine engine = mgr.getEngineByName("AppleScriptEngine");
                if (engine != null) {
                    engine.eval(notificationCenterAppleScript);
                } else {
                    Runtime.getRuntime().exec(new String[] { "/usr/bin/osascript", "-e", notificationCenterAppleScript });
                }
            } catch (ScriptException | IOException e) {
            // ignore, user will notice the tray icon anyway.
            }
        };
    } else {
        final String title = localization.getString("tray.infoMsg.title");
        final String msg = localization.getString("tray.infoMsg.msg");
        notificationCmd = () -> {
            trayIcon.displayMessage(title, msg, MessageType.INFO);
        };
    }
    SwingUtilities.invokeLater(() -> {
        notificationCmd.run();
    });
}
Also used : ScriptException(javax.script.ScriptException) ScriptEngineManager(javax.script.ScriptEngineManager) IOException(java.io.IOException) ScriptEngine(javax.script.ScriptEngine)

Aggregations

ScriptEngineManager (javax.script.ScriptEngineManager)109 ScriptEngine (javax.script.ScriptEngine)66 Test (org.junit.Test)23 ScriptException (javax.script.ScriptException)20 ScriptEngineFactory (javax.script.ScriptEngineFactory)14 File (java.io.File)12 Invocable (javax.script.Invocable)11 QuickTest (com.hazelcast.test.annotation.QuickTest)10 IOException (java.io.IOException)10 FileReader (java.io.FileReader)9 Bindings (javax.script.Bindings)9 Before (org.junit.Before)9 Map (java.util.Map)8 GroovyScriptEngineFactory (org.codehaus.groovy.jsr223.GroovyScriptEngineFactory)8 Reader (java.io.Reader)7 HashMap (java.util.HashMap)6 SimpleBindings (javax.script.SimpleBindings)6 PrintWriter (java.io.PrintWriter)4 StringWriter (java.io.StringWriter)4 InputStreamReader (java.io.InputStreamReader)3